1 /* $Id: cfile.c,v 1.7 2002-08-23 01:52:59 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.
31 typedef struct hogfile {
37 #define MAX_HOGFILES 300
39 hogfile HogFiles[MAX_HOGFILES];
40 char Hogfile_initialized = 0;
43 hogfile AltHogFiles[MAX_HOGFILES];
44 char AltHogfile_initialized = 0;
45 int AltNum_hogfiles = 0;
47 char AltHogFilename[64];
50 char AltHogdir_initialized = 0;
52 // routine to take a DOS path and turn it into a macintosh
53 // pathname. This routine is based on the fact that we should
54 // see a \ character in the dos path. The sequence .\ a tthe
55 // beginning of a path is turned into a :
58 void macify_dospath(char *dos_path, char *mac_path)
62 if (!strncmp(dos_path, ".\\", 2)) {
63 strcpy(mac_path, ":");
64 strcat(mac_path, &(dos_path[2]) );
66 strcpy(mac_path, dos_path);
68 while ( (p = strchr(mac_path, '\\')) != NULL)
74 void cfile_use_alternate_hogdir( char * path )
77 strcpy( AltHogDir, path );
78 AltHogdir_initialized = 1;
80 AltHogdir_initialized = 0;
84 //in case no one installs one
85 int default_error_counter=0;
87 //ptr to counter of how many critical errors
88 int *critical_error_counter_ptr=&default_error_counter;
90 //tell cfile about your critical error counter
91 void cfile_set_critical_error_counter_ptr(int *ptr)
93 critical_error_counter_ptr = ptr;
98 FILE * cfile_get_filehandle( char * filename, char * mode )
103 *critical_error_counter_ptr = 0;
104 fp = fopen( filename, mode );
105 if ( fp && *critical_error_counter_ptr ) {
109 if ( (fp==NULL) && (AltHogdir_initialized) ) {
110 strcpy( temp, AltHogDir );
112 strcat( temp, filename );
113 *critical_error_counter_ptr = 0;
114 fp = fopen( temp, mode );
115 if ( fp && *critical_error_counter_ptr ) {
123 //returns 1 if file loaded with no errors
124 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
132 fp = cfile_get_filehandle( fname, "rb" );
133 if ( fp == NULL ) return 0;
135 fread( id, 3, 1, fp );
136 if ( strncmp( id, "DHF", 3 ) ) {
143 if ( *nfiles >= MAX_HOGFILES ) {
145 Error( "HOGFILE is limited to %d files.\n", MAX_HOGFILES );
147 i = fread( hog_files[*nfiles].name, 13, 1, fp );
148 if ( i != 1 ) { //eof here is ok
152 i = fread( &len, 4, 1, fp );
157 hog_files[*nfiles].length = INTEL_INT(len);
158 hog_files[*nfiles].offset = ftell( fp );
159 *nfiles = (*nfiles) + 1;
161 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
165 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
166 int cfile_init(char *hogname)
171 macify_dospath(hogname, mac_path);
174 Assert(Hogfile_initialized == 0);
177 if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
178 strcpy( HogFilename, hogname );
180 if (cfile_init_hogfile(mac_path, HogFiles, &Num_hogfiles )) {
181 strcpy( HogFilename, mac_path );
183 Hogfile_initialized = 1;
187 return 0; //not loaded!
191 int cfile_size(char *hogname)
196 fp = cfopen(hogname, "rb");
199 fstat(fileno(fp->file), &statbuf);
201 return statbuf.st_size;
204 FILE * cfile_find_libfile(char * name, int * length)
209 if ( AltHogfile_initialized ) {
210 for (i=0; i<AltNum_hogfiles; i++ ) {
211 if ( !stricmp( AltHogFiles[i].name, name )) {
212 fp = cfile_get_filehandle( AltHogFilename, "rb" );
213 if ( fp == NULL ) return NULL;
214 fseek( fp, AltHogFiles[i].offset, SEEK_SET );
215 *length = AltHogFiles[i].length;
221 if ( !Hogfile_initialized ) {
222 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
223 //@@Hogfile_initialized = 1;
225 //Int3(); //hogfile ought to be initialized
228 for (i=0; i<Num_hogfiles; i++ ) {
229 if ( !stricmp( HogFiles[i].name, name )) {
230 fp = cfile_get_filehandle( HogFilename, "rb" );
231 if ( fp == NULL ) return NULL;
232 fseek( fp, HogFiles[i].offset, SEEK_SET );
233 *length = HogFiles[i].length;
240 int cfile_use_alternate_hogfile( char * name )
246 macify_dospath(name, mac_path);
247 strcpy( AltHogFilename, mac_path);
249 strcpy( AltHogFilename, name );
251 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
252 AltHogfile_initialized = 1;
253 return (AltNum_hogfiles > 0);
255 AltHogfile_initialized = 0;
260 int cfexist( char * filename )
266 if (filename[0] != '\x01')
267 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
269 fp = NULL; //don't look in dir, only in hogfile
278 fp = cfile_find_libfile(filename, &length );
281 return 2; // file found in hog
284 return 0; // Couldn't find it.
288 CFILE * cfopen(char * filename, char * mode )
294 if (stricmp( mode, "rb")) {
295 Error( "cfiles can only be opened with mode==rb\n" );
298 if (filename[0] != '\x01') {
302 macify_dospath(filename, mac_path);
303 fp = cfile_get_filehandle( mac_path, mode);
305 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
308 fp = NULL; //don't look in dir, only in hogfile
313 fp = cfile_find_libfile(filename, &length );
315 return NULL; // No file found
316 cfile = d_malloc ( sizeof(CFILE) );
317 if ( cfile == NULL ) {
322 cfile->size = length;
323 cfile->lib_offset = ftell( fp );
324 cfile->raw_position = 0;
327 cfile = d_malloc ( sizeof(CFILE) );
328 if ( cfile == NULL ) {
333 cfile->size = filelength( fileno(fp) );
334 cfile->lib_offset = 0;
335 cfile->raw_position = 0;
340 int cfilelength( CFILE *fp )
345 int cfgetc( CFILE * fp )
349 if (fp->raw_position >= fp->size ) return EOF;
351 c = getc( fp->file );
355 // Assert( fp->raw_position==(ftell(fp->file)-fp->lib_offset) );
361 * read string terminated by zero or a platform's line ending.
362 * Martin: cleaned up line ending mess, and made it read
363 * zero-terminated strings (for descent 1 levels).
364 * assumed that no string has any zero's or other platform's line
366 * platform's line endings reference: UN*X: LF (10), Mac: CR (13),
369 char * cfgets( char * buf, size_t n, CFILE * fp )
375 for (i=0; i<n-1; i++ ) {
376 if (fp->raw_position >= fp->size ) {
380 c = fgetc( fp->file );
382 if (c == 0 || c == 10) // UN*X line ending or zero
384 if (c == 13) { // it could be Mac or DOS line ending
385 c = fgetc( fp->file );
386 if ( c == 10 ) { // DOS line ending
388 } else { // Mac line ending, undo last character read
389 fseek( fp->file, -1, SEEK_CUR);
399 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
401 unsigned int i, size;
403 size = elsize * nelem;
404 if ( size < 1 ) return 0;
406 i = fread ( buf, 1, size, fp->file );
407 fp->raw_position += i;
412 int cftell( CFILE *fp )
414 return fp->raw_position;
417 int cfseek( CFILE *fp, long int offset, int where )
419 int c, goal_position;
423 goal_position = offset;
426 goal_position = fp->raw_position+offset;
429 goal_position = fp->size+offset;
434 c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
435 fp->raw_position = ftell(fp->file)-fp->lib_offset;
439 void cfclose( CFILE * fp )
446 // routines to read basic data types from CFILE's. Put here to
447 // simplify mac/pc reading from cfiles.
449 int cfile_read_int(CFILE *file)
453 if (cfread( &i, sizeof(i), 1, file) != 1)
454 Error( "Error reading int in cfile_read_int()" );
460 short cfile_read_short(CFILE *file)
464 if (cfread( &s, sizeof(s), 1, file) != 1)
465 Error( "Error reading short in cfile_read_short()" );
471 byte cfile_read_byte(CFILE *file)
475 if (cfread( &b, sizeof(b), 1, file) != 1)
476 Error( "Error reading byte in cfile_read_byte()" );
481 fix cfile_read_fix(CFILE *file)
485 if (cfread( &f, sizeof(f), 1, file) != 1)
486 Error( "Error reading fix in cfile_read_fix()" );
488 f = (fix)INTEL_INT((int)f);
492 fixang cfile_read_fixang(CFILE *file)
496 if (cfread(&f, 2, 1, file) != 1)
497 Error("Error reading fixang in cfile_read_fixang()");
499 f = (fixang) INTEL_SHORT((int) f);
503 void cfile_read_vector(vms_vector *v, CFILE *file)
505 v->x = cfile_read_fix(file);
506 v->y = cfile_read_fix(file);
507 v->z = cfile_read_fix(file);
510 void cfile_read_angvec(vms_angvec *v, CFILE *file)
512 v->p = cfile_read_fixang(file);
513 v->b = cfile_read_fixang(file);
514 v->h = cfile_read_fixang(file);
517 void cfile_read_matrix(vms_matrix *m,CFILE *file)
519 cfile_read_vector(&m->rvec,file);
520 cfile_read_vector(&m->uvec,file);
521 cfile_read_vector(&m->fvec,file);