1 /* $Id: physfsx.h,v 1.8 2005-02-25 02:02:22 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>
26 static inline int PHYSFSX_readString(PHYSFS_file *file, char *s)
34 PHYSFS_read(file, ptr, 1, 1);
35 while (!PHYSFS_eof(file) && *ptr++ != 0);
40 static inline int PHYSFSX_gets(PHYSFS_file *file, char *s)
48 PHYSFS_read(file, ptr, 1, 1);
49 while (!PHYSFS_eof(file) && *ptr++ != '\n');
54 static inline int PHYSFSX_writeU8(PHYSFS_file *file, PHYSFS_uint8 val)
56 return PHYSFS_write(file, &val, 1, 1);
59 static inline int PHYSFSX_writeString(PHYSFS_file *file, char *s)
61 return PHYSFS_write(file, s, 1, strlen(s) + 1);
64 static inline int PHYSFSX_puts(PHYSFS_file *file, char *s)
66 return PHYSFS_write(file, s, 1, strlen(s));
69 static inline int PHYSFSX_putc(PHYSFS_file *file, int c)
71 unsigned char ch = (unsigned char)c;
73 if (PHYSFS_write(file, &ch, 1, 1) < 1)
79 static inline int PHYSFSX_getRealPath(const char *stdPath, char *realPath)
81 const char *realDir = PHYSFS_getRealDir(stdPath);
82 const char *sep = PHYSFS_getDirSeparator();
87 realDir = PHYSFS_getWriteDir();
92 strncpy(realPath, realDir, PATH_MAX - 1);
93 if (strlen(realPath) >= strlen(sep))
95 p = realPath + strlen(realPath) - strlen(sep);
96 if (strcmp(p, sep)) // no sep at end of realPath
97 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
100 if (strlen(stdPath) >= 1)
107 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
110 if (strlen(realPath) < PATH_MAX - 2)
112 p = realPath + strlen(realPath);
123 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
125 char old[PATH_MAX], new[PATH_MAX];
127 PHYSFSX_getRealPath(oldpath, old);
128 PHYSFSX_getRealPath(newpath, new);
129 return (rename(old, new) == 0);
133 // returns -1 if error
134 // Gets bytes free in current write dir
135 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
137 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
140 if (!statfs(PHYSFS_getWriteDir(), &sfs))
141 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
149 //Open a file for reading, set up a buffer
150 static inline PHYSFS_file *PHYSFSX_openReadBuffered(char *filename)
153 PHYSFS_uint64 bufSize;
155 if (filename[0] == '\x01')
157 //FIXME: don't look in dir, only in hogfile
161 fp = PHYSFS_openRead(filename);
165 bufSize = PHYSFS_fileLength(fp);
166 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
167 bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
172 //Open a file for writing, set up a buffer
173 static inline PHYSFS_file *PHYSFSX_openWriteBuffered(char *filename)
176 PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size.
178 fp = PHYSFS_openWrite(filename);
182 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
188 #endif /* PHYSFSX_H */