Mercurial > hg > dmlib
annotate dmres.c @ 613:15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 12 Apr 2013 12:34:17 +0300 |
parents | 877dba1b8a1f |
children | e8fc083b7499 |
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 */ | |
603
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
567 static void dmf_reset(DMResource *handle) |
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
568 { |
609
877dba1b8a1f
Fix some resource code checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
604
diff
changeset
|
569 if (handle != NULL && handle->fops != NULL) |
603
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
570 dmfseek(handle, 0, SEEK_SET); |
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
571 } |
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
572 |
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
573 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
574 static void dmf_init_fops(DMResource *handle) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
575 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
576 // Check fops |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
577 if (handle->fops == NULL) |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
578 { |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
579 #ifdef DM_USE_PACKFS |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
580 if (handle->lib->flags & DRF_USE_PACK) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
581 handle->fops = &dfPackFileOps; |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
582 |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
583 # ifdef DM_USE_STDIO |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
584 else |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
585 if (handle->lib->flags & DRF_USE_STDIO) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
586 handle->fops = &dfStdioFileOps; |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
587 # else |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
588 handle->fops = &dfPackFileOps; |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
589 # endif |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
590 #endif |
609
877dba1b8a1f
Fix some resource code checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
604
diff
changeset
|
591 |
877dba1b8a1f
Fix some resource code checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
604
diff
changeset
|
592 dmf_reset(handle); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
593 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
594 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
595 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
596 |
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
|
597 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
|
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 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
|
600 |
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 // Check if we want to preload raw data? |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
602 if ((handle->flags & DMF_PRELOAD_RAW) || |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
603 (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
|
604 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
605 if (handle->flags & DMF_LOADED_RAW) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
606 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
607 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
608 if (handle->fops->preload != NULL) |
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
|
609 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
610 ret = handle->fops->preload(handle); |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
611 if (ret == DMERR_OK) |
604 | 612 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
613 handle->flags |= DMF_LOADED_RAW; |
604 | 614 } |
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
|
615 } |
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 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
|
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 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
|
620 ret = handle->fops->fopen(handle); |
603
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
621 else |
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
622 if (handle->fops->preload != NULL) |
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
623 ret = handle->fops->preload(handle); |
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
|
624 } |
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 // Check if resource data is to be preloaded |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
627 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
|
628 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
629 if (handle->flags & DMF_LOADED_RES) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
630 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
631 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
632 if (handle->rops != NULL && |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
633 handle->rops->load != NULL) |
29
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
634 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
635 ret = handle->rops->load(handle); |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
636 if (ret == DMERR_OK) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
637 { |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
638 // Okay, mark as loaded |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
639 handle->flags |= DMF_LOADED_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
|
640 |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
641 // Check if we can purge the raw data now |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
642 if ((handle->flags & DMF_PERSIST) == 0) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
643 dmres_purge_raw_data(handle); |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
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 } |
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 } |
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
|
647 |
603
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
648 dmf_reset(handle); |
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
|
649 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
|
650 } |
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
|
651 |
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
|
652 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
653 DMResource *dmf_open(DMResourceLib *lib, const char *filename) |
0 | 654 { |
655 int ret; | |
656 DMResource *handle; | |
657 | |
658 // Check master directory for resource | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
659 if ((handle = dmres_find(lib, filename)) == NULL) |
0 | 660 { |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
661 #ifdef DM_USE_STDIO |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
662 if (lib->flags & DRF_USE_STDIO) |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
663 { |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
664 // Hmm.. does not exist? Fall back to a stdio file |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
665 handle = dmres_new(lib, filename, 0, 0); |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
666 if (handle == NULL) |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
667 return NULL; |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
668 |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
669 handle->fops = &dfStdioFileOps; |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
670 dmres_insert(lib, handle); |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
671 } |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
672 else |
0 | 673 return NULL; |
674 #else | |
675 // Stdio not enabled, fail | |
676 return NULL; | |
677 #endif | |
678 } | |
679 | |
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
|
680 // Initialize file ops |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
681 dmf_init_fops(handle); |
0 | 682 |
683 // Check if the data is preloaded | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
684 if (handle->flags & DMF_LOADED_RAW) |
0 | 685 { |
686 dmres_ref(handle); | |
687 return handle; | |
688 } | |
689 | |
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
|
690 // 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
|
691 ret = dmf_preload(handle); |
0 | 692 |
693 if (ret == DMERR_OK) | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
694 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
695 dmres_ref(handle); |
0 | 696 return handle; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
697 } |
0 | 698 |
699 return NULL; | |
700 } | |
701 | |
702 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
703 DMResource * dmf_create_memio(DMResourceLib *lib, const char *filename, Uint8 *buf, size_t len) |
0 | 704 { |
705 DMResource *handle; | |
706 | |
707 // Check master directory for resource | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
708 if ((handle = dmres_find(lib, filename)) == NULL) |
0 | 709 { |
710 // 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
|
711 handle = dmres_new(lib, filename, DMF_LOADED_RAW, len); |
0 | 712 if (handle == NULL) |
713 return NULL; | |
714 | |
715 handle->fops = &dfMemIOFileOps; | |
716 handle->data = buf; | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
717 dmres_insert(lib, handle); |
0 | 718 } |
719 | |
720 // Increase refcount | |
721 dmres_ref(handle); | |
722 | |
603
36b544eb6f4b
Fixes in resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
602
diff
changeset
|
723 dmf_reset(handle); |
0 | 724 return handle; |
725 } | |
726 | |
727 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
728 #ifdef DM_USE_STDIO |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
729 DMResource * dmf_create_stdio(const char *filename, const char *mode) |
0 | 730 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
731 DMResource *handle = dmres_new(NULL, filename, 0, 0); |
0 | 732 if (handle == NULL) |
733 return NULL; | |
734 | |
735 handle->fops = &dfStdioFileOps; | |
736 | |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
737 handle->fh = fopen(filename, mode); |
0 | 738 handle->error = dmGetErrno(); |
739 | |
740 if (handle->fh != NULL) | |
741 { | |
742 dmres_ref(handle); | |
743 return handle; | |
744 } | |
745 else | |
746 { | |
747 dmres_free(handle); | |
748 return NULL; | |
749 } | |
750 } | |
751 | |
752 | |
753 DMResource * dmf_create_stdio_stream(FILE *fh) | |
754 { | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
755 DMResource *handle = dmres_new(NULL, "", 0, 0); |
0 | 756 if (handle == NULL) |
757 return NULL; | |
758 | |
759 handle->fops = &dfStdioFHOps; | |
760 handle->fh = fh; | |
761 dmres_ref(handle); | |
762 return handle; | |
763 } | |
764 #endif | |
765 | |
766 | |
767 void dmf_close(DMResource * f) | |
768 { | |
769 if (f == NULL) | |
770 return; | |
771 | |
772 if (f->fops->fclose != NULL) | |
773 f->fops->fclose(f); | |
774 | |
775 dmres_unref(f); | |
776 } | |
777 | |
778 | |
779 int dmferror(DMResource * f) | |
780 { | |
781 f->atime = time(NULL); | |
782 return f->fops->ferror(f); | |
783 } | |
784 | |
785 int dmfseek(DMResource * f, off_t offset, int whence) | |
786 { | |
787 f->atime = time(NULL); | |
788 return f->fops->fseek(f, offset, whence); | |
789 } | |
790 | |
791 off_t dmfsize(DMResource * f) | |
792 { | |
793 f->atime = time(NULL); | |
794 return f->fops->fsize(f); | |
795 } | |
796 | |
797 off_t dmftell(DMResource * f) | |
798 { | |
799 f->atime = time(NULL); | |
800 return f->fops->ftell(f); | |
801 } | |
802 | |
803 BOOL dmfeof(DMResource * f) | |
804 { | |
805 f->atime = time(NULL); | |
806 return f->fops->feof(f); | |
807 } | |
808 | |
809 int dmfgetc(DMResource * f) | |
810 { | |
811 f->atime = time(NULL); | |
812 return f->fops->fgetc(f); | |
813 } | |
814 | |
74
23ac82365a65
Add file mode to dmf_create_stdio().
Matti Hamalainen <ccr@tnsp.org>
parents:
72
diff
changeset
|
815 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
|
816 { |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
817 f->atime = time(NULL); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
818 return f->fops->fputc(v, f); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
819 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
820 |
0 | 821 size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource * f) |
822 { | |
823 f->atime = time(NULL); | |
824 return f->fops->fread(ptr, size, nmemb, f); | |
825 } | |
826 | |
72
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
827 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
|
828 { |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
829 f->atime = time(NULL); |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
830 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
|
831 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
832 |
235 | 833 char *dmfgets(char *s, int size, DMResource * f) |
834 { | |
835 char *p = s, c; | |
836 int n = 0; | |
837 | |
838 while ((c = f->fops->fgetc(f)) != EOF) | |
839 { | |
237
6d9d43bb68eb
Oops, another silly issue in dmfgets().
Matti Hamalainen <ccr@tnsp.org>
parents:
236
diff
changeset
|
840 n++; |
235 | 841 if (c == '\n') |
842 break; | |
843 else | |
844 if (n < size - 1) | |
845 *p++ = c; | |
846 } | |
847 *p = 0; | |
848 | |
236
8189be40ed7c
Oops, dmfgets() had a nasty think-o and counted the remaining buffer space
Matti Hamalainen <ccr@tnsp.org>
parents:
235
diff
changeset
|
849 return (n > 0) ? s : NULL; |
235 | 850 } |
851 | |
852 | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
853 int dmres_ref(DMResource *node) |
0 | 854 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
855 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
|
856 node->atime = time(NULL); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
857 node->refcount++; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
858 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex); |
0 | 859 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
860 return node->refcount; |
0 | 861 } |
862 | |
863 | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
864 int dmres_unref(DMResource *node) |
0 | 865 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
866 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
|
867 node->refcount--; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
868 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex); |
0 | 869 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
870 return node->refcount; |
0 | 871 } |
872 | |
873 | |
114
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
874 #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
|
875 do { \ |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
876 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
|
877 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
|
878 } while (0) |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
879 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
880 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
|
881 { |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
882 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
|
883 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
|
884 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
|
885 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
|
886 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
|
887 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
|
888 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
|
889 } |
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 #undef NADDFLAG |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
892 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
893 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
894 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
|
895 { |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
896 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
|
897 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
898 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
|
899 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
|
900 { |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
901 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
|
902 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
|
903 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
|
904 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
|
905 } |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
906 return flags; |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
907 } |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
908 |
e9de22e5a6d5
Add functions for converting resource flags to symbolic string form and back.
Matti Hamalainen <ccr@tnsp.org>
parents:
107
diff
changeset
|
909 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
910 int dmres_load_resfile(DMResourceLib *lib, const char *filename) |
0 | 911 { |
33 | 912 int ret = DMERR_OK; |
0 | 913 char line[256]; |
914 FILE *f = fopen(filename, "r"); | |
915 if (f == NULL) | |
916 return DMERR_FOPEN; | |
917 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
918 dmMutexLock(lib->mutex); |
0 | 919 |
920 while (fgets(line, sizeof(line) - 1, f) != NULL) | |
921 { | |
922 int fnstart, fsep; | |
923 for (fnstart = 0; isspace(line[fnstart]); fnstart++); | |
924 for (fsep = fnstart; line[fsep] && line[fsep] != '|'; fsep++); | |
925 if (line[fsep] == '|') | |
926 { | |
927 int flags, i; | |
928 for (i = fsep - 1; i > 0 && isspace(line[i]); i--) | |
929 line[i] = 0; | |
930 | |
931 for (i = fsep; isspace(line[i]); i++); | |
932 } | |
933 } | |
934 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
935 dmMutexUnlock(lib->mutex); |
0 | 936 fclose(f); |
33 | 937 |
0 | 938 return ret; |
939 } | |
940 | |
941 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
942 int dmres_write_resfile(DMResourceLib *lib, const char *filename) |
0 | 943 { |
944 int ret; | |
945 DMResource *node; | |
946 FILE *f = fopen(filename, "w"); | |
947 if (f == NULL) | |
948 return DMERR_FOPEN; | |
949 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
950 dmMutexLock(lib->mutex); |
0 | 951 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
952 for (node = lib->resources; node != NULL; node = node->next) |
0 | 953 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
954 char tmp[64]; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
955 dmres_flags_to_symbolic(tmp, sizeof(tmp), node->flags); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
956 if (fprintf(f, "%s|%s\n", node->filename, tmp) < 0) |
0 | 957 { |
958 ret = DMERR_FWRITE; | |
959 goto error; | |
960 } | |
961 } | |
962 | |
963 error: | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
964 dmMutexUnlock(lib->mutex); |
0 | 965 fclose(f); |
966 return ret; | |
967 } | |
968 | |
969 | |
970 /* Resources subsystem initialization and shutdown routines | |
971 */ | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
972 int dmres_init(DMResourceLib **plib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)) |
0 | 973 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
974 DMResourceLib *lib; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
975 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
976 // Allocate the resource library structure |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
977 if ((*plib = lib = dmMalloc0(sizeof(DMResourceLib))) == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
978 return DMERR_MALLOC; |
0 | 979 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
980 // Basic data |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
981 lib->mutex = dmCreateMutex(); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
982 lib->flags = flags; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
983 lib->resPath = dm_strdup((path != NULL) ? path : DMRES_DATA_PATH); |
0 | 984 |
107 | 985 |
0 | 986 if (flags & DRF_USE_PACK) |
987 { | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
988 #ifdef DM_USE_PACKFS |
0 | 989 int ret; |
990 DMPackEntry *node; | |
991 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
992 lib->packFilename = dm_strdup((filename != NULL) ? filename : DMRES_DATA_PACK); |
0 | 993 |
994 // Initialize PACK, open as read-only | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
995 ret = dm_pack_open(lib->packFilename, &lib->packFile, TRUE); |
0 | 996 if (ret != DMERR_OK) |
997 { | |
998 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
|
999 lib->packFilename, ret, dmErrorStr(ret)); |
0 | 1000 |
1001 return DMERR_INIT_FAIL; | |
1002 } | |
1003 | |
1004 // Initialize resources from a PACK file | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1005 for (node = lib->packFile->entries; node != NULL; node = node->next) |
0 | 1006 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1007 DMResource *res = dmres_new(lib, node->filename, node->resFlags & DMF_MASK, node->size); |
0 | 1008 if (res == NULL) |
1009 { | |
1010 dmError("Could not allocate memory for resource node '%s' [0x%08x], %d.\n", | |
1011 node->filename, node->resFlags, node->size); | |
1012 return DMERR_INIT_FAIL; | |
1013 } | |
1014 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1015 dmres_insert(lib, res); |
0 | 1016 } |
1017 | |
1018 #else | |
1019 // PACK not compiled in, FAIL! | |
1020 return DMERR_INIT_FAIL; | |
1021 #endif | |
1022 } | |
1023 else | |
1024 { | |
1025 // Initialize resources from a resource directory | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1026 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
|
1027 int ret = dmres_load_resfile(lib, resFilename); |
0 | 1028 dmFree(resFilename); |
1029 | |
1030 if (ret != DMERR_OK) | |
1031 return DMERR_INIT_FAIL; | |
1032 } | |
1033 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1034 // Okay, classify resources |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1035 if (lib->resources != NULL && classifier != NULL) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1036 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1037 DMResource *node; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1038 for (node = lib->resources; node != NULL; node = node->next) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1039 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1040 int ret = classifier(node); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1041 if (ret != DMERR_OK) |
107 | 1042 return ret; |
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 } |
0 | 1045 |
1046 // Initialization complete | |
1047 return DMERR_OK; | |
1048 } | |
1049 | |
1050 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1051 int dmres_close(DMResourceLib *lib) |
0 | 1052 { |
1053 DMResource *node; | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1054 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1055 if (lib == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1056 return DMERR_NULLPTR; |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1057 |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1058 dmMutexLock(lib->mutex); |
0 | 1059 |
1060 // Shutdown possible subsystems | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
1061 #ifdef DM_USE_PACKFS |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1062 if (lib->flags & DRF_USE_PACK) |
0 | 1063 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1064 int res = dm_pack_close(lib->packFile); |
0 | 1065 if (res != DMERR_OK) |
1066 { | |
1067 dmError("Error closing PACK, #%i: %s\n", | |
1068 res, dmErrorStr(res)); | |
1069 } | |
1070 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1071 dmFree(lib->packFilename); |
0 | 1072 } |
1073 #endif | |
1074 | |
1075 // Free resource entries | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1076 node = lib->resources; |
0 | 1077 while (node != NULL) |
1078 { | |
1079 DMResource *next = node->next; | |
1080 dmres_free(node); | |
1081 node = next; | |
1082 } | |
1083 | |
1084 // Etc. | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1085 dmFree(lib->resPath); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1086 dmMutexUnlock(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1087 dmDestroyMutex(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1088 return DMERR_OK; |
0 | 1089 } |
1090 | |
1091 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1092 int dmres_preload(DMResourceLib *lib, BOOL start, int *loaded, int *total) |
0 | 1093 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1094 int ret = DMERR_OK; |
0 | 1095 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1096 dmMutexLock(lib->mutex); |
0 | 1097 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1098 // Initialize preloading |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1099 if (lib->preload == NULL || start) |
0 | 1100 { |
1101 DMResource *node; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1102 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1103 lib->preload = lib->resources; |
0 | 1104 *loaded = 0; |
1105 *total = 0; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1106 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1107 // Calculate total number of resources to be preloaded |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1108 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1109 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1110 if ((lib->flags & (DRF_PRELOAD_ALL | DRF_PRELOAD_RES)) || |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1111 (node->flags & (DMF_PRELOAD_RAW | DMF_PRELOAD_RES))) |
0 | 1112 (*total)++; |
1113 } | |
1114 } | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1115 else |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1116 if (lib->preload != NULL) |
0 | 1117 { |
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
|
1118 // Initialize fops and preload |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1119 dmf_init_fops(lib->preload); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1120 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
|
1121 goto error; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1122 |
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
|
1123 (*loaded)++; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1124 lib->preload = lib->preload->next; |
0 | 1125 } |
1126 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1127 dmMutexUnlock(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1128 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1129 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1130 error: |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1131 dmMutexUnlock(lib->mutex); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1132 return ret; |
0 | 1133 } |
1134 | |
1135 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1136 void dmres_prune(DMResourceLib *lib, int agems, int flags) |
0 | 1137 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1138 DMResource *node; |
0 | 1139 int currtime = time(NULL); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1140 dmMutexLock(lib->mutex); |
0 | 1141 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1142 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1143 { |
1144 // Check if node has refcount of 0 and is | |
1145 // not marked as persistent resource | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1146 if (node->refcount == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1147 (node->flags & DMF_PERSIST) == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1148 (node->flags & (DMF_LOADED_RES | DMF_LOADED_RAW))) |
0 | 1149 { |
1150 // Check if we match either one of atime or mtime | |
1151 if (((flags & DMPRUNE_ATIME) && | |
1152 currtime - node->atime >= agems) || | |
1153 ((flags & DMPRUNE_MTIME) && | |
1154 currtime - node->mtime >= agems)) | |
1155 { | |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1156 dmres_free_res_data(node); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1157 dmres_free_raw_data(node); |
0 | 1158 } |
1159 } | |
1160 } | |
1161 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1162 dmMutexUnlock(lib->mutex); |
0 | 1163 } |
1164 | |
1165 | |
1166 /* Helper resource access routines | |
1167 */ | |
553
b60220fd1669
s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents:
439
diff
changeset
|
1168 int dmf_read_str(DMResource *f, void *s, size_t l) |
0 | 1169 { |
553
b60220fd1669
s/Uint8/void/ in dmf_{read,write}_str().
Matti Hamalainen <ccr@tnsp.org>
parents:
439
diff
changeset
|
1170 return dmfread(s, 1, l, f) == l; |
0 | 1171 } |
1172 | |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1173 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
|
1174 { |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1175 int tmp = dmfgetc(f); |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1176 *val = tmp; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1177 return tmp != EOF; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1178 } |
0 | 1179 |
1180 #define DM_DEFINE_FUNC(xname, xtype, xmacro) \ | |
1181 BOOL dmf_read_ ## xname (DMResource *f, xtype *v) { \ | |
1182 xtype result; \ | |
1183 if (dmfread(&result, sizeof( xtype ), 1, f) != 1) \ | |
1184 return FALSE; \ | |
1185 *v = DM_ ## xmacro ## _TO_NATIVE (result); \ | |
1186 return TRUE; \ | |
1187 } | |
1188 | |
1189 DM_DEFINE_FUNC(le16, Uint16, LE16) | |
1190 DM_DEFINE_FUNC(le32, Uint32, LE32) | |
1191 | |
1192 DM_DEFINE_FUNC(be16, Uint16, BE16) | |
1193 DM_DEFINE_FUNC(be32, Uint32, BE32) | |
1194 | |
1195 #ifdef DM_HAVE_64BIT | |
1196 DM_DEFINE_FUNC(le64, Uint64, LE64) | |
1197 DM_DEFINE_FUNC(be64, Uint64, BE64) | |
1198 #endif |