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.
26 typedef struct hogfile {
32 #define MAX_HOGFILES 300
34 hogfile HogFiles[MAX_HOGFILES];
35 char Hogfile_initialized = 0;
38 hogfile AltHogFiles[MAX_HOGFILES];
39 char AltHogfile_initialized = 0;
40 int AltNum_hogfiles = 0;
42 char AltHogFilename[64];
45 char AltHogdir_initialized = 0;
47 // routine to take a DOS path and turn it into a macintosh
48 // pathname. This routine is based on the fact that we should
49 // see a \ character in the dos path. The sequence .\ a tthe
50 // beginning of a path is turned into a :
53 void macify_dospath(char *dos_path, char *mac_path)
57 if (!strncmp(dos_path, ".\\", 2)) {
58 strcpy(mac_path, ":");
59 strcat(mac_path, &(dos_path[2]) );
61 strcpy(mac_path, dos_path);
63 while ( (p = strchr(mac_path, '\\')) != NULL)
69 void cfile_use_alternate_hogdir( char * path )
72 strcpy( AltHogDir, path );
73 AltHogdir_initialized = 1;
75 AltHogdir_initialized = 0;
79 //in case no one installs one
80 int default_error_counter=0;
82 //ptr to counter of how many critical errors
83 int *critical_error_counter_ptr=&default_error_counter;
85 //tell cfile about your critical error counter
86 void cfile_set_critical_error_counter_ptr(int *ptr)
88 critical_error_counter_ptr = ptr;
93 FILE * cfile_get_filehandle( char * filename, char * mode )
98 *critical_error_counter_ptr = 0;
99 fp = fopen( filename, mode );
100 if ( fp && *critical_error_counter_ptr ) {
104 if ( (fp==NULL) && (AltHogdir_initialized) ) {
105 strcpy( temp, AltHogDir );
106 strcat( temp, filename );
107 *critical_error_counter_ptr = 0;
108 fp = fopen( temp, mode );
109 if ( fp && *critical_error_counter_ptr ) {
117 //returns 1 if file loaded with no errors
118 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
126 fp = cfile_get_filehandle( fname, "rb" );
127 if ( fp == NULL ) return 0;
129 fread( id, 3, 1, fp );
130 if ( strncmp( id, "DHF", 3 ) ) {
137 if ( *nfiles >= MAX_HOGFILES ) {
139 Error( "HOGFILE is limited to %d files.\n", MAX_HOGFILES );
141 i = fread( hog_files[*nfiles].name, 13, 1, fp );
142 if ( i != 1 ) { //eof here is ok
146 i = fread( &len, 4, 1, fp );
151 hog_files[*nfiles].length = INTEL_INT(len);
152 hog_files[*nfiles].offset = ftell( fp );
153 *nfiles = (*nfiles) + 1;
155 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
159 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
160 int cfile_init(char *hogname)
165 macify_dospath(hogname, mac_path);
168 Assert(Hogfile_initialized == 0);
171 if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
172 strcpy( HogFilename, hogname );
174 if (cfile_init_hogfile(mac_path, HogFiles, &Num_hogfiles )) {
175 strcpy( HogFilename, mac_path );
177 Hogfile_initialized = 1;
181 return 0; //not loaded!
185 FILE * cfile_find_libfile(char * name, int * length)
190 if ( AltHogfile_initialized ) {
191 for (i=0; i<AltNum_hogfiles; i++ ) {
192 if ( !stricmp( AltHogFiles[i].name, name )) {
193 fp = cfile_get_filehandle( AltHogFilename, "rb" );
194 if ( fp == NULL ) return NULL;
195 fseek( fp, AltHogFiles[i].offset, SEEK_SET );
196 *length = AltHogFiles[i].length;
202 if ( !Hogfile_initialized ) {
203 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
204 //@@Hogfile_initialized = 1;
206 //Int3(); //hogfile ought to be initialized
209 for (i=0; i<Num_hogfiles; i++ ) {
210 if ( !stricmp( HogFiles[i].name, name )) {
211 fp = cfile_get_filehandle( HogFilename, "rb" );
212 if ( fp == NULL ) return NULL;
213 fseek( fp, HogFiles[i].offset, SEEK_SET );
214 *length = HogFiles[i].length;
221 int cfile_use_alternate_hogfile( char * name )
227 macify_dospath(name, mac_path);
228 strcpy( AltHogFilename, mac_path);
230 strcpy( AltHogFilename, name );
232 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
233 AltHogfile_initialized = 1;
234 return (AltNum_hogfiles > 0);
236 AltHogfile_initialized = 0;
241 int cfexist( char * filename )
247 if (filename[0] != '\x01')
248 fp = cfile_get_filehandle( filename, "rb" ); // Check for non-hog file first...
250 fp = NULL; //don't look in dir, only in hogfile
259 fp = cfile_find_libfile(filename, &length );
262 return 2; // file found in hog
265 return 0; // Couldn't find it.
269 CFILE * cfopen(char * filename, char * mode )
275 if (stricmp( mode, "rb")) {
276 Error( "cfiles can only be opened with mode==rb\n" );
279 if (filename[0] != '\x01') {
283 macify_dospath(filename, mac_path);
284 fp = cfile_get_filehandle( mac_path, mode);
286 fp = cfile_get_filehandle( filename, mode ); // Check for non-hog file first...
289 fp = NULL; //don't look in dir, only in hogfile
294 fp = cfile_find_libfile(filename, &length );
296 return NULL; // No file found
297 cfile = d_malloc ( sizeof(CFILE) );
298 if ( cfile == NULL ) {
303 cfile->size = length;
304 cfile->lib_offset = ftell( fp );
305 cfile->raw_position = 0;
308 cfile = d_malloc ( sizeof(CFILE) );
309 if ( cfile == NULL ) {
314 cfile->size = filelength( fileno(fp) );
315 cfile->lib_offset = 0;
316 cfile->raw_position = 0;
321 int cfilelength( CFILE *fp )
326 int cfgetc( CFILE * fp )
330 if (fp->raw_position >= fp->size ) return EOF;
332 c = getc( fp->file );
336 // Assert( fp->raw_position==(ftell(fp->file)-fp->lib_offset) );
341 char * cfgets( char * buf, size_t n, CFILE * fp )
347 for (i=0; i<n-1; i++ ) {
349 if (fp->raw_position >= fp->size ) {
353 c = fgetc( fp->file );
359 c1 = fgetc( fp->file );
360 fseek( fp->file, -1, SEEK_CUR);
368 #ifdef MACINTOSH // because cr-lf is a bad thing on the mac
369 if ( c == 13 ) // and anyway -- 0xod is CR on mac, not 0x0a
373 if ( c=='\n' ) break;
379 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp )
381 unsigned int i, size;
383 size = elsize * nelem;
384 if ( size < 1 ) return 0;
386 i = fread ( buf, 1, size, fp->file );
387 fp->raw_position += i;
392 int cftell( CFILE *fp )
394 return fp->raw_position;
397 int cfseek( CFILE *fp, long int offset, int where )
399 int c, goal_position;
403 goal_position = offset;
406 goal_position = fp->raw_position+offset;
409 goal_position = fp->size+offset;
414 c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
415 fp->raw_position = ftell(fp->file)-fp->lib_offset;
419 void cfclose( CFILE * fp )
426 // routines to read basic data types from CFILE's. Put here to
427 // simplify mac/pc reading from cfiles.
429 int cfile_read_int(CFILE *file)
433 if (cfread( &i, sizeof(i), 1, file) != 1)
434 Error( "Error reading short in cfile_read_int()" );
440 short cfile_read_short(CFILE *file)
444 if (cfread( &s, sizeof(s), 1, file) != 1)
445 Error( "Error reading short in cfile_read_short()" );
451 byte cfile_read_byte(CFILE *file)
455 if (cfread( &b, sizeof(b), 1, file) != 1)
456 Error( "Error reading byte in cfile_read_byte()" );
462 fix read_fix(CFILE *file)
466 if (cfread( &f, sizeof(f), 1, file) != 1)
467 Error( "Error reading fix in gamesave.c" );
469 f = (fix)INTEL_INT((int)f);
473 static void read_vector(vms_vector *v,CFILE *file)
475 v->x = read_fix(file);
476 v->y = read_fix(file);
477 v->z = read_fix(file);