Mercurial > hg > dmlib
annotate src/dmres.c @ 2260:972d56ad2b78
Do closedir() in dmResourcesLoadDirectory(), even when under Win32.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 17 Jun 2019 00:49:21 +0300 |
parents | 8962901faf5d |
children | e286454d305f |
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); |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
52 node->rawSize = 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 { |
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
78 dmFree(node->rawData); |
0e164aaa7409
Work on getting the resource subsystem working again ..
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
79 node->rawData = NULL; |
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 | |
1607 | 234 if (fh->rawSize != 0) |
235 return fh->rawSize; | |
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 | |
1607 | 250 fh->rawSize = 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 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
301 handle->rawData = dmMalloc(handle->rawSize); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
302 if (handle->rawData == NULL) |
0 | 303 return DMERR_MALLOC; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
304 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
305 if (dm_stdio_fread(handle->rawData, sizeof(Uint8), handle->rawSize, handle) != handle->rawSize) |
0 | 306 return DMERR_FREAD; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
307 |
0 | 308 return DMERR_OK; |
309 } | |
310 | |
311 | |
312 DMResourceOps dfStdioFileOps = | |
313 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
314 "Stdio", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
315 |
732 | 316 dm_stdio_freset, |
0 | 317 dm_stdio_ferror, |
318 dm_stdio_fseek, | |
319 dm_stdio_fsize, | |
320 dm_stdio_ftell, | |
321 dm_stdio_feof, | |
322 dm_stdio_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
323 dm_stdio_fputc, |
0 | 324 dm_stdio_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
325 dm_stdio_fwrite, |
0 | 326 |
327 dm_stdio_fopen, | |
328 dm_stdio_fclose, | |
329 dm_stdio_preload | |
330 }; | |
331 | |
332 DMResourceOps dfStdioFHOps = | |
333 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
334 "StdioFH", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
335 |
732 | 336 dm_stdio_freset, |
0 | 337 dm_stdio_ferror, |
338 dm_stdio_fseek, | |
339 dm_stdio_fsize, | |
340 dm_stdio_ftell, | |
341 dm_stdio_feof, | |
342 dm_stdio_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
343 dm_stdio_fputc, |
0 | 344 dm_stdio_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
345 dm_stdio_fwrite, |
0 | 346 |
347 NULL, | |
348 NULL, | |
349 NULL | |
350 }; | |
351 #endif | |
352 | |
353 | |
354 // Some mingw/windows headers define these as macros, which is bad for us | |
1144
7842e977d3df
Use DM_WINDOWS check here instead of _WIN32.
Matti Hamalainen <ccr@tnsp.org>
parents:
1122
diff
changeset
|
355 #ifdef DM_WINDOWS |
7842e977d3df
Use DM_WINDOWS check here instead of _WIN32.
Matti Hamalainen <ccr@tnsp.org>
parents:
1122
diff
changeset
|
356 # undef ferror |
7842e977d3df
Use DM_WINDOWS check here instead of _WIN32.
Matti Hamalainen <ccr@tnsp.org>
parents:
1122
diff
changeset
|
357 # undef feof |
0 | 358 #endif |
359 | |
360 | |
361 /* | |
362 * PACK file routines | |
363 */ | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
364 #ifdef DM_USE_PACKFS |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
365 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
366 #ifdef DM_USE_ZLIB |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
367 |
995
aeafd6d44465
Move and rename a constant define.
Matti Hamalainen <ccr@tnsp.org>
parents:
993
diff
changeset
|
368 #define DMRES_TMPBUF_SIZE (128 * 1024) |
aeafd6d44465
Move and rename a constant define.
Matti Hamalainen <ccr@tnsp.org>
parents:
993
diff
changeset
|
369 |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
370 static int dm_pack_decompress(DMResource *handle, DMPackEntry *node) |
0 | 371 { |
1050 | 372 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
|
373 Uint8 * cbuffer = NULL; |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
374 z_stream zstr; |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
375 BOOL zinit = FALSE; |
0 | 376 |
377 // Allocate a structures and buffers | |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
378 if ((cbuffer = dmMalloc(DMRES_TMPBUF_SIZE)) == NULL) |
0 | 379 { |
1050 | 380 ret = DMERR_MALLOC; |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
381 goto out; |
0 | 382 } |
383 | |
384 // Initialize decompression | |
1167 | 385 dmMemset(&zstr, 0, sizeof(zstr)); |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
386 zstr.next_out = handle->rawData; |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
387 zstr.avail_out = handle->rawSize; |
0 | 388 cdataLeft = node->length; |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
389 |
1050 | 390 if ((zret = inflateInit(&zstr)) != Z_OK) |
0 | 391 { |
1050 | 392 ret = dmErrorDBG(DMERR_INIT_FAIL, |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
393 "Could not initialize zlib stream decompression.\n"); |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
394 goto out; |
0 | 395 } |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
396 zinit = TRUE; |
0 | 397 |
398 // Uncompress the data | |
1050 | 399 while (cdataLeft > 0 && zstr.avail_out > 0 && zret == Z_OK) |
0 | 400 { |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
401 zstr.avail_in = fread( |
0 | 402 cbuffer, sizeof(Uint8), |
995
aeafd6d44465
Move and rename a constant define.
Matti Hamalainen <ccr@tnsp.org>
parents:
993
diff
changeset
|
403 (cdataLeft >= DMRES_TMPBUF_SIZE) ? DMRES_TMPBUF_SIZE : cdataLeft, |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
404 handle->lib->packFile->file); |
0 | 405 |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
406 cdataLeft -= zstr.avail_in; |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
407 zstr.next_in = cbuffer; |
1050 | 408 zret = inflate(&zstr, Z_FULL_FLUSH); |
0 | 409 } |
410 | |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
411 |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
412 out: |
0 | 413 // Cleanup |
1015
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
414 if (zinit) |
5101766dd95c
Error handling consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1014
diff
changeset
|
415 inflateEnd(&zstr); |
0 | 416 |
417 dmFree(cbuffer); | |
1050 | 418 return ret; |
0 | 419 } |
420 | |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
421 #else |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
422 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
423 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
|
424 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
425 DMZLibContext ctx; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
426 Uint8 *inBuf = NULL; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
427 int ret; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
428 |
1051 | 429 // Allocate buffer for compressed data |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
430 if ((inBuf = dmMalloc(node->length)) == NULL) |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
431 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
432 ret = DMERR_MALLOC; |
1010 | 433 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
434 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
435 |
1016 | 436 // Read compressed data |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
437 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
|
438 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
439 ret = DMERR_FREAD; |
1010 | 440 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
441 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
442 |
1016 | 443 // Initialize decompression structures |
1121
043b5942fdb6
Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1106
diff
changeset
|
444 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
|
445 goto out; |
1122 | 446 |
1076
21b0aedb7b5c
Also initialize DMZLibContext.inBufferStart where needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
1074
diff
changeset
|
447 ctx.inBuffer = ctx.inBufferStart = inBuf; |
1074
e98bc627ad08
Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
1056
diff
changeset
|
448 ctx.inBufferEnd = inBuf + node->length; |
e98bc627ad08
Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
1056
diff
changeset
|
449 ctx.outBuffer = ctx.outBufferStart = handle->rawData; |
e98bc627ad08
Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
1056
diff
changeset
|
450 ctx.outBufferEnd = handle->rawData + node->size; |
e98bc627ad08
Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
1056
diff
changeset
|
451 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
|
452 |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
453 // Attempt decompression |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
454 if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) |
1010 | 455 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
456 |
1121
043b5942fdb6
Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1106
diff
changeset
|
457 if ((ret = dmZLibInflate(&ctx)) != DMERR_OK) |
1010 | 458 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
459 |
1074
e98bc627ad08
Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
1056
diff
changeset
|
460 handle->rawData = ctx.outBufferStart; |
e98bc627ad08
Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
1056
diff
changeset
|
461 handle->rawSize = ctx.outBuffer - ctx.outBufferStart; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
462 |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
463 out: |
1121
043b5942fdb6
Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1106
diff
changeset
|
464 dmZLibCloseInflate(&ctx); |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
465 dmFree(inBuf); |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
466 return ret; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
467 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
468 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
469 #endif |
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 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
472 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
|
473 { |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
474 DMPackEntry *node; |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
475 int ret = DMERR_OK; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
476 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
477 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
|
478 return DMERR_NULLPTR; |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
479 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
480 // Search PACK nodelist for file |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
481 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
|
482 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
483 ret = dmErrorDBG(DMERR_NOT_FOUND, |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
484 "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
|
485 handle->filename); |
1010 | 486 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
487 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
488 |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
489 // Seek to entry |
1106
f8e9f6b2a41a
Hmm .. back out the Sint64 changes for now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1103
diff
changeset
|
490 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
|
491 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
492 ret = dmErrorDBG(DMERR_FSEEK, |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
493 "Could not seek node position in PACK file.\n"); |
1010 | 494 goto out; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
495 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
496 |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
497 // Allocate memory for the node |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
498 if ((handle->rawData = dmMalloc(node->size)) == NULL) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
499 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
500 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
|
501 "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
|
502 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
|
503 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
|
504 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
505 |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
506 // 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
|
507 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
|
508 { |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
509 if ((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
|
510 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
|
511 |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
512 if (handle->rawSize != node->size) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
513 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
514 ret = dmErrorDBG(DMERR_COMPRESSION, |
1017
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
515 "Decompressed data size for '%s' does not match size stored in PACK entry (%d <> %d).\n", |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
516 handle->filename, handle->rawSize, node->size); |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
517 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
518 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
519 else |
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 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
|
522 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
523 ret = dmErrorDBG(DMERR_INVALID_DATA, |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
524 "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
|
525 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
|
526 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
|
527 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
528 if (fread(handle->rawData, sizeof(Uint8), node->size, handle->lib->packFile->file) != node->size) |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
529 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
530 ret = dmErrorDBG(DMERR_FREAD, |
1045 | 531 "Error reading raw node data '%s', %d bytes.\n", |
532 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
|
533 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
|
534 } |
6436d80ae0fc
Add DMF_COMPRESSED flag for resource nodes and support for raw data nodes.
Matti Hamalainen <ccr@tnsp.org>
parents:
1016
diff
changeset
|
535 } |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
536 |
1010 | 537 out: |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
538 return ret; |
993
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
539 } |
5bcd219ddde3
Some work towards dmzlib decompression support for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
989
diff
changeset
|
540 |
0 | 541 |
1607 | 542 static int dm_pack_fopen(DMResource *fh) |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
543 { |
1607 | 544 if ((fh->flags & DMF_LOADED_RAW) == 0) |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
545 { |
1607 | 546 int ret = dm_pack_preload(fh); |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
547 if (ret == DMERR_OK) |
1607 | 548 fh->flags |= DMF_LOADED_RAW; |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
549 |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
550 return ret; |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
551 } |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
552 else |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
553 return DMERR_OK; |
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 |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
556 |
1607 | 557 static void dm_pack_fclose(DMResource *fh) |
0 | 558 { |
1607 | 559 if ((fh->flags & DMF_PERSIST) == 0) |
560 dmResourceFreeRawData(fh); | |
0 | 561 } |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
562 #endif |
0 | 563 |
564 | |
1607 | 565 static int dm_mem_freset(DMResource *fh) |
732 | 566 { |
1607 | 567 fh->rawOffset = 0; |
732 | 568 return DMERR_OK; |
569 } | |
570 | |
571 | |
1607 | 572 static int dm_mem_ferror(DMResource *fh) |
0 | 573 { |
1607 | 574 return fh->error; |
0 | 575 } |
576 | |
577 | |
1607 | 578 static int dm_mem_fseek(DMResource *fh, const off_t offset, const int whence) |
0 | 579 { |
580 off_t newPos; | |
581 | |
582 // Calculate the new position | |
583 switch (whence) | |
584 { | |
585 case SEEK_SET: | |
586 newPos = offset; | |
587 break; | |
588 | |
589 case SEEK_CUR: | |
1607 | 590 newPos = fh->rawOffset + offset; |
0 | 591 break; |
592 | |
593 case SEEK_END: | |
1607 | 594 newPos = fh->rawSize + offset; |
0 | 595 break; |
596 | |
597 default: | |
598 return -1; | |
599 } | |
600 | |
601 // Set the new position | |
1607 | 602 fh->rawOffset = newPos; |
0 | 603 |
604 // Check the new position | |
1607 | 605 if (newPos < 0 && (size_t) newPos >= fh->rawSize) |
0 | 606 return -1; |
607 | |
608 return 0; | |
609 } | |
610 | |
611 | |
1607 | 612 static off_t dm_mem_fsize(DMResource *fh) |
0 | 613 { |
1607 | 614 return fh->rawSize; |
0 | 615 } |
616 | |
617 | |
1607 | 618 static off_t dm_mem_ftell(DMResource *fh) |
0 | 619 { |
1607 | 620 return fh->rawOffset; |
0 | 621 } |
622 | |
623 | |
1607 | 624 static BOOL dm_mem_feof(DMResource *fh) |
0 | 625 { |
626 // Check for EOF | |
1607 | 627 if ((size_t) fh->rawOffset <= fh->rawSize) |
0 | 628 return FALSE; |
629 else | |
630 return TRUE; | |
631 } | |
632 | |
633 | |
1607 | 634 static int dm_mem_fgetc(DMResource *fh) |
0 | 635 { |
636 // Check for EOF | |
1607 | 637 if ((size_t) fh->rawOffset < fh->rawSize) |
638 return fh->rawData[fh->rawOffset++]; | |
0 | 639 else |
640 return EOF; | |
641 } | |
642 | |
643 | |
1607 | 644 static size_t dm_mem_fread(void *buf, size_t size, size_t nmemb, DMResource *fh) |
0 | 645 { |
646 size_t length = (size * nmemb); | |
647 | |
648 // Check if we can read the whole chunk | |
1607 | 649 if (((size_t) fh->rawOffset + length) >= fh->rawSize) |
0 | 650 { |
1607 | 651 nmemb = (fh->rawSize - fh->rawOffset) / size; |
0 | 652 length = size * nmemb; |
653 } | |
654 | |
1607 | 655 memcpy(buf, fh->rawData + fh->rawOffset, length); |
656 fh->rawOffset += length; | |
0 | 657 return nmemb; |
658 } | |
659 | |
660 | |
1607 | 661 static int dm_mem_fputc(int ch, DMResource *fh) |
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
|
662 { |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
663 // Check for EOF |
1607 | 664 if ((size_t) fh->rawOffset < fh->rawSize) |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
665 { |
1607 | 666 fh->rawData[fh->rawOffset++] = ch; |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
667 return ch; |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
668 } |
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
|
669 else |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
670 return EOF; |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
671 } |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
672 |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
673 |
1607 | 674 static size_t dm_mem_fwrite(const void *buf, size_t size, size_t nmemb, DMResource *fh) |
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
|
675 { |
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
|
676 size_t length = (size * nmemb); |
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
677 |
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
|
678 // Check if we can write the whole chunk |
1607 | 679 if (((size_t) fh->rawOffset + length) >= fh->rawSize) |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
680 { |
1607 | 681 nmemb = (fh->rawSize - fh->rawOffset) / size; |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
682 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
|
683 } |
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
|
684 |
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
|
685 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
|
686 { |
1607 | 687 memcpy(fh->rawData + fh->rawOffset, buf, length); |
688 fh->rawOffset += length; | |
439
bc1da1f4cb4b
Implement fwrite and fputc in memio resources. They will only operate on pre-allocated memory area, thus not grow-as-write.
Matti Hamalainen <ccr@tnsp.org>
parents:
437
diff
changeset
|
689 } |
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
|
690 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
|
691 } |
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
|
692 |
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
|
693 |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
694 #ifdef DM_USE_PACKFS |
0 | 695 DMResourceOps dfPackFileOps = |
696 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
697 "PackFS", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
698 |
732 | 699 dm_mem_freset, |
0 | 700 dm_mem_ferror, |
701 dm_mem_fseek, | |
702 dm_mem_fsize, | |
703 dm_mem_ftell, | |
704 dm_mem_feof, | |
705 dm_mem_fgetc, | |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
706 NULL, |
0 | 707 dm_mem_fread, |
59
c560703e85ed
Add resource writing functions (only work for stdio backend)
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
708 NULL, |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
709 |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
710 dm_pack_fopen, |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
711 dm_pack_fclose, |
732 | 712 dm_pack_preload, |
0 | 713 }; |
166
112eb95f2fba
Improve build system granularity.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
714 #endif |
0 | 715 |
716 | |
717 DMResourceOps dfMemIOFileOps = | |
718 { | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
719 "MemIO", |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
720 |
732 | 721 dm_mem_freset, |
0 | 722 dm_mem_ferror, |
723 dm_mem_fseek, | |
724 dm_mem_fsize, | |
725 dm_mem_ftell, | |
726 dm_mem_feof, | |
727 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
|
728 dm_mem_fputc, |
0 | 729 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
|
730 dm_mem_fwrite, |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
731 |
0 | 732 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
|
733 NULL, |
0 | 734 NULL |
735 }; | |
736 | |
737 | |
738 /* FS file handling functions. These functions call the actual | |
739 * functions depending on where the file is located. | |
740 */ | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
741 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
|
742 { |
733 | 743 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
|
744 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
745 // Check if 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
|
746 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
|
747 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
|
748 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
749 if (handle->flags & DMF_LOADED_RAW) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
750 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
751 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
752 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
|
753 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
754 ret = handle->fops->preload(handle); |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
755 if (ret == DMERR_OK) |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
756 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
|
757 } |
733 | 758 else |
759 ret = DMERR_INIT_FAIL; | |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
760 |
732 | 761 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
|
762 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
763 |
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
|
764 // 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
|
765 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
|
766 { |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
767 if (handle->flags & DMF_LOADED_RES) |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
768 ret = DMERR_OK; |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
769 else |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
770 if (handle->rops != NULL && |
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
771 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
|
772 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
773 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
|
774 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
775 ret = handle->rops->load(handle); |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
776 handle->fops->fclose(handle); |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
777 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
778 |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
779 if (ret == DMERR_OK) |
602
c1a5652e473d
Sanitize resource preloading function a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
780 handle->flags |= DMF_LOADED_RES; |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
781 } |
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
|
782 |
732 | 783 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
|
784 } |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
785 |
e9f562f07cb0
Modularize some more and fix a nasty bug when raw data is not preloaded but
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
786 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
|
787 } |
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
|
788 |
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 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
790 int dmf_open(DMResourceLib *lib, const char *filename, DMResource **phandle) |
0 | 791 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
792 DMResource *handle; |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
793 int ret; |
0 | 794 |
795 // Check master directory for resource | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
796 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
0 | 797 { |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
798 #ifdef DM_USE_STDIO |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
799 if (lib->flags & DRF_USE_STDIO) |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
800 { |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
801 // 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
|
802 *phandle = handle = dmResourceNew(lib, filename, 0, 0); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
803 if (handle == NULL) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
804 return DMERR_MALLOC; |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
805 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
806 handle->fops = &dfStdioFileOps; |
613
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
807 } |
15cd5b8a29b2
Add DRF_USE_STDIO flag for resources.
Matti Hamalainen <ccr@tnsp.org>
parents:
609
diff
changeset
|
808 else |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
809 return DMERR_INIT_FAIL; |
0 | 810 #else |
811 // Stdio not enabled, fail | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
812 return DMERR_INIT_FAIL; |
0 | 813 #endif |
814 } | |
815 | |
816 // Check if the data is preloaded | |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
817 if ((ret = handle->fops->fopen(handle)) == DMERR_OK) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
818 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
819 dmResourceRef(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
820 if (handle->flags & DMF_TEMPORARY) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
821 { |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
822 handle->flags &= ~DMF_TEMPORARY; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
823 dmResourceInsert(lib, handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
824 } |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
825 } |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
826 else |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
827 if (handle->flags & DMF_TEMPORARY) |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
828 { |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
829 dmResourceFree(handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
830 *phandle = handle = NULL; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
831 } |
0 | 832 |
737
897646df1de1
Handle preloading of resources that do not have rops.
Matti Hamalainen <ccr@tnsp.org>
parents:
736
diff
changeset
|
833 dmfreset(handle); |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
834 return ret; |
0 | 835 } |
836 | |
837 | |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1282
diff
changeset
|
838 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
|
839 Uint8 *buf, const size_t size, DMResource **phandle) |
0 | 840 { |
841 DMResource *handle; | |
842 | |
843 // Check master directory for resource | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
844 if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL) |
0 | 845 { |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
846 if ((*phandle = handle = dmResourceNew( |
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
847 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
|
848 return DMERR_MALLOC; |
0 | 849 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
850 handle->fops = &dfMemIOFileOps; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
851 handle->rawData = buf; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
852 dmResourceInsert(lib, handle); |
0 | 853 } |
854 | |
855 // Increase refcount | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
856 dmResourceRef(handle); |
732 | 857 dmfreset(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
858 return DMERR_OK; |
0 | 859 } |
860 | |
861 | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
862 #ifdef DM_USE_STDIO |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1282
diff
changeset
|
863 int dmf_open_stdio(const char *filename, const char *mode, DMResource **phandle) |
0 | 864 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
865 DMResource *handle; |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
866 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
|
867 return DMERR_MALLOC; |
0 | 868 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
869 handle->fops = &dfStdioFileOps; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
870 handle->fh = fopen(filename, mode); |
0 | 871 handle->error = dmGetErrno(); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
872 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
873 if (handle->fh == NULL) |
0 | 874 { |
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
|
875 int error = handle->error; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
876 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
|
877 return error; |
0 | 878 } |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
879 |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
880 dmResourceRef(handle); |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
881 return DMERR_OK; |
0 | 882 } |
883 | |
884 | |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1282
diff
changeset
|
885 int dmf_open_stdio_stream(FILE *fh, DMResource **phandle) |
0 | 886 { |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
887 DMResource *handle; |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
888 if ((*phandle = handle = dmResourceNew(NULL, "", 0, 0)) == NULL) |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
889 return DMERR_MALLOC; |
0 | 890 |
891 handle->fops = &dfStdioFHOps; | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
892 handle->fh = fh; |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
893 dmResourceRef(handle); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
728
diff
changeset
|
894 return DMERR_OK; |
0 | 895 } |
896 #endif | |
897 | |
898 | |
1607 | 899 void dmf_close(DMResource *handle) |
0 | 900 { |
1607 | 901 if (handle == NULL) |
0 | 902 return; |
903 | |
1607 | 904 dmResourceUnref(handle); |
1150 | 905 |
1607 | 906 if (handle->fops->fclose != NULL) |
907 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
|
908 |
a137e49e9f0d
Fix a silly memory leak in stdio support of DMResource, also fix a related
Matti Hamalainen <ccr@tnsp.org>
parents:
1885
diff
changeset
|
909 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
|
910 dmResourceFree(handle); |
0 | 911 } |
912 | |
913 | |
1607 | 914 int dmfreset(DMResource *fh) |
732 | 915 { |
1607 | 916 if (fh == NULL) |
732 | 917 return DMERR_NULLPTR; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
918 |
1607 | 919 if (fh->fops == NULL || fh->fops->freset == NULL) |
732 | 920 return DMERR_OK; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
921 |
1607 | 922 return fh->fops->freset(fh); |
732 | 923 } |
924 | |
1607 | 925 int dmferror(DMResource *fh) |
0 | 926 { |
1607 | 927 fh->atime = time(NULL); |
928 return fh->fops->ferror(fh); | |
0 | 929 } |
930 | |
1607 | 931 int dmfseek(DMResource *fh, const off_t offset, const int whence) |
0 | 932 { |
1607 | 933 fh->atime = time(NULL); |
934 return fh->fops->fseek(fh, offset, whence); | |
0 | 935 } |
936 | |
1607 | 937 off_t dmfsize(DMResource *fh) |
0 | 938 { |
1607 | 939 fh->atime = time(NULL); |
940 return fh->fops->fsize(fh); | |
0 | 941 } |
942 | |
1607 | 943 off_t dmftell(DMResource *fh) |
0 | 944 { |
1607 | 945 fh->atime = time(NULL); |
946 return fh->fops->ftell(fh); | |
0 | 947 } |
948 | |
1607 | 949 BOOL dmfeof(DMResource *fh) |
0 | 950 { |
1607 | 951 fh->atime = time(NULL); |
952 return fh->fops->feof(fh); | |
0 | 953 } |
954 | |
1607 | 955 int dmfgetc(DMResource *fh) |
0 | 956 { |
1607 | 957 fh->atime = time(NULL); |
958 return fh->fops->fgetc(fh); | |
0 | 959 } |
960 | |
1607 | 961 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
|
962 { |
1607 | 963 fh->atime = time(NULL); |
964 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
|
965 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
966 |
1607 | 967 size_t dmfread(void *ptr, size_t size, size_t nmemb, DMResource *fh) |
0 | 968 { |
1607 | 969 fh->atime = time(NULL); |
970 return fh->fops->fread(ptr, size, nmemb, fh); | |
0 | 971 } |
972 | |
1607 | 973 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
|
974 { |
1607 | 975 fh->atime = time(NULL); |
976 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
|
977 } |
be6160981428
Improve and finish write functions in resource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
978 |
1607 | 979 char *dmfgets(char *str, const int size, DMResource *fh) |
235 | 980 { |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
981 char *ptr = str, *end = str + size - 1; |
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
982 int c; |
235 | 983 |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
984 if (size <= 0) |
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
985 return NULL; |
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
986 |
1607 | 987 while (ptr < end && (c = fh->fops->fgetc(fh)) != EOF) |
235 | 988 { |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
989 *ptr++ = c; |
235 | 990 if (c == '\n') |
991 break; | |
992 } | |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
993 *ptr = 0; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
994 |
1282
086f49d616ac
Improve fgets() implementation in dmres.
Matti Hamalainen <ccr@tnsp.org>
parents:
1274
diff
changeset
|
995 return (ptr > str) ? str : NULL; |
235 | 996 } |
997 | |
1608
7f9fe2a9a87e
Implement dmfputs() in DMResource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
1607
diff
changeset
|
998 int dmfputs(const char *str, DMResource *fh) |
7f9fe2a9a87e
Implement dmfputs() in DMResource subsystem.
Matti Hamalainen <ccr@tnsp.org>
parents:
1607
diff
changeset
|
999 { |
1883
08b0cbadbc45
Fix dmfputs(), it was writing the end NUL byte of given string.
Matti Hamalainen <ccr@tnsp.org>
parents:
1608
diff
changeset
|
1000 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
|
1001 } |
235 | 1002 |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1003 int dmResourceRef(DMResource *node) |
0 | 1004 { |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1005 dmLockLibMutex(node->lib); |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1006 node->atime = time(NULL); |
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1007 node->refcount++; |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1008 dmUnlockLibMutex(node->lib); |
0 | 1009 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1010 return node->refcount; |
0 | 1011 } |
1012 | |
1013 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1014 int dmResourceUnref(DMResource *node) |
0 | 1015 { |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1016 dmLockLibMutex(node->lib); |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1017 node->refcount--; |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1018 dmUnlockLibMutex(node->lib); |
0 | 1019 |
27
21c14afbf63d
Modularize the resource system a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
1020 return node->refcount; |
0 | 1021 } |
1022 | |
1023 | |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1024 #ifdef DM_USE_STDIO |
727 | 1025 static int dmResourcesLoadDirectory(DMResourceLib *lib, const char *path) |
0 | 1026 { |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1027 int ret = DMERR_OK; |
630 | 1028 struct dirent *dh; |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1029 DIR *hdir = opendir(path); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1030 if (hdir == NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1031 return dmGetErrno(); |
0 | 1032 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1033 dmLockLibMutex(lib); |
0 | 1034 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1035 do |
0 | 1036 { |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1037 DMResource *node = NULL; |
630 | 1038 dh = readdir(hdir); |
1039 if (dh != NULL) | |
0 | 1040 { |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1041 struct stat sbuf; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1042 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
|
1043 if (stat(fname, &sbuf) == -1) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1044 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1045 ret = dmErrorDBG(dmGetErrno(), |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1046 "Could not stat() %s, #%d: %s\n", |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1047 fname, ret, dmErrorStr(ret)); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1048 dmFree(fname); |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1049 goto out; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1050 } |
0 | 1051 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1052 if (S_ISREG(sbuf.st_mode)) |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1053 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
|
1054 } |
630 | 1055 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1056 if (node != NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1057 { |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1058 node->fops = &dfStdioFileOps; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1059 dmResourceInsert(lib, node); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1060 } |
630 | 1061 } while (dh != NULL); |
0 | 1062 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1063 out: |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1064 dmUnlockLibMutex(lib); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1065 closedir(hdir); |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1066 return ret; |
0 | 1067 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1068 #endif |
0 | 1069 |
1070 | |
1071 /* Resources subsystem initialization and shutdown routines | |
1072 */ | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1073 int dmResourcesInit(DMResourceLib **plib, const char *filename, const char *path, const int flags, int (*classifier)(DMResource *)) |
0 | 1074 { |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1075 DMResourceLib *lib; |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1076 BOOL initialized = FALSE; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1077 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1078 // Allocate the resource library structure |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1079 if ((*plib = lib = dmMalloc0(sizeof(DMResourceLib))) == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1080 return DMERR_MALLOC; |
0 | 1081 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1082 // Basic data |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1083 lib->mutex = dmCreateMutex(); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1084 lib->flags = flags; |
2106
78a0f44aa8b5
Clean out the resource path handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2090
diff
changeset
|
1085 lib->resPath = dm_strdup(path); |
0 | 1086 |
107 | 1087 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1088 #ifdef DM_USE_PACKFS |
734 | 1089 if (flags & DRF_USE_PACK) |
0 | 1090 { |
1091 int ret; | |
1092 DMPackEntry *node; | |
1093 | |
2106
78a0f44aa8b5
Clean out the resource path handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
2090
diff
changeset
|
1094 lib->packFilename = dm_strdup(filename); |
0 | 1095 |
1096 // Initialize PACK, open as read-only | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1097 ret = dmPackOpen(lib->packFilename, &lib->packFile, TRUE); |
0 | 1098 if (ret != DMERR_OK) |
1099 { | |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1100 if ((flags & DRF_USE_STDIO) == 0) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1101 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1102 return dmErrorDBG(DMERR_INIT_FAIL, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1103 "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
|
1104 lib->packFilename, ret, dmErrorStr(ret)); |
0 | 1105 } |
734 | 1106 else |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1107 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1108 // Non-fatal |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1109 dmErrorDBGMsg( |
1274
1b1e7c771698
Clarify certain initialization failure situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1234
diff
changeset
|
1110 "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
|
1111 lib->packFilename, ret, dmErrorStr(ret)); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
872
diff
changeset
|
1112 } |
0 | 1113 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1114 else |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1115 { |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1116 // Initialize resources from a PACK file |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1117 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
|
1118 { |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1119 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
|
1120 if (res == NULL) |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1121 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1045
diff
changeset
|
1122 return dmErrorDBG(DMERR_INIT_FAIL, |
1040
ebabf5aefb76
Work on resource handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1038
diff
changeset
|
1123 "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
|
1124 node->filename, node->flags, node->size); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1125 } |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1126 |
736
ea42faa02a9a
Set packfile fileops in the appropriate handles.
Matti Hamalainen <ccr@tnsp.org>
parents:
734
diff
changeset
|
1127 res->fops = &dfPackFileOps; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1128 dmResourceInsert(lib, res); |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1129 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1130 |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1131 initialized = TRUE; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1132 } |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1133 } |
2107
8962901faf5d
Silence a warning when building without PACKFS support.
Matti Hamalainen <ccr@tnsp.org>
parents:
2106
diff
changeset
|
1134 #else |
8962901faf5d
Silence a warning when building without PACKFS support.
Matti Hamalainen <ccr@tnsp.org>
parents:
2106
diff
changeset
|
1135 (void) filename; |
0 | 1136 #endif |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1137 |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1138 #ifdef DM_USE_STDIO |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1139 if (!initialized && (flags & DRF_USE_STDIO)) |
0 | 1140 { |
1141 // Initialize resources from a resource directory | |
727 | 1142 int ret = dmResourcesLoadDirectory(lib, lib->resPath); |
0 | 1143 if (ret != DMERR_OK) |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1144 return ret; |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1145 initialized = TRUE; |
0 | 1146 } |
625
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1147 #endif |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1148 |
e8fc083b7499
More work on resource management.
Matti Hamalainen <ccr@tnsp.org>
parents:
613
diff
changeset
|
1149 if (!initialized) |
1274
1b1e7c771698
Clarify certain initialization failure situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
1234
diff
changeset
|
1150 return dmErrorDBG(DMERR_INIT_FAIL, "Failed all resource initialization modes.\n"); |
0 | 1151 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1152 // Okay, classify resources |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1153 if (lib->resources != NULL && classifier != NULL) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1154 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1155 DMResource *node; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1156 for (node = lib->resources; node != NULL; node = node->next) |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1157 { |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1158 int ret = classifier(node); |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1159 if (ret != DMERR_OK) |
107 | 1160 return ret; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1161 } |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1162 } |
0 | 1163 |
1164 // Initialization complete | |
1165 return DMERR_OK; | |
1166 } | |
1167 | |
1168 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1169 int dmResourcesClose(DMResourceLib *lib) |
0 | 1170 { |
1171 DMResource *node; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1172 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1173 if (lib == NULL) |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1174 return DMERR_NULLPTR; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1175 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1176 dmLockLibMutex(lib); |
0 | 1177 |
1178 // Shutdown possible subsystems | |
124
d5cfd29c87c4
Rename some conditional compilation directives.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
1179 #ifdef DM_USE_PACKFS |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1180 if (lib->flags & DRF_USE_PACK) |
0 | 1181 { |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1182 int ret = dmPackClose(lib->packFile); |
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1183 if (ret != DMERR_OK) |
0 | 1184 { |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1185 dmErrorDBGMsg( |
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1186 "Error closing PACK, #%i: %s\n", |
1014
501ee5f0c043
Rename variables for consistency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1010
diff
changeset
|
1187 ret, dmErrorStr(ret)); |
0 | 1188 } |
1189 | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1190 dmFree(lib->packFilename); |
0 | 1191 } |
1192 #endif | |
1193 | |
1194 // Free resource entries | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1195 node = lib->resources; |
0 | 1196 while (node != NULL) |
1197 { | |
1198 DMResource *next = node->next; | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1199 dmResourceFree(node); |
0 | 1200 node = next; |
1201 } | |
1202 | |
1203 // Etc. | |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1204 dmFree(lib->resPath); |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1205 dmUnlockLibMutex(lib); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1206 dmDestroyMutex(lib->mutex); |
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1207 return DMERR_OK; |
0 | 1208 } |
1209 | |
1210 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1211 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total) |
0 | 1212 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1213 int ret = DMERR_OK; |
0 | 1214 |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1215 dmLockLibMutex(lib); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1216 |
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1217 // Initialize preloading |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1218 if (lib->preload == NULL || start) |
0 | 1219 { |
1220 DMResource *node; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1088
diff
changeset
|
1221 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1222 lib->preload = lib->resources; |
0 | 1223 *loaded = 0; |
1224 *total = 0; | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1225 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1226 // 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
|
1227 if (lib->flags & (DRF_PRELOAD_RAW | DRF_PRELOAD_RES)) |
0 | 1228 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1229 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1230 (*total)++; |
1231 } | |
1232 } | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1233 else |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1234 if (lib->preload != NULL) |
0 | 1235 { |
728
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
1236 // Attempt to preload the resource |
867fc7f205a8
More work on resource handling cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
727
diff
changeset
|
1237 if ((ret = dmResourcePreload(lib->preload)) != DMERR_OK) |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1238 { |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
1239 dmErrorDBGMsg("Error preloading '%s', %d: %s\n", |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1240 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
|
1241 goto error; |
628
0533f94e843b
Add error message to failed preloading.
Matti Hamalainen <ccr@tnsp.org>
parents:
627
diff
changeset
|
1242 } |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1243 |
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
|
1244 (*loaded)++; |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1245 lib->preload = lib->preload->next; |
0 | 1246 } |
1247 | |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1248 dmUnlockLibMutex(lib); |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1249 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS; |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1250 |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1251 error: |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1252 dmUnlockLibMutex(lib); |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1253 return ret; |
0 | 1254 } |
1255 | |
1256 | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1257 void dmResourcePrune(DMResourceLib *lib, const int agems, int const flags) |
0 | 1258 { |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1259 DMResource *node; |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1260 const int stamp = time(NULL); |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1261 dmLockLibMutex(lib); |
0 | 1262 |
359
59045853853d
Make resource management re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
1263 for (node = lib->resources; node != NULL; node = node->next) |
0 | 1264 { |
1265 // Check if node has refcount of 0 and is | |
1266 // not marked as persistent resource | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1267 if (node->refcount == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1268 (node->flags & DMF_PERSIST) == 0 && |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1269 (node->flags & (DMF_LOADED_RES | DMF_LOADED_RAW))) |
0 | 1270 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1271 if (((flags & DMPRUNE_ATIME) && stamp - node->atime >= agems) || |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1272 ((flags & DMPRUNE_MTIME) && stamp - node->mtime >= agems)) |
0 | 1273 { |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1274 dmResourceFreeResData(node); |
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
630
diff
changeset
|
1275 dmResourceFreeRawData(node); |
0 | 1276 } |
1277 } | |
1278 } | |
1279 | |
2090
0d7c73f42a0b
Use helper functions for resource lib mutex locking.
Matti Hamalainen <ccr@tnsp.org>
parents:
1943
diff
changeset
|
1280 dmUnlockLibMutex(lib); |
0 | 1281 } |
1282 | |
1283 | |
1284 /* Helper resource access routines | |
1285 */ | |
1885
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1286 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
|
1287 { |
1943
6e28a33ed851
Oooops .. fix a memory leak in dmvfprintf() :S That was a bad one.
Matti Hamalainen <ccr@tnsp.org>
parents:
1937
diff
changeset
|
1288 int len, res; |
1885
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1289 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
|
1290 if (str == NULL) |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1291 { |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1292 fh->error = DMERR_MALLOC; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1293 return -1; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1294 } |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1295 |
1943
6e28a33ed851
Oooops .. fix a memory leak in dmvfprintf() :S That was a bad one.
Matti Hamalainen <ccr@tnsp.org>
parents:
1937
diff
changeset
|
1296 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
|
1297 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
|
1298 return res; |
1885
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1299 } |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1300 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1301 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1302 int dmfprintf(DMResource *fh, const char *fmt, ...) |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1303 { |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1304 int res; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1305 va_list ap; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1306 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1307 va_start(ap, fmt); |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1308 res = dmvfprintf(fh, fmt, ap); |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1309 va_end(ap); |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1310 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1311 return res; |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1312 } |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1313 |
e69de47d2419
Implement resource helper functions dmfprintf() and dmvfprintf().
Matti Hamalainen <ccr@tnsp.org>
parents:
1883
diff
changeset
|
1314 |
1607 | 1315 BOOL dmf_read_str(DMResource *fh, void *ptr, const size_t len) |
0 | 1316 { |
1607 | 1317 return dmfread(ptr, len, 1, fh) == 1; |
0 | 1318 } |
1319 | |
1087 | 1320 |
1607 | 1321 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
|
1322 { |
1607 | 1323 int tmp = dmfgetc(fh); |
437
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1324 *val = tmp; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1325 return tmp != EOF; |
3d9c044ec08d
Add dmf_{write,read}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
1326 } |
0 | 1327 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
1328 |
1607 | 1329 #define DM_DEFINE_FFUNC(xname, xtype, xmacro) \ |
1330 BOOL dmf_read_ ## xname (DMResource *fh, xtype *val) { \ | |
1331 xtype result; \ | |
1332 if (dmfread(&result, sizeof( xtype ), 1, fh) != 1) \ | |
1333 return FALSE; \ | |
1334 *val = DM_ ## xmacro ## _TO_NATIVE (result); \ | |
1335 return TRUE; \ | |
0 | 1336 } |
1337 | |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
1338 #include "dmfiletmpl.h" |
0 | 1339 |
1088
9f06f6661cdf
Cleanups for file endianess read/write helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
1087
diff
changeset
|
1340 #undef DM_DEFINE_FFUNC |