Mercurial > hg > dmlib
annotate src/dmres.c @ 2437:f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 25 Feb 2020 07:26:40 +0200 |
parents | 69a5af2eb1ea |
children | fd31b5d1ed5e |
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 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
572 if (ctx->maxSize > 0 && newSize > ctx->maxSize) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
573 { |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
574 ctx->error = DMERR_BOUNDS; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
575 return FALSE; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
576 } |
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 if (newSize < ctx->memAlloc) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
579 return TRUE; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
580 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
581 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
|
582 if (newSize - ctx->memAlloc > grow) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
583 grow += newSize - ctx->memAlloc; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
584 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
585 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
|
586 { |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
587 ctx->error = DMERR_BOUNDS; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
588 return FALSE; |
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 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
591 ctx->memAlloc += grow; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
592 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
|
593 { |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
594 ctx->error = DMERR_MALLOC; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
595 return FALSE; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
596 } |
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->memSize = newSize; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
599 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
600 return TRUE; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
601 } |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
602 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
603 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
604 static int dm_mem_freset(DMResource *ctx) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
605 { |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
606 ctx->memOffset = 0; |
732 | 607 return DMERR_OK; |
608 } | |
609 | |
610 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
611 static int dm_mem_ferror(DMResource *ctx) |
0 | 612 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
613 return ctx->error; |
0 | 614 } |
615 | |
616 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
617 static int dm_mem_fseek(DMResource *ctx, const off_t offset, const int whence) |
0 | 618 { |
619 off_t newPos; | |
620 | |
621 // Calculate the new position | |
622 switch (whence) | |
623 { | |
624 case SEEK_SET: | |
625 newPos = offset; | |
626 break; | |
627 | |
628 case SEEK_CUR: | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
629 newPos = ctx->memOffset + offset; |
0 | 630 break; |
631 | |
632 case SEEK_END: | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
633 newPos = ctx->memSize + offset; |
0 | 634 break; |
635 | |
636 default: | |
637 return -1; | |
638 } | |
639 | |
640 // Set the new position | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
641 ctx->memOffset = newPos; |
0 | 642 |
643 // Check the new position | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
644 if (newPos < 0) |
0 | 645 return -1; |
646 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
647 //if (!dm_mem_realloc(ctx, newPos)) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
648 // return -1; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
649 |
0 | 650 return 0; |
651 } | |
652 | |
653 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
654 static off_t dm_mem_fsize(DMResource *ctx) |
0 | 655 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
656 return ctx->memSize; |
0 | 657 } |
658 | |
659 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
660 static off_t dm_mem_ftell(DMResource *ctx) |
0 | 661 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
662 return ctx->memOffset; |
0 | 663 } |
664 | |
665 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
666 static BOOL dm_mem_feof(DMResource *ctx) |
0 | 667 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
668 return ((size_t) ctx->memOffset) >= ctx->memSize; |
0 | 669 } |
670 | |
671 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
672 static int dm_mem_fgetc(DMResource *ctx) |
0 | 673 { |
674 // Check for EOF | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
675 if ((size_t) ctx->memOffset < ctx->memSize) |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
676 return ctx->memData[ctx->memOffset++]; |
0 | 677 else |
678 return EOF; | |
679 } | |
680 | |
681 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
682 static size_t dm_mem_fread(void *buf, size_t size, size_t nmemb, DMResource *ctx) |
0 | 683 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
684 size_t length = size * nmemb; |
0 | 685 |
686 // 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
|
687 if (((size_t) ctx->memOffset + length) >= ctx->memSize) |
0 | 688 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
689 nmemb = (ctx->memSize - ctx->memOffset) / size; |
0 | 690 length = size * nmemb; |
691 } | |
692 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
693 memcpy(buf, ctx->memData + ctx->memOffset, length); |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
694 ctx->memOffset += length; |
0 | 695 return nmemb; |
696 } | |
697 | |
698 | |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
699 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
|
700 { |
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
|
701 // Check for EOF |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
702 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
|
703 return EOF; |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
704 |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
705 ctx->memData[ctx->memOffset++] = ch; |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
706 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
|
707 } |
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 |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
709 |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
710 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
|
711 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
712 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
|
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 // 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
|
715 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
|
716 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
717 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
|
718 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
|
719 } |
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
|
720 |
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 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
|
722 { |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
723 memcpy(ctx->memData + ctx->memOffset, buf, length); |
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
724 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
|
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 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
|
727 } |
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
|
728 |
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
|
729 |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
730 #ifdef DM_USE_PACKFS |
0 | 731 DMResourceOps dfPackFileOps = |
732 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
733 "PackFS", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
734 |
732 | 735 dm_mem_freset, |
0 | 736 dm_mem_ferror, |
737 dm_mem_fseek, | |
738 dm_mem_fsize, | |
739 dm_mem_ftell, | |
740 dm_mem_feof, | |
741 dm_mem_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
742 NULL, |
0 | 743 dm_mem_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
744 NULL, |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
745 |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
746 dm_pack_fopen, |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
747 dm_pack_fclose, |
732 | 748 dm_pack_preload, |
0 | 749 }; |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
750 #endif |
0 | 751 |
752 | |
753 DMResourceOps dfMemIOFileOps = | |
754 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
755 "MemIO", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
756 |
732 | 757 dm_mem_freset, |
0 | 758 dm_mem_ferror, |
759 dm_mem_fseek, | |
760 dm_mem_fsize, | |
761 dm_mem_ftell, | |
762 dm_mem_feof, | |
763 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
|
764 dm_mem_fputc, |
0 | 765 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
|
766 dm_mem_fwrite, |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
767 |
0 | 768 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
|
769 NULL, |
0 | 770 NULL |
771 }; | |
772 | |
773 | |
774 /* FS file handling functions. These functions call the actual | |
775 * functions depending on where the file is located. | |
776 */ | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
777 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
|
778 { |
733 | 779 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
|
780 |
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
|
781 // 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
|
782 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
|
783 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
|
784 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
785 if (handle->flags & DMF_LOADED_RAW) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
786 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
787 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
788 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
|
789 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
790 ret = handle->fops->preload(handle); |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
791 if (ret == DMERR_OK) |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
792 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
|
793 } |
733 | 794 else |
795 ret = DMERR_INIT_FAIL; | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
796 |
732 | 797 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
|
798 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
799 |
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
|
800 // 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
|
801 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
|
802 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
803 if (handle->flags & DMF_LOADED_RES) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
804 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
805 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
806 if (handle->rops != NULL && |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
807 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
|
808 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
809 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
|
810 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
811 ret = handle->rops->load(handle); |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
812 handle->fops->fclose(handle); |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
813 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
814 |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
815 if (ret == DMERR_OK) |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
816 handle->flags |= DMF_LOADED_RES; |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
817 } |
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
|
818 |
732 | 819 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
|
820 } |
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
|
821 |
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
|
822 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
|
823 } |
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
|
824 |
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 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
826 int dmf_open(DMResourceLib *lib, const char *filename, DMResource **phandle) |
0 | 827 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
828 DMResource *handle; |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
829 int ret; |
0 | 830 |
831 // Check master directory for resource | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
832 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
0 | 833 { |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
834 #ifdef DM_USE_STDIO |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
835 if (lib->flags & DRF_USE_STDIO) |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
836 { |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
837 // 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
|
838 *phandle = handle = dmResourceNew(lib, filename, 0, 0); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
839 if (handle == NULL) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
840 return DMERR_MALLOC; |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
841 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
842 handle->fops = &dfStdioFileOps; |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
843 } |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
844 else |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
845 return DMERR_INIT_FAIL; |
0 | 846 #else |
847 // Stdio not enabled, fail | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
848 return DMERR_INIT_FAIL; |
0 | 849 #endif |
850 } | |
851 | |
852 // Check if the data is preloaded | |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
853 if ((ret = handle->fops->fopen(handle)) == DMERR_OK) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
854 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
855 dmResourceRef(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
856 if (handle->flags & DMF_TEMPORARY) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
857 { |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
858 handle->flags &= ~DMF_TEMPORARY; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
859 dmResourceInsert(lib, handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
860 } |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
861 } |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
862 else |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
863 if (handle->flags & DMF_TEMPORARY) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
864 { |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
865 dmResourceFree(handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
866 *phandle = handle = NULL; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
867 } |
0 | 868 |
737
897646df1de1
Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents:
736
diff
changeset
|
869 dmfreset(handle); |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
870 return ret; |
0 | 871 } |
872 | |
873 | |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1282
diff
changeset
|
874 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
|
875 Uint8 *buf, const size_t size, DMResource **phandle) |
0 | 876 { |
877 DMResource *handle; | |
878 | |
879 // Check master directory for resource | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
880 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
0 | 881 { |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
882 if ((*phandle = handle = dmResourceNew( |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
883 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
|
884 return DMERR_MALLOC; |
0 | 885 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
886 handle->fops = &dfMemIOFileOps; |
2437
f7d54ac51880
Fix and improvements to dmres MemIO subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
2414
diff
changeset
|
887 handle->memData = buf; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
888 dmResourceInsert(lib, handle); |
0 | 889 } |
890 | |
891 // Increase refcount | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
892 dmResourceRef(handle); |
732 | 893 dmfreset(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
894 return DMERR_OK; |
0 | 895 } |
896 | |
897 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
898 #ifdef DM_USE_STDIO |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1282
diff
changeset
|
899 int dmf_open_stdio(const char *filename, const char *mode, DMResource **phandle) |
0 | 900 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
901 DMResource *handle; |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
902 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
|
903 return DMERR_MALLOC; |
0 | 904 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
905 handle->fops = &dfStdioFileOps; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
906 handle->fh = fopen(filename, mode); |
0 | 907 handle->error = dmGetErrno(); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
908 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
909 if (handle->fh == NULL) |
0 | 910 { |
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
|
911 int error = handle->error; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
912 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
|
913 return error; |
0 | 914 } |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
915 |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
916 dmResourceRef(handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
917 return DMERR_OK; |
0 | 918 } |
919 | |
920 | |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1282
diff
changeset
|
921 int dmf_open_stdio_stream(FILE *fh, DMResource **phandle) |
0 | 922 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
923 DMResource *handle; |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
924 if ((*phandle = handle = dmResourceNew(NULL, "", 0, 0)) == NULL) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
925 return DMERR_MALLOC; |
0 | 926 |
927 handle->fops = &dfStdioFHOps; | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
928 handle->fh = fh; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
929 dmResourceRef(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
930 return DMERR_OK; |
0 | 931 } |
932 #endif | |
933 | |
934 | |
1607 | 935 void dmf_close(DMResource *handle) |
0 | 936 { |
1607 | 937 if (handle == NULL) |
0 | 938 return; |
939 | |
1607 | 940 dmResourceUnref(handle); |
1150 | 941 |
1607 | 942 if (handle->fops->fclose != NULL) |
943 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
|
944 |
a137e49e9f0d
Fix a silly memory leak in stdio support of DMResource, also fix a related
Matti Hamalainen <ccr@tnsp.org>
parents:
1885
diff
changeset
|
945 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
|
946 dmResourceFree(handle); |
0 | 947 } |
948 | |
949 | |
1607 | 950 int dmfreset(DMResource *fh) |
732 | 951 { |
1607 | 952 if (fh == NULL) |
732 | 953 return DMERR_NULLPTR; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
954 |
1607 | 955 if (fh->fops == NULL || fh->fops->freset == NULL) |
732 | 956 return DMERR_OK; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
957 |
1607 | 958 return fh->fops->freset(fh); |
732 | 959 } |
960 | |
2261 | 961 |
1607 | 962 int dmferror(DMResource *fh) |
0 | 963 { |
1607 | 964 fh->atime = time(NULL); |
965 return fh->fops->ferror(fh); | |
0 | 966 } |
967 | |
2261 | 968 |
1607 | 969 int dmfseek(DMResource *fh, const off_t offset, const int whence) |
0 | 970 { |
1607 | 971 fh->atime = time(NULL); |
972 return fh->fops->fseek(fh, offset, whence); | |
0 | 973 } |
974 | |
2261 | 975 |
1607 | 976 off_t dmfsize(DMResource *fh) |
0 | 977 { |
1607 | 978 fh->atime = time(NULL); |
979 return fh->fops->fsize(fh); | |
0 | 980 } |
981 | |
2261 | 982 |
1607 | 983 off_t dmftell(DMResource *fh) |
0 | 984 { |
1607 | 985 fh->atime = time(NULL); |
986 return fh->fops->ftell(fh); | |
0 | 987 } |
988 | |
2261 | 989 |
1607 | 990 BOOL dmfeof(DMResource *fh) |
0 | 991 { |
1607 | 992 fh->atime = time(NULL); |
993 return fh->fops->feof(fh); | |
0 | 994 } |
995 | |
2261 | 996 |
1607 | 997 int dmfgetc(DMResource *fh) |
0 | 998 { |
1607 | 999 fh->atime = time(NULL); |
1000 return fh->fops->fgetc(fh); | |
0 | 1001 } |
1002 | |
2261 | 1003 |
1607 | 1004 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
|
1005 { |
1607 | 1006 fh->atime = time(NULL); |
1007 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
|
1008 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
1009 |
2261 | 1010 |
1607 | 1011 size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource *fh) |
0 | 1012 { |
1607 | 1013 fh->atime = time(NULL); |
1014 return fh->fops->fread(ptr, size, nmemb, fh); | |
0 | 1015 } |
1016 | |
2261 | 1017 |
1607 | 1018 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
|
1019 { |
1607 | 1020 fh->atime = time(NULL); |
1021 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
|
1022 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
1023 |
2261 | 1024 |
1607 | 1025 char *dmfgets(char *str, const int size, DMResource *fh) |
235 | 1026 { |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1027 char *ptr = str, *end = str + size - 1; |
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1028 int c; |
235 | 1029 |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1030 if (size <= 0) |
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1031 return NULL; |
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1032 |
1607 | 1033 while (ptr < end && (c = fh->fops->fgetc(fh)) != EOF) |
235 | 1034 { |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1035 *ptr++ = c; |
235 | 1036 if (c == '\n') |
1037 break; | |
1038 } | |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1039 *ptr = 0; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1040 |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
1041 return (ptr > str) ? str : NULL; |
235 | 1042 } |
1043 | |
2261 | 1044 |
1608
7f9fe2a9a87e
Implement dmfputs() in DMResource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
1607
diff
changeset
|
1045 int dmfputs(const char *str, DMResource *fh) |
7f9fe2a9a87e
Implement dmfputs() in DMResource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
1607
diff
changeset
|
1046 { |
1883
08b0cbadbc45
Fix dmfputs(), it was writing the end NUL byte of given string.
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
1047 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
|
1048 } |
235 | 1049 |
2261 | 1050 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1051 int dmResourceRef(DMResource *node) |
0 | 1052 { |
2262
f92d4f056587
The return value of dmResourceRef() and dmResourceUnref() had a possibility
Matti Hamalainen <ccr@tnsp.org>
parents:
2261
diff
changeset
|
1053 int res; |
f92d4f056587
The return value of dmResourceRef() and dmResourceUnref() had a possibility
Matti Hamalainen <ccr@tnsp.org>
parents:
2261
diff
changeset
|
1054 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1055 dmLockLibMutex(node->lib); |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1056 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
|
1057 res = ++node->refcount; |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1058 dmUnlockLibMutex(node->lib); |
0 | 1059 |
2262
f92d4f056587
The return value of dmResourceRef() and dmResourceUnref() had a possibility
Matti Hamalainen <ccr@tnsp.org>
parents:
2261
diff
changeset
|
1060 return res; |
0 | 1061 } |
1062 | |
1063 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1064 int dmResourceUnref(DMResource *node) |
0 | 1065 { |
2262
f92d4f056587
The return value of dmResourceRef() and dmResourceUnref() had a possibility
Matti Hamalainen <ccr@tnsp.org>
parents:
2261
diff
changeset
|
1066 int res; |
f92d4f056587
The return value of dmResourceRef() and dmResourceUnref() had a possibility
Matti Hamalainen <ccr@tnsp.org>
parents:
2261
diff
changeset
|
1067 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1068 dmLockLibMutex(node->lib); |
2262
f92d4f056587
The return value of dmResourceRef() and dmResourceUnref() had a possibility
Matti Hamalainen <ccr@tnsp.org>
parents:
2261
diff
changeset
|
1069 res = --node->refcount; |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1070 dmUnlockLibMutex(node->lib); |
0 | 1071 |
2262
f92d4f056587
The return value of dmResourceRef() and dmResourceUnref() had a possibility
Matti Hamalainen <ccr@tnsp.org>
parents:
2261
diff
changeset
|
1072 return res; |
0 | 1073 } |
1074 | |
1075 | |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1076 #ifdef DM_USE_STDIO |
727 | 1077 static int dmResourcesLoadDirectory(DMResourceLib *lib, const char *path) |
0 | 1078 { |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1079 int ret = DMERR_OK; |
630 | 1080 struct dirent *dh; |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1081 DIR *hdir = opendir(path); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1082 if (hdir == NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1083 return dmGetErrno(); |
0 | 1084 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1085 dmLockLibMutex(lib); |
0 | 1086 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1087 do |
0 | 1088 { |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1089 DMResource *node = NULL; |
630 | 1090 dh = readdir(hdir); |
1091 if (dh != NULL) | |
0 | 1092 { |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1093 struct stat sbuf; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1094 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
|
1095 if (stat(fname, &sbuf) == -1) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1096 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1097 ret = dmErrorDBG(dmGetErrno(), |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1098 "Could not stat() %s, #%d: %s\n", |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1099 fname, ret, dmErrorStr(ret)); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1100 dmFree(fname); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1101 goto out; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1102 } |
0 | 1103 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1104 if (S_ISREG(sbuf.st_mode)) |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1105 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
|
1106 } |
630 | 1107 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1108 if (node != NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1109 { |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1110 node->fops = &dfStdioFileOps; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1111 dmResourceInsert(lib, node); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1112 } |
630 | 1113 } while (dh != NULL); |
0 | 1114 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1115 out: |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1116 dmUnlockLibMutex(lib); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1117 closedir(hdir); |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1118 return ret; |
0 | 1119 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1120 #endif |
0 | 1121 |
1122 | |
1123 /* Resources subsystem initialization and shutdown routines | |
1124 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1125 int dmResourcesInit(DMResourceLib **plib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)) |
0 | 1126 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1127 DMResourceLib *lib; |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1128 BOOL initialized = FALSE; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1129 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1130 // Allocate the resource library structure |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1131 if ((*plib = lib = dmMalloc0(sizeof(DMResourceLib))) == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1132 return DMERR_MALLOC; |
0 | 1133 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1134 // Basic data |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1135 lib->mutex = dmCreateMutex(); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1136 lib->flags = flags; |
2106
78a0f44aa8b5
Clean out the resource path handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2090
diff
changeset
|
1137 lib->resPath = dm_strdup(path); |
0 | 1138 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1139 #ifdef DM_USE_PACKFS |
734 | 1140 if (flags & DRF_USE_PACK) |
0 | 1141 { |
1142 int ret; | |
1143 DMPackEntry *node; | |
1144 | |
2106
78a0f44aa8b5
Clean out the resource path handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2090
diff
changeset
|
1145 lib->packFilename = dm_strdup(filename); |
0 | 1146 |
1147 // Initialize PACK, open as read-only | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1148 ret = dmPackOpen(lib->packFilename, &lib->packFile, TRUE); |
0 | 1149 if (ret != DMERR_OK) |
1150 { | |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1151 if ((flags & DRF_USE_STDIO) == 0) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1152 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1153 return dmErrorDBG(DMERR_INIT_FAIL, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1154 "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
|
1155 lib->packFilename, ret, dmErrorStr(ret)); |
0 | 1156 } |
734 | 1157 else |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1158 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1159 // Non-fatal |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1160 dmErrorDBGMsg( |
1274
1b1e7c771698
Clarify certain initialization failure situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1234
diff
changeset
|
1161 "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
|
1162 lib->packFilename, ret, dmErrorStr(ret)); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1163 } |
0 | 1164 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1165 else |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1166 { |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1167 // Initialize resources from a PACK file |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1168 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
|
1169 { |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1170 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
|
1171 if (res == NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1172 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1173 return dmErrorDBG(DMERR_INIT_FAIL, |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1174 "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
|
1175 node->filename, node->flags, node->size); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1176 } |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1177 |
736
ea42faa02a9a
Set packfile fileops in the appropriate handles.
Matti Hamalainen <ccr@tnsp.org>
parents:
734
diff
changeset
|
1178 res->fops = &dfPackFileOps; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1179 dmResourceInsert(lib, res); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1180 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1181 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1182 initialized = TRUE; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1183 } |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1184 } |
2107
8962901faf5d
Silence a warning when building without PACKFS support.
Matti Hamalainen <ccr@tnsp.org>
parents:
2106
diff
changeset
|
1185 #else |
8962901faf5d
Silence a warning when building without PACKFS support.
Matti Hamalainen <ccr@tnsp.org>
parents:
2106
diff
changeset
|
1186 (void) filename; |
0 | 1187 #endif |
625
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 #ifdef DM_USE_STDIO |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1190 if (!initialized && (flags & DRF_USE_STDIO)) |
0 | 1191 { |
1192 // Initialize resources from a resource directory | |
727 | 1193 int ret = dmResourcesLoadDirectory(lib, lib->resPath); |
0 | 1194 if (ret != DMERR_OK) |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1195 return ret; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1196 initialized = TRUE; |
0 | 1197 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1198 #endif |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1199 |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1200 if (!initialized) |
1274
1b1e7c771698
Clarify certain initialization failure situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1234
diff
changeset
|
1201 return dmErrorDBG(DMERR_INIT_FAIL, "Failed all resource initialization modes.\n"); |
0 | 1202 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1203 // Okay, classify resources |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1204 if (lib->resources != NULL && classifier != NULL) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1205 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1206 DMResource *node; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1207 for (node = lib->resources; node != NULL; node = node->next) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1208 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1209 int ret = classifier(node); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1210 if (ret != DMERR_OK) |
107 | 1211 return ret; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1212 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1213 } |
0 | 1214 |
1215 // Initialization complete | |
1216 return DMERR_OK; | |
1217 } | |
1218 | |
1219 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1220 int dmResourcesClose(DMResourceLib *lib) |
0 | 1221 { |
1222 DMResource *node; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1223 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1224 if (lib == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1225 return DMERR_NULLPTR; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1226 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1227 dmLockLibMutex(lib); |
0 | 1228 |
1229 // Shutdown possible subsystems | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
1230 #ifdef DM_USE_PACKFS |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1231 if (lib->flags & DRF_USE_PACK) |
0 | 1232 { |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1233 int ret = dmPackClose(lib->packFile); |
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1234 if (ret != DMERR_OK) |
0 | 1235 { |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1236 dmErrorDBGMsg( |
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1237 "Error closing PACK, #%i: %s\n", |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1238 ret, dmErrorStr(ret)); |
0 | 1239 } |
1240 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1241 dmFree(lib->packFilename); |
0 | 1242 } |
1243 #endif | |
1244 | |
1245 // Free resource entries | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1246 node = lib->resources; |
0 | 1247 while (node != NULL) |
1248 { | |
1249 DMResource *next = node->next; | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1250 dmResourceFree(node); |
0 | 1251 node = next; |
1252 } | |
1253 | |
1254 // Etc. | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1255 dmFree(lib->resPath); |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1256 dmUnlockLibMutex(lib); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1257 dmDestroyMutex(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1258 return DMERR_OK; |
0 | 1259 } |
1260 | |
1261 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1262 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total) |
0 | 1263 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1264 int ret = DMERR_OK; |
0 | 1265 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1266 dmLockLibMutex(lib); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1267 |
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1268 // Initialize preloading |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1269 if (lib->preload == NULL || start) |
0 | 1270 { |
1271 DMResource *node; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1272 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1273 lib->preload = lib->resources; |
0 | 1274 *loaded = 0; |
1275 *total = 0; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1276 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1277 // 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
|
1278 if (lib->flags & (DRF_PRELOAD_RAW | DRF_PRELOAD_RES)) |
0 | 1279 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1280 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1281 (*total)++; |
1282 } | |
1283 } | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1284 else |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1285 if (lib->preload != NULL) |
0 | 1286 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
1287 // Attempt to preload the resource |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
1288 if ((ret = dmResourcePreload(lib->preload)) != DMERR_OK) |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1289 { |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1290 dmErrorDBGMsg("Error preloading '%s', %d: %s\n", |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1291 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
|
1292 goto error; |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1293 } |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1294 |
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
|
1295 (*loaded)++; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1296 lib->preload = lib->preload->next; |
0 | 1297 } |
1298 | |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1299 dmUnlockLibMutex(lib); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1300 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1301 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1302 error: |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1303 dmUnlockLibMutex(lib); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1304 return ret; |
0 | 1305 } |
1306 | |
1307 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1308 void dmResourcePrune(DMResourceLib *lib, const int agems, int const flags) |
0 | 1309 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1310 DMResource *node; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1311 const int stamp = time(NULL); |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1312 dmLockLibMutex(lib); |
0 | 1313 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1314 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1315 { |
1316 // Check if node has refcount of 0 and is | |
1317 // not marked as persistent resource | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1318 if (node->refcount == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1319 (node->flags & DMF_PERSIST) == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1320 (node->flags & (DMF_LOADED_RES | DMF_LOADED_RAW))) |
0 | 1321 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1322 if (((flags & DMPRUNE_ATIME) && stamp - node->atime >= agems) || |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1323 ((flags & DMPRUNE_MTIME) && stamp - node->mtime >= agems)) |
0 | 1324 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1325 dmResourceFreeResData(node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1326 dmResourceFreeRawData(node); |
0 | 1327 } |
1328 } | |
1329 } | |
1330 | |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1331 dmUnlockLibMutex(lib); |
0 | 1332 } |
1333 | |
1334 | |
1335 /* Helper resource access routines | |
1336 */ | |
1885
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1337 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
|
1338 { |
1943
6e28a33ed851
Oooops .. fix a memory leak in dmvfprintf() :S That was a bad one.
Matti Hamalainen <ccr@tnsp.org>
parents:
1937
diff
changeset
|
1339 int len, res; |
1885
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1340 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
|
1341 if (str == NULL) |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1342 { |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1343 fh->error = DMERR_MALLOC; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1344 return -1; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1345 } |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1346 |
1943
6e28a33ed851
Oooops .. fix a memory leak in dmvfprintf() :S That was a bad one.
Matti Hamalainen <ccr@tnsp.org>
parents:
1937
diff
changeset
|
1347 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
|
1348 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
|
1349 return res; |
1885
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 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1352 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1353 int dmfprintf(DMResource *fh, const char *fmt, ...) |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1354 { |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1355 int res; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1356 va_list ap; |
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 va_start(ap, fmt); |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1359 res = dmvfprintf(fh, fmt, ap); |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1360 va_end(ap); |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1361 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1362 return res; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1363 } |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1364 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1365 |
1607 | 1366 BOOL dmf_read_str(DMResource *fh, void *ptr, const size_t len) |
0 | 1367 { |
1607 | 1368 return dmfread(ptr, len, 1, fh) == 1; |
0 | 1369 } |
1370 | |
1087 | 1371 |
1607 | 1372 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
|
1373 { |
1607 | 1374 int tmp = dmfgetc(fh); |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1375 *val = tmp; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1376 return tmp != EOF; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1377 } |
0 | 1378 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
1379 |
1607 | 1380 #define DM_DEFINE_FFUNC(xname, xtype, xmacro) \ |
1381 BOOL dmf_read_ ## xname (DMResource *fh, xtype *val) { \ | |
1382 xtype result; \ | |
1383 if (dmfread(&result, sizeof( xtype ), 1, fh) != 1) \ | |
1384 return FALSE; \ | |
1385 *val = DM_ ## xmacro ## _TO_NATIVE (result); \ | |
1386 return TRUE; \ | |
0 | 1387 } |
1388 | |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
1389 #include "dmfiletmpl.h" |
0 | 1390 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
1391 #undef DM_DEFINE_FFUNC |