]> icculus.org git repositories - btb/d2x.git/blob - cfile/cfile.c
Partial application of linux/alpha patch. Courtesy of Falk Hueffner <falk.hueffner...
[btb/d2x.git] / cfile / cfile.c
1 /*
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.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "pstypes.h"
22 #include "u_mem.h"
23 #include "strutil.h"
24 #include "d_io.h"
25 #include "error.h"
26 #include "cfile.h"
27 #include "byteswap.h"
28
29 typedef struct hogfile {
30         char    name[13];
31         int     offset;
32         int     length;
33 } hogfile;
34
35 #define MAX_HOGFILES 300
36
37 hogfile HogFiles[MAX_HOGFILES];
38 char Hogfile_initialized = 0;
39 int Num_hogfiles = 0;
40
41 hogfile AltHogFiles[MAX_HOGFILES];
42 char AltHogfile_initialized = 0;
43 int AltNum_hogfiles = 0;
44 char HogFilename[64];
45 char AltHogFilename[64];
46
47 char AltHogDir[64];
48 char AltHogdir_initialized = 0;
49
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 :
54
55 #ifdef MACINTOSH
56 void macify_dospath(char *dos_path, char *mac_path)
57 {
58         char *p;
59         
60         if (!strncmp(dos_path, ".\\", 2)) {
61                 strcpy(mac_path, ":");
62                 strcat(mac_path, &(dos_path[2]) );
63         } else
64                 strcpy(mac_path, dos_path);
65                 
66         while ( (p = strchr(mac_path, '\\')) != NULL)
67                 *p = ':';
68         
69 }
70 #endif
71
72 void cfile_use_alternate_hogdir( char * path )
73 {
74         if ( path )     {
75                 strcpy( AltHogDir, path );
76                 AltHogdir_initialized = 1;
77         } else {
78                 AltHogdir_initialized = 0;
79         }
80 }
81
82 //in case no one installs one
83 int default_error_counter=0;
84
85 //ptr to counter of how many critical errors
86 int *critical_error_counter_ptr=&default_error_counter;
87
88 //tell cfile about your critical error counter 
89 void cfile_set_critical_error_counter_ptr(int *ptr)
90 {
91         critical_error_counter_ptr = ptr;
92
93 }
94
95
96 FILE * cfile_get_filehandle( char * filename, char * mode )
97 {
98         FILE * fp;
99         char temp[128];
100
101         *critical_error_counter_ptr = 0;
102         fp = fopen( filename, mode );
103         if ( fp && *critical_error_counter_ptr )        {
104                 fclose(fp);
105                 fp = NULL;
106         }
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 )        {
113                         fclose(fp);
114                         fp = NULL;
115                 }
116         } 
117         return fp;
118 }
119
120 //returns 1 if file loaded with no errors
121 int cfile_init_hogfile(char *fname, hogfile * hog_files, int * nfiles )
122 {
123         char id[4];
124         FILE * fp;
125         int i, len;
126
127         *nfiles = 0;
128
129         fp = cfile_get_filehandle( fname, "rb" );
130         if ( fp == NULL ) return 0;
131
132         fread( id, 3, 1, fp );
133         if ( strncmp( id, "DHF", 3 ) )  {
134                 fclose(fp);
135                 return 0;
136         }
137
138         while( 1 )      
139         {       
140                 if ( *nfiles >= MAX_HOGFILES ) {
141                         fclose(fp);
142                         Error( "HOGFILE is limited to %d files.\n",  MAX_HOGFILES );
143                 }
144                 i = fread( hog_files[*nfiles].name, 13, 1, fp );
145                 if ( i != 1 )   {               //eof here is ok
146                         fclose(fp);
147                         return 1;
148                 }
149                 i = fread( &len, 4, 1, fp );
150                 if ( i != 1 )   {
151                         fclose(fp);
152                         return 0;
153                 }
154                 hog_files[*nfiles].length = INTEL_INT(len);
155                 hog_files[*nfiles].offset = ftell( fp );
156                 *nfiles = (*nfiles) + 1;
157                 // Skip over
158                 i = fseek( fp, INTEL_INT(len), SEEK_CUR );
159         }
160 }
161
162 //Specify the name of the hogfile.  Returns 1 if hogfile found & had files
163 int cfile_init(char *hogname)
164 {
165         #ifdef MACINTOSH
166         char mac_path[255];
167         
168         macify_dospath(hogname, mac_path);
169         #endif
170         
171         Assert(Hogfile_initialized == 0);
172
173         #ifndef MACINTOSH
174         if (cfile_init_hogfile(hogname, HogFiles, &Num_hogfiles )) {
175                 strcpy( HogFilename, hogname );
176         #else
177         if (cfile_init_hogfile(mac_path, HogFiles, &Num_hogfiles )) {
178                 strcpy( HogFilename, mac_path );
179         #endif  
180                 Hogfile_initialized = 1;
181                 return 1;
182         }
183         else
184                 return 0;       //not loaded!
185 }
186
187
188 FILE * cfile_find_libfile(char * name, int * length)
189 {
190         FILE * fp;
191         int i;
192
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;
200                                 return fp;
201                         }
202                 }
203         }
204
205         if ( !Hogfile_initialized )     {
206                 //@@cfile_init_hogfile( "DESCENT2.HOG", HogFiles, &Num_hogfiles );
207                 //@@Hogfile_initialized = 1;
208
209                 //Int3();       //hogfile ought to be initialized
210         }
211
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;
218                         return fp;
219                 }
220         }
221         return NULL;
222 }
223
224 int cfile_use_alternate_hogfile( char * name )
225 {
226         if ( name )     {
227                 #ifdef MACINTOSH
228                 char mac_path[255];
229                 
230                 macify_dospath(name, mac_path);
231                 strcpy( AltHogFilename, mac_path);
232                 #else
233                 strcpy( AltHogFilename, name );
234                 #endif
235                 cfile_init_hogfile( AltHogFilename, AltHogFiles, &AltNum_hogfiles );
236                 AltHogfile_initialized = 1;
237                 return (AltNum_hogfiles > 0);
238         } else {
239                 AltHogfile_initialized = 0;
240                 return 1;
241         }
242 }
243
244 int cfexist( char * filename )
245 {
246         int length;
247         FILE *fp;
248
249
250         if (filename[0] != '\x01')
251                 fp = cfile_get_filehandle( filename, "rb" );            // Check for non-hog file first...
252         else {
253                 fp = NULL;              //don't look in dir, only in hogfile
254                 filename++;
255         }
256
257         if ( fp )       {
258                 fclose(fp);
259                 return 1;
260         }
261
262         fp = cfile_find_libfile(filename, &length );
263         if ( fp )       {
264                 fclose(fp);
265                 return 2;               // file found in hog
266         }
267
268         return 0;               // Couldn't find it.
269 }
270
271
272 CFILE * cfopen(char * filename, char * mode ) 
273 {
274         int length;
275         FILE * fp;
276         CFILE *cfile;
277         
278         if (stricmp( mode, "rb"))       {
279                 Error( "cfiles can only be opened with mode==rb\n" );
280         }
281
282         if (filename[0] != '\x01') {
283                 #ifdef MACINTOSH
284                 char mac_path[255];
285                 
286                 macify_dospath(filename, mac_path);
287                 fp = cfile_get_filehandle( mac_path, mode);
288                 #else
289                 fp = cfile_get_filehandle( filename, mode );            // Check for non-hog file first...
290                 #endif
291         } else {
292                 fp = NULL;              //don't look in dir, only in hogfile
293                 filename++;
294         }
295
296         if ( !fp ) {
297                 fp = cfile_find_libfile(filename, &length );
298                 if ( !fp )
299                         return NULL;            // No file found
300                 cfile = d_malloc ( sizeof(CFILE) );
301                 if ( cfile == NULL ) {
302                         fclose(fp);
303                         return NULL;
304                 }
305                 cfile->file = fp;
306                 cfile->size = length;
307                 cfile->lib_offset = ftell( fp );
308                 cfile->raw_position = 0;
309                 return cfile;
310         } else {
311                 cfile = d_malloc ( sizeof(CFILE) );
312                 if ( cfile == NULL ) {
313                         fclose(fp);
314                         return NULL;
315                 }
316                 cfile->file = fp;
317                 cfile->size = filelength( fileno(fp) );
318                 cfile->lib_offset = 0;
319                 cfile->raw_position = 0;
320                 return cfile;
321         }
322 }
323
324 int cfilelength( CFILE *fp )
325 {
326         return fp->size;
327 }
328
329 int cfgetc( CFILE * fp ) 
330 {
331         int c;
332
333         if (fp->raw_position >= fp->size ) return EOF;
334
335         c = getc( fp->file );
336         if (c!=EOF) 
337                 fp->raw_position++;
338
339 //      Assert( fp->raw_position==(ftell(fp->file)-fp->lib_offset) );
340
341         return c;
342 }
343
344 char * cfgets( char * buf, size_t n, CFILE * fp )
345 {
346         char * t = buf;
347         int i;
348         int c;
349
350         for (i=0; i<n-1; i++ )  {
351                 do {
352                         if (fp->raw_position >= fp->size ) {
353                                 *buf = 0;
354                                 return NULL;
355                         }
356                         c = fgetc( fp->file );
357                         fp->raw_position++;
358 #ifdef MACINTOSH
359                         if (c == 13) {
360                                 int c1;
361                                 
362                                 c1 = fgetc( fp->file );
363                                 fseek( fp->file, -1, SEEK_CUR);
364                                 if ( c1 == 10 )
365                                         continue;
366                                 else
367                                         break;
368                         }
369 #endif
370                 } while ( c == 13 );
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
373                         c = '\n';
374 #endif
375                 *buf++ = c;
376                 if ( c=='\n' ) break;
377         }
378         *buf++ = 0;
379         return  t;
380 }
381
382 size_t cfread( void * buf, size_t elsize, size_t nelem, CFILE * fp ) 
383 {
384         unsigned int i, size;
385
386         size = elsize * nelem;
387         if ( size < 1 ) return 0;
388
389         i = fread ( buf, 1, size, fp->file );
390         fp->raw_position += i;
391         return i/elsize;
392 }
393
394
395 int cftell( CFILE *fp ) 
396 {
397         return fp->raw_position;
398 }
399
400 int cfseek( CFILE *fp, long int offset, int where )
401 {
402         int c, goal_position;
403
404         switch( where ) {
405         case SEEK_SET:
406                 goal_position = offset;
407                 break;
408         case SEEK_CUR:
409                 goal_position = fp->raw_position+offset;
410                 break;
411         case SEEK_END:
412                 goal_position = fp->size+offset;
413                 break;
414         default:
415                 return 1;
416         }       
417         c = fseek( fp->file, fp->lib_offset + goal_position, SEEK_SET );
418         fp->raw_position = ftell(fp->file)-fp->lib_offset;
419         return c;
420 }
421
422 void cfclose( CFILE * fp ) 
423 {       
424         fclose(fp->file);
425         d_free(fp);
426         return;
427 }
428
429 // routines to read basic data types from CFILE's.  Put here to
430 // simplify mac/pc reading from cfiles.
431
432 int cfile_read_int(CFILE *file)
433 {
434         int32_t i;
435
436         if (cfread( &i, sizeof(i), 1, file) != 1)
437                 Error( "Error reading int in cfile_read_int()" );
438
439         i = INTEL_INT(i);
440         return i;
441 }
442
443 short cfile_read_short(CFILE *file)
444 {
445         int16_t s;
446
447         if (cfread( &s, sizeof(s), 1, file) != 1)
448                 Error( "Error reading short in cfile_read_short()" );
449
450         s = INTEL_SHORT(s);
451         return s;
452 }
453
454 byte cfile_read_byte(CFILE *file)
455 {
456         byte b;
457
458         if (cfread( &b, sizeof(b), 1, file) != 1)
459                 Error( "Error reading byte in cfile_read_byte()" );
460
461         return b;
462 }
463
464 fix cfile_read_fix(CFILE *file)
465 {
466         fix f;
467
468         if (cfread( &f, sizeof(f), 1, file) != 1)
469                 Error( "Error reading fix in cfile_read_fix()" );
470
471         f = (fix)INTEL_INT((int)f);
472         return f;
473 }
474
475 fixang cfile_read_fixang(CFILE *file)
476 {
477         fixang f;
478
479         if (cfread(&f, 2, 1, file) != 1)
480                 Error("Error reading fixang in cfile_read_fixang()");
481
482         f = (fixang) INTEL_SHORT((int) f);
483         return f;
484 }
485
486 void cfile_read_vector(vms_vector *v, CFILE *file)
487 {
488         v->x = cfile_read_fix(file);
489         v->y = cfile_read_fix(file);
490         v->z = cfile_read_fix(file);
491 }
492
493 void cfile_read_angvec(vms_angvec *v, CFILE *file)
494 {
495         v->p = cfile_read_fixang(file);
496         v->b = cfile_read_fixang(file);
497         v->h = cfile_read_fixang(file);
498 }