Mercurial > hg > dmlib
annotate dmres.c @ 235:4a672d96978f
Add dmfgets() function.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 08 Oct 2012 15:45:38 +0300 |
parents | c6b4fa03744c |
children | 8189be40ed7c |
rev | line source |
---|---|
0 | 1 /* |
2 * dmlib | |
3 * -- Resource management | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
5 * (C) Copyright 2003-2012 Tecnic Software productions (TNSP) | |
6 */ | |
7 #include "dmres.h" | |
8 #include <time.h> | |
9 | |
10 | |
11 #define DMRES_LOCK(x) dmMutexLock(dfResourcesMutex) | |
12 #define DMRES_UNLOCK(x) dmMutexUnlock(dfResourcesMutex) | |
13 | |
14 | |
15 /* Global variables | |
16 */ | |
17 static BOOL dfResInitialized = FALSE; | |
18 static int dfResFlags = 0; | |
19 static char * dfResPath = NULL; | |
20 DMResource * dfResources = NULL; | |
21 DMMutex * dfResourcesMutex = NULL; | |
22 | |
23 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
24 #ifdef DM_USE_PACKFS |
0 | 25 static DMPackFile *dfResPackFile = NULL; |
26 static char * dfResPackFilename = NULL; | |
27 #endif | |
28 | |
29 | |
30 DMResource *dmres_new(const char *filename, int flags, size_t size) | |
31 { | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
32 DMResource *node = dmMalloc0(sizeof(DMResource)); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
33 if (node == NULL) |
0 | 34 return NULL; |
35 | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
36 node->filename = dm_strdup(filename); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
37 node->flags = flags; |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
38 node->dataSize = size; |
0 | 39 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
40 return node; |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
41 } |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
42 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
43 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
44 void dmres_free_res_data(DMResource *node) |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
45 { |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
46 if (node->rdata != NULL && |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
47 node->rops != NULL && |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
48 node->rops->free != NULL) |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
49 { |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
50 node->rops->free(node); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
51 } |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
52 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
53 node->rdata = NULL; |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
54 node->flags &= !DMF_LOADED_RES; |
0 | 55 } |
56 | |
57 | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
58 void dmres_free_raw_data(DMResource *node) |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
59 { |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
60 dmFree(node->data); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
61 node->data = NULL; |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
62 node->flags &= !DMF_LOADED_RAW; |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
63 } |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
64 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
65 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
66 void dmres_purge_raw_data(DMResource *node) |
0 | 67 { |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
68 if ((node->flags & DMF_PRELOAD_RAW) == 0 && |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
69 (node->flags & DMF_LOADED_RAW) && |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
70 node->data != NULL) |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
71 dmres_free_raw_data(node); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
72 } |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
73 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
74 |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
75 void dmres_free(DMResource *node) |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
76 { |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
77 if (node != NULL) |
0 | 78 { |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
79 dmres_free_res_data(node); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
80 dmres_free_raw_data(node); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
81 dmFree(node->filename); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
82 dmFree(node); |
0 | 83 } |
84 } | |
85 | |
86 | |
208
c6b4fa03744c
Make some functions static, some public in the header.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
87 static void dmres_insert(DMResource * node) |
0 | 88 { |
89 if (dfResources != NULL) | |
90 { | |
91 node->prev = dfResources->prev; | |
92 dfResources->prev->next = node; | |
93 dfResources->prev = node; | |
94 } | |
95 else | |
96 { | |
97 dfResources = node->prev = node; | |
98 } | |
99 | |
100 node->next = NULL; | |
101 } | |
102 | |
103 | |
208
c6b4fa03744c
Make some functions static, some public in the header.
Matti Hamalainen <ccr@tnsp.org>
parents:
166
diff
changeset
|
104 static void dmres_delete(DMResource * node) |
0 | 105 { |
106 if (node->prev) | |
107 node->prev->next = node->next; | |
108 | |
109 if (node->next) | |
110 node->next->prev = node->prev; | |
111 else | |
112 dfResources->prev = node->prev; | |
113 | |
114 node->prev = node->next = NULL; | |
115 } | |
116 | |
117 | |
118 DMResource * dmres_find(const char *filename) | |
119 { | |
120 DMResource *node, *found = NULL; | |
121 | |
122 DMRES_LOCK(); | |
123 | |
124 for (node = dfResources; node != NULL; node = node->next) | |
125 { | |
126 if (strcmp(node->filename, filename) == 0) | |
127 { | |
128 found = node; | |
129 break; | |
130 } | |
131 } | |
132 | |
133 DMRES_UNLOCK(); | |
134 | |
135 return found; | |
136 } | |
137 | |
138 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
139 #ifdef DM_USE_STDIO |
0 | 140 /* Basic stdio file routines |
141 */ | |
142 static int dm_stdio_fopen(DMResource *handle) | |
143 { | |
144 char *rfilename = dm_strdup_printf("%s%s", DMRES_DATA_PATH, handle->filename); | |
145 if (rfilename == NULL) | |
146 return DMERR_MALLOC; | |
147 | |
148 handle->fh = fopen(rfilename, "rb"); | |
149 dmFree(rfilename); | |
150 | |
151 handle->error = dmGetErrno(); | |
152 return (handle->fh != NULL) ? DMERR_OK : DMERR_FOPEN; | |
153 } | |
154 | |
155 | |
156 static void dm_stdio_fclose(DMResource * f) | |
157 { | |
158 if (f->fh != NULL) | |
159 { | |
160 fclose(f->fh); | |
161 f->fh = NULL; | |
162 } | |
163 } | |
164 | |
165 | |
166 static int dm_stdio_ferror(DMResource * f) | |
167 { | |
168 return f->error; | |
169 } | |
170 | |
171 | |
172 static int dm_stdio_fseek(DMResource *f, const off_t pos, const int whence) | |
173 { | |
174 int ret = fseek(f->fh, pos, whence); | |
175 f->error = dmGetErrno(); | |
176 return ret; | |
177 } | |
178 | |
179 | |
180 static off_t dm_stdio_fsize(DMResource *f) | |
181 { | |
182 off_t savePos, fileSize; | |
183 | |
184 // Check if the size is cached | |
185 if (f->dataSize != 0) | |
186 return f->dataSize; | |
187 | |
188 // Get file size | |
189 savePos = ftell(f->fh); | |
190 if (fseek(f->fh, 0L, SEEK_END) != 0) | |
191 { | |
192 f->error = dmGetErrno(); | |
193 return -1; | |
194 } | |
195 | |
196 fileSize = ftell(f->fh); | |
197 if (fseek(f->fh, savePos, SEEK_SET) != 0) | |
198 { | |
199 f->error = dmGetErrno(); | |
200 return -1; | |
201 } | |
202 | |
203 f->dataSize = fileSize; | |
204 return fileSize; | |
205 } | |
206 | |
207 | |
208 static off_t dm_stdio_ftell(DMResource * f) | |
209 { | |
210 return ftell(f->fh); | |
211 } | |
212 | |
213 | |
214 static BOOL dm_stdio_feof(DMResource * f) | |
215 { | |
216 return feof(f->fh); | |
217 } | |
218 | |
219 | |
220 static int dm_stdio_fgetc(DMResource * f) | |
221 { | |
222 int ret = fgetc(f->fh); | |
223 f->error = dmGetErrno(); | |
224 return ret; | |
225 } | |
226 | |
227 | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
228 static int dm_stdio_fputc(int v, DMResource * f) |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
229 { |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
230 int ret = fputc(v, f->fh); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
231 f->error = dmGetErrno(); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
232 return ret; |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
233 } |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
234 |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
235 |
0 | 236 static size_t dm_stdio_fread(void *ptr, size_t size, size_t nmemb, DMResource * f) |
237 { | |
238 size_t ret = fread(ptr, size, nmemb, f->fh); | |
239 f->error = dmGetErrno(); | |
240 return ret; | |
241 } | |
242 | |
243 | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
244 static size_t dm_stdio_fwrite(void *ptr, size_t size, size_t nmemb, DMResource * f) |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
245 { |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
246 size_t ret = fwrite(ptr, size, nmemb, f->fh); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
247 f->error = dmGetErrno(); |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
248 return ret; |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
249 } |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
250 |
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
251 |
0 | 252 static int dm_stdio_preload(DMResource *handle) |
253 { | |
254 int ret = dm_stdio_fopen(handle); | |
255 if (ret != DMERR_OK) | |
256 return ret; | |
257 | |
258 dm_stdio_fsize(handle); | |
259 | |
260 handle->data = dmMalloc(handle->dataSize); | |
261 if (handle->data == NULL) | |
262 return DMERR_MALLOC; | |
263 | |
264 if (dm_stdio_fread(handle->data, sizeof(Uint8), handle->dataSize, handle) != handle->dataSize) | |
265 return DMERR_FREAD; | |
266 | |
267 return DMERR_OK; | |
268 } | |
269 | |
270 | |
271 DMResourceOps dfStdioFileOps = | |
272 { | |
273 dm_stdio_ferror, | |
274 dm_stdio_fseek, | |
275 dm_stdio_fsize, | |
276 dm_stdio_ftell, | |
277 dm_stdio_feof, | |
278 dm_stdio_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
279 dm_stdio_fputc, |
0 | 280 dm_stdio_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
281 dm_stdio_fwrite, |
0 | 282 |
283 dm_stdio_fopen, | |
284 dm_stdio_fclose, | |
285 dm_stdio_preload | |
286 }; | |
287 | |
288 DMResourceOps dfStdioFHOps = | |
289 { | |
290 dm_stdio_ferror, | |
291 dm_stdio_fseek, | |
292 dm_stdio_fsize, | |
293 dm_stdio_ftell, | |
294 dm_stdio_feof, | |
295 dm_stdio_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
296 dm_stdio_fputc, |
0 | 297 dm_stdio_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
298 dm_stdio_fwrite, |
0 | 299 |
300 NULL, | |
301 NULL, | |
302 NULL | |
303 }; | |
304 #endif | |
305 | |
306 | |
307 // Some mingw/windows headers define these as macros, which is bad for us | |
308 #ifdef __WIN32 | |
309 #undef ferror | |
310 #undef feof | |
311 #endif | |
312 | |
313 | |
314 /* | |
315 * PACK file routines | |
316 */ | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
317 #ifdef DM_USE_PACKFS |
0 | 318 static int dm_pack_preload(DMResource *handle) |
319 { | |
320 DMPackEntry *node; | |
321 int res = DMERR_OK, cres, cdataLeft; | |
322 z_stream cstream; | |
323 Uint8 * cbuffer = NULL; | |
324 | |
325 // Search PACK nodelist for file | |
326 if ((node = dm_pack_find(dfResPackFile->entries, handle->filename)) == NULL) | |
327 { | |
328 dmError("Entry '%s' not found in PACK file.\n", handle->filename); | |
329 res = DMERR_NOT_FOUND; | |
330 goto error; | |
331 } | |
332 | |
333 // Seek to entry | |
334 if (fseek(dfResPackFile->file, node->offset, SEEK_SET) == -1) | |
335 { | |
336 dmError("Could not seek node position in PACK file.\n"); | |
337 res = DMERR_FSEEK; | |
338 goto error; | |
339 } | |
340 | |
341 // Allocate a structures and buffers | |
342 cbuffer = (Uint8 *) dmMalloc(DPACK_TMPSIZE); | |
343 if (cbuffer == NULL) | |
344 { | |
345 res = DMERR_MALLOC; | |
346 goto error; | |
347 } | |
348 | |
349 // Initialize fields | |
350 handle->dataOffset = 0; | |
351 handle->dataSize = node->size; | |
352 handle->data = (Uint8 *) dmMalloc(node->size); | |
353 if (handle->data == NULL) | |
354 { | |
355 res = DMERR_MALLOC; | |
356 goto error; | |
357 } | |
358 | |
359 // Initialize decompression | |
360 cstream.zalloc = (alloc_func) Z_NULL; | |
361 cstream.zfree = (free_func) Z_NULL; | |
362 cstream.opaque = (voidpf) Z_NULL; | |
363 cstream.next_out = handle->data; | |
364 cstream.avail_out = handle->dataSize; | |
365 cdataLeft = node->length; | |
366 cres = inflateInit(&(cstream)); | |
367 if (cres != Z_OK) | |
368 { | |
369 dmError("Could not initialize zlib stream inflation.\n"); | |
370 res = DMERR_INIT_FAIL; | |
371 goto error; | |
372 } | |
373 | |
374 // Uncompress the data | |
375 while (cdataLeft > 0 && | |
376 cstream.avail_out > 0 && cres == Z_OK) | |
377 { | |
378 cstream.avail_in = fread( | |
379 cbuffer, sizeof(Uint8), | |
380 (cdataLeft >= DPACK_TMPSIZE) ? DPACK_TMPSIZE : cdataLeft, | |
381 dfResPackFile->file); | |
382 | |
383 cdataLeft -= cstream.avail_in; | |
384 cstream.next_in = cbuffer; | |
385 cres = inflate(&cstream, Z_FULL_FLUSH); | |
386 } | |
387 | |
388 // Cleanup | |
389 inflateEnd(&(cstream)); | |
390 | |
391 error: | |
392 dmFree(cbuffer); | |
393 return res; | |
394 } | |
395 | |
396 | |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
397 static void dm_pack_fclose(DMResource * f) |
0 | 398 { |
399 f->dataSize = 0; | |
400 f->dataOffset = 0; | |
401 dmFree(f->data); | |
402 f->data = NULL; | |
403 } | |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
404 #endif |
0 | 405 |
406 | |
407 static int dm_mem_ferror(DMResource * f) | |
408 { | |
409 return f->error; | |
410 } | |
411 | |
412 | |
413 static int dm_mem_fseek(DMResource * f, const off_t offset, const int whence) | |
414 { | |
415 off_t newPos; | |
416 | |
417 // Calculate the new position | |
418 switch (whence) | |
419 { | |
420 case SEEK_SET: | |
421 newPos = offset; | |
422 break; | |
423 | |
424 case SEEK_CUR: | |
425 newPos = f->dataOffset + offset; | |
426 break; | |
427 | |
428 case SEEK_END: | |
429 newPos = f->dataSize + offset; | |
430 break; | |
431 | |
432 default: | |
433 return -1; | |
434 } | |
435 | |
436 // Set the new position | |
437 f->dataOffset = newPos; | |
438 | |
439 // Check the new position | |
440 if (newPos < 0 && (size_t) newPos >= f->dataSize) | |
441 return -1; | |
442 | |
443 return 0; | |
444 } | |
445 | |
446 | |
447 static off_t dm_mem_fsize(DMResource * f) | |
448 { | |
449 return f->dataSize; | |
450 } | |
451 | |
452 | |
453 static off_t dm_mem_ftell(DMResource * f) | |
454 { | |
455 return f->dataOffset; | |
456 } | |
457 | |
458 | |
459 static BOOL dm_mem_feof(DMResource * f) | |
460 { | |
461 // Check for EOF | |
462 if ((size_t) f->dataOffset <= f->dataSize) | |
463 return FALSE; | |
464 else | |
465 return TRUE; | |
466 } | |
467 | |
468 | |
469 static int dm_mem_fgetc(DMResource * f) | |
470 { | |
471 // Check for EOF | |
472 if ((size_t) f->dataOffset < f->dataSize) | |
473 return (int) f->data[f->dataOffset++]; | |
474 else | |
475 return EOF; | |
476 } | |
477 | |
478 | |
479 static size_t dm_mem_fread(void *buf, size_t size, size_t nmemb, DMResource * f) | |
480 { | |
481 size_t length = (size * nmemb); | |
482 | |
483 // Check if we can read the whole chunk | |
484 if (((size_t) f->dataOffset + length) >= f->dataSize) | |
485 { | |
486 nmemb = (f->dataSize - f->dataOffset) / size; | |
487 length = size * nmemb; | |
488 } | |
489 | |
490 memcpy(buf, f->data + f->dataOffset, length); | |
491 f->dataOffset += length; | |
492 return nmemb; | |
493 } | |
494 | |
495 | |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
496 #ifdef DM_USE_PACKFS |
0 | 497 DMResourceOps dfPackFileOps = |
498 { | |
499 dm_mem_ferror, | |
500 dm_mem_fseek, | |
501 dm_mem_fsize, | |
502 dm_mem_ftell, | |
503 dm_mem_feof, | |
504 dm_mem_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
505 NULL, |
0 | 506 dm_mem_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
507 NULL, |
0 | 508 |
509 NULL, | |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
510 dm_pack_fclose, |
0 | 511 dm_pack_preload |
512 }; | |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
513 #endif |
0 | 514 |
515 | |
516 DMResourceOps dfMemIOFileOps = | |
517 { | |
518 dm_mem_ferror, | |
519 dm_mem_fseek, | |
520 dm_mem_fsize, | |
521 dm_mem_ftell, | |
522 dm_mem_feof, | |
523 dm_mem_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
524 NULL, |
0 | 525 dm_mem_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
526 NULL, |
0 | 527 |
528 NULL, | |
529 NULL, | |
530 NULL | |
531 }; | |
532 | |
533 | |
534 /* FS file handling functions. These functions call the actual | |
535 * functions depending on where the file is located. | |
536 */ | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
537 static void dmf_init_fops(DMResource *handle) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
538 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
539 // Check fops |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
540 if (handle->fops == NULL) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
541 { |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
542 #ifdef DM_USE_PACKFS |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
543 if (dfResFlags & DRF_USE_PACK) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
544 handle->fops = &dfPackFileOps; |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
545 #ifdef DM_USE_STDIO |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
546 else |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
547 handle->fops = &dfStdioFileOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
548 #else |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
549 handle->fops = &dfPackFileOps; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
550 #endif |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
551 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
552 #else |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
553 handle->fops = NULL; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
554 #endif |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
555 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
556 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
557 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
558 |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
559 int dmf_preload(DMResource *handle) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
560 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
561 int ret = DMERR_INIT_FAIL; |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
562 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
563 // Check if we want to preload raw data? |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
564 if (((handle->flags & DMF_PRELOAD_RAW) || |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
565 (dfResFlags & DRF_PRELOAD_ALL)) && |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
566 (handle->flags & DMF_LOADED_RAW) == 0 && |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
567 handle->fops->preload != NULL) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
568 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
569 ret = handle->fops->preload(handle); |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
570 if (ret == DMERR_OK) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
571 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
572 handle->flags |= DMF_LOADED_RAW; |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
573 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
574 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
575 else |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
576 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
577 if (handle->fops->fopen != NULL) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
578 ret = handle->fops->fopen(handle); |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
579 else |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
580 if (handle->fops->preload != NULL) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
581 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
582 ret = handle->fops->preload(handle); |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
583 if (ret == DMERR_OK) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
584 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
585 handle->flags |= DMF_LOADED_RAW; |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
586 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
587 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
588 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
589 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
590 // Check if resource data is to be preloaded |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
591 if (((handle->flags & DMF_PRELOAD_RES) || (dfResFlags & DRF_PRELOAD_RES)) && |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
592 (handle->flags & DMF_LOADED_RES) == 0 && |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
593 handle->rops != NULL && |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
594 handle->rops->load != NULL) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
595 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
596 ret = handle->rops->load(handle); |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
597 if (ret == DMERR_OK) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
598 { |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
599 // Okay, mark as loaded |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
600 handle->flags |= DMF_LOADED_RES; |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
601 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
602 // Check if we can purge the raw data now |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
603 if ((handle->flags & DMF_PERSIST) == 0) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
604 dmres_purge_raw_data(handle); |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
605 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
606 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
607 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
608 return ret; |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
609 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
610 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
611 |
0 | 612 DMResource *dmf_open(const char *filename) |
613 { | |
614 int ret; | |
615 DMResource *handle; | |
616 | |
617 // Check master directory for resource | |
618 if ((handle = dmres_find(filename)) == NULL) | |
619 { | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
620 #ifdef DM_USE_STDIO |
0 | 621 // Hmm.. does not exist? Fall back to a stdio file |
622 handle = dmres_new(filename, 0, 0); | |
623 if (handle == NULL) | |
624 return NULL; | |
625 | |
626 handle->fops = &dfStdioFileOps; | |
627 dmres_insert(handle); | |
628 #else | |
629 // Stdio not enabled, fail | |
630 return NULL; | |
631 #endif | |
632 } | |
633 | |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
634 // Initialize file ops |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
635 dmf_init_fops(handle); |
0 | 636 |
637 // Check if the data is preloaded | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
638 if (handle->flags & DMF_LOADED_RAW) |
0 | 639 { |
640 dmres_ref(handle); | |
641 return handle; | |
642 } | |
643 | |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
644 // Try preloading |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
645 ret = dmf_preload(handle); |
0 | 646 |
647 if (ret == DMERR_OK) | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
648 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
649 dmres_ref(handle); |
0 | 650 return handle; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
651 } |
0 | 652 |
653 return NULL; | |
654 } | |
655 | |
656 | |
657 DMResource * dmf_open_memio(const char *filename, Uint8 *buf, size_t len) | |
658 { | |
659 DMResource *handle; | |
660 | |
661 // Check master directory for resource | |
662 if ((handle = dmres_find(filename)) == NULL) | |
663 { | |
664 // Hmm.. does not exist? Fall back to a stdio file | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
665 handle = dmres_new(filename, DMF_LOADED_RAW, len); |
0 | 666 if (handle == NULL) |
667 return NULL; | |
668 | |
669 handle->fops = &dfMemIOFileOps; | |
670 handle->data = buf; | |
671 dmres_insert(handle); | |
672 } | |
673 | |
674 // Increase refcount | |
675 dmres_ref(handle); | |
676 | |
677 return handle; | |
678 } | |
679 | |
680 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
681 #ifdef DM_USE_STDIO |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
682 DMResource * dmf_create_stdio(const char *filename, const char *mode) |
0 | 683 { |
684 DMResource *handle = dmres_new(filename, 0, 0); | |
685 if (handle == NULL) | |
686 return NULL; | |
687 | |
688 handle->fops = &dfStdioFileOps; | |
689 | |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
690 handle->fh = fopen(filename, mode); |
0 | 691 handle->error = dmGetErrno(); |
692 | |
693 if (handle->fh != NULL) | |
694 { | |
695 dmres_ref(handle); | |
696 return handle; | |
697 } | |
698 else | |
699 { | |
700 dmres_free(handle); | |
701 return NULL; | |
702 } | |
703 } | |
704 | |
705 | |
706 DMResource * dmf_create_stdio_stream(FILE *fh) | |
707 { | |
708 DMResource *handle = dmres_new("", 0, 0); | |
709 if (handle == NULL) | |
710 return NULL; | |
711 | |
712 handle->fops = &dfStdioFHOps; | |
713 handle->fh = fh; | |
714 dmres_ref(handle); | |
715 return handle; | |
716 } | |
717 #endif | |
718 | |
719 | |
720 void dmf_close(DMResource * f) | |
721 { | |
722 if (f == NULL) | |
723 return; | |
724 | |
725 if (f->fops->fclose != NULL) | |
726 f->fops->fclose(f); | |
727 | |
728 dmres_unref(f); | |
729 } | |
730 | |
731 | |
732 int dmferror(DMResource * f) | |
733 { | |
734 f->atime = time(NULL); | |
735 return f->fops->ferror(f); | |
736 } | |
737 | |
738 int dmfseek(DMResource * f, off_t offset, int whence) | |
739 { | |
740 f->atime = time(NULL); | |
741 return f->fops->fseek(f, offset, whence); | |
742 } | |
743 | |
744 off_t dmfsize(DMResource * f) | |
745 { | |
746 f->atime = time(NULL); | |
747 return f->fops->fsize(f); | |
748 } | |
749 | |
750 off_t dmftell(DMResource * f) | |
751 { | |
752 f->atime = time(NULL); | |
753 return f->fops->ftell(f); | |
754 } | |
755 | |
756 BOOL dmfeof(DMResource * f) | |
757 { | |
758 f->atime = time(NULL); | |
759 return f->fops->feof(f); | |
760 } | |
761 | |
762 int dmfgetc(DMResource * f) | |
763 { | |
764 f->atime = time(NULL); | |
765 return f->fops->fgetc(f); | |
766 } | |
767 | |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
768 int dmfputc(int v, DMResource * f) |
72
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
769 { |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
770 f->atime = time(NULL); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
771 return f->fops->fputc(v, f); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
772 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
773 |
0 | 774 size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource * f) |
775 { | |
776 f->atime = time(NULL); | |
777 return f->fops->fread(ptr, size, nmemb, f); | |
778 } | |
779 | |
72
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
780 size_t dmfwrite(void *ptr, size_t size, size_t nmemb, DMResource * f) |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
781 { |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
782 f->atime = time(NULL); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
783 return f->fops->fwrite(ptr, size, nmemb, f); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
784 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
785 |
235 | 786 char *dmfgets(char *s, int size, DMResource * f) |
787 { | |
788 char *p = s, c; | |
789 int n = 0; | |
790 | |
791 while ((c = f->fops->fgetc(f)) != EOF) | |
792 { | |
793 if (c == '\n') | |
794 break; | |
795 else | |
796 if (n < size - 1) | |
797 { | |
798 *p++ = c; | |
799 size--; | |
800 } | |
801 } | |
802 *p = 0; | |
803 | |
804 return s; | |
805 } | |
806 | |
807 | |
0 | 808 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
809 int dmres_ref(DMResource *node) |
0 | 810 { |
811 DMRES_LOCK(); | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
812 node->atime = time(NULL); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
813 node->refcount++; |
0 | 814 DMRES_UNLOCK(); |
815 | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
816 return node->refcount; |
0 | 817 } |
818 | |
819 | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
820 int dmres_unref(DMResource *node) |
0 | 821 { |
822 DMRES_LOCK(); | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
823 node->refcount--; |
0 | 824 DMRES_UNLOCK(); |
825 | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
826 return node->refcount; |
0 | 827 } |
828 | |
829 | |
114
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
830 #define NADDFLAG(flg, ch) \ |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
831 do { \ |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
832 if ((flags & (flg)) && offs < size - 1) \ |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
833 str[offs++] = ch; \ |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
834 } while (0) |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
835 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
836 void dmres_flags_to_symbolic(char *str, size_t size, int flags) |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
837 { |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
838 size_t offs = 0; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
839 NADDFLAG(DMF_PRELOAD_RAW, 'r'); |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
840 NADDFLAG(DMF_PRELOAD_RES, 'e'); |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
841 NADDFLAG(DMF_PERSIST, 'p'); |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
842 NADDFLAG(DMF_STREAM, 's'); |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
843 if (offs < size) |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
844 str[offs] = 0; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
845 } |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
846 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
847 #undef NADDFLAG |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
848 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
849 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
850 int dmres_symbolic_to_flags(const char *str) |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
851 { |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
852 int offs, flags; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
853 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
854 for (flags = offs = 0; str[offs]; offs++) |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
855 switch (tolower(str[offs])) |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
856 { |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
857 case 'r': flags |= DMF_PRELOAD_RAW; break; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
858 case 'e': flags |= DMF_PRELOAD_RES; break; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
859 case 'p': flags |= DMF_PERSIST; break; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
860 case 's': flags |= DMF_STREAM; break; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
861 } |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
862 return flags; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
863 } |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
864 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
865 |
0 | 866 int dmres_load_resfile(const char *filename) |
867 { | |
33 | 868 int ret = DMERR_OK; |
0 | 869 char line[256]; |
870 FILE *f = fopen(filename, "r"); | |
871 if (f == NULL) | |
872 return DMERR_FOPEN; | |
873 | |
874 DMRES_LOCK(); | |
875 | |
876 while (fgets(line, sizeof(line) - 1, f) != NULL) | |
877 { | |
878 int fnstart, fsep; | |
879 for (fnstart = 0; isspace(line[fnstart]); fnstart++); | |
880 for (fsep = fnstart; line[fsep] && line[fsep] != '|'; fsep++); | |
881 if (line[fsep] == '|') | |
882 { | |
883 int flags, i; | |
884 for (i = fsep - 1; i > 0 && isspace(line[i]); i--) | |
885 line[i] = 0; | |
886 | |
887 for (i = fsep; isspace(line[i]); i++); | |
888 | |
889 if (sscanf(&line[i], "%x", &flags) == 1 && | |
890 strlen(&line[fnstart]) > 0) | |
891 { | |
892 | |
893 } | |
894 } | |
895 } | |
896 | |
897 DMRES_UNLOCK(); | |
898 fclose(f); | |
33 | 899 |
0 | 900 return ret; |
901 } | |
902 | |
903 | |
904 int dmres_write_resfile(const char *filename) | |
905 { | |
906 int ret; | |
907 DMResource *node; | |
908 FILE *f = fopen(filename, "w"); | |
909 if (f == NULL) | |
910 return DMERR_FOPEN; | |
911 | |
912 DMRES_LOCK(); | |
913 | |
914 for (node = dfResources; node != NULL; node = node->next) | |
915 { | |
916 if (fprintf(f, "%s|%08x\n", node->filename, node->flags) < 0) | |
917 { | |
918 ret = DMERR_FWRITE; | |
919 goto error; | |
920 } | |
921 } | |
922 | |
923 error: | |
924 DMRES_UNLOCK(); | |
925 fclose(f); | |
926 return ret; | |
927 } | |
928 | |
929 | |
930 /* Resources subsystem initialization and shutdown routines | |
931 */ | |
107 | 932 int dmres_init(const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)) |
0 | 933 { |
934 // Check if we are already initialized | |
935 if (dfResInitialized) | |
936 return DMERR_ALREADY_INIT; | |
937 | |
938 dfResFlags = flags; | |
939 dfResPath = dm_strdup((path != NULL) ? path : DMRES_DATA_PATH); | |
940 dfResourcesMutex = dmCreateMutex(); | |
941 | |
107 | 942 |
0 | 943 if (flags & DRF_USE_PACK) |
944 { | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
945 #ifdef DM_USE_PACKFS |
0 | 946 int ret; |
947 DMPackEntry *node; | |
948 | |
949 dfResPackFilename = dm_strdup((filename != NULL) ? filename : DMRES_DATA_PACK); | |
950 | |
951 // Initialize PACK, open as read-only | |
952 ret = dm_pack_open(dfResPackFilename, &dfResPackFile, TRUE); | |
953 if (ret != DMERR_OK) | |
954 { | |
955 dmError("Error opening PACK file '%s', #%i: %s\n", | |
956 dfResPackFilename, ret, dmErrorStr(ret)); | |
957 | |
958 return DMERR_INIT_FAIL; | |
959 } | |
960 | |
961 // Initialize resources from a PACK file | |
962 for (node = dfResPackFile->entries; node != NULL; node = node->next) | |
963 { | |
4
e0fc7863d024
Mask out bits from resFlags that should not be there after initialization.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
964 DMResource *res = dmres_new(node->filename, node->resFlags & DMF_MASK, node->size); |
0 | 965 if (res == NULL) |
966 { | |
967 dmError("Could not allocate memory for resource node '%s' [0x%08x], %d.\n", | |
968 node->filename, node->resFlags, node->size); | |
969 return DMERR_INIT_FAIL; | |
970 } | |
971 | |
972 dmres_insert(res); | |
973 } | |
974 | |
975 #else | |
976 // PACK not compiled in, FAIL! | |
977 return DMERR_INIT_FAIL; | |
978 #endif | |
979 } | |
980 else | |
981 { | |
982 // Initialize resources from a resource directory | |
983 char *resFilename = dm_strdup_printf("%s%s", dfResPath, DMRES_RES_FILE); | |
984 int ret = dmres_load_resfile(resFilename); | |
985 dmFree(resFilename); | |
986 | |
987 if (ret != DMERR_OK) | |
988 return DMERR_INIT_FAIL; | |
989 } | |
990 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
991 // Okay, classify resources |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
992 if (dfResources != NULL && classifier != NULL) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
993 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
994 DMResource *node; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
995 for (node = dfResources; node != NULL; node = node->next) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
996 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
997 int ret = classifier(node); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
998 if (ret != DMERR_OK) |
107 | 999 return ret; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1000 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1001 } |
0 | 1002 |
1003 // Initialization complete | |
1004 dfResInitialized = TRUE; | |
1005 return DMERR_OK; | |
1006 } | |
1007 | |
1008 | |
1009 void dmres_close(void) | |
1010 { | |
1011 DMResource *node; | |
1012 DMRES_LOCK(); | |
1013 | |
1014 if (!dfResInitialized) | |
1015 return; | |
1016 | |
1017 // Shutdown possible subsystems | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
1018 #ifdef DM_USE_PACKFS |
0 | 1019 if (dfResFlags & DRF_USE_PACK) |
1020 { | |
1021 int res = dm_pack_close(dfResPackFile); | |
1022 if (res != DMERR_OK) | |
1023 { | |
1024 dmError("Error closing PACK, #%i: %s\n", | |
1025 res, dmErrorStr(res)); | |
1026 } | |
1027 | |
1028 dmFree(dfResPackFilename); | |
1029 } | |
1030 #endif | |
1031 | |
1032 // Free resource entries | |
1033 node = dfResources; | |
1034 while (node != NULL) | |
1035 { | |
1036 DMResource *next = node->next; | |
1037 dmres_free(node); | |
1038 node = next; | |
1039 } | |
1040 | |
1041 // Etc. | |
1042 dmFree(dfResPath); | |
1043 DMRES_UNLOCK(); | |
1044 dmDestroyMutex(dfResourcesMutex); | |
1045 dfResInitialized = FALSE; | |
1046 } | |
1047 | |
1048 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1049 int dmres_preload(BOOL start, int *loaded, int *total) |
0 | 1050 { |
1051 static DMResource *dfPreload = NULL; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1052 int ret = DMERR_OK; |
0 | 1053 |
1054 DMRES_LOCK(); | |
1055 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1056 // Initialize preloading |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1057 if (dfPreload == NULL || start) |
0 | 1058 { |
1059 DMResource *node; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1060 |
0 | 1061 dfPreload = dfResources; |
1062 *loaded = 0; | |
1063 *total = 0; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1064 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1065 // Calculate total number of resources to be preloaded |
0 | 1066 for (node = dfResources; node != NULL; node = node->next) |
1067 { | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1068 if ((dfResFlags & (DRF_PRELOAD_ALL | DRF_PRELOAD_RES)) || |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1069 (node->flags & (DMF_PRELOAD_RAW | DMF_PRELOAD_RES))) |
0 | 1070 (*total)++; |
1071 } | |
1072 } | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1073 else |
0 | 1074 if (dfPreload != NULL) |
1075 { | |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1076 // Initialize fops and preload |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1077 dmf_init_fops(dfPreload); |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1078 if ((ret = dmf_preload(dfPreload)) != DMERR_OK) |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1079 goto error; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1080 |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1081 (*loaded)++; |
0 | 1082 dfPreload = dfPreload->next; |
1083 } | |
1084 | |
1085 DMRES_UNLOCK(); | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1086 return (dfPreload == NULL) ? DMERR_OK : DMERR_PROGRESS; |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1087 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1088 error: |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1089 DMRES_UNLOCK(); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1090 return ret; |
0 | 1091 } |
1092 | |
1093 | |
1094 void dmres_prune(int agems, int flags) | |
1095 { | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1096 DMResource *node; |
0 | 1097 int currtime = time(NULL); |
1098 DMRES_LOCK(); | |
1099 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1100 for (node = dfResources; node != NULL; node = node->next) |
0 | 1101 { |
1102 // Check if node has refcount of 0 and is | |
1103 // not marked as persistent resource | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1104 if (node->refcount == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1105 (node->flags & DMF_PERSIST) == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1106 (node->flags & (DMF_LOADED_RES | DMF_LOADED_RAW))) |
0 | 1107 { |
1108 // Check if we match either one of atime or mtime | |
1109 if (((flags & DMPRUNE_ATIME) && | |
1110 currtime - node->atime >= agems) || | |
1111 ((flags & DMPRUNE_MTIME) && | |
1112 currtime - node->mtime >= agems)) | |
1113 { | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1114 dmres_free_res_data(node); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1115 dmres_free_raw_data(node); |
0 | 1116 } |
1117 } | |
1118 } | |
1119 | |
1120 DMRES_UNLOCK(); | |
1121 } | |
1122 | |
1123 | |
1124 /* Helper resource access routines | |
1125 */ | |
1126 int dmf_read_str(DMResource *f, Uint8 *s, size_t l) | |
1127 { | |
1128 return dmfread(s, sizeof(Uint8), l, f) == l; | |
1129 } | |
1130 | |
1131 | |
1132 #define DM_DEFINE_FUNC(xname, xtype, xmacro) \ | |
1133 BOOL dmf_read_ ## xname (DMResource *f, xtype *v) { \ | |
1134 xtype result; \ | |
1135 if (dmfread(&result, sizeof( xtype ), 1, f) != 1) \ | |
1136 return FALSE; \ | |
1137 *v = DM_ ## xmacro ## _TO_NATIVE (result); \ | |
1138 return TRUE; \ | |
1139 } | |
1140 | |
1141 DM_DEFINE_FUNC(le16, Uint16, LE16) | |
1142 DM_DEFINE_FUNC(le32, Uint32, LE32) | |
1143 | |
1144 DM_DEFINE_FUNC(be16, Uint16, BE16) | |
1145 DM_DEFINE_FUNC(be32, Uint32, BE32) | |
1146 | |
1147 #ifdef DM_HAVE_64BIT | |
1148 DM_DEFINE_FUNC(le64, Uint64, LE64) | |
1149 DM_DEFINE_FUNC(be64, Uint64, BE64) | |
1150 #endif |