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