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. This code has
15 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
16 * Please see LICENSE in the root of the source tree.
18 * SDL falls under the LGPL license. You can get SDL at http://www.libsdl.org/
20 * This file was written by Ryan C. Gordon. (icculus@clutteredmind.org).
23 #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
24 #include "physfsrwops.h"
26 static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
28 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
31 if (whence == SEEK_SET)
36 else if (whence == SEEK_CUR)
38 PHYSFS_sint64 current = PHYSFS_tell(handle);
41 SDL_SetError("Can't find position in file: %s",
42 PHYSFS_getLastError());
47 if ( ((PHYSFS_sint64) pos) != current )
49 SDL_SetError("Can't fit current file position in an int!");
53 if (offset == 0) /* this is a "tell" call. We're done. */
59 else if (whence == SEEK_END)
61 PHYSFS_sint64 len = PHYSFS_fileLength(handle);
64 SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
69 if ( ((PHYSFS_sint64) pos) != len )
71 SDL_SetError("Can't fit end-of-file position in an int!");
80 SDL_SetError("Invalid 'whence' parameter.");
86 SDL_SetError("Attempt to seek past start of file.");
90 if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
92 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
97 } /* physfsrwops_seek */
100 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
102 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
103 PHYSFS_sint64 rc = PHYSFS_read(handle, ptr, size, maxnum);
106 if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
107 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
111 } /* physfsrwops_read */
114 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
116 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
117 PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num);
119 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
122 } /* physfsrwops_write */
125 static int physfsrwops_close(SDL_RWops *rw)
127 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
128 if (!PHYSFS_close(handle))
130 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
136 } /* physfsrwops_close */
139 static SDL_RWops *create_rwops(PHYSFS_file *handle)
141 SDL_RWops *retval = NULL;
144 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
147 retval = SDL_AllocRW();
150 retval->seek = physfsrwops_seek;
151 retval->read = physfsrwops_read;
152 retval->write = physfsrwops_write;
153 retval->close = physfsrwops_close;
154 retval->hidden.unknown.data1 = handle;
162 SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_file *handle)
164 SDL_RWops *retval = NULL;
166 SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
168 retval = create_rwops(handle);
171 } /* PHYSFSRWOPS_makeRWops */
174 SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
176 return(create_rwops(PHYSFS_openRead(fname)));
177 } /* PHYSFSRWOPS_openRead */
180 SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
182 return(create_rwops(PHYSFS_openWrite(fname)));
183 } /* PHYSFSRWOPS_openWrite */
186 SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
188 return(create_rwops(PHYSFS_openAppend(fname)));
189 } /* PHYSFSRWOPS_openAppend */
192 /* end of physfsrwops.c ... */