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