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