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