/
sys_filein.c
611 lines (528 loc) · 16.2 KB
/
sys_filein.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//**************************************************************************
//**
//** F_FILEIN.C
//**
//** File input. Can read from real files or WAD lumps.
//** Note that reading from WAD lumps means that a copy is taken of the
//** lump when the corresponding 'file' is opened. With big files this
//** uses considerable memory and time.
//**
//**************************************************************************
// HEADER FILES ------------------------------------------------------------
#include <io.h>
#include <ctype.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_misc.h"
// MACROS ------------------------------------------------------------------
#define MAX_LUMPDIRS 1024
#define MAX_FILES 64
// TYPES -------------------------------------------------------------------
typedef struct
{
char lump[9];
char *path; // Full path name.
}
lumpdirec_t;
typedef struct
{
f_forall_func_t func;
int parm;
const char *pattern;
}
zipforall_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static lumpdirec_t direc[MAX_LUMPDIRS + 1];
static DFILE files[MAX_FILES];
// CODE --------------------------------------------------------------------
//===========================================================================
// F_MatchName
// Returns true if the string matches the pattern.
// This is a case-insensitive test.
// I do hope this algorithm works like it should...
//===========================================================================
int F_MatchName(const char *string, const char *pattern)
{
const char *in = string, *st = pattern;
while(*in)
{
if(*st == '*')
{
st++;
continue;
}
if(*st != '?'
&& (tolower( (unsigned char) *st)
!= tolower( (unsigned char) *in)))
{
// A mismatch. Hmm. Go back to a previous *.
while(st >= pattern && *st != '*') st--;
if(st < pattern) return false; // No match!
// The asterisk lets us continue.
}
// This character of the pattern is OK.
st++;
in++;
}
// Match is good if the end of the pattern was reached.
while(*st == '*') st++; // Skip remaining asterisks.
return *st == 0;
}
//===========================================================================
// F_SkipSpace
// Skips all whitespace except newlines.
//===========================================================================
char *F_SkipSpace(char *ptr)
{
while(*ptr && *ptr != '\n' && isspace(*ptr)) ptr++;
return ptr;
}
//===========================================================================
// F_FindNewline
//===========================================================================
char *F_FindNewline(char *ptr)
{
while(*ptr && *ptr != '\n') ptr++;
return ptr;
}
//===========================================================================
// F_GetDirecIdx
//===========================================================================
int F_GetDirecIdx(char *exact_path)
{
int i;
for(i = 0; direc[i].path; i++)
if(!stricmp(direc[i].path, exact_path))
return i;
return -1;
}
//===========================================================================
// F_AddDirec
// The path names are converted to full paths before adding to the table.
//===========================================================================
void F_AddDirec(char *lumpname, char *symbolic_path)
{
char path[256], *full;
lumpdirec_t *ld;
int i;
if(!lumpname[0] || !symbolic_path[0]) return;
for(i = 0; direc[i].path && i < MAX_LUMPDIRS; i++);
if(i == MAX_LUMPDIRS)
{
// FIXME: Why no dynamic allocation?
Con_Error("F_AddDirec: Not enough direcs (%s).\n", symbolic_path);
}
ld = direc + i;
// Convert the symbolic path into a real path.
for(i = 0; symbolic_path[i]; i++) // Slashes to backslashes.
if(symbolic_path[i] == '/') symbolic_path[i] = '\\';
if(symbolic_path[0] == '\\')
sprintf(path, "%s%s", ddBasePath, symbolic_path + 1);
else
sprintf(path, "%s%s", ddRuntimeDir.path, symbolic_path);
// Since the basepath might be relative, let's explicitly make
// the path absolute. _fullpath will allocate memory, which will
// be freed in F_ShutdownDirec() (also via F_InitDirec()).
full = _fullpath(0, path, 0);
// If this path already exists, we'll just update the lump name.
i = F_GetDirecIdx(full);
if(i >= 0)
{
// Already exists!
free(full);
ld = direc + i;
full = ld->path;
}
ld->path = full;
strcpy(ld->lump, lumpname);
strupr(ld->lump);
if(verbose)
{
Con_Message("F_AddDirec: %s -> %s\n", ld->lump, ld->path);
}
}
//===========================================================================
// F_ParseDirecData
// LUMPNAM0 \Path\In\The\Base.ext
// LUMPNAM1 Path\In\The\RuntimeDir.ext
// :
//===========================================================================
void F_ParseDirecData(char *buffer)
{
char *ptr = buffer, *end;
char name[9], path[256];
int len;
for(; *ptr; )
{
ptr = F_SkipSpace(ptr);
if(!*ptr) break;
if(*ptr == '\n')
{
ptr++; // Advance to the next line.
continue;
}
// We're at the lump name.
end = M_FindWhite(ptr);
if(!*end) break;
len = end - ptr;
if(len > 8) len = 8;
memset(name, 0, sizeof(name));
strncpy(name, ptr, len);
ptr = F_SkipSpace(end);
if(!*ptr || *ptr == '\n') continue; // Missing filename?
// We're at the path name.
end = F_FindNewline(ptr);
// Get rid of extra whitespace.
while(end > ptr && isspace(*end)) end--;
end = M_FindWhite(end);
len = end - ptr;
if(len > 255) len = 255;
memset(path, 0, sizeof(path));
strncpy(path, ptr, len);
F_AddDirec(name, path);
ptr = end;
}
}
//===========================================================================
// F_InitDirec
// Initialize the WAD/dir translations. Called after WADs have been read.
//===========================================================================
void F_InitDirec(void)
{
int i, len;
byte *buf;
// Free old paths, if any.
F_ShutdownDirec();
memset(direc, 0, sizeof(direc));
// Add the contents of all DD_DIREC lumps.
for(i = 0; i < numlumps; i++)
if(!strnicmp(lumpinfo[i].name, "DD_DIREC", 8))
{
// Make a copy of it so we can make it end in a null.
len = W_LumpLength(i);
buf = Z_Malloc(len + 1, PU_STATIC, 0);
memcpy(buf, W_CacheLumpNum(i, PU_CACHE), len);
buf[len] = 0;
F_ParseDirecData(buf);
Z_Free(buf);
}
}
//===========================================================================
// F_CloseAll
//===========================================================================
void F_CloseAll(void)
{
int i;
for(i = 0; i < MAX_FILES; i++)
if(files[i].flags.open) F_Close(files + i);
}
//===========================================================================
// F_ShutdownDirec
//===========================================================================
void F_ShutdownDirec(void)
{
int i;
for(i = 0; direc[i].path; i++)
if(direc[i].path) free(direc[i].path); // Allocated by _fullpath.
F_CloseAll();
}
//===========================================================================
// F_Access
// Returns true if the file can be opened for reading.
//===========================================================================
int F_Access(const char *path)
{
// Open for reading, but don't buffer anything.
DFILE *file = F_Open(path, "rx");
if(!file) return false;
F_Close(file);
return true;
}
//===========================================================================
// F_GetFreeFile
//===========================================================================
DFILE *F_GetFreeFile(void)
{
int i;
for(i = 0; i < MAX_FILES; i++)
if(!files[i].flags.open)
{
memset(files + i, 0, sizeof(DFILE));
return files + i;
}
return NULL;
}
//===========================================================================
// F_OpenLump
//===========================================================================
DFILE *F_OpenLump(const char *name, boolean dontBuffer)
{
int num = W_CheckNumForName( (char*) name);
DFILE *file;
if(num < 0) return NULL;
file = F_GetFreeFile();
if(!file) return NULL;
// Init and load in the lump data.
file->flags.open = true;
file->flags.file = false;
if(!dontBuffer)
{
file->size = lumpinfo[num].size;
file->pos = file->data = Z_Malloc(file->size, PU_STATIC, 0);
memcpy(file->data, W_CacheLumpNum(num, PU_CACHE), file->size);
}
return file;
}
//===========================================================================
// F_OpenFile
//===========================================================================
DFILE *F_OpenFile(const char *path, const char *mymode)
{
DFILE *file = F_GetFreeFile();
char mode[8];
if(!file) return NULL;
strcpy(mode, "r"); // Open for reading.
if(strchr(mymode, 't')) strcat(mode, "t");
if(strchr(mymode, 'b')) strcat(mode, "b");
// Try opening as a real file.
file->data = fopen(path, mode);
if(!file->data) return NULL; // Can't find the file.
file->flags.open = true;
file->flags.file = true;
return file;
}
//===========================================================================
// F_TranslateZipFileName
//===========================================================================
void F_TranslateZipFileName(const char *zipFileName, char *translated)
{
char buf[256];
// Make a 'real' path out of the zip file name.
M_PrependBasePath(zipFileName, buf);
_fullpath(translated, buf, 255);
}
//===========================================================================
// F_ZipFinder
// Returns true if the names match.
//===========================================================================
int F_ZipFinder(const char *zipFileName, void *path)
{
char fullZipFn[256];
F_TranslateZipFileName(zipFileName, fullZipFn);
// Are they the same?
return !stricmp(path, fullZipFn);
}
//===========================================================================
// F_OpenZip
// Zip data is buffered like lump data.
//===========================================================================
DFILE *F_OpenZip(zipindex_t zipIndex, boolean dontBuffer)
{
DFILE *file = F_GetFreeFile();
if(!file) return NULL;
// Init and load in the lump data.
file->flags.open = true;
file->flags.file = false;
if(!dontBuffer)
{
file->size = Zip_GetSize(zipIndex);
file->pos = file->data = Z_Malloc(file->size, PU_STATIC, 0);
Zip_Read(zipIndex, file->data);
}
return file;
}
//===========================================================================
// F_Open
// Opens the given file (will be translated) or lump for reading.
// "t" = text mode (with real files, lumps are always binary)
// "b" = binary
// "f" = must be a real file
// "w" = file must be in a WAD
// "x" = just test for access (don't buffer anything)
//===========================================================================
DFILE *F_Open(const char *path, const char *mode)
{
char trans[256], full[256];
boolean dontBuffer;
int i;
if(!mode) mode = "";
dontBuffer = (strchr(mode, 'x') != NULL);
// Make it a full path.
M_TranslatePath(path, trans);
_fullpath(full, trans, 255);
// Lumpdirecs take precedence.
if(!strchr(mode, 'f')) // Doesn't need to be a real file?
{
// First check the Zip directory.
zipindex_t foundZip = Zip_Find(F_ZipFinder, full);
if(foundZip)
return F_OpenZip(foundZip, dontBuffer);
for(i = 0; direc[i].path; i++)
if(!stricmp(full, direc[i].path))
return F_OpenLump(direc[i].lump, dontBuffer);
}
if(strchr(mode, 'w')) return NULL; // Must be in a WAD...
// Try to open as a real file, then.
return F_OpenFile(full, mode);
}
//===========================================================================
// F_Close
//===========================================================================
void F_Close(DFILE *file)
{
if(!file->flags.open) return;
if(file->flags.file)
{
fclose(file->data);
}
else
{
// Free the stored data.
if(file->data) Z_Free(file->data);
}
memset(file, 0, sizeof(*file));
}
//===========================================================================
// F_Read
// Returns the number of bytes read (up to 'count').
//===========================================================================
int F_Read(void *dest, int count, DFILE *file)
{
int bytesleft;
if(!file->flags.open) return 0;
if(file->flags.file) // Normal file?
{
count = fread(dest, 1, count, file->data);
if(feof((FILE*)file->data)) file->flags.eof = true;
return count;
}
// Is there enough room in the file?
bytesleft = file->size - (file->pos - (char*) file->data);
if(count > bytesleft)
{
count = bytesleft;
file->flags.eof = true;
}
if(count)
{
memcpy(dest, file->pos, count);
file->pos += count;
}
return count;
}
//===========================================================================
// F_GetC
//===========================================================================
int F_GetC(DFILE *file)
{
unsigned char ch = 0;
if(!file->flags.open) return 0;
F_Read(&ch, 1, file);
return ch;
}
//===========================================================================
// F_Tell
//===========================================================================
int F_Tell(DFILE *file)
{
if(!file->flags.open) return 0;
if(file->flags.file) return ftell(file->data);
return file->pos - (char*) file->data;
}
//===========================================================================
// F_Seek
// Returns the current position in the file, before the move, as an offset
// from the beginning of the file.
//===========================================================================
int F_Seek(DFILE *file, int offset, int whence)
{
int oldpos = F_Tell(file);
if(!file->flags.open) return 0;
file->flags.eof = false;
if(file->flags.file)
fseek(file->data, offset, whence);
else
{
if(whence == SEEK_SET)
file->pos = (char*) file->data + offset;
else if(whence == SEEK_END)
file->pos = (char*) file->data + (file->size + offset);
else if(whence == SEEK_CUR)
file->pos += offset;
}
return oldpos;
}
//===========================================================================
// F_Rewind
//===========================================================================
void F_Rewind(DFILE *file)
{
F_Seek(file, 0, SEEK_SET);
}
//===========================================================================
// F_ZipFinderForAll
// Returns true to stop searching when forall_func returns false.
//===========================================================================
int F_ZipFinderForAll(const char *zipFileName, void *parm)
{
zipforall_t *info = parm;
char fullZipFn[256];
// Convert the zip file name into a real path.
F_TranslateZipFileName(zipFileName, fullZipFn);
if(F_MatchName(fullZipFn, info->pattern))
if(!info->func(fullZipFn, info->parm))
return true; // Stop searching.
// Continue searching.
return false;
}
//===========================================================================
// F_ForAll
// Parm is passed on to the callback, which is called for each file
// matching the filespec. Zip directory, DD_DIREC and the real files are
// scanned.
//===========================================================================
int F_ForAll(const char *filespec, int parm, f_forall_func_t func)
{
long hFile;
struct _finddata_t fd;
directory_t specdir;
char fn[256];
int i;
zipforall_t zipFindInfo;
Dir_FileDir(filespec, &specdir);
// First check the Zip directory.
_fullpath(fn, filespec, 255);
zipFindInfo.func = func;
zipFindInfo.parm = parm;
zipFindInfo.pattern = fn;
if(Zip_Find(F_ZipFinderForAll, &zipFindInfo))
{
// Find didn't finish.
return false;
}
// Check through the dir/WAD direcs.
for(i = 0; direc[i].path; i++)
if(F_MatchName(direc[i].path, fn))
if(!func(direc[i].path, parm)) return false;
// Try searching for the real file.
if((hFile = _findfirst(filespec, &fd)) != -1L)
{
// The first file found!
do
{
// Compile the full pathname of the found file.
strcpy(fn, specdir.path);
strcat(fn, fd.name);
// If the callback returns false, stop immediately.
if(!func(fn, parm)) return false;
}
while(!_findnext(hFile, &fd));
}
return true;
}