1 /* $Id: cfile.h,v 1.13 2004-12-01 13:01:00 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.
17 * Wrappers for physfs abstraction layer
33 #define CFILE PHYSFS_file
34 #define cfread(p,s,n,fp) PHYSFS_read(fp,p,s,n)
35 #define cfclose PHYSFS_close
36 #define cftell PHYSFS_tell
37 #define cfexist PHYSFS_exists
38 #define cfilelength PHYSFS_fileLength
40 //Open a file, set up a read buffer
41 static inline PHYSFS_file *cfopen(char *filename, char *mode)
44 PHYSFS_uint64 bufSize;
46 mode = mode; // no warning
47 fp = PHYSFS_openRead(filename);
51 bufSize = PHYSFS_fileLength(fp);
52 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
53 bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
58 //Specify the name of the hogfile. Returns 1 if hogfile found & had files
59 static inline int cfile_init(char *hogname)
63 if (!PHYSFSX_getRealPath(hogname, pathname))
66 return PHYSFS_addToSearchPath(pathname, 1);
69 static inline int cfile_close(char *hogname)
73 if (!PHYSFSX_getRealPath(hogname, pathname))
76 return PHYSFS_removeFromSearchPath(pathname);
80 static inline int cfile_size(char *hogname)
85 fp = PHYSFS_openRead(hogname);
88 size = PHYSFS_fileLength(fp);
94 static inline int cfgetc(PHYSFS_file *const fp)
98 if (PHYSFS_read(fp, &c, 1, 1) != 1)
104 static inline int cfseek(PHYSFS_file *fp, long int offset, int where)
106 int c, goal_position;
111 goal_position = offset;
114 goal_position = PHYSFS_tell(fp) + offset;
117 goal_position = PHYSFS_fileLength(fp) + offset;
122 c = PHYSFS_seek(fp, goal_position);
126 static inline char * cfgets(char *buf, size_t n, PHYSFS_file *const fp)
131 for (i = 0; i < n - 1; i++)
142 if (c == 0 || c == 10) // Unix line ending
144 if (c == 13) // Mac or DOS line ending
149 if (c1 != EOF) // The file could end with a Mac line ending
150 cfseek(fp, -1, SEEK_CUR);
151 if (c1 == 10) // DOS line ending
153 else // Mac line ending
157 if (c == 13) // because cr-lf is a bad thing on the mac
158 c = '\n'; // and anyway -- 0xod is CR on mac, not 0x0a
170 * read some data types...
173 static inline int cfile_read_int(PHYSFS_file *file)
177 if (!PHYSFS_readSLE32(file, &i))
179 fprintf(stderr, "Error reading int in cfile_read_int()");
186 static inline short cfile_read_short(PHYSFS_file *file)
190 if (!PHYSFS_readSLE16(file, &s))
192 fprintf(stderr, "Error reading short in cfile_read_short()");
199 static inline sbyte cfile_read_byte(PHYSFS_file *file)
203 if (PHYSFS_read(file, &b, sizeof(b), 1) != 1)
205 fprintf(stderr, "Error reading byte in cfile_read_byte()");
212 static inline fix cfile_read_fix(PHYSFS_file *file)
214 int f; // a fix is defined as a long for Mac OS 9, and MPW can't convert from (long *) to (int *)
216 if (!PHYSFS_readSLE32(file, &f))
218 fprintf(stderr, "Error reading fix in cfile_read_fix()");
225 static inline fixang cfile_read_fixang(PHYSFS_file *file)
229 if (!PHYSFS_readSLE16(file, &f))
231 fprintf(stderr, "Error reading fixang in cfile_read_fixang()");
238 static inline void cfile_read_vector(vms_vector *v, PHYSFS_file *file)
240 v->x = cfile_read_fix(file);
241 v->y = cfile_read_fix(file);
242 v->z = cfile_read_fix(file);
245 static inline void cfile_read_angvec(vms_angvec *v, PHYSFS_file *file)
247 v->p = cfile_read_fixang(file);
248 v->b = cfile_read_fixang(file);
249 v->h = cfile_read_fixang(file);
252 static inline void cfile_read_matrix(vms_matrix *m,PHYSFS_file *file)
254 cfile_read_vector(&m->rvec,file);
255 cfile_read_vector(&m->uvec,file);
256 cfile_read_vector(&m->fvec,file);