Skip to content

Latest commit

 

History

History
245 lines (203 loc) · 6.52 KB

physfsrwops.c

File metadata and controls

245 lines (203 loc) · 6.52 KB
 
Mar 16, 2002
Mar 16, 2002
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
Jul 20, 2003
Jul 20, 2003
12
13
* not require you to send me patches if you make changes. This code has
* NO WARRANTY.
Mar 16, 2002
Mar 16, 2002
14
*
Jul 20, 2003
Jul 20, 2003
15
* Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
Mar 11, 2007
Mar 11, 2007
16
* Please see LICENSE.txt in the root of the source tree.
Mar 16, 2002
Mar 16, 2002
17
*
Oct 19, 2011
Oct 19, 2011
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/
Mar 16, 2002
Mar 16, 2002
20
*
Jan 1, 2006
Jan 1, 2006
21
* This file was written by Ryan C. Gordon. (icculus@icculus.org).
Mar 16, 2002
Mar 16, 2002
22
23
24
25
26
*/
#include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
#include "physfsrwops.h"
Jul 28, 2015
Jul 28, 2015
27
/* SDL's RWOPS interface changed a little in SDL 2.0... */
Oct 19, 2011
Oct 19, 2011
28
#if defined(SDL_VERSION_ATLEAST)
Jul 28, 2015
Jul 28, 2015
29
30
#if SDL_VERSION_ATLEAST(2, 0, 0)
#define TARGET_SDL2 1
Oct 19, 2011
Oct 19, 2011
31
32
33
#endif
#endif
Jul 28, 2015
Jul 28, 2015
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)
Oct 19, 2011
Oct 19, 2011
51
#else
Mar 16, 2002
Mar 16, 2002
52
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
Oct 19, 2011
Oct 19, 2011
53
#endif
Mar 16, 2002
Mar 16, 2002
54
{
Sep 26, 2004
Sep 26, 2004
55
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
Oct 19, 2011
Oct 19, 2011
56
PHYSFS_sint64 pos = 0;
Mar 16, 2002
Mar 16, 2002
57
Jul 28, 2015
Jul 28, 2015
58
if (whence == RW_SEEK_SET)
Oct 19, 2011
Oct 19, 2011
59
pos = (PHYSFS_sint64) offset;
Mar 16, 2002
Mar 16, 2002
60
Jul 28, 2015
Jul 28, 2015
61
else if (whence == RW_SEEK_CUR)
Mar 16, 2002
Mar 16, 2002
62
{
Oct 19, 2011
Oct 19, 2011
63
const PHYSFS_sint64 current = PHYSFS_tell(handle);
Mar 16, 2002
Mar 16, 2002
64
65
66
67
if (current == -1)
{
SDL_SetError("Can't find position in file: %s",
PHYSFS_getLastError());
Jan 28, 2010
Jan 28, 2010
68
return -1;
Mar 16, 2002
Mar 16, 2002
69
70
} /* if */
Oct 19, 2011
Oct 19, 2011
71
if (offset == 0) /* this is a "tell" call. We're done. */
Mar 24, 2002
Mar 24, 2002
72
{
Jul 28, 2015
Jul 28, 2015
73
74
#if TARGET_SDL2
return (Sint64) current;
Oct 19, 2011
Oct 19, 2011
75
76
77
#else
return (int) current;
#endif
Mar 24, 2002
Mar 24, 2002
78
79
} /* if */
Oct 19, 2011
Oct 19, 2011
80
pos = current + ((PHYSFS_sint64) offset);
Mar 16, 2002
Mar 16, 2002
81
82
} /* else if */
Jul 28, 2015
Jul 28, 2015
83
else if (whence == RW_SEEK_END)
Mar 16, 2002
Mar 16, 2002
84
{
Oct 19, 2011
Oct 19, 2011
85
const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
Mar 16, 2002
Mar 16, 2002
86
87
88
if (len == -1)
{
SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
Jan 28, 2010
Jan 28, 2010
89
return -1;
Mar 16, 2002
Mar 16, 2002
90
91
} /* if */
Oct 19, 2011
Oct 19, 2011
92
pos = len + ((PHYSFS_sint64) offset);
Mar 16, 2002
Mar 16, 2002
93
94
95
96
97
} /* else if */
else
{
SDL_SetError("Invalid 'whence' parameter.");
Jan 28, 2010
Jan 28, 2010
98
return -1;
Mar 16, 2002
Mar 16, 2002
99
100
} /* else */
Mar 24, 2002
Mar 24, 2002
101
102
103
if ( pos < 0 )
{
SDL_SetError("Attempt to seek past start of file.");
Jan 28, 2010
Jan 28, 2010
104
return -1;
Mar 24, 2002
Mar 24, 2002
105
106
107
} /* if */
if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
Mar 16, 2002
Mar 16, 2002
108
109
{
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
Jan 28, 2010
Jan 28, 2010
110
return -1;
Mar 16, 2002
Mar 16, 2002
111
112
} /* if */
Jul 28, 2015
Jul 28, 2015
113
114
#if TARGET_SDL2
return (Sint64) pos;
Oct 19, 2011
Oct 19, 2011
115
116
117
#else
return (int) pos;
#endif
Mar 16, 2002
Mar 16, 2002
118
119
120
} /* physfsrwops_seek */
Jul 28, 2015
Jul 28, 2015
121
#if TARGET_SDL2
Oct 19, 2011
Oct 19, 2011
122
123
124
static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr,
size_t size, size_t maxnum)
#else
Mar 16, 2002
Mar 16, 2002
125
static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
Oct 19, 2011
Oct 19, 2011
126
#endif
Mar 16, 2002
Mar 16, 2002
127
{
Sep 26, 2004
Sep 26, 2004
128
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
Oct 19, 2011
Oct 19, 2011
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))
Mar 16, 2002
Mar 16, 2002
132
133
{
if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
Jul 28, 2015
Jul 28, 2015
134
{
Mar 16, 2002
Mar 16, 2002
135
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
Jul 28, 2015
Jul 28, 2015
136
137
138
139
140
141
142
#if TARGET_SDL2
return 0;
#else
return -1;
#endif
} /* if */
Mar 16, 2002
Mar 16, 2002
143
144
} /* if */
Jul 28, 2015
Jul 28, 2015
145
146
#if TARGET_SDL2
return (size_t) rc / size;
Oct 19, 2011
Oct 19, 2011
147
#else
Jul 28, 2015
Jul 28, 2015
148
return (int) rc / size;
Oct 19, 2011
Oct 19, 2011
149
#endif
Mar 16, 2002
Mar 16, 2002
150
151
152
} /* physfsrwops_read */
Jul 28, 2015
Jul 28, 2015
153
#if TARGET_SDL2
Oct 19, 2011
Oct 19, 2011
154
155
156
static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr,
size_t size, size_t num)
#else
Mar 16, 2002
Mar 16, 2002
157
static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
Oct 19, 2011
Oct 19, 2011
158
#endif
Mar 16, 2002
Mar 16, 2002
159
{
Sep 26, 2004
Sep 26, 2004
160
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
Oct 19, 2011
Oct 19, 2011
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))
Mar 16, 2002
Mar 16, 2002
164
165
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
Jul 28, 2015
Jul 28, 2015
166
#if TARGET_SDL2
Oct 19, 2011
Oct 19, 2011
167
168
169
170
return (size_t) rc;
#else
return (int) rc;
#endif
Mar 16, 2002
Mar 16, 2002
171
172
173
174
175
} /* physfsrwops_write */
static int physfsrwops_close(SDL_RWops *rw)
{
Sep 26, 2004
Sep 26, 2004
176
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
Mar 16, 2002
Mar 16, 2002
177
178
179
if (!PHYSFS_close(handle))
{
SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
Jan 28, 2010
Jan 28, 2010
180
return -1;
Mar 16, 2002
Mar 16, 2002
181
182
183
} /* if */
SDL_FreeRW(rw);
Jan 28, 2010
Jan 28, 2010
184
return 0;
Mar 16, 2002
Mar 16, 2002
185
186
187
} /* physfsrwops_close */
Sep 26, 2004
Sep 26, 2004
188
static SDL_RWops *create_rwops(PHYSFS_File *handle)
Mar 16, 2002
Mar 16, 2002
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)
{
Jul 28, 2015
Jul 28, 2015
199
200
201
#if TARGET_SDL2
retval->size = physfsrwops_size;
#endif
Mar 16, 2002
Mar 16, 2002
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 */
Jan 28, 2010
Jan 28, 2010
210
return retval;
Mar 16, 2002
Mar 16, 2002
211
212
213
} /* create_rwops */
Sep 26, 2004
Sep 26, 2004
214
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
Mar 16, 2002
Mar 16, 2002
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);
Jan 28, 2010
Jan 28, 2010
222
return retval;
Mar 16, 2002
Mar 16, 2002
223
224
225
226
227
} /* PHYSFSRWOPS_makeRWops */
SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
{
Jan 28, 2010
Jan 28, 2010
228
return create_rwops(PHYSFS_openRead(fname));
Mar 16, 2002
Mar 16, 2002
229
230
231
232
233
} /* PHYSFSRWOPS_openRead */
SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
{
Jan 28, 2010
Jan 28, 2010
234
return create_rwops(PHYSFS_openWrite(fname));
Mar 16, 2002
Mar 16, 2002
235
236
237
238
239
} /* PHYSFSRWOPS_openWrite */
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
{
Jan 28, 2010
Jan 28, 2010
240
return create_rwops(PHYSFS_openAppend(fname));
Mar 16, 2002
Mar 16, 2002
241
242
243
244
} /* PHYSFSRWOPS_openAppend */
/* end of physfsrwops.c ... */