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