]> icculus.org git repositories - btb/d2x.git/blob - include/physfsx.h
Merge branch 'master' into cmd-queue
[btb/d2x.git] / include / physfsx.h
1 /* $Id: physfsx.h,v 1.12 2006-07-29 04:02:40 chris Exp $ */
2
3 /*
4  *
5  * Some simple physfs extensions
6  *
7  */
8
9 #ifndef PHYSFSX_H
10 #define PHYSFSX_H
11
12 #if !defined(macintosh) && !defined(_MSC_VER)
13 #include <sys/param.h>
14 #endif
15 #if defined(__linux__)
16 #include <sys/vfs.h>
17 #elif defined(__MACH__) && defined(__APPLE__)
18 #include <sys/mount.h>
19 #endif
20 #include <string.h>
21 #include <stdarg.h>
22
23 #include <physfs.h>
24
25 #include "pstypes.h"
26 #include "error.h"
27 #include "vecmat.h"
28 #include "args.h"
29
30 // Initialise PhysicsFS, set up basic search paths and add arguments from .ini file(s).
31 // The arguments are used to determine the search paths, so the first .ini file must be
32 // in the same directory as D2X. A second one can be in the user directory.
33 static inline void PHYSFSX_init(int argc, char *argv[])
34 {
35         int t;
36
37         PHYSFS_init(argv[0]);
38         PHYSFS_permitSymbolicLinks(1);
39
40         PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
41         InitArgs( argc,argv );
42
43         if ((t = FindArg("-userdir"))
44 #ifdef __unix__
45                 || 1    // or if it's a unix platform
46 #endif
47                 )
48         {
49                 // This stuff below seems overly complicated - brad
50
51                 char *path = Args[t+1];
52                 char fullPath[PATH_MAX + 5];
53
54 #ifdef __unix__
55                 if (!t)
56                         path = "~/.d2x";
57 #endif
58                 PHYSFS_removeFromSearchPath(PHYSFS_getBaseDir());
59
60                 if (path[0] == '~') // yes, this tilde can be put before non-unix paths.
61                 {
62                         const char *home = PHYSFS_getUserDir();
63
64                         strcpy(fullPath, home); // prepend home to the path
65                         path++;
66                         if (*path == *PHYSFS_getDirSeparator())
67                                 path++;
68                         strncat(fullPath, path, PATH_MAX + 5 - strlen(home));
69                 }
70                 else
71                         strncpy(fullPath, path, PATH_MAX + 5);
72
73                 PHYSFS_setWriteDir(fullPath);
74                 if (!PHYSFS_getWriteDir())
75                 {                                               // need to make it
76                         char *p;
77                         char ancestor[PATH_MAX + 5];    // the directory which actually exists
78                         char child[PATH_MAX + 5];               // the directory relative to the above we're trying to make
79
80                         strcpy(ancestor, fullPath);
81                         while (!PHYSFS_getWriteDir() && ((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
82                         {
83                                 if (p[1] == 0)
84                                 {                                       // separator at the end (intended here, for safety)
85                                         *p = 0;                 // kill this separator
86                                         if (!((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
87                                                 break;          // give up, this is (usually) the root directory
88                                 }
89
90                                 p[1] = 0;                       // go to parent
91                                 PHYSFS_setWriteDir(ancestor);
92                         }
93
94                         strcpy(child, fullPath + strlen(ancestor));
95                         for (p = child; (p = strchr(p, *PHYSFS_getDirSeparator())); p++)
96                                 *p = '/';
97                         PHYSFS_mkdir(child);
98                         PHYSFS_setWriteDir(fullPath);
99                 }
100
101                 PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 1);
102                 AppendArgs();
103         }
104
105         if (!PHYSFS_getWriteDir())
106         {
107                 PHYSFS_setWriteDir(PHYSFS_getBaseDir());
108                 if (!PHYSFS_getWriteDir())
109                         Error("can't set write dir\n");
110                 else
111                         PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 0);
112         }
113
114         //tell cfile where hogdir is
115         if ((t=FindArg("-hogdir")))
116                 PHYSFS_addToSearchPath(Args[t + 1], 1);
117 #ifdef __unix__
118         else if (!FindArg("-nohogdir"))
119                 PHYSFS_addToSearchPath(SHAREPATH, 1);
120 #endif
121 #ifdef MACINTOSH
122         PHYSFS_addToSearchPath(":Data:", 1); // untested
123 #endif
124 }
125
126 static inline PHYSFS_sint64 PHYSFSX_readString(PHYSFS_file *file, char *s)
127 {
128         char *ptr = s;
129
130         if (PHYSFS_eof(file))
131                 *ptr = 0;
132         else
133                 do
134                         PHYSFS_read(file, ptr, 1, 1);
135                 while (!PHYSFS_eof(file) && *ptr++ != 0);
136
137         return strlen(s);
138 }
139
140 static inline PHYSFS_sint64 PHYSFSX_gets(PHYSFS_file *file, char *s)
141 {
142         char *ptr = s;
143
144         if (PHYSFS_eof(file))
145                 *ptr = 0;
146         else
147                 do
148                         PHYSFS_read(file, ptr, 1, 1);
149                 while (!PHYSFS_eof(file) && *ptr++ != '\n');
150
151         return strlen(s);
152 }
153
154 static inline PHYSFS_sint64 PHYSFSX_writeU8(PHYSFS_file *file, PHYSFS_uint8 val)
155 {
156         return PHYSFS_write(file, &val, 1, 1);
157 }
158
159 static inline PHYSFS_sint64 PHYSFSX_writeString(PHYSFS_file *file, char *s)
160 {
161         return PHYSFS_write(file, s, 1, (PHYSFS_uint32)strlen(s) + 1);
162 }
163
164 static inline PHYSFS_sint64 PHYSFSX_puts(PHYSFS_file *file, char *s)
165 {
166         return PHYSFS_write(file, s, 1, (PHYSFS_uint32)strlen(s));
167 }
168
169 static inline int PHYSFSX_putc(PHYSFS_file *file, int c)
170 {
171         unsigned char ch = (unsigned char)c;
172
173         if (PHYSFS_write(file, &ch, 1, 1) < 1)
174                 return -1;
175         else
176                 return (int)c;
177 }
178
179 static inline PHYSFS_sint64 PHYSFSX_printf(PHYSFS_file *file, char *format, ...)
180 {
181         char buffer[1024];
182         va_list args;
183
184         va_start(args, format);
185         vsprintf(buffer, format, args);
186
187         return PHYSFSX_puts(file, buffer);
188 }
189
190 #define PHYSFSX_writeFix        PHYSFS_writeSLE32
191 #define PHYSFSX_writeFixAng     PHYSFS_writeSLE16
192
193 static inline int PHYSFSX_writeVector(PHYSFS_file *file, vms_vector *v)
194 {
195         if (PHYSFSX_writeFix(file, v->x) < 1 ||
196          PHYSFSX_writeFix(file, v->y) < 1 ||
197          PHYSFSX_writeFix(file, v->z) < 1)
198                 return 0;
199
200         return 1;
201 }
202
203 static inline int PHYSFSX_writeAngleVec(PHYSFS_file *file, vms_angvec *v)
204 {
205         if (PHYSFSX_writeFixAng(file, v->p) < 1 ||
206          PHYSFSX_writeFixAng(file, v->b) < 1 ||
207          PHYSFSX_writeFixAng(file, v->h) < 1)
208                 return 0;
209
210         return 1;
211 }
212
213 static inline int PHYSFSX_writeMatrix(PHYSFS_file *file, vms_matrix *m)
214 {
215         if (PHYSFSX_writeVector(file, &m->rvec) < 1 ||
216          PHYSFSX_writeVector(file, &m->uvec) < 1 ||
217          PHYSFSX_writeVector(file, &m->fvec) < 1)
218                 return 0;
219
220         return 1;
221 }
222
223 static inline int PHYSFSX_getRealPath(const char *stdPath, char *realPath)
224 {
225         const char *realDir = PHYSFS_getRealDir(stdPath);
226         const char *sep = PHYSFS_getDirSeparator();
227         char *p;
228
229         if (!realDir)
230         {
231                 realDir = PHYSFS_getWriteDir();
232                 if (!realDir)
233                         return 0;
234         }
235
236         strncpy(realPath, realDir, PATH_MAX - 1);
237         if (strlen(realPath) >= strlen(sep))
238         {
239                 p = realPath + strlen(realPath) - strlen(sep);
240                 if (strcmp(p, sep)) // no sep at end of realPath
241                         strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
242         }
243
244         if (strlen(stdPath) >= 1)
245                 if (*stdPath == '/')
246                         stdPath++;
247
248         while (*stdPath)
249         {
250                 if (*stdPath == '/')
251                         strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
252                 else
253                 {
254                         if (strlen(realPath) < PATH_MAX - 2)
255                         {
256                                 p = realPath + strlen(realPath);
257                                 p[0] = *stdPath;
258                                 p[1] = '\0';
259                         }
260                 }
261                 stdPath++;
262         }
263
264         return 1;
265 }
266
267 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
268 {
269         char old[PATH_MAX], new[PATH_MAX];
270
271         PHYSFSX_getRealPath(oldpath, old);
272         PHYSFSX_getRealPath(newpath, new);
273         return (rename(old, new) == 0);
274 }
275
276
277 // returns -1 if error
278 // Gets bytes free in current write dir
279 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
280 {
281 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
282         struct statfs sfs;
283
284         if (!statfs(PHYSFS_getWriteDir(), &sfs))
285                 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
286
287         return -1;
288 #else
289         return 0x7FFFFFFF;
290 #endif
291 }
292
293 //Open a file for reading, set up a buffer
294 static inline PHYSFS_file *PHYSFSX_openReadBuffered(char *filename)
295 {
296         PHYSFS_file *fp;
297         PHYSFS_uint64 bufSize;
298
299         if (filename[0] == '\x01')
300         {
301                 //FIXME: don't look in dir, only in hogfile
302                 filename++;
303         }
304
305         fp = PHYSFS_openRead(filename);
306         if (!fp)
307                 return NULL;
308
309         bufSize = PHYSFS_fileLength(fp);
310         while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
311                 bufSize /= 2;   // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
312
313         return fp;
314 }
315
316 //Open a file for writing, set up a buffer
317 static inline PHYSFS_file *PHYSFSX_openWriteBuffered(char *filename)
318 {
319         PHYSFS_file *fp;
320         PHYSFS_uint64 bufSize = 1024*1024;      // hmm, seems like an OK size.
321
322         fp = PHYSFS_openWrite(filename);
323         if (!fp)
324                 return NULL;
325
326         while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
327                 bufSize /= 2;
328
329         return fp;
330 }
331
332 #endif /* PHYSFSX_H */