1 /* $Id: physfsx.h,v 1.10 2005-06-13 09:36:45 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>
28 static inline int PHYSFSX_readString(PHYSFS_file *file, char *s)
36 PHYSFS_read(file, ptr, 1, 1);
37 while (!PHYSFS_eof(file) && *ptr++ != 0);
42 static inline int PHYSFSX_gets(PHYSFS_file *file, char *s)
50 PHYSFS_read(file, ptr, 1, 1);
51 while (!PHYSFS_eof(file) && *ptr++ != '\n');
56 static inline int PHYSFSX_writeU8(PHYSFS_file *file, PHYSFS_uint8 val)
58 return PHYSFS_write(file, &val, 1, 1);
61 static inline int PHYSFSX_writeString(PHYSFS_file *file, char *s)
63 return PHYSFS_write(file, s, 1, strlen(s) + 1);
66 static inline int PHYSFSX_puts(PHYSFS_file *file, char *s)
68 return PHYSFS_write(file, s, 1, strlen(s));
71 static inline int PHYSFSX_putc(PHYSFS_file *file, int c)
73 unsigned char ch = (unsigned char)c;
75 if (PHYSFS_write(file, &ch, 1, 1) < 1)
81 #define PHYSFSX_writeFix PHYSFS_writeSLE32
82 #define PHYSFSX_writeFixAng PHYSFS_writeSLE16
84 static inline int PHYSFSX_writeVector(PHYSFS_file *file, vms_vector *v)
86 if (PHYSFSX_writeFix(file, v->x) < 1 ||
87 PHYSFSX_writeFix(file, v->y) < 1 ||
88 PHYSFSX_writeFix(file, v->z) < 1)
94 static inline int PHYSFSX_writeAngleVec(PHYSFS_file *file, vms_angvec *v)
96 if (PHYSFSX_writeFixAng(file, v->p) < 1 ||
97 PHYSFSX_writeFixAng(file, v->b) < 1 ||
98 PHYSFSX_writeFixAng(file, v->h) < 1)
104 static inline int PHYSFSX_writeMatrix(PHYSFS_file *file, vms_matrix *m)
106 if (PHYSFSX_writeVector(file, &m->rvec) < 1 ||
107 PHYSFSX_writeVector(file, &m->uvec) < 1 ||
108 PHYSFSX_writeVector(file, &m->fvec) < 1)
114 static inline int PHYSFSX_getRealPath(const char *stdPath, char *realPath)
116 const char *realDir = PHYSFS_getRealDir(stdPath);
117 const char *sep = PHYSFS_getDirSeparator();
122 realDir = PHYSFS_getWriteDir();
127 strncpy(realPath, realDir, PATH_MAX - 1);
128 if (strlen(realPath) >= strlen(sep))
130 p = realPath + strlen(realPath) - strlen(sep);
131 if (strcmp(p, sep)) // no sep at end of realPath
132 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
135 if (strlen(stdPath) >= 1)
142 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
145 if (strlen(realPath) < PATH_MAX - 2)
147 p = realPath + strlen(realPath);
158 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
160 char old[PATH_MAX], new[PATH_MAX];
162 PHYSFSX_getRealPath(oldpath, old);
163 PHYSFSX_getRealPath(newpath, new);
164 return (rename(old, new) == 0);
168 // returns -1 if error
169 // Gets bytes free in current write dir
170 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
172 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
175 if (!statfs(PHYSFS_getWriteDir(), &sfs))
176 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
184 //Open a file for reading, set up a buffer
185 static inline PHYSFS_file *PHYSFSX_openReadBuffered(char *filename)
188 PHYSFS_uint64 bufSize;
190 if (filename[0] == '\x01')
192 //FIXME: don't look in dir, only in hogfile
196 fp = PHYSFS_openRead(filename);
200 bufSize = PHYSFS_fileLength(fp);
201 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
202 bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
207 //Open a file for writing, set up a buffer
208 static inline PHYSFS_file *PHYSFSX_openWriteBuffered(char *filename)
211 PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size.
213 fp = PHYSFS_openWrite(filename);
217 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
223 #endif /* PHYSFSX_H */