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