28#if defined(SDL_VERSION_ATLEAST)
29#if SDL_VERSION_ATLEAST(2, 0, 0)
36#define RW_SEEK_SET SEEK_SET
39#define RW_SEEK_CUR SEEK_CUR
42#define RW_SEEK_END SEEK_END
47static Sint64 SDLCALL physfsrwops_size(
struct SDL_RWops *rw)
49 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
50 return (Sint64) PHYSFS_fileLength(handle);
56static Sint64 SDLCALL physfsrwops_seek(
struct SDL_RWops *rw, Sint64 offset,
int whence)
58static int physfsrwops_seek(SDL_RWops *rw,
int offset,
int whence)
61 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
62 PHYSFS_sint64 pos = 0;
65 pos = (PHYSFS_sint64) offset;
69 const PHYSFS_sint64 current = PHYSFS_tell(handle);
72 SDL_SetError(
"Can't find position in file: %s",
73 PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
80 return (Sint64) current;
86 pos = current + ((PHYSFS_sint64) offset);
91 const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
94 SDL_SetError(
"Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
98 pos = len + ((PHYSFS_sint64) offset);
103 SDL_SetError(
"Invalid 'whence' parameter.");
109 SDL_SetError(
"Attempt to seek past start of file.");
113 if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
115 SDL_SetError(
"PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
128static size_t SDLCALL physfsrwops_read(
struct SDL_RWops *rw,
void *ptr,
129 size_t size,
size_t maxnum)
131static int physfsrwops_read(SDL_RWops *rw,
void *ptr,
int size,
int maxnum)
134 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
135 const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size);
136 const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen);
137 if (rc != ((PHYSFS_sint64) readlen))
139 if (!PHYSFS_eof(handle))
141 SDL_SetError(
"PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
152 return (
size_t) rc / size;
154 return (
int) rc / size;
160static size_t SDLCALL physfsrwops_write(
struct SDL_RWops *rw,
const void *ptr,
161 size_t size,
size_t num)
163static int physfsrwops_write(SDL_RWops *rw,
const void *ptr,
int size,
int num)
166 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
167 const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size);
168 const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen);
169 if (rc != ((PHYSFS_sint64) writelen))
170 SDL_SetError(
"PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
180static int physfsrwops_close(SDL_RWops *rw)
182 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
183 if (!PHYSFS_close(handle))
185 SDL_SetError(
"PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
194static SDL_RWops *create_rwops(PHYSFS_File *handle)
196 SDL_RWops *retval = NULL;
199 SDL_SetError(
"PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
202 retval = SDL_AllocRW();
206 retval->size = physfsrwops_size;
208 retval->seek = physfsrwops_seek;
209 retval->read = physfsrwops_read;
210 retval->write = physfsrwops_write;
211 retval->close = physfsrwops_close;
212 retval->hidden.unknown.data1 = handle;
222 SDL_RWops *retval = NULL;
224 SDL_SetError(
"NULL pointer passed to PHYSFSRWOPS_makeRWops().");
226 retval = create_rwops(handle);
234 return create_rwops(PHYSFS_openRead(fname));
240 return create_rwops(PHYSFS_openWrite(fname));
246 return create_rwops(PHYSFS_openAppend(fname));
SDL_RWops * PHYSFSRWOPS_openRead(const char *fname)
Open a platform-independent filename for reading, and make it accessible via an SDL_RWops structure.
SDL_RWops * PHYSFSRWOPS_openWrite(const char *fname)
Open a platform-independent filename for writing, and make it accessible via an SDL_RWops structure.
SDL_RWops * PHYSFSRWOPS_openAppend(const char *fname)
Open a platform-independent filename for appending, and make it accessible via an SDL_RWops structure...
SDL_RWops * PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
Make a SDL_RWops from an existing PhysicsFS file handle.