/
physfsrwops.c
251 lines (209 loc) · 6.78 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
* SDL 1.2 falls under the LGPL license. SDL 1.3+ is zlib, like PhysicsFS.
19
* You can get SDL at https://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
#if !TARGET_SDL2
35
#ifndef RW_SEEK_SET
36
#define RW_SEEK_SET SEEK_SET
37
38
#endif
#ifndef RW_SEEK_CUR
39
#define RW_SEEK_CUR SEEK_CUR
40
41
#endif
#ifndef RW_SEEK_END
42
43
#define RW_SEEK_END SEEK_END
#endif
44
#endif
45
46
47
48
49
50
51
52
53
54
55
56
#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)
57
#else
58
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
59
#endif
60
{
61
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
62
PHYSFS_sint64 pos = 0;
63
64
if (whence == RW_SEEK_SET)
65
pos = (PHYSFS_sint64) offset;
66
67
else if (whence == RW_SEEK_CUR)
68
{
69
const PHYSFS_sint64 current = PHYSFS_tell(handle);
70
71
72
if (current == -1)
{
SDL_SetError("Can't find position in file: %s",
73
PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
74
return -1;
75
76
} /* if */
77
if (offset == 0) /* this is a "tell" call. We're done. */
78
{
79
80
#if TARGET_SDL2
return (Sint64) current;
81
82
83
#else
return (int) current;
#endif
84
85
} /* if */
86
pos = current + ((PHYSFS_sint64) offset);
87
88
} /* else if */
89
else if (whence == RW_SEEK_END)
90
{
91
const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
92
93
if (len == -1)
{
94
SDL_SetError("Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
95
return -1;
96
97
} /* if */
98
pos = len + ((PHYSFS_sint64) offset);
99
100
101
102
103
} /* else if */
else
{
SDL_SetError("Invalid 'whence' parameter.");
104
return -1;
105
106
} /* else */
107
108
109
if ( pos < 0 )
{
SDL_SetError("Attempt to seek past start of file.");
110
return -1;
111
112
113
} /* if */
if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
114
{
115
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
116
return -1;
117
118
} /* if */
119
120
#if TARGET_SDL2
return (Sint64) pos;
121
122
123
#else
return (int) pos;
#endif
124
125
126
} /* physfsrwops_seek */
127
#if TARGET_SDL2
128
129
130
static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr,
size_t size, size_t maxnum)
#else
131
static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
132
#endif
133
{
134
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
135
136
137
const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size);
const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen);
if (rc != ((PHYSFS_sint64) readlen))
138
139
{
if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
140
{
141
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
142
143
144
145
146
147
148
#if TARGET_SDL2
return 0;
#else
return -1;
#endif
} /* if */
149
150
} /* if */
151
152
#if TARGET_SDL2
return (size_t) rc / size;
153
#else
154
return (int) rc / size;
155
#endif
156
157
158
} /* physfsrwops_read */
159
#if TARGET_SDL2
160
161
162
static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr,
size_t size, size_t num)
#else
163
static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
164
#endif
165
{
166
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
167
168
169
const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size);
const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen);
if (rc != ((PHYSFS_sint64) writelen))
170
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
171
172
#if TARGET_SDL2
173
174
175
176
return (size_t) rc;
#else
return (int) rc;
#endif
177
178
179
180
181
} /* physfsrwops_write */
static int physfsrwops_close(SDL_RWops *rw)
{
182
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
183
184
if (!PHYSFS_close(handle))
{
185
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
186
return -1;
187
188
189
} /* if */
SDL_FreeRW(rw);
190
return 0;
191
192
193
} /* physfsrwops_close */
194
static SDL_RWops *create_rwops(PHYSFS_File *handle)
195
196
197
198
{
SDL_RWops *retval = NULL;
if (handle == NULL)
199
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
200
201
202
203
204
else
{
retval = SDL_AllocRW();
if (retval != NULL)
{
205
206
207
#if TARGET_SDL2
retval->size = physfsrwops_size;
#endif
208
209
210
211
212
213
214
215
retval->seek = physfsrwops_seek;
retval->read = physfsrwops_read;
retval->write = physfsrwops_write;
retval->close = physfsrwops_close;
retval->hidden.unknown.data1 = handle;
} /* if */
} /* else */
216
return retval;
217
218
219
} /* create_rwops */
220
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
221
222
223
224
225
226
227
{
SDL_RWops *retval = NULL;
if (handle == NULL)
SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
else
retval = create_rwops(handle);
228
return retval;
229
230
231
232
233
} /* PHYSFSRWOPS_makeRWops */
SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
{
234
return create_rwops(PHYSFS_openRead(fname));
235
236
237
238
239
} /* PHYSFSRWOPS_openRead */
SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
{
240
return create_rwops(PHYSFS_openWrite(fname));
241
242
243
244
245
} /* PHYSFSRWOPS_openWrite */
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
{
246
return create_rwops(PHYSFS_openAppend(fname));
247
248
249
250
} /* PHYSFSRWOPS_openAppend */
/* end of physfsrwops.c ... */