-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfile.c
More file actions
448 lines (398 loc) · 10.1 KB
/
Copy pathcfile.c
File metadata and controls
448 lines (398 loc) · 10.1 KB
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
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
/*
* $Source: BigRed:miner:source:cfile::RCS:cfile.c $
* $Revision: 1.7 $
* $Author: allender $
* $Date: 1995/10/27 15:18:20 $
*
* Functions for accessing compressed files.
*
* $Log: cfile.c $
* Revision 1.7 1995/10/27 15:18:20 allender
* get back to descent directory before trying to read a hog file
*
* Revision 1.6 1995/10/21 23:48:24 allender
* hogfile(s) are now in :Data: folder
*
* Revision 1.5 1995/08/14 09:27:31 allender
* added byteswap header
*
* Revision 1.4 1995/05/12 11:54:33 allender
* changed memory stuff again
*
* Revision 1.3 1995/05/04 20:03:38 allender
* added code that was missing...use NewPtr instead of malloc
*
* Revision 1.2 1995/04/03 09:59:49 allender
* *** empty log message ***
*
* Revision 1.1 1995/03/30 10:25:02 allender
* Initial revision
*
*
* --- PC RCS Information ---
* Revision 1.24 1995/03/15 14:20:27 john
* Added critical error checker.
*
* Revision 1.23 1995/03/13 15:16:53 john
* Added alternate directory stuff.
*
* Revision 1.22 1995/02/09 23:08:47 matt
* Increased the max number of files in hogfile to 250
*
* Revision 1.21 1995/02/01 20:56:47 john
* Added cfexist function
*
* Revision 1.20 1995/01/21 17:53:48 john
* Added alternate pig file thing.
*
* Revision 1.19 1994/12/29 15:10:02 john
* Increased hogfile max files to 200.
*
* Revision 1.18 1994/12/12 13:20:57 john
* Made cfile work with fiellentth.
*
* Revision 1.17 1994/12/12 13:14:25 john
* Made cfiles prefer non-hog files.
*
* Revision 1.16 1994/12/09 18:53:26 john
* *** empty log message ***
*
* Revision 1.15 1994/12/09 18:52:56 john
* Took out mem, error checking.
*
* Revision 1.14 1994/12/09 18:10:31 john
* Speed up cfgets, which was slowing down the reading of
* bitmaps.tbl, which was making POF loading look slow.
*
* Revision 1.13 1994/12/09 17:53:51 john
* Added error checking to number of hogfiles..
*
* Revision 1.12 1994/12/08 19:02:55 john
* Added cfgets.
*
* Revision 1.11 1994/12/07 21:57:48 john
* Took out data dir.
*
* Revision 1.10 1994/12/07 21:38:02 john
* Made cfile not return error..
*
* Revision 1.9 1994/12/07 21:35:34 john
* Made it read from data directory.
*
* Revision 1.8 1994/12/07 21:33:55 john
* Stripped out compression stuff...
*
* Revision 1.7 1994/04/13 23:44:59 matt
* When file cannot be opened, free up the buffer for that file.
*
* Revision 1.6 1994/02/18 12:38:20 john
* Optimized a bit
*
* Revision 1.5 1994/02/15 18:13:20 john
* Fixed more bugs.
*
* Revision 1.4 1994/02/15 13:27:58 john
* Works ok...
*
* Revision 1.3 1994/02/15 12:51:57 john
* Crappy inbetween version
*
* Revision 1.2 1994/02/14 20:12:29 john
* First version working with new cfile stuff.
*
* Revision 1.1 1994/02/14 15:51:33 john
* Initial revision
*
* Revision 1.1 1994/02/10 15:45:12 john
* Initial revision
*
*
*/
#pragma off (unreferenced)
static char rcsid[] = "$Id: cfile.c 1.7 1995/10/27 15:18:20 allender Exp allender $";
#pragma on (unreferenced)
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <Memory.h>
#include "cfile.h"
#include "mem.h"
#include "byteswap.h"
typedef struct hogfile {
char name[13];
int offset;
int length;
} hogfile;
#define MAX_HOGFILES 250
hogfile HogFiles[MAX_HOGFILES];
char Hogfile_initialized = 0;
int Num_hogfiles = 0;
hogfile AltHogFiles[MAX_HOGFILES];
char AltHogfile_initialized = 0;
int AltNum_hogfiles = 0;
char AltHogFilename[64];
char AltHogDir[64];
char AltHogdir_initialized = 0;
void cfile_use_alternate_hogdir( char * path )
{
if ( path ) {
strcpy( AltHogDir, path );
AltHogdir_initialized = 1;
} else {
AltHogdir_initialized = 0;
}
}
extern int descent_critical_error;
FILE * cfile_get_filehandle( char * filename, char * mode )
{
FILE * fp;
char temp[128];
CDToDescentDir();
descent_critical_error = 0;
fp = fopen( filename, mode );
if ( fp && descent_critical_error ) {
fclose(fp);
fp = NULL;
}
if ( (fp==NULL) && (AltHogdir_initialized) ) {
strcpy( temp, AltHogDir );
strcat( temp, filename );
descent_critical_error = 0;
fp = fopen( temp, mode );
if ( fp && descent_critical_error ) {
fclose(fp);
fp = NULL;
}
}
return fp;
}
void cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
{
char id[4];
FILE * fp;
int i, len;
*nfiles = 0;
CDToDescentDir();
fp = cfile_get_filehandle( fname, "rb" );
if ( fp == NULL ) return;
fread( id, 3, 1, fp );
if ( strncmp( id, "DHF", 3 ) ) {
fclose(fp);
return;
}
while( 1 )
{
if ( *nfiles >= MAX_HOGFILES ) {
Warning( "ERROR: HOGFILE IS LIMITED TO %d FILES\n", MAX_HOGFILES );
fclose(fp);
exit(1);
}
i = fread( hog_files[*nfiles].name, 13, 1, fp );
if ( i != 1 ) {
fclose(fp);
return;
}
i = fread( &len, 4, 1, fp );
if ( i != 1 ) {
fclose(fp);
return;
}
hog_files[*nfiles].length = swapint(len);
if (hog_files[*nfiles].length < 0)
Warning ("Hogfile length < 0");
hog_files[*nfiles].offset = ftell( fp );
*nfiles = (*nfiles) + 1;
// Skip over
i = fseek( fp, swapint(len), SEEK_CUR );
}
}
FILE * cfile_find_libfile(char * name, int * length)
{
FILE * fp;
int i;
if ( AltHogfile_initialized ) {
for (i=0; i<AltNum_hogfiles; i++ ) {
if ( !stricmp( AltHogFiles[i].name, name )) {
fp = cfile_get_filehandle( AltHogFilename, "rb" );
if ( fp == NULL ) return NULL;
fseek( fp, AltHogFiles[i].offset, SEEK_SET );
*length = AltHogFiles[i].length;
return fp;
}
}
}
if ( !Hogfile_initialized ) {
cfile_init_hogfile( ":Data:descent.hog", HogFiles, &Num_hogfiles );
Hogfile_initialized = 1;
}
for (i=0; i<Num_hogfiles; i++ ) {
if ( !stricmp( HogFiles[i].name, name )) {
fp = cfile_get_filehandle( ":Data:descent.hog", "rb" );
if ( fp == NULL ) return NULL;
fseek( fp, HogFiles[i].offset, SEEK_SET );
*length = HogFiles[i].length;
return fp;
}
}
return NULL;
}
void cfile_use_alternate_hogfile( char * name )
{
if ( name ) {
strcpy( AltHogFilename, name );
cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
AltHogfile_initialized = 1;
} else {
AltHogfile_initialized = 0;
}
}
int cfexist( char * filename )
{
int length;
FILE *fp;
fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
if ( fp ) {
fclose(fp);
return 1;
}
fp = cfile_find_libfile(filename, &length );
if ( fp ) {
fclose(fp);
return 2; // file found in hog
}
return 0; // Couldn't find it.
}
CFILE * cfopen(char * filename, char * mode )
{
int length;
FILE * fp;
CFILE *cfile;
char new_filename[256], *p;
if (stricmp( mode, "rb")) {
Warning( "CFILES CAN ONLY BE OPENED WITH RB\n" );
exit(1);
}
strcpy(new_filename, filename);
while ( (p = strchr(new_filename, 13) ) )
*p = '\0';
while ( (p = strchr(new_filename, 10) ) )
*p = '\0';
fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
if ( !fp ) {
fp = cfile_find_libfile(filename, &length );
if ( !fp )
return NULL; // No file found
cfile = (CFILE *)mymalloc ( sizeof(CFILE) );
if ( cfile == NULL ) {
fclose(fp);
return NULL;
}
cfile->file = fp;
cfile->size = length;
cfile->lib_offset = ftell( fp );
cfile->raw_position = 0;
return cfile;
} else {
cfile = (CFILE *)mymalloc ( sizeof(CFILE) );
if ( cfile == NULL ) {
fclose(fp);
return NULL;
}
cfile->file = fp;
cfile->size = filelength( fileno(fp) );
cfile->lib_offset = 0;
cfile->raw_position = 0;
return cfile;
}
}
int cfilelength( CFILE *fp )
{
return fp->size;
}
int cfgetc( CFILE * fp )
{
int c;
if (fp->raw_position >= fp->size ) return EOF;
c = getc( fp->file );
if (c!=EOF)
fp->raw_position++;
// Assert( fp->raw_position==(ftell(fp->file)-fp->lib_offset) );
return c;
}
char * cfgets( char * buf, size_t n, CFILE * fp )
{
char * t = buf;
int i;
int c;
for (i=0; i<n-1; i++ ) {
do {
if (fp->raw_position >= fp->size ) {
*buf = 0;
return NULL;
}
c = fgetc( fp->file );
fp->raw_position++;
} while ( c == 13 );
*buf++ = c;
if ( c == 10 )
c = '\n';
if ( c=='\n' ) break;
}
*buf++ = 0;
return t;
}
size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
{
int i;
if ((fp->raw_position+(elsize*nelem)) > fp->size ) return EOF;
i = fread( buf, elsize, nelem, fp->file );
fp->raw_position += i*elsize;
return i;
}
int cftell( CFILE *fp )
{
return fp->raw_position;
}
int cfseek( CFILE *fp, long int offset, int where )
{
int c, goal_position;
switch( where ) {
case SEEK_SET:
goal_position = offset;
break;
case SEEK_CUR:
goal_position = fp->raw_position+offset;
break;
case SEEK_END:
goal_position = fp->size+offset;
break;
default:
return 1;
}
c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
fp->raw_position = ftell(fp->file)-fp->lib_offset;
return c;
}
void cfclose( CFILE * fp )
{
fclose(fp->file);
myfree(fp);
return;
}