]> icculus.org git repositories - btb/d2x.git/blob - include/cfile.h
merged physfs branch
[btb/d2x.git] / include / cfile.h
1 /* $Id: cfile.h,v 1.12 2004-12-01 12:48:13 btb Exp $ */
2 /*
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.
13 */
14
15 /*
16  *
17  * Wrappers for physfs abstraction layer
18  *
19  */
20
21 #ifndef _CFILE_H
22 #define _CFILE_H
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <physfs.h>
27
28 #include "pstypes.h"
29 #include "maths.h"
30 #include "vecmat.h"
31 #include "physfsx.h"
32
33 #define CFILE            PHYSFS_file
34 #define cfopen(n,m)      PHYSFS_openRead(n)
35 #define cfread(p,s,n,fp) PHYSFS_read(fp,p,s,n)
36 #define cfclose          PHYSFS_close
37 #define cftell           PHYSFS_tell
38 #define cfexist          PHYSFS_exists
39 #define cfilelength      PHYSFS_fileLength
40
41 //Specify the name of the hogfile.  Returns 1 if hogfile found & had files
42 static inline int cfile_init(char *hogname)
43 {
44         char pathname[1024];
45
46         if (!PHYSFSX_getRealPath(hogname, pathname))
47                 return 0;
48
49         return PHYSFS_addToSearchPath(pathname, 1);
50 }
51
52 static inline int cfile_close(char *hogname)
53 {
54         char pathname[1024];
55
56         if (!PHYSFSX_getRealPath(hogname, pathname))
57                 return 0;
58
59         return PHYSFS_removeFromSearchPath(pathname);
60 }
61
62
63 static inline int cfile_size(char *hogname)
64 {
65         PHYSFS_file *fp;
66         int size;
67
68         fp = PHYSFS_openRead(hogname);
69         if (fp == NULL)
70                 return -1;
71         size = PHYSFS_fileLength(fp);
72         cfclose(fp);
73
74         return size;
75 }
76
77 static inline int cfgetc(PHYSFS_file *const fp)
78 {
79         unsigned char c;
80
81         if (PHYSFS_read(fp, &c, 1, 1) != 1)
82                 return EOF;
83
84         return c;
85 }
86
87 static inline int cfseek(PHYSFS_file *fp, long int offset, int where)
88 {
89         int c, goal_position;
90
91         switch(where)
92         {
93         case SEEK_SET:
94                 goal_position = offset;
95                 break;
96         case SEEK_CUR:
97                 goal_position = PHYSFS_tell(fp) + offset;
98                 break;
99         case SEEK_END:
100                 goal_position = PHYSFS_fileLength(fp) + offset;
101                 break;
102         default:
103                 return 1;
104         }
105         c = PHYSFS_seek(fp, goal_position);
106         return !c;
107 }
108
109 static inline char * cfgets(char *buf, size_t n, PHYSFS_file *const fp)
110 {
111         int i;
112         int c;
113
114         for (i = 0; i < n - 1; i++)
115         {
116                 do
117                 {
118                         c = cfgetc(fp);
119                         if (c == EOF)
120                         {
121                                 *buf = 0;
122
123                                 return NULL;
124                         }
125                         if (c == 0 || c == 10)  // Unix line ending
126                                 break;
127                         if (c == 13)            // Mac or DOS line ending
128                         {
129                                 int c1;
130
131                                 c1 = cfgetc(fp);
132                                 if (c1 != EOF)  // The file could end with a Mac line ending
133                                         cfseek(fp, -1, SEEK_CUR);
134                                 if (c1 == 10) // DOS line ending
135                                         continue;
136                                 else            // Mac line ending
137                                         break;
138                         }
139                 } while (c == 13);
140                 if (c == 13)    // because cr-lf is a bad thing on the mac
141                         c = '\n';   // and anyway -- 0xod is CR on mac, not 0x0a
142                 if (c == '\n')
143                         break;
144                 *buf++ = c;
145         }
146         *buf = 0;
147
148         return buf;
149 }
150
151
152 /*
153  * read some data types...
154  */
155
156 static inline int cfile_read_int(PHYSFS_file *file)
157 {
158         int i;
159
160         if (!PHYSFS_readSLE32(file, &i))
161         {
162                 fprintf(stderr, "Error reading int in cfile_read_int()");
163                 exit(1);
164         }
165
166         return i;
167 }
168
169 static inline short cfile_read_short(PHYSFS_file *file)
170 {
171         int16_t s;
172
173         if (!PHYSFS_readSLE16(file, &s))
174         {
175                 fprintf(stderr, "Error reading short in cfile_read_short()");
176                 exit(1);
177         }
178
179         return s;
180 }
181
182 static inline sbyte cfile_read_byte(PHYSFS_file *file)
183 {
184         sbyte b;
185
186         if (PHYSFS_read(file, &b, sizeof(b), 1) != 1)
187         {
188                 fprintf(stderr, "Error reading byte in cfile_read_byte()");
189                 exit(1);
190         }
191
192         return b;
193 }
194
195 static inline fix cfile_read_fix(PHYSFS_file *file)
196 {
197         int f;  // a fix is defined as a long for Mac OS 9, and MPW can't convert from (long *) to (int *)
198
199         if (!PHYSFS_readSLE32(file, &f))
200         {
201                 fprintf(stderr, "Error reading fix in cfile_read_fix()");
202                 exit(1);
203         }
204
205         return f;
206 }
207
208 static inline fixang cfile_read_fixang(PHYSFS_file *file)
209 {
210         fixang f;
211
212         if (!PHYSFS_readSLE16(file, &f))
213         {
214                 fprintf(stderr, "Error reading fixang in cfile_read_fixang()");
215                 exit(1);
216         }
217
218         return f;
219 }
220
221 static inline void cfile_read_vector(vms_vector *v, PHYSFS_file *file)
222 {
223         v->x = cfile_read_fix(file);
224         v->y = cfile_read_fix(file);
225         v->z = cfile_read_fix(file);
226 }
227
228 static inline void cfile_read_angvec(vms_angvec *v, PHYSFS_file *file)
229 {
230         v->p = cfile_read_fixang(file);
231         v->b = cfile_read_fixang(file);
232         v->h = cfile_read_fixang(file);
233 }
234
235 static inline void cfile_read_matrix(vms_matrix *m,PHYSFS_file *file)
236 {
237         cfile_read_vector(&m->rvec,file);
238         cfile_read_vector(&m->uvec,file);
239         cfile_read_vector(&m->fvec,file);
240 }
241
242 #endif