1 /* $Id: cfile.c,v 1.32 2004-10-23 16:28:32 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.
18 * (Actually, the files are not compressed, but concatenated within hogfiles)
30 #elif defined(macintosh)
34 # include <sys/stat.h>
45 /* a file (if offset == 0) or a part of a hog file */
59 #define MAX_FILES_IN_HOG 300
61 /* a hog file is an archive, like a tar file */
65 struct file_in_hog files[MAX_FILES_IN_HOG];
68 hog *builtin_hog = NULL;
72 void free_builtin_hog() { if (builtin_hog) d_free (builtin_hog); }
73 void free_alt_hog() { if (alt_hog) d_free (alt_hog); }
74 void free_d1_hog() { if (d1_hog) d_free (d1_hog); }
77 char AltHogdir_initialized = 0;
79 // routine to take a DOS path and turn it into a macintosh
80 // pathname. This routine is based on the fact that we should
81 // see a \ character in the dos path. The sequence .\ a tthe
82 // beginning of a path is turned into a :
84 // routine to take a POSIX style path and turn it into a pre OS X
85 // pathname. This routine uses CFURL's. This function is necessary
86 // because even though fopen exists in StdCLib
87 // it must take a path in the OS native format.
90 void macify_posix_path(char *posix_path, char *mac_path)
94 url = CFURLCreateWithBytes (kCFAllocatorDefault, (ubyte *) posix_path, strlen(posix_path), GetApplicationTextEncoding(), NULL);
95 CFURLGetFileSystemRepresentation (url, 0, (ubyte *) mac_path, 255);
100 void cfile_use_alternate_hogdir( char * path )
103 strcpy( AltHogDir, path );
104 AltHogdir_initialized = 1;
106 AltHogdir_initialized = 0;
110 //in case no one installs one
111 int default_error_counter=0;
113 //ptr to counter of how many critical errors
114 int *critical_error_counter_ptr=&default_error_counter;
116 //tell cfile about your critical error counter
117 void cfile_set_critical_error_counter_ptr(int *ptr)
119 critical_error_counter_ptr = ptr;
124 FILE * cfile_get_filehandle( char * filename, char * mode )
132 *critical_error_counter_ptr = 0;
134 macify_posix_path(filename, mac_path);
135 fp = fopen( mac_path, mode );
137 fp = fopen( filename, mode );
139 if ( fp && *critical_error_counter_ptr ) {
143 if ( (fp==NULL) && (AltHogdir_initialized) ) {
144 strcpy( temp, AltHogDir );
146 strcat( temp, filename );
147 *critical_error_counter_ptr = 0;
149 macify_posix_path(temp, mac_path);
150 fp = fopen( mac_path, mode );
152 fp = fopen( temp, mode );
154 if ( fp && *critical_error_counter_ptr ) {
162 hog * cfile_init_hogfile (char *fname)
169 fp = cfile_get_filehandle (fname, "rb");
173 // verify that it is really a Descent Hog File
174 fread( id, 3, 1, fp );
175 if ( strncmp( id, "DHF", 3 ) ) {
180 new_hog = (hog *) d_malloc (sizeof (hog));
182 new_hog->num_files = 0;
183 strcpy (new_hog->filename, fname);
185 // read file name or reach EOF
186 while (! feof (fp)) {
187 if (new_hog->num_files >= MAX_FILES_IN_HOG) {
190 Error ("Exceeded max. number of files in hog (%d).\n",
193 i = fread (new_hog->files[new_hog->num_files].name, 13, 1, fp);
195 break; // we assume it is EOF, so it is OK
197 i = fread( &len, 4, 1, fp );
200 new_hog->files[new_hog->num_files].length = INTEL_INT (len);
201 new_hog->files[new_hog->num_files].offset = ftell (fp);
202 // skip over embedded file:
203 i = fseek (fp, INTEL_INT (len), SEEK_CUR);
204 new_hog->num_files++;
210 // Opens builtin hog given its filename. Returns 1 on success.
211 int cfile_init(char *hogname)
213 Assert (builtin_hog == NULL);
215 builtin_hog = cfile_init_hogfile (hogname);
216 atexit (free_builtin_hog);
217 return builtin_hog != NULL ? 1 : 0;
221 int cfile_size(char *hogname)
226 fp = cfopen(hogname, "rb");
229 size = ffilelength(fp->file);
234 FILE * cfile_find_in_hog (char * name, int * length, hog *my_hog) {
237 for (i = 0; i < my_hog->num_files; i++ )
238 if (! stricmp (my_hog->files[i].name, name)) {
239 fp = cfile_get_filehandle (my_hog->filename, "rb");
242 fseek (fp, my_hog->files[i].offset, SEEK_SET);
243 *length = my_hog->files[i].length;
250 * return handle for file called "name", embedded in one of the hogfiles
252 FILE * cfile_find_embedded_file (char * name, int * length)
256 fp = cfile_find_in_hog (name, length, alt_hog);
262 fp = cfile_find_in_hog (name, length, builtin_hog);
268 fp = cfile_find_in_hog (name, length, d1_hog);
276 int cfile_use_alternate_hogfile (char * name)
283 alt_hog = cfile_init_hogfile (name);
284 atexit (free_alt_hog);
291 /* we use the d1 hog for d1 textures... */
292 int cfile_use_descent1_hogfile( char * name )
299 d1_hog = cfile_init_hogfile (name);
300 atexit (free_d1_hog);
307 // cfeof() Tests for end-of-file on a stream
309 // returns a nonzero value after the first read operation that attempts to read
310 // past the end of the file. It returns 0 if the current position is not end of file.
311 // There is no error return.
313 int cfeof(CFILE *cfile)
315 Assert(cfile != NULL);
317 Assert(cfile->file != NULL);
319 return (cfile->raw_position >= cfile->size);
323 int cferror(CFILE *cfile)
325 return ferror(cfile->file);
329 int cfexist( char * filename )
335 if (filename[0] != '\x01')
336 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
338 fp = NULL; //don't look in dir, only in hogfile
347 fp = cfile_find_embedded_file (filename, &length);
350 return 2; // file found in hog
353 return 0; // Couldn't find it.
358 int cfile_delete(char *filename)
361 return remove(filename);
363 return !DeleteFile(filename);
369 int cfile_rename(char *oldname, char *newname)
372 return rename(oldname, newname);
374 return !MoveFile(oldname, newname);
380 int cfile_mkdir(char *pathname)
384 return !CreateDirectory(pathname, NULL);
386 return _mkdir(pathname);
388 #elif defined(macintosh)
391 long dirID; // Insists on returning this
393 macify_posix_path(pathname, mac_path);
394 CopyCStringToPascal(mac_path, pascal_path);
395 return DirCreate(0, 0, pascal_path, &dirID);
397 return mkdir(pathname, 0755);
402 CFILE * cfopen(char * filename, char * mode )
408 if (filename[0] != '\x01')
409 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
411 fp = NULL; //don't look in dir, only in hogfile
416 fp = cfile_find_embedded_file (filename, &length);
418 return NULL; // No file found
419 if (stricmp(mode, "rb"))
420 Error("mode must be rb for files in hog.\n");
421 cfile = d_malloc ( sizeof(CFILE) );
422 if ( cfile == NULL ) {
427 cfile->size = length;
428 cfile->offset = ftell( fp );
429 cfile->raw_position = 0;
432 cfile = d_malloc ( sizeof(CFILE) );
433 if ( cfile == NULL ) {
438 cfile->size = ffilelength(fp);
440 cfile->raw_position = 0;
445 int cfilelength( CFILE *fp )
451 // cfwrite() writes to the file
453 // returns: number of full elements actually written
456 int cfwrite(void *buf, int elsize, int nelem, CFILE *cfile)
460 Assert(cfile != NULL);
464 Assert(cfile->file != NULL);
465 Assert(cfile->offset == 0);
467 items_written = fwrite(buf, elsize, nelem, cfile->file);
468 cfile->raw_position = ftell(cfile->file);
470 return items_written;
474 // cfputc() writes a character to a file
476 // returns: success ==> returns character written
479 int cfputc(int c, CFILE *cfile)
483 Assert(cfile != NULL);
485 Assert(cfile->file != NULL);
486 Assert(cfile->offset == 0);
488 char_written = fputc(c, cfile->file);
489 cfile->raw_position = ftell(cfile->file);
495 int cfgetc( CFILE * fp )
499 if (fp->raw_position >= fp->size ) return EOF;
501 c = getc( fp->file );
503 fp->raw_position = ftell(fp->file)-fp->offset;
509 // cfputs() writes a string to a file
511 // returns: success ==> non-negative value
514 int cfputs(char *str, CFILE *cfile)
518 Assert(cfile != NULL);
521 Assert(cfile->file != NULL);
523 ret = fputs(str, cfile->file);
524 cfile->raw_position = ftell(cfile->file);
530 char * cfgets( char * buf, size_t n, CFILE * fp )
535 #if 0 // don't use the standard fgets, because it will only handle the native line-ending style
536 if (fp->offset == 0) // This is not an archived file
538 t = fgets(buf, n, fp->file);
539 fp->raw_position = ftell(fp->file);
544 for (i=0; i<n-1; i++ ) {
546 if (fp->raw_position >= fp->size ) {
551 if (c == 0 || c == 10) // Unix line ending
553 if (c == 13) { // Mac or DOS line ending
557 if (c1 != EOF) // The file could end with a Mac line ending
558 cfseek(fp, -1, SEEK_CUR);
559 if ( c1 == 10 ) // DOS line ending
561 else // Mac line ending
565 if ( c == 13 ) // because cr-lf is a bad thing on the mac
566 c = '\n'; // and anyway -- 0xod is CR on mac, not 0x0a
567 if ( c=='\n' ) break;
574 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
576 unsigned int i, size;
578 size = elsize * nelem;
579 if ( size < 1 ) return 0;
581 i = fread ( buf, 1, size, fp->file );
582 fp->raw_position += i;
587 int cftell( CFILE *fp )
589 return fp->raw_position;
592 int cfseek( CFILE *fp, long int offset, int where )
594 int c, goal_position;
598 goal_position = offset;
601 goal_position = fp->raw_position+offset;
604 goal_position = fp->size+offset;
609 c = fseek( fp->file, fp->offset + goal_position, SEEK_SET );
610 fp->raw_position = ftell(fp->file)-fp->offset;
614 int cfclose(CFILE *fp)
618 result = fclose(fp->file);
624 // routines to read basic data types from CFILE's. Put here to
625 // simplify mac/pc reading from cfiles.
627 int cfile_read_int(CFILE *file)
631 if (cfread( &i, sizeof(i), 1, file) != 1)
632 Error( "Error reading int in cfile_read_int()" );
638 short cfile_read_short(CFILE *file)
642 if (cfread( &s, sizeof(s), 1, file) != 1)
643 Error( "Error reading short in cfile_read_short()" );
649 sbyte cfile_read_byte(CFILE *file)
653 if (cfread( &b, sizeof(b), 1, file) != 1)
654 Error( "Error reading byte in cfile_read_byte()" );
659 fix cfile_read_fix(CFILE *file)
663 if (cfread( &f, sizeof(f), 1, file) != 1)
664 Error( "Error reading fix in cfile_read_fix()" );
666 f = (fix)INTEL_INT((int)f);
670 fixang cfile_read_fixang(CFILE *file)
674 if (cfread(&f, 2, 1, file) != 1)
675 Error("Error reading fixang in cfile_read_fixang()");
677 f = (fixang) INTEL_SHORT((int) f);
681 void cfile_read_vector(vms_vector *v, CFILE *file)
683 v->x = cfile_read_fix(file);
684 v->y = cfile_read_fix(file);
685 v->z = cfile_read_fix(file);
688 void cfile_read_angvec(vms_angvec *v, CFILE *file)
690 v->p = cfile_read_fixang(file);
691 v->b = cfile_read_fixang(file);
692 v->h = cfile_read_fixang(file);
695 void cfile_read_matrix(vms_matrix *m,CFILE *file)
697 cfile_read_vector(&m->rvec,file);
698 cfile_read_vector(&m->uvec,file);
699 cfile_read_vector(&m->fvec,file);
703 void cfile_read_string(char *buf, int n, CFILE *file)
708 c = (char)cfile_read_byte(file);
718 // equivalent write functions of above read functions follow
720 int cfile_write_int(int i, CFILE *file)
723 return cfwrite(&i, sizeof(i), 1, file);
727 int cfile_write_short(short s, CFILE *file)
730 return cfwrite(&s, sizeof(s), 1, file);
734 int cfile_write_byte(sbyte b, CFILE *file)
736 return cfwrite(&b, sizeof(b), 1, file);
740 int cfile_write_string(char *buf, CFILE *file)
744 if ((!buf) || (buf && !buf[0]))
745 return cfile_write_byte(0, file);
748 if (!cfwrite(buf, len, 1, file))
751 return cfile_write_byte(0, file); // write out NULL termination