-
Notifications
You must be signed in to change notification settings - Fork 279
/
a5_physfs.c
316 lines (238 loc) · 5.88 KB
/
a5_physfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* PhysicsFS addon for Allegro.
*
* By Peter Wang.
*/
#include <physfs.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_physfs.h"
#include "allegro_physfs_intern.h"
ALLEGRO_DEBUG_CHANNEL("physfs")
typedef struct ALLEGRO_FILE_PHYSFS ALLEGRO_FILE_PHYSFS;
struct ALLEGRO_FILE_PHYSFS
{
PHYSFS_file *phys;
bool error_indicator;
char error_msg[80];
};
/* forward declaration */
static const ALLEGRO_FILE_INTERFACE file_phys_vtable;
const ALLEGRO_FILE_INTERFACE *_al_get_phys_vtable(void)
{
return &file_phys_vtable;
}
#define streq(a, b) (0 == strcmp(a, b))
static ALLEGRO_FILE_PHYSFS *cast_stream(ALLEGRO_FILE *f)
{
return (ALLEGRO_FILE_PHYSFS *)al_get_file_userdata(f);
}
static void phys_set_errno(ALLEGRO_FILE_PHYSFS *fp)
{
/* It might be worth mapping some common error strings from
* PHYSFS_getLastError() onto errno values. There are no guarantees,
* though.
*/
al_set_errno(-1);
PHYSFS_ErrorCode error = PHYSFS_getLastErrorCode();
const char* msg = PHYSFS_getErrorByCode(error);
if (error != PHYSFS_ERR_OK) {
ALLEGRO_ERROR("PhysFS error code: %s\n", msg);
}
if (fp) {
fp->error_indicator = true;
if (error != PHYSFS_ERR_OK) {
strncpy(fp->error_msg, msg, sizeof(fp->error_msg));
fp->error_msg[sizeof(fp->error_msg) - 1] = '\0';
}
else {
fp->error_msg[0] = '\0';
}
}
}
static void *file_phys_fopen(const char *filename, const char *mode)
{
ALLEGRO_USTR *us;
PHYSFS_file *phys;
ALLEGRO_FILE_PHYSFS *fp;
us = _al_physfs_process_path(filename);
/* XXX handle '+' modes */
/* It might be worth adding a function to parse mode strings, to be
* shared amongst these kinds of addons.
*/
if (streq(mode, "r") || streq(mode, "rb"))
phys = PHYSFS_openRead(al_cstr(us));
else if (streq(mode, "w") || streq(mode, "wb"))
phys = PHYSFS_openWrite(al_cstr(us));
else if (streq(mode, "a") || streq(mode, "ab"))
phys = PHYSFS_openAppend(al_cstr(us));
else
phys = NULL;
al_ustr_free(us);
if (!phys) {
phys_set_errno(NULL);
return NULL;
}
fp = al_malloc(sizeof(*fp));
if (!fp) {
al_set_errno(ENOMEM);
PHYSFS_close(phys);
return NULL;
}
fp->phys = phys;
fp->error_indicator = false;
fp->error_msg[0] = '\0';
return fp;
}
static bool file_phys_fclose(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
PHYSFS_file *phys_fp = fp->phys;
al_free(fp);
if (PHYSFS_close(phys_fp) == 0) {
al_set_errno(-1);
return false;
}
return true;
}
static size_t file_phys_fread(ALLEGRO_FILE *f, void *buf, size_t buf_size)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
PHYSFS_sint64 n;
if (buf_size == 0)
return 0;
n = PHYSFS_readBytes(fp->phys, buf, buf_size);
if (n < 0) {
phys_set_errno(fp);
return 0;
}
return n;
}
static size_t file_phys_fwrite(ALLEGRO_FILE *f, const void *buf,
size_t buf_size)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
PHYSFS_sint64 n;
n = PHYSFS_writeBytes(fp->phys, buf, buf_size);
if (n < 0) {
phys_set_errno(fp);
return 0;
}
return n;
}
static bool file_phys_fflush(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
if (!PHYSFS_flush(fp->phys)) {
phys_set_errno(fp);
return false;
}
return true;
}
static int64_t file_phys_ftell(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
PHYSFS_sint64 n;
n = PHYSFS_tell(fp->phys);
if (n < 0) {
phys_set_errno(fp);
return -1;
}
return n;
}
static bool file_phys_seek(ALLEGRO_FILE *f, int64_t offset, int whence)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
PHYSFS_sint64 base;
switch (whence) {
case ALLEGRO_SEEK_SET:
base = 0;
break;
case ALLEGRO_SEEK_CUR:
base = PHYSFS_tell(fp->phys);
if (base < 0) {
phys_set_errno(fp);
return false;
}
break;
case ALLEGRO_SEEK_END:
base = PHYSFS_fileLength(fp->phys);
if (base < 0) {
phys_set_errno(fp);
return false;
}
break;
default:
al_set_errno(EINVAL);
return false;
}
if (!PHYSFS_seek(fp->phys, base + offset)) {
phys_set_errno(fp);
return false;
}
return true;
}
static bool file_phys_feof(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
return PHYSFS_eof(fp->phys);
}
static int file_phys_ferror(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
return (fp->error_indicator) ? 1 : 0;
}
static const char *file_phys_ferrmsg(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
if (fp->error_indicator)
return fp->error_msg;
else
return "";
}
static void file_phys_fclearerr(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
fp->error_indicator = false;
/* PhysicsFS doesn't provide a way to clear the EOF indicator. */
}
static off_t file_phys_fsize(ALLEGRO_FILE *f)
{
ALLEGRO_FILE_PHYSFS *fp = cast_stream(f);
PHYSFS_sint64 n;
n = PHYSFS_fileLength(fp->phys);
if (n < 0) {
phys_set_errno(fp);
return -1;
}
return n;
}
static const ALLEGRO_FILE_INTERFACE file_phys_vtable =
{
file_phys_fopen,
file_phys_fclose,
file_phys_fread,
file_phys_fwrite,
file_phys_fflush,
file_phys_ftell,
file_phys_seek,
file_phys_feof,
file_phys_ferror,
file_phys_ferrmsg,
file_phys_fclearerr,
NULL, /* ungetc */
file_phys_fsize
};
/* Function: al_set_physfs_file_interface
*/
void al_set_physfs_file_interface(void)
{
al_set_new_file_interface(&file_phys_vtable);
_al_set_physfs_fs_interface();
}
/* Function: al_get_allegro_physfs_version
*/
uint32_t al_get_allegro_physfs_version(void)
{
return ALLEGRO_VERSION_INT;
}
/* vim: set sts=3 sw=3 et: */