naev 0.12.5
physfsrwops.c
1// clang-format off
2/*
3 * This code provides a glue layer between PhysicsFS and Simple Directmedia
4 * Layer's (SDL) RWops i/o abstraction.
5 *
6 * License: this code is public domain. I make no warranty that it is useful,
7 * correct, harmless, or environmentally safe.
8 *
9 * This particular file may be used however you like, including copying it
10 * verbatim into a closed-source project, exploiting it commercially, and
11 * removing any trace of my name from the source (although I hope you won't
12 * do that). I welcome enhancements and corrections to this file, but I do
13 * not require you to send me patches if you make changes. This code has
14 * NO WARRANTY.
15 *
16 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
17 * Please see LICENSE.txt in the root of the source tree.
18 *
19 * SDL 1.2 falls under the LGPL license. SDL 1.3+ is zlib, like PhysicsFS.
20 * You can get SDL at https://www.libsdl.org/
21 *
22 * This file was written by Ryan C. Gordon. (icculus@icculus.org).
23 */
24
25#include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
26#include "physfsrwops.h"
27
28/* SDL's RWOPS interface changed a little in SDL 2.0... */
29#if defined(SDL_VERSION_ATLEAST)
30#if SDL_VERSION_ATLEAST(2, 0, 0)
31#define TARGET_SDL2 1
32#endif
33#endif
34
35#if !TARGET_SDL2
36#ifndef RW_SEEK_SET
37#define RW_SEEK_SET SEEK_SET
38#endif
39#ifndef RW_SEEK_CUR
40#define RW_SEEK_CUR SEEK_CUR
41#endif
42#ifndef RW_SEEK_END
43#define RW_SEEK_END SEEK_END
44#endif
45#endif
46
47#if TARGET_SDL2
48static Sint64 SDLCALL physfsrwops_size(struct SDL_RWops *rw)
49{
50 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
51 return (Sint64) PHYSFS_fileLength(handle);
52} /* physfsrwops_size */
53#endif
54
55
56#if TARGET_SDL2
57static Sint64 SDLCALL physfsrwops_seek(struct SDL_RWops *rw, Sint64 offset, int whence)
58#else
59static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
60#endif
61{
62 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
63 PHYSFS_sint64 pos = 0;
64
65 if (whence == RW_SEEK_SET)
66 pos = (PHYSFS_sint64) offset;
67
68 else if (whence == RW_SEEK_CUR)
69 {
70 const PHYSFS_sint64 current = PHYSFS_tell(handle);
71 if (current == -1)
72 {
73 SDL_SetError("Can't find position in file: %s",
74 PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
75 return -1;
76 } /* if */
77
78 if (offset == 0) /* this is a "tell" call. We're done. */
79 {
80 #if TARGET_SDL2
81 return (Sint64) current;
82 #else
83 return (int) current;
84 #endif
85 } /* if */
86
87 pos = current + ((PHYSFS_sint64) offset);
88 } /* else if */
89
90 else if (whence == RW_SEEK_END)
91 {
92 const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
93 if (len == -1)
94 {
95 SDL_SetError("Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
96 return -1;
97 } /* if */
98
99 pos = len + ((PHYSFS_sint64) offset);
100 } /* else if */
101
102 else
103 {
104 SDL_SetError("Invalid 'whence' parameter.");
105 return -1;
106 } /* else */
107
108 if ( pos < 0 )
109 {
110 SDL_SetError("Attempt to seek past start of file.");
111 return -1;
112 } /* if */
113
114 if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
115 {
116 SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
117 return -1;
118 } /* if */
119
120 #if TARGET_SDL2
121 return (Sint64) pos;
122 #else
123 return (int) pos;
124 #endif
125} /* physfsrwops_seek */
126
127
128#if TARGET_SDL2
129static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr,
130 size_t size, size_t maxnum)
131#else
132static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
133#endif
134{
135 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
136 const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size);
137 const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen);
138 if (rc != ((PHYSFS_sint64) readlen))
139 {
140 if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
141 {
142 SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
143
144 #if TARGET_SDL2
145 return 0;
146 #else
147 return -1;
148 #endif
149 } /* if */
150 } /* if */
151
152 #if TARGET_SDL2
153 return (size_t) rc / size;
154 #else
155 return (int) rc / size;
156 #endif
157} /* physfsrwops_read */
158
159
160#if TARGET_SDL2
161static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr,
162 size_t size, size_t num)
163#else
164static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
165#endif
166{
167 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
168 const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size);
169 const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen);
170 if (rc != ((PHYSFS_sint64) writelen))
171 SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
172
173 #if TARGET_SDL2
174 return (size_t) rc;
175 #else
176 return (int) rc;
177 #endif
178} /* physfsrwops_write */
179
180
181static int physfsrwops_close(SDL_RWops *rw)
182{
183 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
184 if (!PHYSFS_close(handle))
185 {
186 SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
187 return -1;
188 } /* if */
189
190 SDL_FreeRW(rw);
191 return 0;
192} /* physfsrwops_close */
193
194
195static SDL_RWops *create_rwops(PHYSFS_File *handle)
196{
197 SDL_RWops *retval = NULL;
198
199 if (handle == NULL)
200 SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
201 else
202 {
203 retval = SDL_AllocRW();
204 if (retval != NULL)
205 {
206 #if TARGET_SDL2
207 retval->size = physfsrwops_size;
208 #endif
209 retval->seek = physfsrwops_seek;
210 retval->read = physfsrwops_read;
211 retval->write = physfsrwops_write;
212 retval->close = physfsrwops_close;
213 retval->hidden.unknown.data1 = handle;
214 } /* if */
215 } /* else */
216
217 return retval;
218} /* create_rwops */
219
220
221SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
222{
223 SDL_RWops *retval = NULL;
224 if (handle == NULL)
225 SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
226 else
227 retval = create_rwops(handle);
228
229 return retval;
230} /* PHYSFSRWOPS_makeRWops */
231
232
233SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
234{
235 return create_rwops(PHYSFS_openRead(fname));
236} /* PHYSFSRWOPS_openRead */
237
238
239SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
240{
241 return create_rwops(PHYSFS_openWrite(fname));
242} /* PHYSFSRWOPS_openWrite */
243
244
245SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
246{
247 return create_rwops(PHYSFS_openAppend(fname));
248} /* PHYSFSRWOPS_openAppend */
249
250
251/* end of physfsrwops.c ... */