2 * This code provides a glue layer between PhysicsFS and Simple Directmedia
3 * Layer's (SDL) RWops i/o abstraction.
5 * License: this code is public domain. I make no warranty that it is useful,
6 * correct, harmless, or environmentally safe.
8 * This particular file may be used however you like, including copying it
9 * verbatim into a closed-source project, exploiting it commercially, and
10 * removing any trace of my name from the source (although I hope you won't
11 * do that). I welcome enhancements and corrections to this file, but I do
12 * not require you to send me patches if you make changes.
14 * Unless otherwise stated, the rest of PhysicsFS falls under the GNU Lesser
15 * General Public License: http://www.gnu.org/licenses/lgpl.txt
17 * SDL falls under the LGPL, too. You can get SDL at http://www.libsdl.org/
19 * This file was written by Ryan C. Gordon. (icculus@icculus.org).
22 #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
23 #include "physfsrwops.h"
25 static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
27 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
30 if (whence == SEEK_SET)
35 else if (whence == SEEK_CUR)
37 PHYSFS_sint64 current = PHYSFS_tell(handle);
40 SDL_SetError("Can't find position in file: %s",
41 PHYSFS_getLastError());
46 if ( ((PHYSFS_sint64) pos) != current )
48 SDL_SetError("Can't fit current file position in an int!");
52 if (offset == 0) /* this is a "tell" call. We're done. */
58 else if (whence == SEEK_END)
60 PHYSFS_sint64 len = PHYSFS_fileLength(handle);
63 SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
68 if ( ((PHYSFS_sint64) pos) != len )
70 SDL_SetError("Can't fit end-of-file position in an int!");
79 SDL_SetError("Invalid 'whence' parameter.");
85 SDL_SetError("Attempt to seek past start of file.");
89 if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
91 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
96 } /* physfsrwops_seek */
99 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
101 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
102 PHYSFS_sint64 rc = PHYSFS_read(handle, ptr, size, maxnum);
105 if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
106 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
110 } /* physfsrwops_read */
113 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
115 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
116 PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num);
118 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
121 } /* physfsrwops_write */
124 static int physfsrwops_close(SDL_RWops *rw)
126 PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
127 if (!PHYSFS_close(handle))
129 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
135 } /* physfsrwops_close */
138 static SDL_RWops *create_rwops(PHYSFS_File *handle)
140 SDL_RWops *retval = NULL;
143 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
146 retval = SDL_AllocRW();
149 retval->seek = physfsrwops_seek;
150 retval->read = physfsrwops_read;
151 retval->write = physfsrwops_write;
152 retval->close = physfsrwops_close;
153 retval->hidden.unknown.data1 = handle;
161 SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
163 SDL_RWops *retval = NULL;
165 SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
167 retval = create_rwops(handle);
170 } /* PHYSFSRWOPS_makeRWops */
173 SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
175 return(create_rwops(PHYSFS_openRead(fname)));
176 } /* PHYSFSRWOPS_openRead */
179 SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
181 return(create_rwops(PHYSFS_openWrite(fname)));
182 } /* PHYSFSRWOPS_openWrite */
185 SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
187 return(create_rwops(PHYSFS_openAppend(fname)));
188 } /* PHYSFSRWOPS_openAppend */
191 /* end of physfsrwops.c ... */