1 /* $Id: cfile.c,v 1.21 2003-10-05 22:35:47 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"
145 typedef struct hogfile {
151 #define MAX_HOGFILES 300
153 hogfile HogFiles[MAX_HOGFILES];
154 char Hogfile_initialized = 0;
155 int Num_hogfiles = 0;
156 char HogFilename[64];
158 hogfile D1HogFiles[MAX_HOGFILES];
159 char D1Hogfile_initialized = 0;
160 int D1Num_hogfiles = 0;
161 char D1HogFilename[64];
163 hogfile AltHogFiles[MAX_HOGFILES];
164 char AltHogfile_initialized = 0;
165 int AltNum_hogfiles = 0;
166 char AltHogFilename[64];
169 char AltHogdir_initialized = 0;
171 // routine to take a DOS path and turn it into a macintosh
172 // pathname. This routine is based on the fact that we should
173 // see a \ character in the dos path. The sequence .\ a tthe
174 // beginning of a path is turned into a :
177 void macify_dospath(char *dos_path, char *mac_path)
181 if (!strncmp(dos_path, ".\\", 2)) {
182 strcpy(mac_path, ":");
183 strcat(mac_path, &(dos_path[2]) );
185 strcpy(mac_path, dos_path);
187 while ( (p = strchr(mac_path, '\\')) != NULL)
193 void cfile_use_alternate_hogdir( char * path )
196 strcpy( AltHogDir, path );
197 AltHogdir_initialized = 1;
199 AltHogdir_initialized = 0;
203 //in case no one installs one
204 int default_error_counter=0;
206 //ptr to counter of how many critical errors
207 int *critical_error_counter_ptr=&default_error_counter;
209 //tell cfile about your critical error counter
210 void cfile_set_critical_error_counter_ptr(int *ptr)
212 critical_error_counter_ptr = ptr;
217 FILE * cfile_get_filehandle( char * filename, char * mode )
222 *critical_error_counter_ptr = 0;
223 fp = fopen( filename, mode );
224 if ( fp && *critical_error_counter_ptr ) {
228 if ( (fp==NULL) && (AltHogdir_initialized) ) {
229 strcpy( temp, AltHogDir );
231 strcat( temp, filename );
232 *critical_error_counter_ptr = 0;
233 fp = fopen( temp, mode );
234 if ( fp && *critical_error_counter_ptr ) {
242 //returns 1 if file loaded with no errors
243 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
251 fp = cfile_get_filehandle( fname, "rb" );
252 if ( fp == NULL ) return 0;
254 fread( id, 3, 1, fp );
255 if ( strncmp( id, "DHF", 3 ) ) {
262 if ( *nfiles >= MAX_HOGFILES ) {
264 Error( "HOGFILE is limited to %d files.\n", MAX_HOGFILES );
266 i = fread( hog_files[*nfiles].name, 13, 1, fp );
267 if ( i != 1 ) { //eof here is ok
271 i = fread( &len, 4, 1, fp );
276 hog_files[*nfiles].length = INTEL_INT(len);
277 hog_files[*nfiles].offset = ftell( fp );
278 *nfiles = (*nfiles) + 1;
280 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
284 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
285 int cfile_init(char *hogname)
290 macify_dospath(hogname, mac_path);
293 Assert(Hogfile_initialized == 0);
296 if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
297 strcpy( HogFilename, hogname );
299 if (cfile_init_hogfile(mac_path, HogFiles, &Num_hogfiles )) {
300 strcpy( HogFilename, mac_path );
302 Hogfile_initialized = 1;
306 return 0; //not loaded!
310 int cfile_size(char *hogname)
315 fp = cfopen(hogname, "rb");
318 fstat(fileno(fp->file), &statbuf);
320 return statbuf.st_size;
324 * return handle for file called "name", embedded in one of the hogfiles
326 FILE * cfile_find_libfile(char * name, int * length)
331 if ( AltHogfile_initialized ) {
332 for (i=0; i<AltNum_hogfiles; i++ ) {
333 if ( !stricmp( AltHogFiles[i].name, name )) {
334 fp = cfile_get_filehandle( AltHogFilename, "rb" );
335 if ( fp == NULL ) return NULL;
336 fseek( fp, AltHogFiles[i].offset, SEEK_SET );
337 *length = AltHogFiles[i].length;
343 if ( !Hogfile_initialized ) {
344 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
345 //@@Hogfile_initialized = 1;
347 //Int3(); //hogfile ought to be initialized
350 for (i=0; i<Num_hogfiles; i++ ) {
351 if ( !stricmp( HogFiles[i].name, name )) {
352 fp = cfile_get_filehandle( HogFilename, "rb" );
353 if ( fp == NULL ) return NULL;
354 fseek( fp, HogFiles[i].offset, SEEK_SET );
355 *length = HogFiles[i].length;
360 if (D1Hogfile_initialized) {
361 for (i = 0; i < D1Num_hogfiles; i++) {
362 if (!stricmp(D1HogFiles[i].name, name)) {
363 fp = cfile_get_filehandle(D1HogFilename, "rb");
364 if (fp == NULL) return NULL;
365 fseek(fp, D1HogFiles[i].offset, SEEK_SET);
366 *length = D1HogFiles[i].length;
375 int cfile_use_alternate_hogfile( char * name )
381 macify_dospath(name, mac_path);
382 strcpy( AltHogFilename, mac_path);
384 strcpy( AltHogFilename, name );
386 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
387 AltHogfile_initialized = 1;
388 return (AltNum_hogfiles > 0);
390 AltHogfile_initialized = 0;
395 int cfile_use_descent1_hogfile( char * name )
401 macify_dospath(name, mac_path);
402 strcpy(D1HogFilename, mac_path);
404 strcpy(D1HogFilename, name);
406 cfile_init_hogfile(D1HogFilename, D1HogFiles, &D1Num_hogfiles);
407 D1Hogfile_initialized = 1;
408 return (D1Num_hogfiles > 0);
410 D1Hogfile_initialized = 0;
416 // cfeof() Tests for end-of-file on a stream
418 // returns a nonzero value after the first read operation that attempts to read
419 // past the end of the file. It returns 0 if the current position is not end of file.
420 // There is no error return.
422 int cfeof(CFILE *cfile)
424 Assert(cfile != NULL);
426 Assert(cfile->file != NULL);
428 return (cfile->raw_position >= cfile->size);
432 int cferror(CFILE *cfile)
434 return ferror(cfile->file);
438 int cfexist( char * filename )
444 if (filename[0] != '\x01')
445 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
447 fp = NULL; //don't look in dir, only in hogfile
456 fp = cfile_find_libfile(filename, &length );
459 return 2; // file found in hog
462 return 0; // Couldn't find it.
467 int cfile_delete(char *filename)
469 return remove(filename);
474 int cfile_rename(char *oldname, char *newname)
476 return rename(oldname, newname);
481 int cfile_mkdir(char *pathname)
483 #if defined(__WINDOWS__) || defined(__MINGW32__)
484 return mkdir(pathname);
486 return mkdir(pathname, 0755);
491 CFILE * cfopen(char * filename, char * mode )
497 if (filename[0] != '\x01') {
501 macify_dospath(filename, mac_path);
502 fp = cfile_get_filehandle( mac_path, mode);
504 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
507 fp = NULL; //don't look in dir, only in hogfile
512 fp = cfile_find_libfile(filename, &length );
514 return NULL; // No file found
515 if (stricmp(mode, "rb"))
516 Error("mode must be rb for files in hog.\n");
517 cfile = d_malloc ( sizeof(CFILE) );
518 if ( cfile == NULL ) {
523 cfile->size = length;
524 cfile->lib_offset = ftell( fp );
525 cfile->raw_position = 0;
528 cfile = d_malloc ( sizeof(CFILE) );
529 if ( cfile == NULL ) {
534 cfile->size = ffilelength(fp);
535 cfile->lib_offset = 0;
536 cfile->raw_position = 0;
541 int cfilelength( CFILE *fp )
547 // cfwrite() writes to the file
549 // returns: number of full elements actually written
552 int cfwrite(void *buf, int elsize, int nelem, CFILE *cfile)
556 Assert(cfile != NULL);
560 Assert(cfile->file != NULL);
561 Assert(cfile->lib_offset == 0);
563 items_written = fwrite(buf, elsize, nelem, cfile->file);
564 cfile->raw_position = ftell(cfile->file);
566 return items_written;
570 // cfputc() writes a character to a file
572 // returns: success ==> returns character written
575 int cfputc(int c, CFILE *cfile)
579 Assert(cfile != NULL);
581 Assert(cfile->file != NULL);
582 Assert(cfile->lib_offset == 0);
584 char_written = fputc(c, cfile->file);
585 cfile->raw_position = ftell(cfile->file);
591 int cfgetc( CFILE * fp )
595 if (fp->raw_position >= fp->size ) return EOF;
597 c = getc( fp->file );
599 fp->raw_position = ftell(fp->file)-fp->lib_offset;
605 // cfputs() writes a string to a file
607 // returns: success ==> non-negative value
610 int cfputs(char *str, CFILE *cfile)
614 Assert(cfile != NULL);
617 Assert(cfile->file != NULL);
619 ret = fputs(str, cfile->file);
620 cfile->raw_position = ftell(cfile->file);
626 char * cfgets( char * buf, size_t n, CFILE * fp )
632 #if 0 // don't use the standard fgets, because it will only handle the native line-ending style
633 if (fp->lib_offset == 0) // This is not an archived file
635 t = fgets(buf, n, fp->file);
636 fp->raw_position = ftell(fp->file);
641 for (i=0; i<n-1; i++ ) {
643 if (fp->raw_position >= fp->size ) {
648 if (c == 0 || c == 10) // Unix line ending
650 if (c == 13) { // Mac or DOS line ending
654 cfseek(fp, -1, SEEK_CUR);
655 if ( c1 == 10 ) // DOS line ending
657 else // Mac line ending
661 if ( c == 13 ) // because cr-lf is a bad thing on the mac
662 c = '\n'; // and anyway -- 0xod is CR on mac, not 0x0a
664 if ( c=='\n' ) break;
670 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
672 unsigned int i, size;
674 size = elsize * nelem;
675 if ( size < 1 ) return 0;
677 i = fread ( buf, 1, size, fp->file );
678 fp->raw_position += i;
683 int cftell( CFILE *fp )
685 return fp->raw_position;
688 int cfseek( CFILE *fp, long int offset, int where )
690 int c, goal_position;
694 goal_position = offset;
697 goal_position = fp->raw_position+offset;
700 goal_position = fp->size+offset;
705 c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
706 fp->raw_position = ftell(fp->file)-fp->lib_offset;
710 int cfclose(CFILE *fp)
714 result = fclose(fp->file);
720 // routines to read basic data types from CFILE's. Put here to
721 // simplify mac/pc reading from cfiles.
723 int cfile_read_int(CFILE *file)
727 if (cfread( &i, sizeof(i), 1, file) != 1)
728 Error( "Error reading int in cfile_read_int()" );
734 short cfile_read_short(CFILE *file)
738 if (cfread( &s, sizeof(s), 1, file) != 1)
739 Error( "Error reading short in cfile_read_short()" );
745 sbyte cfile_read_byte(CFILE *file)
749 if (cfread( &b, sizeof(b), 1, file) != 1)
750 Error( "Error reading byte in cfile_read_byte()" );
755 fix cfile_read_fix(CFILE *file)
759 if (cfread( &f, sizeof(f), 1, file) != 1)
760 Error( "Error reading fix in cfile_read_fix()" );
762 f = (fix)INTEL_INT((int)f);
766 fixang cfile_read_fixang(CFILE *file)
770 if (cfread(&f, 2, 1, file) != 1)
771 Error("Error reading fixang in cfile_read_fixang()");
773 f = (fixang) INTEL_SHORT((int) f);
777 void cfile_read_vector(vms_vector *v, CFILE *file)
779 v->x = cfile_read_fix(file);
780 v->y = cfile_read_fix(file);
781 v->z = cfile_read_fix(file);
784 void cfile_read_angvec(vms_angvec *v, CFILE *file)
786 v->p = cfile_read_fixang(file);
787 v->b = cfile_read_fixang(file);
788 v->h = cfile_read_fixang(file);
791 void cfile_read_matrix(vms_matrix *m,CFILE *file)
793 cfile_read_vector(&m->rvec,file);
794 cfile_read_vector(&m->uvec,file);
795 cfile_read_vector(&m->fvec,file);
799 void cfile_read_string(char *buf, int n, CFILE *file)
804 c = (char)cfile_read_byte(file);
814 // equivalent write functions of above read functions follow
816 int cfile_write_int(int i, CFILE *file)
819 return cfwrite(&i, sizeof(i), 1, file);
823 int cfile_write_short(short s, CFILE *file)
826 return cfwrite(&s, sizeof(s), 1, file);
830 int cfile_write_byte(sbyte b, CFILE *file)
832 return cfwrite(&b, sizeof(b), 1, file);
836 int cfile_write_string(char *buf, CFILE *file)
840 if ((!buf) || (buf && !buf[0]))
841 return cfile_write_byte(0, file);
844 if (!cfwrite(buf, len, 1, file))
847 return cfile_write_byte(0, file); // write out NULL termination