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