author | Ryan C. Gordon <icculus@icculus.org> |
Sat, 23 Jul 2005 22:56:45 +0000 | |
changeset 730 | 2786533a5dab |
parent 694 | 80bc8858b4ab |
child 754 | e7cd7411eadf |
permissions | -rw-r--r-- |
609 | 1 |
/* |
2 |
* WAD support routines for PhysicsFS. |
|
3 |
* |
|
4 |
* This driver handles DOOM engine archives ("wads"). |
|
5 |
* This format (but not this driver) was designed by id Software for use |
|
6 |
* with the DOOM engine. |
|
7 |
* The specs of the format are from the unofficial doom specs v1.666 |
|
8 |
* found here: http://www.gamers.org/dhs/helpdocs/dmsp1666.html |
|
9 |
* The format of the archive: (from the specs) |
|
10 |
* |
|
11 |
* A WAD file has three parts: |
|
12 |
* (1) a twelve-byte header |
|
13 |
* (2) one or more "lumps" |
|
14 |
* (3) a directory or "info table" that contains the names, offsets, and |
|
15 |
* sizes of all the lumps in the WAD |
|
16 |
* |
|
17 |
* The header consists of three four-byte parts: |
|
18 |
* (a) an ASCII string which must be either "IWAD" or "PWAD" |
|
19 |
* (b) a 4-byte (long) integer which is the number of lumps in the wad |
|
20 |
* (c) a long integer which is the file offset to the start of |
|
21 |
* the directory |
|
22 |
* |
|
23 |
* The directory has one 16-byte entry for every lump. Each entry consists |
|
24 |
* of three parts: |
|
25 |
* |
|
26 |
* (a) a long integer, the file offset to the start of the lump |
|
27 |
* (b) a long integer, the size of the lump in bytes |
|
28 |
* (c) an 8-byte ASCII string, the name of the lump, padded with zeros. |
|
29 |
* For example, the "DEMO1" entry in hexadecimal would be |
|
30 |
* (44 45 4D 4F 31 00 00 00) |
|
31 |
* |
|
32 |
* Note that there is no way to tell if an opened WAD archive is a |
|
33 |
* IWAD or PWAD with this archiver. |
|
34 |
* I couldn't think of a way to provide that information, without being too |
|
35 |
* hacky. |
|
36 |
* I don't think it's really that important though. |
|
37 |
* |
|
38 |
* |
|
39 |
* Please see the file LICENSE in the source's root directory. |
|
40 |
* |
|
41 |
* This file written by Travis Wells, based on the GRP archiver by |
|
42 |
* Ryan C. Gordon. |
|
43 |
*/ |
|
44 |
||
45 |
#if HAVE_CONFIG_H |
|
46 |
# include <config.h> |
|
47 |
#endif |
|
48 |
||
49 |
#if (defined PHYSFS_SUPPORTS_WAD) |
|
50 |
||
51 |
#include <stdio.h> |
|
52 |
#include <stdlib.h> |
|
53 |
#include <string.h> |
|
54 |
#include "physfs.h" |
|
55 |
||
56 |
#define __PHYSICSFS_INTERNAL__ |
|
57 |
#include "physfs_internal.h" |
|
58 |
||
59 |
typedef struct |
|
60 |
{ |
|
610 | 61 |
char name[18]; |
609 | 62 |
PHYSFS_uint32 startPos; |
63 |
PHYSFS_uint32 size; |
|
64 |
} WADentry; |
|
65 |
||
66 |
typedef struct |
|
67 |
{ |
|
68 |
char *filename; |
|
69 |
PHYSFS_sint64 last_mod_time; |
|
70 |
PHYSFS_uint32 entryCount; |
|
71 |
PHYSFS_uint32 entryOffset; |
|
72 |
WADentry *entries; |
|
73 |
} WADinfo; |
|
74 |
||
75 |
typedef struct |
|
76 |
{ |
|
77 |
void *handle; |
|
78 |
WADentry *entry; |
|
79 |
PHYSFS_uint32 curPos; |
|
80 |
} WADfileinfo; |
|
81 |
||
82 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
83 |
static void WAD_dirClose(dvoid *opaque) |
609 | 84 |
{ |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
85 |
WADinfo *info = ((WADinfo *) opaque); |
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
86 |
allocator.Free(info->filename); |
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
87 |
allocator.Free(info->entries); |
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
88 |
allocator.Free(info); |
609 | 89 |
} /* WAD_dirClose */ |
90 |
||
91 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
92 |
static PHYSFS_sint64 WAD_read(fvoid *opaque, void *buffer, |
609 | 93 |
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount) |
94 |
{ |
|
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
95 |
WADfileinfo *finfo = (WADfileinfo *) opaque; |
609 | 96 |
WADentry *entry = finfo->entry; |
97 |
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos; |
|
98 |
PHYSFS_uint32 objsLeft = (bytesLeft / objSize); |
|
99 |
PHYSFS_sint64 rc; |
|
100 |
||
101 |
if (objsLeft < objCount) |
|
102 |
objCount = objsLeft; |
|
103 |
||
104 |
rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount); |
|
105 |
if (rc > 0) |
|
106 |
finfo->curPos += (PHYSFS_uint32) (rc * objSize); |
|
107 |
||
108 |
return(rc); |
|
109 |
} /* WAD_read */ |
|
110 |
||
111 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
112 |
static PHYSFS_sint64 WAD_write(fvoid *opaque, const void *buffer, |
609 | 113 |
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount) |
114 |
{ |
|
115 |
BAIL_MACRO(ERR_NOT_SUPPORTED, -1); |
|
116 |
} /* WAD_write */ |
|
117 |
||
118 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
119 |
static int WAD_eof(fvoid *opaque) |
609 | 120 |
{ |
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
121 |
WADfileinfo *finfo = (WADfileinfo *) opaque; |
609 | 122 |
WADentry *entry = finfo->entry; |
123 |
return(finfo->curPos >= entry->size); |
|
124 |
} /* WAD_eof */ |
|
125 |
||
126 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
127 |
static PHYSFS_sint64 WAD_tell(fvoid *opaque) |
609 | 128 |
{ |
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
129 |
return(((WADfileinfo *) opaque)->curPos); |
609 | 130 |
} /* WAD_tell */ |
131 |
||
132 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
133 |
static int WAD_seek(fvoid *opaque, PHYSFS_uint64 offset) |
609 | 134 |
{ |
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
135 |
WADfileinfo *finfo = (WADfileinfo *) opaque; |
609 | 136 |
WADentry *entry = finfo->entry; |
137 |
int rc; |
|
138 |
||
139 |
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0); |
|
140 |
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0); |
|
141 |
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset); |
|
142 |
if (rc) |
|
143 |
finfo->curPos = (PHYSFS_uint32) offset; |
|
144 |
||
145 |
return(rc); |
|
146 |
} /* WAD_seek */ |
|
147 |
||
148 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
149 |
static PHYSFS_sint64 WAD_fileLength(fvoid *opaque) |
609 | 150 |
{ |
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
151 |
WADfileinfo *finfo = (WADfileinfo *) opaque; |
609 | 152 |
return((PHYSFS_sint64) finfo->entry->size); |
153 |
} /* WAD_fileLength */ |
|
154 |
||
155 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
156 |
static int WAD_fileClose(fvoid *opaque) |
609 | 157 |
{ |
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
158 |
WADfileinfo *finfo = (WADfileinfo *) opaque; |
609 | 159 |
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0); |
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
160 |
allocator.Free(finfo); |
609 | 161 |
return(1); |
162 |
} /* WAD_fileClose */ |
|
163 |
||
164 |
||
165 |
static int wad_open(const char *filename, int forWriting, |
|
166 |
void **fh, PHYSFS_uint32 *count,PHYSFS_uint32 *offset) |
|
167 |
{ |
|
168 |
PHYSFS_uint8 buf[4]; |
|
169 |
||
170 |
*fh = NULL; |
|
171 |
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0); |
|
172 |
||
173 |
*fh = __PHYSFS_platformOpenRead(filename); |
|
174 |
BAIL_IF_MACRO(*fh == NULL, NULL, 0); |
|
175 |
||
176 |
if (__PHYSFS_platformRead(*fh, buf, 4, 1) != 1) |
|
177 |
goto openWad_failed; |
|
178 |
||
179 |
if (memcmp(buf, "IWAD", 4) != 0 && memcmp(buf, "PWAD", 4) != 0) |
|
180 |
{ |
|
181 |
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE); |
|
182 |
goto openWad_failed; |
|
183 |
} /* if */ |
|
184 |
||
185 |
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1) |
|
186 |
goto openWad_failed; |
|
187 |
||
188 |
*count = PHYSFS_swapULE32(*count); |
|
189 |
||
190 |
if (__PHYSFS_platformRead(*fh, offset, sizeof (PHYSFS_uint32), 1) != 1) |
|
191 |
goto openWad_failed; |
|
192 |
||
193 |
*offset = PHYSFS_swapULE32(*offset); |
|
194 |
||
195 |
return(1); |
|
196 |
||
197 |
openWad_failed: |
|
198 |
if (*fh != NULL) |
|
199 |
__PHYSFS_platformClose(*fh); |
|
200 |
||
201 |
*count = -1; |
|
202 |
*fh = NULL; |
|
203 |
return(0); |
|
204 |
} /* wad_open */ |
|
205 |
||
206 |
||
207 |
static int WAD_isArchive(const char *filename, int forWriting) |
|
208 |
{ |
|
209 |
void *fh; |
|
210 |
PHYSFS_uint32 fileCount,offset; |
|
211 |
int retval = wad_open(filename, forWriting, &fh, &fileCount,&offset); |
|
212 |
||
213 |
if (fh != NULL) |
|
214 |
__PHYSFS_platformClose(fh); |
|
215 |
||
216 |
return(retval); |
|
217 |
} /* WAD_isArchive */ |
|
218 |
||
219 |
||
220 |
static int wad_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) |
|
221 |
{ |
|
222 |
WADentry *a = (WADentry *) _a; |
|
223 |
return(strcmp(a[one].name, a[two].name)); |
|
224 |
} /* wad_entry_cmp */ |
|
225 |
||
226 |
||
227 |
static void wad_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) |
|
228 |
{ |
|
229 |
WADentry tmp; |
|
230 |
WADentry *first = &(((WADentry *) _a)[one]); |
|
231 |
WADentry *second = &(((WADentry *) _a)[two]); |
|
232 |
memcpy(&tmp, first, sizeof (WADentry)); |
|
233 |
memcpy(first, second, sizeof (WADentry)); |
|
234 |
memcpy(second, &tmp, sizeof (WADentry)); |
|
235 |
} /* wad_entry_swap */ |
|
236 |
||
237 |
||
238 |
static int wad_load_entries(const char *name, int forWriting, WADinfo *info) |
|
239 |
{ |
|
240 |
void *fh = NULL; |
|
241 |
PHYSFS_uint32 fileCount; |
|
242 |
PHYSFS_uint32 directoryOffset; |
|
243 |
WADentry *entry; |
|
622
c8e67ca63ad6
Patches to get this building on Mac Classic again.
Ryan C. Gordon <icculus@icculus.org>
parents:
610
diff
changeset
|
244 |
char lastDirectory[9]; |
610 | 245 |
|
246 |
lastDirectory[8] = 0; /* Make sure lastDirectory stays null-terminated. */ |
|
609 | 247 |
|
248 |
BAIL_IF_MACRO(!wad_open(name, forWriting, &fh, &fileCount,&directoryOffset), NULL, 0); |
|
249 |
info->entryCount = fileCount; |
|
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
250 |
info->entries = (WADentry *) allocator.Malloc(sizeof(WADentry)*fileCount); |
609 | 251 |
if (info->entries == NULL) |
252 |
{ |
|
253 |
__PHYSFS_platformClose(fh); |
|
254 |
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0); |
|
255 |
} /* if */ |
|
256 |
||
257 |
__PHYSFS_platformSeek(fh,directoryOffset); |
|
258 |
||
259 |
for (entry = info->entries; fileCount > 0; fileCount--, entry++) |
|
260 |
{ |
|
261 |
if (__PHYSFS_platformRead(fh, &entry->startPos, 4, 1) != 1) |
|
262 |
{ |
|
263 |
__PHYSFS_platformClose(fh); |
|
264 |
return(0); |
|
265 |
} /* if */ |
|
266 |
||
267 |
if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1) |
|
268 |
{ |
|
269 |
__PHYSFS_platformClose(fh); |
|
270 |
return(0); |
|
271 |
} /* if */ |
|
272 |
||
273 |
if (__PHYSFS_platformRead(fh, &entry->name, 8, 1) != 1) |
|
274 |
{ |
|
275 |
__PHYSFS_platformClose(fh); |
|
276 |
return(0); |
|
277 |
} /* if */ |
|
278 |
||
279 |
entry->name[8] = '\0'; /* name might not be null-terminated in file. */ |
|
280 |
entry->size = PHYSFS_swapULE32(entry->size); |
|
281 |
entry->startPos = PHYSFS_swapULE32(entry->startPos); |
|
282 |
} /* for */ |
|
283 |
||
284 |
__PHYSFS_platformClose(fh); |
|
285 |
||
286 |
__PHYSFS_sort(info->entries, info->entryCount, |
|
287 |
wad_entry_cmp, wad_entry_swap); |
|
288 |
return(1); |
|
289 |
} /* wad_load_entries */ |
|
290 |
||
291 |
||
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
292 |
static void *WAD_openArchive(const char *name, int forWriting) |
609 | 293 |
{ |
294 |
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name); |
|
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
295 |
WADinfo *info = (WADinfo *) allocator.Malloc(sizeof (WADinfo)); |
609 | 296 |
|
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
297 |
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL); |
609 | 298 |
memset(info, '\0', sizeof (WADinfo)); |
299 |
||
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
300 |
info->filename = (char *) allocator.Malloc(strlen(name) + 1); |
678
73a2641375a0
Cleaned up some minor bloat with my new evil GOTO_*_MACRO macros.
Ryan C. Gordon <icculus@icculus.org>
parents:
658
diff
changeset
|
301 |
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, WAD_openArchive_failed); |
609 | 302 |
|
303 |
if (!wad_load_entries(name, forWriting, info)) |
|
304 |
goto WAD_openArchive_failed; |
|
305 |
||
306 |
strcpy(info->filename, name); |
|
307 |
info->last_mod_time = modtime; |
|
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
308 |
return(info); |
609 | 309 |
|
310 |
WAD_openArchive_failed: |
|
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
311 |
if (info != NULL) |
609 | 312 |
{ |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
313 |
if (info->filename != NULL) |
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
314 |
allocator.Free(info->filename); |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
315 |
if (info->entries != NULL) |
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
316 |
allocator.Free(info->entries); |
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
317 |
allocator.Free(info); |
609 | 318 |
} /* if */ |
319 |
||
320 |
return(NULL); |
|
321 |
} /* WAD_openArchive */ |
|
322 |
||
323 |
||
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
324 |
static void WAD_enumerateFiles(dvoid *opaque, const char *dname, |
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
325 |
int omitSymLinks, PHYSFS_StringCallback cb, |
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
326 |
void *callbackdata) |
609 | 327 |
{ |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
328 |
WADinfo *info = ((WADinfo *) opaque); |
609 | 329 |
WADentry *entry = info->entries; |
330 |
PHYSFS_uint32 max = info->entryCount; |
|
331 |
PHYSFS_uint32 i; |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
332 |
const char *name; |
610 | 333 |
char *sep; |
609 | 334 |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
335 |
if (*dname == '\0') /* root directory enumeration? */ |
610 | 336 |
{ |
337 |
for (i = 0; i < max; i++, entry++) |
|
338 |
{ |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
339 |
name = entry->name; |
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
340 |
if (strchr(name, '/') == NULL) |
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
341 |
cb(callbackdata, name); |
610 | 342 |
} /* for */ |
343 |
} /* if */ |
|
344 |
else |
|
345 |
{ |
|
346 |
for (i = 0; i < max; i++, entry++) |
|
347 |
{ |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
348 |
name = entry->name; |
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
349 |
sep = strchr(name, '/'); |
610 | 350 |
if (sep != NULL) |
351 |
{ |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
352 |
if (strncmp(dname, name, (sep - name)) == 0) |
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
650
diff
changeset
|
353 |
cb(callbackdata, sep + 1); |
610 | 354 |
} /* if */ |
355 |
} /* for */ |
|
356 |
} /* else */ |
|
609 | 357 |
} /* WAD_enumerateFiles */ |
358 |
||
359 |
||
360 |
static WADentry *wad_find_entry(WADinfo *info, const char *name) |
|
361 |
{ |
|
362 |
WADentry *a = info->entries; |
|
363 |
PHYSFS_sint32 lo = 0; |
|
364 |
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1); |
|
365 |
PHYSFS_sint32 middle; |
|
366 |
int rc; |
|
367 |
||
368 |
while (lo <= hi) |
|
369 |
{ |
|
370 |
middle = lo + ((hi - lo) / 2); |
|
371 |
rc = strcmp(name, a[middle].name); |
|
372 |
if (rc == 0) /* found it! */ |
|
373 |
return(&a[middle]); |
|
374 |
else if (rc > 0) |
|
375 |
lo = middle + 1; |
|
376 |
else |
|
377 |
hi = middle - 1; |
|
378 |
} /* while */ |
|
379 |
||
380 |
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL); |
|
381 |
} /* wad_find_entry */ |
|
382 |
||
383 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
384 |
static int WAD_exists(dvoid *opaque, const char *name) |
609 | 385 |
{ |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
386 |
return(wad_find_entry(((WADinfo *) opaque), name) != NULL); |
609 | 387 |
} /* WAD_exists */ |
388 |
||
389 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
390 |
static int WAD_isDirectory(dvoid *opaque, const char *name, int *fileExists) |
609 | 391 |
{ |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
392 |
WADentry *entry = wad_find_entry(((WADinfo *) opaque), name); |
610 | 393 |
if (entry != NULL) |
394 |
{ |
|
395 |
char *n; |
|
396 |
||
397 |
*fileExists = 1; |
|
398 |
||
399 |
/* Can't be a directory if it's a subdirectory. */ |
|
400 |
if (strchr(entry->name, '/') != NULL) |
|
401 |
return(0); |
|
402 |
||
403 |
/* Check if it matches "MAP??" or "E?M?" ... */ |
|
404 |
n = entry->name; |
|
405 |
if ((n[0] == 'E' && n[2] == 'M') || |
|
406 |
(n[0] == 'M' && n[1] == 'A' && n[2] == 'P' && n[6] == 0)) |
|
407 |
{ |
|
408 |
return(1); |
|
409 |
} /* if */ |
|
410 |
return(0); |
|
411 |
} /* if */ |
|
412 |
else |
|
413 |
{ |
|
414 |
*fileExists = 0; |
|
415 |
return(0); |
|
416 |
} /* else */ |
|
609 | 417 |
} /* WAD_isDirectory */ |
418 |
||
419 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
420 |
static int WAD_isSymLink(dvoid *opaque, const char *name, int *fileExists) |
609 | 421 |
{ |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
422 |
*fileExists = WAD_exists(opaque, name); |
609 | 423 |
return(0); /* never symlinks in a wad. */ |
424 |
} /* WAD_isSymLink */ |
|
425 |
||
426 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
427 |
static PHYSFS_sint64 WAD_getLastModTime(dvoid *opaque, |
609 | 428 |
const char *name, |
429 |
int *fileExists) |
|
430 |
{ |
|
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
431 |
WADinfo *info = ((WADinfo *) opaque); |
609 | 432 |
PHYSFS_sint64 retval = -1; |
433 |
||
434 |
*fileExists = (wad_find_entry(info, name) != NULL); |
|
435 |
if (*fileExists) /* use time of WAD itself in the physical filesystem. */ |
|
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
436 |
retval = info->last_mod_time; |
609 | 437 |
|
438 |
return(retval); |
|
439 |
} /* WAD_getLastModTime */ |
|
440 |
||
441 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
442 |
static fvoid *WAD_openRead(dvoid *opaque, const char *fnm, int *fileExists) |
609 | 443 |
{ |
648
5c993684b8f2
Cleaned up archiver interface to not deal with DirHandles anymore,
Ryan C. Gordon <icculus@icculus.org>
parents:
622
diff
changeset
|
444 |
WADinfo *info = ((WADinfo *) opaque); |
609 | 445 |
WADfileinfo *finfo; |
446 |
WADentry *entry; |
|
447 |
||
448 |
entry = wad_find_entry(info, fnm); |
|
449 |
*fileExists = (entry != NULL); |
|
450 |
BAIL_IF_MACRO(entry == NULL, NULL, NULL); |
|
451 |
||
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
452 |
finfo = (WADfileinfo *) allocator.Malloc(sizeof (WADfileinfo)); |
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
453 |
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL); |
609 | 454 |
|
455 |
finfo->handle = __PHYSFS_platformOpenRead(info->filename); |
|
456 |
if ( (finfo->handle == NULL) || |
|
457 |
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) ) |
|
458 |
{ |
|
691
71d9affe0d8a
All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents:
678
diff
changeset
|
459 |
allocator.Free(finfo); |
609 | 460 |
return(NULL); |
461 |
} /* if */ |
|
462 |
||
463 |
finfo->curPos = 0; |
|
464 |
finfo->entry = entry; |
|
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
465 |
return(finfo); |
609 | 466 |
} /* WAD_openRead */ |
467 |
||
468 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
469 |
static fvoid *WAD_openWrite(dvoid *opaque, const char *name) |
609 | 470 |
{ |
471 |
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL); |
|
472 |
} /* WAD_openWrite */ |
|
473 |
||
474 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
475 |
static fvoid *WAD_openAppend(dvoid *opaque, const char *name) |
609 | 476 |
{ |
477 |
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL); |
|
478 |
} /* WAD_openAppend */ |
|
479 |
||
480 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
481 |
static int WAD_remove(dvoid *opaque, const char *name) |
609 | 482 |
{ |
483 |
BAIL_MACRO(ERR_NOT_SUPPORTED, 0); |
|
484 |
} /* WAD_remove */ |
|
485 |
||
486 |
||
650
298b8bb26775
Did the same thing to FileHandles than I did to DirHandles, but this
Ryan C. Gordon <icculus@icculus.org>
parents:
648
diff
changeset
|
487 |
static int WAD_mkdir(dvoid *opaque, const char *name) |
609 | 488 |
{ |
489 |
BAIL_MACRO(ERR_NOT_SUPPORTED, 0); |
|
490 |
} /* WAD_mkdir */ |
|
491 |
||
658
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
492 |
|
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
493 |
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD = |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
494 |
{ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
495 |
"WAD", |
694
80bc8858b4ab
Added translation for WAD file type.
Ryan C. Gordon <icculus@icculus.org>
parents:
691
diff
changeset
|
496 |
WAD_ARCHIVE_DESCRIPTION, |
658
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
497 |
"Travis Wells <traviswells@mchsi.com>", |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
498 |
"http://www.3dmm2.com/doom/", |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
499 |
}; |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
500 |
|
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
501 |
|
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
502 |
const PHYSFS_Archiver __PHYSFS_Archiver_WAD = |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
503 |
{ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
504 |
&__PHYSFS_ArchiveInfo_WAD, |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
505 |
WAD_isArchive, /* isArchive() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
506 |
WAD_openArchive, /* openArchive() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
507 |
WAD_enumerateFiles, /* enumerateFiles() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
508 |
WAD_exists, /* exists() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
509 |
WAD_isDirectory, /* isDirectory() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
510 |
WAD_isSymLink, /* isSymLink() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
511 |
WAD_getLastModTime, /* getLastModTime() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
512 |
WAD_openRead, /* openRead() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
513 |
WAD_openWrite, /* openWrite() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
514 |
WAD_openAppend, /* openAppend() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
515 |
WAD_remove, /* remove() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
516 |
WAD_mkdir, /* mkdir() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
517 |
WAD_dirClose, /* dirClose() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
518 |
WAD_read, /* read() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
519 |
WAD_write, /* write() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
520 |
WAD_eof, /* eof() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
521 |
WAD_tell, /* tell() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
522 |
WAD_seek, /* seek() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
523 |
WAD_fileLength, /* fileLength() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
524 |
WAD_fileClose /* fileClose() method */ |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
525 |
}; |
1981818c6170
Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
526 |
|
609 | 527 |
#endif /* defined PHYSFS_SUPPORTS_WAD */ |
528 |
||
529 |
/* end of wad.c ... */ |
|
530 |