author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 31 Oct 2018 14:33:43 +0100 | |
changeset 14047 | 006f97a6f6a7 |
parent 13918 | f8b5708835de |
permissions | -rw-r--r-- |
7768 | 1 |
/* |
2 |
* This code provides a glue layer between PhysicsFS and Simple Directmedia |
|
3 |
* Layer's (SDL) RWops i/o abstraction. |
|
4 |
* |
|
5 |
* License: this code is public domain. I make no warranty that it is useful, |
|
6 |
* correct, harmless, or environmentally safe. |
|
7 |
* |
|
8 |
* This particular file may be used however you like, including copying it |
|
9 |
* verbatim into a closed-source project, exploiting it commercially, and |
|
10 |
* removing any trace of my name from the source (although I hope you won't |
|
11 |
* do that). I welcome enhancements and corrections to this file, but I do |
|
12 |
* not require you to send me patches if you make changes. This code has |
|
13 |
* 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 http://www.libsdl.org/ |
|
20 |
* |
|
21 |
* This file was written by Ryan C. Gordon. (icculus@icculus.org). |
|
22 |
*/ |
|
23 |
||
24 |
#include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */ |
|
25 |
#include "physfsrwops.h" |
|
26 |
||
27 |
/* SDL's RWOPS interface changed a little in SDL 1.3... */ |
|
28 |
#if defined(SDL_VERSION_ATLEAST) |
|
29 |
#if SDL_VERSION_ATLEAST(1, 3, 0) |
|
30 |
#define TARGET_SDL13 1 |
|
31 |
#endif |
|
32 |
#endif |
|
33 |
||
34 |
#if TARGET_SDL13 |
|
13918
f8b5708835de
.. or rather move calling conventions to proper position
alfadur
parents:
13917
diff
changeset
|
35 |
static Sint64 SDLCALL physfsrwops_size(struct SDL_RWops *rw) |
9378
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
36 |
{ |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
37 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
38 |
return PHYSFS_fileLength(handle); |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
39 |
} |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
40 |
#endif |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
41 |
|
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
42 |
#if TARGET_SDL13 |
13918
f8b5708835de
.. or rather move calling conventions to proper position
alfadur
parents:
13917
diff
changeset
|
43 |
static Sint64 SDLCALL physfsrwops_seek(struct SDL_RWops *rw, Sint64 offset, int whence) |
7768 | 44 |
#else |
45 |
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence) |
|
46 |
#endif |
|
47 |
{ |
|
48 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
49 |
PHYSFS_sint64 pos = 0; |
|
50 |
||
51 |
if (whence == SEEK_SET) |
|
52 |
pos = (PHYSFS_sint64) offset; |
|
53 |
else if (whence == SEEK_CUR) |
|
54 |
{ |
|
55 |
const PHYSFS_sint64 current = PHYSFS_tell(handle); |
|
56 |
if (current == -1) |
|
57 |
{ |
|
58 |
SDL_SetError("Can't find position in file: %s", |
|
13543
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11661
diff
changeset
|
59 |
PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 60 |
return -1; |
61 |
} /* if */ |
|
62 |
||
63 |
if (offset == 0) /* this is a "tell" call. We're done. */ |
|
64 |
{ |
|
65 |
#if TARGET_SDL13 |
|
66 |
return (long) current; |
|
67 |
#else |
|
68 |
return (int) current; |
|
69 |
#endif |
|
70 |
} /* if */ |
|
71 |
||
72 |
pos = current + ((PHYSFS_sint64) offset); |
|
73 |
} /* else if */ |
|
74 |
||
75 |
else if (whence == SEEK_END) |
|
76 |
{ |
|
77 |
const PHYSFS_sint64 len = PHYSFS_fileLength(handle); |
|
78 |
if (len == -1) |
|
79 |
{ |
|
13543
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11661
diff
changeset
|
80 |
SDL_SetError("Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 81 |
return -1; |
82 |
} /* if */ |
|
83 |
||
84 |
pos = len + ((PHYSFS_sint64) offset); |
|
85 |
} /* else if */ |
|
86 |
||
87 |
else |
|
88 |
{ |
|
89 |
SDL_SetError("Invalid 'whence' parameter."); |
|
90 |
return -1; |
|
91 |
} /* else */ |
|
92 |
||
93 |
if ( pos < 0 ) |
|
94 |
{ |
|
95 |
SDL_SetError("Attempt to seek past start of file."); |
|
96 |
return -1; |
|
97 |
} /* if */ |
|
10017 | 98 |
|
7768 | 99 |
if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos)) |
100 |
{ |
|
13543
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11661
diff
changeset
|
101 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 102 |
return -1; |
103 |
} /* if */ |
|
104 |
||
105 |
#if TARGET_SDL13 |
|
106 |
return (long) pos; |
|
107 |
#else |
|
108 |
return (int) pos; |
|
109 |
#endif |
|
110 |
} /* physfsrwops_seek */ |
|
111 |
||
112 |
||
113 |
#if TARGET_SDL13 |
|
114 |
static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr, |
|
115 |
size_t size, size_t maxnum) |
|
116 |
#else |
|
117 |
static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum) |
|
118 |
#endif |
|
119 |
{ |
|
120 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
121 |
const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size); |
|
122 |
const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen); |
|
123 |
if (rc != ((PHYSFS_sint64) readlen)) |
|
124 |
{ |
|
125 |
if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */ |
|
13543
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11661
diff
changeset
|
126 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 127 |
} /* if */ |
128 |
||
129 |
#if TARGET_SDL13 |
|
130 |
return (size_t) rc; |
|
131 |
#else |
|
132 |
return (int) rc; |
|
133 |
#endif |
|
134 |
} /* physfsrwops_read */ |
|
135 |
||
136 |
||
137 |
#if TARGET_SDL13 |
|
138 |
static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr, |
|
139 |
size_t size, size_t num) |
|
140 |
#else |
|
141 |
static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num) |
|
142 |
#endif |
|
143 |
{ |
|
144 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
145 |
const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size); |
|
146 |
const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen); |
|
147 |
if (rc != ((PHYSFS_sint64) writelen)) |
|
13543
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11661
diff
changeset
|
148 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 149 |
|
150 |
#if TARGET_SDL13 |
|
151 |
return (size_t) rc; |
|
152 |
#else |
|
153 |
return (int) rc; |
|
154 |
#endif |
|
155 |
} /* physfsrwops_write */ |
|
156 |
||
157 |
||
158 |
static int physfsrwops_close(SDL_RWops *rw) |
|
159 |
{ |
|
160 |
PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1; |
|
161 |
if (!PHYSFS_close(handle)) |
|
162 |
{ |
|
13543
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11661
diff
changeset
|
163 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 164 |
return -1; |
165 |
} /* if */ |
|
166 |
||
167 |
SDL_FreeRW(rw); |
|
168 |
return 0; |
|
169 |
} /* physfsrwops_close */ |
|
170 |
||
171 |
||
172 |
static SDL_RWops *create_rwops(PHYSFS_File *handle) |
|
173 |
{ |
|
174 |
SDL_RWops *retval = NULL; |
|
175 |
||
176 |
if (handle == NULL) |
|
13543
f8c0a62fa3ac
Fix deprecation warnings in libphyslayer
Wuzzy <Wuzzy2@mail.ru>
parents:
11661
diff
changeset
|
177 |
SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); |
7768 | 178 |
else |
179 |
{ |
|
180 |
retval = SDL_AllocRW(); |
|
181 |
if (retval != NULL) |
|
182 |
{ |
|
11661 | 183 |
#if TARGET_SDL13 && !defined(EMSCRIPTEN) |
9378
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
184 |
retval->size = physfsrwops_size; |
2be457289e60
update physlayer and sdl bindings to the new rwops interface
koda
parents:
9309
diff
changeset
|
185 |
#endif |
7768 | 186 |
retval->seek = physfsrwops_seek; |
187 |
retval->read = physfsrwops_read; |
|
188 |
retval->write = physfsrwops_write; |
|
189 |
retval->close = physfsrwops_close; |
|
190 |
retval->hidden.unknown.data1 = handle; |
|
191 |
} /* if */ |
|
192 |
} /* else */ |
|
193 |
||
194 |
return retval; |
|
195 |
} /* create_rwops */ |
|
196 |
||
197 |
||
198 |
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle) |
|
199 |
{ |
|
200 |
SDL_RWops *retval = NULL; |
|
201 |
if (handle == NULL) |
|
202 |
SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops()."); |
|
203 |
else |
|
204 |
retval = create_rwops(handle); |
|
205 |
||
206 |
return retval; |
|
207 |
} /* PHYSFSRWOPS_makeRWops */ |
|
208 |
||
209 |
||
210 |
SDL_RWops *PHYSFSRWOPS_openRead(const char *fname) |
|
211 |
{ |
|
212 |
return create_rwops(PHYSFS_openRead(fname)); |
|
213 |
} /* PHYSFSRWOPS_openRead */ |
|
214 |
||
215 |
||
216 |
SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname) |
|
217 |
{ |
|
218 |
return create_rwops(PHYSFS_openWrite(fname)); |
|
219 |
} /* PHYSFSRWOPS_openWrite */ |
|
220 |
||
221 |
||
222 |
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname) |
|
223 |
{ |
|
224 |
return create_rwops(PHYSFS_openAppend(fname)); |
|
225 |
} /* PHYSFSRWOPS_openAppend */ |
|
226 |
||
227 |
||
228 |
/* end of physfsrwops.c ... */ |
|
229 |