1 /* $Id: cfile.c,v 1.11 2003-04-14 18:34:40 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Functions for accessing compressed files.
20 * Revision 1.7 1995/10/27 15:18:20 allender
21 * get back to descent directory before trying to read a hog file
23 * Revision 1.6 1995/10/21 23:48:24 allender
24 * hogfile(s) are now in :Data: folder
26 * Revision 1.5 1995/08/14 09:27:31 allender
27 * added byteswap header
29 * Revision 1.4 1995/05/12 11:54:33 allender
30 * changed memory stuff again
32 * Revision 1.3 1995/05/04 20:03:38 allender
33 * added code that was missing...use NewPtr instead of malloc
35 * Revision 1.2 1995/04/03 09:59:49 allender
36 * *** empty log message ***
38 * Revision 1.1 1995/03/30 10:25:02 allender
42 * --- PC RCS Information ---
43 * Revision 1.24 1995/03/15 14:20:27 john
44 * Added critical error checker.
46 * Revision 1.23 1995/03/13 15:16:53 john
47 * Added alternate directory stuff.
49 * Revision 1.22 1995/02/09 23:08:47 matt
50 * Increased the max number of files in hogfile to 250
52 * Revision 1.21 1995/02/01 20:56:47 john
53 * Added cfexist function
55 * Revision 1.20 1995/01/21 17:53:48 john
56 * Added alternate pig file thing.
58 * Revision 1.19 1994/12/29 15:10:02 john
59 * Increased hogfile max files to 200.
61 * Revision 1.18 1994/12/12 13:20:57 john
62 * Made cfile work with fiellentth.
64 * Revision 1.17 1994/12/12 13:14:25 john
65 * Made cfiles prefer non-hog files.
67 * Revision 1.16 1994/12/09 18:53:26 john
68 * *** empty log message ***
70 * Revision 1.15 1994/12/09 18:52:56 john
71 * Took out mem, error checking.
73 * Revision 1.14 1994/12/09 18:10:31 john
74 * Speed up cfgets, which was slowing down the reading of
75 * bitmaps.tbl, which was making POF loading look slow.
77 * Revision 1.13 1994/12/09 17:53:51 john
78 * Added error checking to number of hogfiles..
80 * Revision 1.12 1994/12/08 19:02:55 john
83 * Revision 1.11 1994/12/07 21:57:48 john
86 * Revision 1.10 1994/12/07 21:38:02 john
87 * Made cfile not return error..
89 * Revision 1.9 1994/12/07 21:35:34 john
90 * Made it read from data directory.
92 * Revision 1.8 1994/12/07 21:33:55 john
93 * Stripped out compression stuff...
95 * Revision 1.7 1994/04/13 23:44:59 matt
96 * When file cannot be opened, free up the buffer for that file.
98 * Revision 1.6 1994/02/18 12:38:20 john
101 * Revision 1.5 1994/02/15 18:13:20 john
104 * Revision 1.4 1994/02/15 13:27:58 john
107 * Revision 1.3 1994/02/15 12:51:57 john
108 * Crappy inbetween version
110 * Revision 1.2 1994/02/14 20:12:29 john
111 * First version working with new cfile stuff.
113 * Revision 1.1 1994/02/14 15:51:33 john
116 * Revision 1.1 1994/02/10 15:45:12 john
128 #include <sys/stat.h>
136 #include "byteswap.h"
138 typedef struct hogfile {
144 #define MAX_HOGFILES 300
146 hogfile HogFiles[MAX_HOGFILES];
147 char Hogfile_initialized = 0;
148 int Num_hogfiles = 0;
149 char HogFilename[64];
151 hogfile D1HogFiles[MAX_HOGFILES];
152 char D1Hogfile_initialized = 0;
153 int D1Num_hogfiles = 0;
154 char D1HogFilename[64];
156 hogfile AltHogFiles[MAX_HOGFILES];
157 char AltHogfile_initialized = 0;
158 int AltNum_hogfiles = 0;
159 char AltHogFilename[64];
162 char AltHogdir_initialized = 0;
164 // routine to take a DOS path and turn it into a macintosh
165 // pathname. This routine is based on the fact that we should
166 // see a \ character in the dos path. The sequence .\ a tthe
167 // beginning of a path is turned into a :
170 void macify_dospath(char *dos_path, char *mac_path)
174 if (!strncmp(dos_path, ".\\", 2)) {
175 strcpy(mac_path, ":");
176 strcat(mac_path, &(dos_path[2]) );
178 strcpy(mac_path, dos_path);
180 while ( (p = strchr(mac_path, '\\')) != NULL)
186 void cfile_use_alternate_hogdir( char * path )
189 strcpy( AltHogDir, path );
190 AltHogdir_initialized = 1;
192 AltHogdir_initialized = 0;
196 //in case no one installs one
197 int default_error_counter=0;
199 //ptr to counter of how many critical errors
200 int *critical_error_counter_ptr=&default_error_counter;
202 //tell cfile about your critical error counter
203 void cfile_set_critical_error_counter_ptr(int *ptr)
205 critical_error_counter_ptr = ptr;
210 FILE * cfile_get_filehandle( char * filename, char * mode )
215 *critical_error_counter_ptr = 0;
216 fp = fopen( filename, mode );
217 if ( fp && *critical_error_counter_ptr ) {
221 if ( (fp==NULL) && (AltHogdir_initialized) ) {
222 strcpy( temp, AltHogDir );
224 strcat( temp, filename );
225 *critical_error_counter_ptr = 0;
226 fp = fopen( temp, mode );
227 if ( fp && *critical_error_counter_ptr ) {
235 //returns 1 if file loaded with no errors
236 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
244 fp = cfile_get_filehandle( fname, "rb" );
245 if ( fp == NULL ) return 0;
247 fread( id, 3, 1, fp );
248 if ( strncmp( id, "DHF", 3 ) ) {
255 if ( *nfiles >= MAX_HOGFILES ) {
257 Error( "HOGFILE is limited to %d files.\n", MAX_HOGFILES );
259 i = fread( hog_files[*nfiles].name, 13, 1, fp );
260 if ( i != 1 ) { //eof here is ok
264 i = fread( &len, 4, 1, fp );
269 hog_files[*nfiles].length = INTEL_INT(len);
270 hog_files[*nfiles].offset = ftell( fp );
271 *nfiles = (*nfiles) + 1;
273 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
277 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
278 int cfile_init(char *hogname)
283 macify_dospath(hogname, mac_path);
286 Assert(Hogfile_initialized == 0);
289 if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
290 strcpy( HogFilename, hogname );
292 if (cfile_init_hogfile(mac_path, HogFiles, &Num_hogfiles )) {
293 strcpy( HogFilename, mac_path );
295 Hogfile_initialized = 1;
299 return 0; //not loaded!
303 int cfile_size(char *hogname)
308 fp = cfopen(hogname, "rb");
311 fstat(fileno(fp->file), &statbuf);
313 return statbuf.st_size;
317 * return handle for file called "name", embedded in one of the hogfiles
319 FILE * cfile_find_libfile(char * name, int * length)
324 if ( AltHogfile_initialized ) {
325 for (i=0; i<AltNum_hogfiles; i++ ) {
326 if ( !stricmp( AltHogFiles[i].name, name )) {
327 fp = cfile_get_filehandle( AltHogFilename, "rb" );
328 if ( fp == NULL ) return NULL;
329 fseek( fp, AltHogFiles[i].offset, SEEK_SET );
330 *length = AltHogFiles[i].length;
336 if ( !Hogfile_initialized ) {
337 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
338 //@@Hogfile_initialized = 1;
340 //Int3(); //hogfile ought to be initialized
343 for (i=0; i<Num_hogfiles; i++ ) {
344 if ( !stricmp( HogFiles[i].name, name )) {
345 fp = cfile_get_filehandle( HogFilename, "rb" );
346 if ( fp == NULL ) return NULL;
347 fseek( fp, HogFiles[i].offset, SEEK_SET );
348 *length = HogFiles[i].length;
353 if (D1Hogfile_initialized) {
354 for (i = 0; i < D1Num_hogfiles; i++) {
355 if (!stricmp(D1HogFiles[i].name, name)) {
356 fp = cfile_get_filehandle(D1HogFilename, "rb");
357 if (fp == NULL) return NULL;
358 fseek(fp, D1HogFiles[i].offset, SEEK_SET);
359 *length = D1HogFiles[i].length;
368 int cfile_use_alternate_hogfile( char * name )
374 macify_dospath(name, mac_path);
375 strcpy( AltHogFilename, mac_path);
377 strcpy( AltHogFilename, name );
379 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
380 AltHogfile_initialized = 1;
381 return (AltNum_hogfiles > 0);
383 AltHogfile_initialized = 0;
388 int cfile_use_descent1_hogfile( char * name )
394 macify_dospath(name, mac_path);
395 strcpy(D1HogFilename, mac_path);
397 strcpy(D1HogFilename, name);
399 cfile_init_hogfile(D1HogFilename, D1HogFiles, &D1Num_hogfiles);
400 D1Hogfile_initialized = 1;
401 return (D1Num_hogfiles > 0);
403 D1Hogfile_initialized = 0;
408 int cfexist( char * filename )
414 if (filename[0] != '\x01')
415 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
417 fp = NULL; //don't look in dir, only in hogfile
426 fp = cfile_find_libfile(filename, &length );
429 return 2; // file found in hog
432 return 0; // Couldn't find it.
436 CFILE * cfopen(char * filename, char * mode )
442 if (stricmp( mode, "rb")) {
443 Error( "cfiles can only be opened with mode==rb\n" );
446 if (filename[0] != '\x01') {
450 macify_dospath(filename, mac_path);
451 fp = cfile_get_filehandle( mac_path, mode);
453 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
456 fp = NULL; //don't look in dir, only in hogfile
461 fp = cfile_find_libfile(filename, &length );
463 return NULL; // No file found
464 cfile = d_malloc ( sizeof(CFILE) );
465 if ( cfile == NULL ) {
470 cfile->size = length;
471 cfile->lib_offset = ftell( fp );
472 cfile->raw_position = 0;
475 cfile = d_malloc ( sizeof(CFILE) );
476 if ( cfile == NULL ) {
481 cfile->size = filelength( fileno(fp) );
482 cfile->lib_offset = 0;
483 cfile->raw_position = 0;
488 int cfilelength( CFILE *fp )
493 int cfgetc( CFILE * fp )
497 if (fp->raw_position >= fp->size ) return EOF;
499 c = getc( fp->file );
503 // Assert( fp->raw_position==(ftell(fp->file)-fp->lib_offset) );
508 char * cfgets( char * buf, size_t n, CFILE * fp )
514 for (i=0; i<n-1; i++ ) {
516 if (fp->raw_position >= fp->size ) {
520 c = fgetc( fp->file );
522 if (c == 0 || c == 10) // Unix line ending
524 if (c == 13) { // Mac or DOS line ending
527 c1 = fgetc( fp->file );
528 fseek( fp->file, -1, SEEK_CUR);
529 if ( c1 == 10 ) // DOS line ending
531 else // Mac line ending
535 if ( c == 13 ) // because cr-lf is a bad thing on the mac
536 c = '\n'; // and anyway -- 0xod is CR on mac, not 0x0a
538 if ( c=='\n' ) break;
544 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
546 unsigned int i, size;
548 size = elsize * nelem;
549 if ( size < 1 ) return 0;
551 i = fread ( buf, 1, size, fp->file );
552 fp->raw_position += i;
557 int cftell( CFILE *fp )
559 return fp->raw_position;
562 int cfseek( CFILE *fp, long int offset, int where )
564 int c, goal_position;
568 goal_position = offset;
571 goal_position = fp->raw_position+offset;
574 goal_position = fp->size+offset;
579 c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
580 fp->raw_position = ftell(fp->file)-fp->lib_offset;
584 void cfclose( CFILE * fp )
591 // routines to read basic data types from CFILE's. Put here to
592 // simplify mac/pc reading from cfiles.
594 int cfile_read_int(CFILE *file)
598 if (cfread( &i, sizeof(i), 1, file) != 1)
599 Error( "Error reading int in cfile_read_int()" );
605 short cfile_read_short(CFILE *file)
609 if (cfread( &s, sizeof(s), 1, file) != 1)
610 Error( "Error reading short in cfile_read_short()" );
616 byte cfile_read_byte(CFILE *file)
620 if (cfread( &b, sizeof(b), 1, file) != 1)
621 Error( "Error reading byte in cfile_read_byte()" );
626 fix cfile_read_fix(CFILE *file)
630 if (cfread( &f, sizeof(f), 1, file) != 1)
631 Error( "Error reading fix in cfile_read_fix()" );
633 f = (fix)INTEL_INT((int)f);
637 fixang cfile_read_fixang(CFILE *file)
641 if (cfread(&f, 2, 1, file) != 1)
642 Error("Error reading fixang in cfile_read_fixang()");
644 f = (fixang) INTEL_SHORT((int) f);
648 void cfile_read_vector(vms_vector *v, CFILE *file)
650 v->x = cfile_read_fix(file);
651 v->y = cfile_read_fix(file);
652 v->z = cfile_read_fix(file);
655 void cfile_read_angvec(vms_angvec *v, CFILE *file)
657 v->p = cfile_read_fixang(file);
658 v->b = cfile_read_fixang(file);
659 v->h = cfile_read_fixang(file);
662 void cfile_read_matrix(vms_matrix *m,CFILE *file)
664 cfile_read_vector(&m->rvec,file);
665 cfile_read_vector(&m->uvec,file);
666 cfile_read_vector(&m->fvec,file);