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