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