Skip to content

Latest commit

 

History

History
251 lines (209 loc) · 6.6 KB

physfsrwops.c

File metadata and controls

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