Skip to content

Latest commit

 

History

History
253 lines (210 loc) · 6.84 KB

physfsrwops.c

File metadata and controls

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