1 /* $Id: physfsx.h,v 1.12 2006-07-29 04:02:40 chris Exp $ */
5 * Some simple physfs extensions
12 #if !defined(macintosh) && !defined(_MSC_VER)
13 #include <sys/param.h>
15 #if defined(__linux__)
17 #elif defined(__MACH__) && defined(__APPLE__)
18 #include <sys/mount.h>
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[])
38 PHYSFS_permitSymbolicLinks(1);
40 PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
41 InitArgs( argc,argv );
43 if ((t = FindArg("-userdir"))
45 || 1 // or if it's a unix platform
49 // This stuff below seems overly complicated - brad
51 char *path = Args[t+1];
52 char fullPath[PATH_MAX + 5];
58 PHYSFS_removeFromSearchPath(PHYSFS_getBaseDir());
60 if (path[0] == '~') // yes, this tilde can be put before non-unix paths.
62 const char *home = PHYSFS_getUserDir();
64 strcpy(fullPath, home); // prepend home to the path
66 if (*path == *PHYSFS_getDirSeparator())
68 strncat(fullPath, path, PATH_MAX + 5 - strlen(home));
71 strncpy(fullPath, path, PATH_MAX + 5);
73 PHYSFS_setWriteDir(fullPath);
74 if (!PHYSFS_getWriteDir())
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
80 strcpy(ancestor, fullPath);
81 while (!PHYSFS_getWriteDir() && ((p = strrchr(ancestor, *PHYSFS_getDirSeparator()))))
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
90 p[1] = 0; // go to parent
91 PHYSFS_setWriteDir(ancestor);
94 strcpy(child, fullPath + strlen(ancestor));
95 for (p = child; (p = strchr(p, *PHYSFS_getDirSeparator())); p++)
98 PHYSFS_setWriteDir(fullPath);
101 PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 1);
105 if (!PHYSFS_getWriteDir())
107 PHYSFS_setWriteDir(PHYSFS_getBaseDir());
108 if (!PHYSFS_getWriteDir())
109 Error("can't set write dir\n");
111 PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 0);
114 //tell cfile where hogdir is
115 if ((t=FindArg("-hogdir")))
116 PHYSFS_addToSearchPath(Args[t + 1], 1);
118 else if (!FindArg("-nohogdir"))
119 PHYSFS_addToSearchPath(SHAREPATH, 1);
123 static inline int PHYSFSX_readString(PHYSFS_file *file, char *s)
127 if (PHYSFS_eof(file))
131 PHYSFS_read(file, ptr, 1, 1);
132 while (!PHYSFS_eof(file) && *ptr++ != 0);
137 static inline int PHYSFSX_gets(PHYSFS_file *file, char *s)
141 if (PHYSFS_eof(file))
145 PHYSFS_read(file, ptr, 1, 1);
146 while (!PHYSFS_eof(file) && *ptr++ != '\n');
151 static inline int PHYSFSX_writeU8(PHYSFS_file *file, PHYSFS_uint8 val)
153 return PHYSFS_write(file, &val, 1, 1);
156 static inline int PHYSFSX_writeString(PHYSFS_file *file, char *s)
158 return PHYSFS_write(file, s, 1, strlen(s) + 1);
161 static inline int PHYSFSX_puts(PHYSFS_file *file, char *s)
163 return PHYSFS_write(file, s, 1, strlen(s));
166 static inline int PHYSFSX_putc(PHYSFS_file *file, int c)
168 unsigned char ch = (unsigned char)c;
170 if (PHYSFS_write(file, &ch, 1, 1) < 1)
176 static inline int PHYSFSX_printf(PHYSFS_file *file, char *format, ...)
181 va_start(args, format);
182 vsprintf(buffer, format, args);
184 return PHYSFSX_puts(file, buffer);
187 #define PHYSFSX_writeFix PHYSFS_writeSLE32
188 #define PHYSFSX_writeFixAng PHYSFS_writeSLE16
190 static inline int PHYSFSX_writeVector(PHYSFS_file *file, vms_vector *v)
192 if (PHYSFSX_writeFix(file, v->x) < 1 ||
193 PHYSFSX_writeFix(file, v->y) < 1 ||
194 PHYSFSX_writeFix(file, v->z) < 1)
200 static inline int PHYSFSX_writeAngleVec(PHYSFS_file *file, vms_angvec *v)
202 if (PHYSFSX_writeFixAng(file, v->p) < 1 ||
203 PHYSFSX_writeFixAng(file, v->b) < 1 ||
204 PHYSFSX_writeFixAng(file, v->h) < 1)
210 static inline int PHYSFSX_writeMatrix(PHYSFS_file *file, vms_matrix *m)
212 if (PHYSFSX_writeVector(file, &m->rvec) < 1 ||
213 PHYSFSX_writeVector(file, &m->uvec) < 1 ||
214 PHYSFSX_writeVector(file, &m->fvec) < 1)
220 static inline int PHYSFSX_getRealPath(const char *stdPath, char *realPath)
222 const char *realDir = PHYSFS_getRealDir(stdPath);
223 const char *sep = PHYSFS_getDirSeparator();
228 realDir = PHYSFS_getWriteDir();
233 strncpy(realPath, realDir, PATH_MAX - 1);
234 if (strlen(realPath) >= strlen(sep))
236 p = realPath + strlen(realPath) - strlen(sep);
237 if (strcmp(p, sep)) // no sep at end of realPath
238 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
241 if (strlen(stdPath) >= 1)
248 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
251 if (strlen(realPath) < PATH_MAX - 2)
253 p = realPath + strlen(realPath);
264 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
266 char old[PATH_MAX], new[PATH_MAX];
268 PHYSFSX_getRealPath(oldpath, old);
269 PHYSFSX_getRealPath(newpath, new);
270 return (rename(old, new) == 0);
274 // returns -1 if error
275 // Gets bytes free in current write dir
276 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
278 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
281 if (!statfs(PHYSFS_getWriteDir(), &sfs))
282 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
290 //Open a file for reading, set up a buffer
291 static inline PHYSFS_file *PHYSFSX_openReadBuffered(char *filename)
294 PHYSFS_uint64 bufSize;
296 if (filename[0] == '\x01')
298 //FIXME: don't look in dir, only in hogfile
302 fp = PHYSFS_openRead(filename);
306 bufSize = PHYSFS_fileLength(fp);
307 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
308 bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
313 //Open a file for writing, set up a buffer
314 static inline PHYSFS_file *PHYSFSX_openWriteBuffered(char *filename)
317 PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size.
319 fp = PHYSFS_openWrite(filename);
323 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
329 #endif /* PHYSFSX_H */