1 /* $Id: cfile.c,v 1.29 2004-08-29 17:57:23 schaffner 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.
29 #elif defined(macintosh)
33 # include <sys/stat.h>
51 typedef struct hogfile {
57 #define MAX_HOGFILES 300
59 hogfile HogFiles[MAX_HOGFILES];
60 char Hogfile_initialized = 0;
64 hogfile D1HogFiles[MAX_HOGFILES];
65 char D1Hogfile_initialized = 0;
66 int D1Num_hogfiles = 0;
67 char D1HogFilename[64];
69 hogfile AltHogFiles[MAX_HOGFILES];
70 char AltHogfile_initialized = 0;
71 int AltNum_hogfiles = 0;
72 char AltHogFilename[64];
75 char AltHogdir_initialized = 0;
77 // routine to take a DOS path and turn it into a macintosh
78 // pathname. This routine is based on the fact that we should
79 // see a \ character in the dos path. The sequence .\ a tthe
80 // beginning of a path is turned into a :
82 // routine to take a POSIX style path and turn it into a pre OS X
83 // pathname. This routine uses CFURL's. This function is necessary
84 // because even though fopen exists in StdCLib
85 // it must take a path in the OS native format.
88 void macify_posix_path(char *posix_path, char *mac_path)
92 url = CFURLCreateWithBytes (kCFAllocatorDefault, (ubyte *) posix_path, strlen(posix_path), GetApplicationTextEncoding(), NULL);
93 CFURLGetFileSystemRepresentation (url, 0, (ubyte *) mac_path, 255);
98 void cfile_use_alternate_hogdir( char * path )
101 strcpy( AltHogDir, path );
102 AltHogdir_initialized = 1;
104 AltHogdir_initialized = 0;
108 //in case no one installs one
109 int default_error_counter=0;
111 //ptr to counter of how many critical errors
112 int *critical_error_counter_ptr=&default_error_counter;
114 //tell cfile about your critical error counter
115 void cfile_set_critical_error_counter_ptr(int *ptr)
117 critical_error_counter_ptr = ptr;
122 FILE * cfile_get_filehandle( char * filename, char * mode )
130 *critical_error_counter_ptr = 0;
132 macify_posix_path(filename, mac_path);
133 fp = fopen( mac_path, mode );
135 fp = fopen( filename, mode );
137 if ( fp && *critical_error_counter_ptr ) {
141 if ( (fp==NULL) && (AltHogdir_initialized) ) {
142 strcpy( temp, AltHogDir );
144 strcat( temp, filename );
145 *critical_error_counter_ptr = 0;
147 macify_posix_path(temp, mac_path);
148 fp = fopen( mac_path, mode );
150 fp = fopen( temp, mode );
152 if ( fp && *critical_error_counter_ptr ) {
160 //returns 1 if file loaded with no errors
161 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
169 fp = cfile_get_filehandle( fname, "rb" );
170 if ( fp == NULL ) return 0;
172 fread( id, 3, 1, fp );
173 if ( strncmp( id, "DHF", 3 ) ) {
180 if ( *nfiles >= MAX_HOGFILES ) {
182 Error( "HOGFILE is limited to %d files.\n", MAX_HOGFILES );
184 i = fread( hog_files[*nfiles].name, 13, 1, fp );
185 if ( i != 1 ) { //eof here is ok
189 i = fread( &len, 4, 1, fp );
194 hog_files[*nfiles].length = INTEL_INT(len);
195 hog_files[*nfiles].offset = ftell( fp );
196 *nfiles = (*nfiles) + 1;
198 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
202 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
203 int cfile_init(char *hogname)
206 Assert(Hogfile_initialized == 0);
208 if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
209 strcpy( HogFilename, hogname );
210 Hogfile_initialized = 1;
214 return 0; //not loaded!
218 int cfile_size(char *hogname)
223 fp = cfopen(hogname, "rb");
226 size = ffilelength(fp->file);
232 * return handle for file called "name", embedded in one of the hogfiles
234 FILE * cfile_find_libfile(char * name, int * length)
239 if ( AltHogfile_initialized ) {
240 for (i=0; i<AltNum_hogfiles; i++ ) {
241 if ( !stricmp( AltHogFiles[i].name, name )) {
242 fp = cfile_get_filehandle( AltHogFilename, "rb" );
243 if ( fp == NULL ) return NULL;
244 fseek( fp, AltHogFiles[i].offset, SEEK_SET );
245 *length = AltHogFiles[i].length;
251 if ( !Hogfile_initialized ) {
252 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
253 //@@Hogfile_initialized = 1;
255 //Int3(); //hogfile ought to be initialized
258 for (i=0; i<Num_hogfiles; i++ ) {
259 if ( !stricmp( HogFiles[i].name, name )) {
260 fp = cfile_get_filehandle( HogFilename, "rb" );
261 if ( fp == NULL ) return NULL;
262 fseek( fp, HogFiles[i].offset, SEEK_SET );
263 *length = HogFiles[i].length;
268 if (D1Hogfile_initialized) {
269 for (i = 0; i < D1Num_hogfiles; i++) {
270 if (!stricmp(D1HogFiles[i].name, name)) {
271 fp = cfile_get_filehandle(D1HogFilename, "rb");
272 if (fp == NULL) return NULL;
273 fseek(fp, D1HogFiles[i].offset, SEEK_SET);
274 *length = D1HogFiles[i].length;
283 int cfile_use_alternate_hogfile( char * name )
286 strcpy( AltHogFilename, name );
287 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
288 AltHogfile_initialized = 1;
289 return (AltNum_hogfiles > 0);
291 AltHogfile_initialized = 0;
296 int cfile_use_descent1_hogfile( char * name )
299 strcpy(D1HogFilename, name);
300 cfile_init_hogfile(D1HogFilename, D1HogFiles, &D1Num_hogfiles);
301 D1Hogfile_initialized = 1;
302 return (D1Num_hogfiles > 0);
304 D1Hogfile_initialized = 0;
310 // cfeof() Tests for end-of-file on a stream
312 // returns a nonzero value after the first read operation that attempts to read
313 // past the end of the file. It returns 0 if the current position is not end of file.
314 // There is no error return.
316 int cfeof(CFILE *cfile)
318 Assert(cfile != NULL);
320 Assert(cfile->file != NULL);
322 return (cfile->raw_position >= cfile->size);
326 int cferror(CFILE *cfile)
328 return ferror(cfile->file);
332 int cfexist( char * filename )
338 if (filename[0] != '\x01')
339 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
341 fp = NULL; //don't look in dir, only in hogfile
350 fp = cfile_find_libfile(filename, &length );
353 return 2; // file found in hog
356 return 0; // Couldn't find it.
361 int cfile_delete(char *filename)
364 return remove(filename);
366 return !DeleteFile(filename);
372 int cfile_rename(char *oldname, char *newname)
375 return rename(oldname, newname);
377 return !MoveFile(oldname, newname);
383 int cfile_mkdir(char *pathname)
387 return !CreateDirectory(pathname, NULL);
389 return _mkdir(pathname);
391 #elif defined(macintosh)
394 long dirID; // Insists on returning this
396 macify_posix_path(pathname, mac_path);
397 CopyCStringToPascal(mac_path, pascal_path);
398 return DirCreate(0, 0, pascal_path, &dirID);
400 return mkdir(pathname, 0755);
405 CFILE * cfopen(char * filename, char * mode )
411 if (filename[0] != '\x01')
412 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
414 fp = NULL; //don't look in dir, only in hogfile
419 fp = cfile_find_libfile(filename, &length );
421 return NULL; // No file found
422 if (stricmp(mode, "rb"))
423 Error("mode must be rb for files in hog.\n");
424 cfile = d_malloc ( sizeof(CFILE) );
425 if ( cfile == NULL ) {
430 cfile->size = length;
431 cfile->lib_offset = ftell( fp );
432 cfile->raw_position = 0;
435 cfile = d_malloc ( sizeof(CFILE) );
436 if ( cfile == NULL ) {
441 cfile->size = ffilelength(fp);
442 cfile->lib_offset = 0;
443 cfile->raw_position = 0;
448 int cfilelength( CFILE *fp )
454 // cfwrite() writes to the file
456 // returns: number of full elements actually written
459 int cfwrite(void *buf, int elsize, int nelem, CFILE *cfile)
463 Assert(cfile != NULL);
467 Assert(cfile->file != NULL);
468 Assert(cfile->lib_offset == 0);
470 items_written = fwrite(buf, elsize, nelem, cfile->file);
471 cfile->raw_position = ftell(cfile->file);
473 return items_written;
477 // cfputc() writes a character to a file
479 // returns: success ==> returns character written
482 int cfputc(int c, CFILE *cfile)
486 Assert(cfile != NULL);
488 Assert(cfile->file != NULL);
489 Assert(cfile->lib_offset == 0);
491 char_written = fputc(c, cfile->file);
492 cfile->raw_position = ftell(cfile->file);
498 int cfgetc( CFILE * fp )
502 if (fp->raw_position >= fp->size ) return EOF;
504 c = getc( fp->file );
506 fp->raw_position = ftell(fp->file)-fp->lib_offset;
512 // cfputs() writes a string to a file
514 // returns: success ==> non-negative value
517 int cfputs(char *str, CFILE *cfile)
521 Assert(cfile != NULL);
524 Assert(cfile->file != NULL);
526 ret = fputs(str, cfile->file);
527 cfile->raw_position = ftell(cfile->file);
533 char * cfgets( char * buf, size_t n, CFILE * fp )
538 #if 0 // don't use the standard fgets, because it will only handle the native line-ending style
539 if (fp->lib_offset == 0) // This is not an archived file
541 t = fgets(buf, n, fp->file);
542 fp->raw_position = ftell(fp->file);
547 for (i=0; i<n-1; i++ ) {
549 if (fp->raw_position >= fp->size ) {
554 if (c == 0 || c == 10) // Unix line ending
556 if (c == 13) { // Mac or DOS line ending
560 if (c1 != EOF) // The file could end with a Mac line ending
561 cfseek(fp, -1, SEEK_CUR);
562 if ( c1 == 10 ) // DOS line ending
564 else // Mac line ending
568 if ( c == 13 ) // because cr-lf is a bad thing on the mac
569 c = '\n'; // and anyway -- 0xod is CR on mac, not 0x0a
570 if ( c=='\n' ) break;
577 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
579 unsigned int i, size;
581 size = elsize * nelem;
582 if ( size < 1 ) return 0;
584 i = fread ( buf, 1, size, fp->file );
585 fp->raw_position += i;
590 int cftell( CFILE *fp )
592 return fp->raw_position;
595 int cfseek( CFILE *fp, long int offset, int where )
597 int c, goal_position;
601 goal_position = offset;
604 goal_position = fp->raw_position+offset;
607 goal_position = fp->size+offset;
612 c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
613 fp->raw_position = ftell(fp->file)-fp->lib_offset;
617 int cfclose(CFILE *fp)
621 result = fclose(fp->file);
627 // routines to read basic data types from CFILE's. Put here to
628 // simplify mac/pc reading from cfiles.
630 int cfile_read_int(CFILE *file)
634 if (cfread( &i, sizeof(i), 1, file) != 1)
635 Error( "Error reading int in cfile_read_int()" );
641 short cfile_read_short(CFILE *file)
645 if (cfread( &s, sizeof(s), 1, file) != 1)
646 Error( "Error reading short in cfile_read_short()" );
652 sbyte cfile_read_byte(CFILE *file)
656 if (cfread( &b, sizeof(b), 1, file) != 1)
657 Error( "Error reading byte in cfile_read_byte()" );
662 fix cfile_read_fix(CFILE *file)
666 if (cfread( &f, sizeof(f), 1, file) != 1)
667 Error( "Error reading fix in cfile_read_fix()" );
669 f = (fix)INTEL_INT((int)f);
673 fixang cfile_read_fixang(CFILE *file)
677 if (cfread(&f, 2, 1, file) != 1)
678 Error("Error reading fixang in cfile_read_fixang()");
680 f = (fixang) INTEL_SHORT((int) f);
684 void cfile_read_vector(vms_vector *v, CFILE *file)
686 v->x = cfile_read_fix(file);
687 v->y = cfile_read_fix(file);
688 v->z = cfile_read_fix(file);
691 void cfile_read_angvec(vms_angvec *v, CFILE *file)
693 v->p = cfile_read_fixang(file);
694 v->b = cfile_read_fixang(file);
695 v->h = cfile_read_fixang(file);
698 void cfile_read_matrix(vms_matrix *m,CFILE *file)
700 cfile_read_vector(&m->rvec,file);
701 cfile_read_vector(&m->uvec,file);
702 cfile_read_vector(&m->fvec,file);
706 void cfile_read_string(char *buf, int n, CFILE *file)
711 c = (char)cfile_read_byte(file);
721 // equivalent write functions of above read functions follow
723 int cfile_write_int(int i, CFILE *file)
726 return cfwrite(&i, sizeof(i), 1, file);
730 int cfile_write_short(short s, CFILE *file)
733 return cfwrite(&s, sizeof(s), 1, file);
737 int cfile_write_byte(sbyte b, CFILE *file)
739 return cfwrite(&b, sizeof(b), 1, file);
743 int cfile_write_string(char *buf, CFILE *file)
747 if ((!buf) || (buf && !buf[0]))
748 return cfile_write_byte(0, file);
751 if (!cfwrite(buf, len, 1, file))
754 return cfile_write_byte(0, file); // write out NULL termination