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 );
109 strcat( temp, filename );
110 *critical_error_counter_ptr = 0;
111 fp = fopen( temp, mode );
112 if ( fp && *critical_error_counter_ptr ) {
120 //returns 1 if file loaded with no errors
121 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
129 fp = cfile_get_filehandle( fname, "rb" );
130 if ( fp == NULL ) return 0;
132 fread( id, 3, 1, fp );
133 if ( strncmp( id, "DHF", 3 ) ) {
140 if ( *nfiles >= MAX_HOGFILES ) {
142 Error( "HOGFILE is limited to %d files.\n", MAX_HOGFILES );
144 i = fread( hog_files[*nfiles].name, 13, 1, fp );
145 if ( i != 1 ) { //eof here is ok
149 i = fread( &len, 4, 1, fp );
154 hog_files[*nfiles].length = INTEL_INT(len);
155 hog_files[*nfiles].offset = ftell( fp );
156 *nfiles = (*nfiles) + 1;
158 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
162 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
163 int cfile_init(char *hogname)
168 macify_dospath(hogname, mac_path);
171 Assert(Hogfile_initialized == 0);
174 if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
175 strcpy( HogFilename, hogname );
177 if (cfile_init_hogfile(mac_path, HogFiles, &Num_hogfiles )) {
178 strcpy( HogFilename, mac_path );
180 Hogfile_initialized = 1;
184 return 0; //not loaded!
188 FILE * cfile_find_libfile(char * name, int * length)
193 if ( AltHogfile_initialized ) {
194 for (i=0; i<AltNum_hogfiles; i++ ) {
195 if ( !stricmp( AltHogFiles[i].name, name )) {
196 fp = cfile_get_filehandle( AltHogFilename, "rb" );
197 if ( fp == NULL ) return NULL;
198 fseek( fp, AltHogFiles[i].offset, SEEK_SET );
199 *length = AltHogFiles[i].length;
205 if ( !Hogfile_initialized ) {
206 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
207 //@@Hogfile_initialized = 1;
209 //Int3(); //hogfile ought to be initialized
212 for (i=0; i<Num_hogfiles; i++ ) {
213 if ( !stricmp( HogFiles[i].name, name )) {
214 fp = cfile_get_filehandle( HogFilename, "rb" );
215 if ( fp == NULL ) return NULL;
216 fseek( fp, HogFiles[i].offset, SEEK_SET );
217 *length = HogFiles[i].length;
224 int cfile_use_alternate_hogfile( char * name )
230 macify_dospath(name, mac_path);
231 strcpy( AltHogFilename, mac_path);
233 strcpy( AltHogFilename, name );
235 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
236 AltHogfile_initialized = 1;
237 return (AltNum_hogfiles > 0);
239 AltHogfile_initialized = 0;
244 int cfexist( char * filename )
250 if (filename[0] != '\x01')
251 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
253 fp = NULL; //don't look in dir, only in hogfile
262 fp = cfile_find_libfile(filename, &length );
265 return 2; // file found in hog
268 return 0; // Couldn't find it.
272 CFILE * cfopen(char * filename, char * mode )
278 if (stricmp( mode, "rb")) {
279 Error( "cfiles can only be opened with mode==rb\n" );
282 if (filename[0] != '\x01') {
286 macify_dospath(filename, mac_path);
287 fp = cfile_get_filehandle( mac_path, mode);
289 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
292 fp = NULL; //don't look in dir, only in hogfile
297 fp = cfile_find_libfile(filename, &length );
299 return NULL; // No file found
300 cfile = d_malloc ( sizeof(CFILE) );
301 if ( cfile == NULL ) {
306 cfile->size = length;
307 cfile->lib_offset = ftell( fp );
308 cfile->raw_position = 0;
311 cfile = d_malloc ( sizeof(CFILE) );
312 if ( cfile == NULL ) {
317 cfile->size = filelength( fileno(fp) );
318 cfile->lib_offset = 0;
319 cfile->raw_position = 0;
324 int cfilelength( CFILE *fp )
329 int cfgetc( CFILE * fp )
333 if (fp->raw_position >= fp->size ) return EOF;
335 c = getc( fp->file );
339 // Assert( fp->raw_position==(ftell(fp->file)-fp->lib_offset) );
344 char * cfgets( char * buf, size_t n, CFILE * fp )
350 for (i=0; i<n-1; i++ ) {
352 if (fp->raw_position >= fp->size ) {
356 c = fgetc( fp->file );
362 c1 = fgetc( fp->file );
363 fseek( fp->file, -1, SEEK_CUR);
371 #ifdef MACINTOSH // because cr-lf is a bad thing on the mac
372 if ( c == 13 ) // and anyway -- 0xod is CR on mac, not 0x0a
376 if ( c=='\n' ) break;
382 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
384 unsigned int i, size;
386 size = elsize * nelem;
387 if ( size < 1 ) return 0;
389 i = fread ( buf, 1, size, fp->file );
390 fp->raw_position += i;
395 int cftell( CFILE *fp )
397 return fp->raw_position;
400 int cfseek( CFILE *fp, long int offset, int where )
402 int c, goal_position;
406 goal_position = offset;
409 goal_position = fp->raw_position+offset;
412 goal_position = fp->size+offset;
417 c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
418 fp->raw_position = ftell(fp->file)-fp->lib_offset;
422 void cfclose( CFILE * fp )
429 // routines to read basic data types from CFILE's. Put here to
430 // simplify mac/pc reading from cfiles.
432 int cfile_read_int(CFILE *file)
436 if (cfread( &i, sizeof(i), 1, file) != 1)
437 Error( "Error reading int in cfile_read_int()" );
443 short cfile_read_short(CFILE *file)
447 if (cfread( &s, sizeof(s), 1, file) != 1)
448 Error( "Error reading short in cfile_read_short()" );
454 byte cfile_read_byte(CFILE *file)
458 if (cfread( &b, sizeof(b), 1, file) != 1)
459 Error( "Error reading byte in cfile_read_byte()" );
464 fix cfile_read_fix(CFILE *file)
468 if (cfread( &f, sizeof(f), 1, file) != 1)
469 Error( "Error reading fix in cfile_read_fix()" );
471 f = (fix)INTEL_INT((int)f);
475 fixang cfile_read_fixang(CFILE *file)
479 if (cfread(&f, 2, 1, file) != 1)
480 Error("Error reading fixang in cfile_read_fixang()");
482 f = (fixang) INTEL_SHORT((int) f);
486 void cfile_read_vector(vms_vector *v, CFILE *file)
488 v->x = cfile_read_fix(file);
489 v->y = cfile_read_fix(file);
490 v->z = cfile_read_fix(file);
493 void cfile_read_angvec(vms_angvec *v, CFILE *file)
495 v->p = cfile_read_fixang(file);
496 v->b = cfile_read_fixang(file);
497 v->h = cfile_read_fixang(file);