2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
29 typedef struct hogfile {
35 #define MAX_HOGFILES 300
37 hogfile HogFiles[MAX_HOGFILES];
38 char Hogfile_initialized = 0;
41 hogfile AltHogFiles[MAX_HOGFILES];
42 char AltHogfile_initialized = 0;
43 int AltNum_hogfiles = 0;
45 char AltHogFilename[64];
48 char AltHogdir_initialized = 0;
50 // routine to take a DOS path and turn it into a macintosh
51 // pathname. This routine is based on the fact that we should
52 // see a \ character in the dos path. The sequence .\ a tthe
53 // beginning of a path is turned into a :
56 void macify_dospath(char *dos_path, char *mac_path)
60 if (!strncmp(dos_path, ".\\", 2)) {
61 strcpy(mac_path, ":");
62 strcat(mac_path, &(dos_path[2]) );
64 strcpy(mac_path, dos_path);
66 while ( (p = strchr(mac_path, '\\')) != NULL)
72 void cfile_use_alternate_hogdir( char * path )
75 strcpy( AltHogDir, path );
76 AltHogdir_initialized = 1;
78 AltHogdir_initialized = 0;
82 //in case no one installs one
83 int default_error_counter=0;
85 //ptr to counter of how many critical errors
86 int *critical_error_counter_ptr=&default_error_counter;
88 //tell cfile about your critical error counter
89 void cfile_set_critical_error_counter_ptr(int *ptr)
91 critical_error_counter_ptr = ptr;
96 FILE * cfile_get_filehandle( char * filename, char * mode )
101 *critical_error_counter_ptr = 0;
102 fp = fopen( filename, mode );
103 if ( fp && *critical_error_counter_ptr ) {
107 if ( (fp==NULL) && (AltHogdir_initialized) ) {
108 strcpy( temp, AltHogDir );
110 strcat( temp, filename );
111 *critical_error_counter_ptr = 0;
112 fp = fopen( temp, mode );
113 if ( fp && *critical_error_counter_ptr ) {
121 //returns 1 if file loaded with no errors
122 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
130 fp = cfile_get_filehandle( fname, "rb" );
131 if ( fp == NULL ) return 0;
133 fread( id, 3, 1, fp );
134 if ( strncmp( id, "DHF", 3 ) ) {
141 if ( *nfiles >= MAX_HOGFILES ) {
143 Error( "HOGFILE is limited to %d files.\n", MAX_HOGFILES );
145 i = fread( hog_files[*nfiles].name, 13, 1, fp );
146 if ( i != 1 ) { //eof here is ok
150 i = fread( &len, 4, 1, fp );
155 hog_files[*nfiles].length = INTEL_INT(len);
156 hog_files[*nfiles].offset = ftell( fp );
157 *nfiles = (*nfiles) + 1;
159 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
163 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
164 int cfile_init(char *hogname)
169 macify_dospath(hogname, mac_path);
172 Assert(Hogfile_initialized == 0);
175 if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
176 strcpy( HogFilename, hogname );
178 if (cfile_init_hogfile(mac_path, HogFiles, &Num_hogfiles )) {
179 strcpy( HogFilename, mac_path );
181 Hogfile_initialized = 1;
185 return 0; //not loaded!
189 FILE * cfile_find_libfile(char * name, int * length)
194 if ( AltHogfile_initialized ) {
195 for (i=0; i<AltNum_hogfiles; i++ ) {
196 if ( !stricmp( AltHogFiles[i].name, name )) {
197 fp = cfile_get_filehandle( AltHogFilename, "rb" );
198 if ( fp == NULL ) return NULL;
199 fseek( fp, AltHogFiles[i].offset, SEEK_SET );
200 *length = AltHogFiles[i].length;
206 if ( !Hogfile_initialized ) {
207 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
208 //@@Hogfile_initialized = 1;
210 //Int3(); //hogfile ought to be initialized
213 for (i=0; i<Num_hogfiles; i++ ) {
214 if ( !stricmp( HogFiles[i].name, name )) {
215 fp = cfile_get_filehandle( HogFilename, "rb" );
216 if ( fp == NULL ) return NULL;
217 fseek( fp, HogFiles[i].offset, SEEK_SET );
218 *length = HogFiles[i].length;
225 int cfile_use_alternate_hogfile( char * name )
231 macify_dospath(name, mac_path);
232 strcpy( AltHogFilename, mac_path);
234 strcpy( AltHogFilename, name );
236 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
237 AltHogfile_initialized = 1;
238 return (AltNum_hogfiles > 0);
240 AltHogfile_initialized = 0;
245 int cfexist( char * filename )
251 if (filename[0] != '\x01')
252 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
254 fp = NULL; //don't look in dir, only in hogfile
263 fp = cfile_find_libfile(filename, &length );
266 return 2; // file found in hog
269 return 0; // Couldn't find it.
273 CFILE * cfopen(char * filename, char * mode )
279 if (stricmp( mode, "rb")) {
280 Error( "cfiles can only be opened with mode==rb\n" );
283 if (filename[0] != '\x01') {
287 macify_dospath(filename, mac_path);
288 fp = cfile_get_filehandle( mac_path, mode);
290 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
293 fp = NULL; //don't look in dir, only in hogfile
298 fp = cfile_find_libfile(filename, &length );
300 return NULL; // No file found
301 cfile = d_malloc ( sizeof(CFILE) );
302 if ( cfile == NULL ) {
307 cfile->size = length;
308 cfile->lib_offset = ftell( fp );
309 cfile->raw_position = 0;
312 cfile = d_malloc ( sizeof(CFILE) );
313 if ( cfile == NULL ) {
318 cfile->size = filelength( fileno(fp) );
319 cfile->lib_offset = 0;
320 cfile->raw_position = 0;
325 int cfilelength( CFILE *fp )
330 int cfgetc( CFILE * fp )
334 if (fp->raw_position >= fp->size ) return EOF;
336 c = getc( fp->file );
340 // Assert( fp->raw_position==(ftell(fp->file)-fp->lib_offset) );
345 char * cfgets( char * buf, size_t n, CFILE * fp )
351 for (i=0; i<n-1; i++ ) {
353 if (fp->raw_position >= fp->size ) {
357 c = fgetc( fp->file );
363 c1 = fgetc( fp->file );
364 fseek( fp->file, -1, SEEK_CUR);
372 #ifdef MACINTOSH // because cr-lf is a bad thing on the mac
373 if ( c == 13 ) // and anyway -- 0xod is CR on mac, not 0x0a
377 if ( c=='\n' ) break;
383 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
385 unsigned int i, size;
387 size = elsize * nelem;
388 if ( size < 1 ) return 0;
390 i = fread ( buf, 1, size, fp->file );
391 fp->raw_position += i;
396 int cftell( CFILE *fp )
398 return fp->raw_position;
401 int cfseek( CFILE *fp, long int offset, int where )
403 int c, goal_position;
407 goal_position = offset;
410 goal_position = fp->raw_position+offset;
413 goal_position = fp->size+offset;
418 c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
419 fp->raw_position = ftell(fp->file)-fp->lib_offset;
423 void cfclose( CFILE * fp )
430 // routines to read basic data types from CFILE's. Put here to
431 // simplify mac/pc reading from cfiles.
433 int cfile_read_int(CFILE *file)
437 if (cfread( &i, sizeof(i), 1, file) != 1)
438 Error( "Error reading int in cfile_read_int()" );
444 short cfile_read_short(CFILE *file)
448 if (cfread( &s, sizeof(s), 1, file) != 1)
449 Error( "Error reading short in cfile_read_short()" );
455 byte cfile_read_byte(CFILE *file)
459 if (cfread( &b, sizeof(b), 1, file) != 1)
460 Error( "Error reading byte in cfile_read_byte()" );
465 fix cfile_read_fix(CFILE *file)
469 if (cfread( &f, sizeof(f), 1, file) != 1)
470 Error( "Error reading fix in cfile_read_fix()" );
472 f = (fix)INTEL_INT((int)f);
476 fixang cfile_read_fixang(CFILE *file)
480 if (cfread(&f, 2, 1, file) != 1)
481 Error("Error reading fixang in cfile_read_fixang()");
483 f = (fixang) INTEL_SHORT((int) f);
487 void cfile_read_vector(vms_vector *v, CFILE *file)
489 v->x = cfile_read_fix(file);
490 v->y = cfile_read_fix(file);
491 v->z = cfile_read_fix(file);
494 void cfile_read_angvec(vms_angvec *v, CFILE *file)
496 v->p = cfile_read_fixang(file);
497 v->b = cfile_read_fixang(file);
498 v->h = cfile_read_fixang(file);