author | Ryan C. Gordon <icculus@icculus.org> |
Sun, 05 Nov 2006 11:10:14 +0000 | |
changeset 785 | 1ea0f6549eb2 |
parent 767 | db29bf06d171 |
child 809 | 116b8fe30371 |
permissions | -rw-r--r-- |
123 | 1 |
/* |
2 |
* This code provides a glue layer between PhysicsFS and Simple Directmedia |
|
3 |
* Layer's (SDL) RWops i/o abstraction. |
|
4 |
* |
|
5 |
* License: this code is public domain. I make no warranty that it is useful, |
|
6 |
* correct, harmless, or environmentally safe. |
|
7 |
* |
|
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 |
|
576
5da65f8e9a50
Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents:
132
diff
changeset
|
12 |
* not require you to send me patches if you make changes. This code has |
5da65f8e9a50
Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents:
132
diff
changeset
|
13 |
* NO WARRANTY. |
123 | 14 |
* |
576
5da65f8e9a50
Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents:
132
diff
changeset
|
15 |
* Unless otherwise stated, the rest of PhysicsFS falls under the zlib license. |
5da65f8e9a50
Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents:
132
diff
changeset
|
16 |
* Please see LICENSE in the root of the source tree. |
123 | 17 |
* |
576
5da65f8e9a50
Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents:
132
diff
changeset
|
18 |
* SDL falls under the LGPL license. You can get SDL at http://www.libsdl.org/ |
123 | 19 |
* |
767
db29bf06d171
Changed my email address.
Ryan C. Gordon <icculus@icculus.org>
parents:
654
diff
changeset
|
20 |
* This file was written by Ryan C. Gordon. (icculus@icculus.org). |
123 | 21 |
*/ |
22 |
||
23 |
#include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */ |
|
24 |
#include "physfsrwops.h" |
|
25 |
||
26 |
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence) |
|
27 |
{ |
|
654
c0ae01de361d
Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents:
576
diff
changeset
|
28 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
123 | 29 |
int pos = 0; |
30 |
||
31 |
if (whence == SEEK_SET) |
|
32 |
{ |
|
33 |
pos = offset; |
|
34 |
} /* if */ |
|
35 |
||
36 |
else if (whence == SEEK_CUR) |
|
37 |
{ |
|
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
38 |
PHYSFS_sint64 current = PHYSFS_tell(handle); |
123 | 39 |
if (current == -1) |
40 |
{ |
|
41 |
SDL_SetError("Can't find position in file: %s", |
|
42 |
PHYSFS_getLastError()); |
|
43 |
return(-1); |
|
44 |
} /* if */ |
|
45 |
||
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
46 |
pos = (int) current; |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
47 |
if ( ((PHYSFS_sint64) pos) != current ) |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
48 |
{ |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
49 |
SDL_SetError("Can't fit current file position in an int!"); |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
50 |
return(-1); |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
51 |
} /* if */ |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
52 |
|
123 | 53 |
if (offset == 0) /* this is a "tell" call. We're done. */ |
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
54 |
return(pos); |
123 | 55 |
|
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
56 |
pos += offset; |
123 | 57 |
} /* else if */ |
58 |
||
59 |
else if (whence == SEEK_END) |
|
60 |
{ |
|
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
61 |
PHYSFS_sint64 len = PHYSFS_fileLength(handle); |
123 | 62 |
if (len == -1) |
63 |
{ |
|
64 |
SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError()); |
|
65 |
return(-1); |
|
66 |
} /* if */ |
|
67 |
||
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
68 |
pos = (int) len; |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
69 |
if ( ((PHYSFS_sint64) pos) != len ) |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
70 |
{ |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
71 |
SDL_SetError("Can't fit end-of-file position in an int!"); |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
72 |
return(-1); |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
73 |
} /* if */ |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
74 |
|
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
75 |
pos += offset; |
123 | 76 |
} /* else if */ |
77 |
||
78 |
else |
|
79 |
{ |
|
80 |
SDL_SetError("Invalid 'whence' parameter."); |
|
81 |
return(-1); |
|
82 |
} /* else */ |
|
83 |
||
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
84 |
if ( pos < 0 ) |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
85 |
{ |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
86 |
SDL_SetError("Attempt to seek past start of file."); |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
87 |
return(-1); |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
88 |
} /* if */ |
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
89 |
|
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
90 |
if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos)) |
123 | 91 |
{ |
92 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); |
|
93 |
return(-1); |
|
94 |
} /* if */ |
|
95 |
||
96 |
return(pos); |
|
97 |
} /* physfsrwops_seek */ |
|
98 |
||
99 |
||
100 |
static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum) |
|
101 |
{ |
|
654
c0ae01de361d
Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents:
576
diff
changeset
|
102 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
103 |
PHYSFS_sint64 rc = PHYSFS_read(handle, ptr, size, maxnum); |
123 | 104 |
if (rc != maxnum) |
105 |
{ |
|
106 |
if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */ |
|
107 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); |
|
108 |
} /* if */ |
|
109 |
||
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
110 |
return((int) rc); |
123 | 111 |
} /* physfsrwops_read */ |
112 |
||
113 |
||
114 |
static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num) |
|
115 |
{ |
|
654
c0ae01de361d
Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents:
576
diff
changeset
|
116 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
117 |
PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num); |
123 | 118 |
if (rc != num) |
119 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); |
|
120 |
||
132
b53fa5093749
Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents:
123
diff
changeset
|
121 |
return((int) rc); |
123 | 122 |
} /* physfsrwops_write */ |
123 |
||
124 |
||
125 |
static int physfsrwops_close(SDL_RWops *rw) |
|
126 |
{ |
|
654
c0ae01de361d
Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents:
576
diff
changeset
|
127 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
123 | 128 |
if (!PHYSFS_close(handle)) |
129 |
{ |
|
130 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); |
|
131 |
return(-1); |
|
132 |
} /* if */ |
|
133 |
||
134 |
SDL_FreeRW(rw); |
|
135 |
return(0); |
|
136 |
} /* physfsrwops_close */ |
|
137 |
||
138 |
||
654
c0ae01de361d
Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents:
576
diff
changeset
|
139 |
static SDL_RWops *create_rwops(PHYSFS_File *handle) |
123 | 140 |
{ |
141 |
SDL_RWops *retval = NULL; |
|
142 |
||
143 |
if (handle == NULL) |
|
144 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); |
|
145 |
else |
|
146 |
{ |
|
147 |
retval = SDL_AllocRW(); |
|
148 |
if (retval != NULL) |
|
149 |
{ |
|
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; |
|
155 |
} /* if */ |
|
156 |
} /* else */ |
|
157 |
||
158 |
return(retval); |
|
159 |
} /* create_rwops */ |
|
160 |
||
161 |
||
654
c0ae01de361d
Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents:
576
diff
changeset
|
162 |
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle) |
123 | 163 |
{ |
164 |
SDL_RWops *retval = NULL; |
|
165 |
if (handle == NULL) |
|
166 |
SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops()."); |
|
167 |
else |
|
168 |
retval = create_rwops(handle); |
|
169 |
||
170 |
return(retval); |
|
171 |
} /* PHYSFSRWOPS_makeRWops */ |
|
172 |
||
173 |
||
174 |
SDL_RWops *PHYSFSRWOPS_openRead(const char *fname) |
|
175 |
{ |
|
176 |
return(create_rwops(PHYSFS_openRead(fname))); |
|
177 |
} /* PHYSFSRWOPS_openRead */ |
|
178 |
||
179 |
||
180 |
SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname) |
|
181 |
{ |
|
182 |
return(create_rwops(PHYSFS_openWrite(fname))); |
|
183 |
} /* PHYSFSRWOPS_openWrite */ |
|
184 |
||
185 |
||
186 |
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname) |
|
187 |
{ |
|
188 |
return(create_rwops(PHYSFS_openAppend(fname))); |
|
189 |
} /* PHYSFSRWOPS_openAppend */ |
|
190 |
||
191 |
||
192 |
/* end of physfsrwops.c ... */ |
|
193 |