1 /* $Id: physfsx.h,v 1.9 2005-02-25 04:25:58 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>
27 static inline int PHYSFSX_readString(PHYSFS_file *file, char *s)
35 PHYSFS_read(file, ptr, 1, 1);
36 while (!PHYSFS_eof(file) && *ptr++ != 0);
41 static inline int PHYSFSX_gets(PHYSFS_file *file, char *s)
49 PHYSFS_read(file, ptr, 1, 1);
50 while (!PHYSFS_eof(file) && *ptr++ != '\n');
55 static inline int PHYSFSX_writeU8(PHYSFS_file *file, PHYSFS_uint8 val)
57 return PHYSFS_write(file, &val, 1, 1);
60 static inline int PHYSFSX_writeString(PHYSFS_file *file, char *s)
62 return PHYSFS_write(file, s, 1, strlen(s) + 1);
65 static inline int PHYSFSX_puts(PHYSFS_file *file, char *s)
67 return PHYSFS_write(file, s, 1, strlen(s));
70 static inline int PHYSFSX_putc(PHYSFS_file *file, int c)
72 unsigned char ch = (unsigned char)c;
74 if (PHYSFS_write(file, &ch, 1, 1) < 1)
80 static inline int PHYSFSX_getRealPath(const char *stdPath, char *realPath)
82 const char *realDir = PHYSFS_getRealDir(stdPath);
83 const char *sep = PHYSFS_getDirSeparator();
88 realDir = PHYSFS_getWriteDir();
93 strncpy(realPath, realDir, PATH_MAX - 1);
94 if (strlen(realPath) >= strlen(sep))
96 p = realPath + strlen(realPath) - strlen(sep);
97 if (strcmp(p, sep)) // no sep at end of realPath
98 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
101 if (strlen(stdPath) >= 1)
108 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
111 if (strlen(realPath) < PATH_MAX - 2)
113 p = realPath + strlen(realPath);
124 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
126 char old[PATH_MAX], new[PATH_MAX];
128 PHYSFSX_getRealPath(oldpath, old);
129 PHYSFSX_getRealPath(newpath, new);
130 return (rename(old, new) == 0);
134 // returns -1 if error
135 // Gets bytes free in current write dir
136 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
138 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
141 if (!statfs(PHYSFS_getWriteDir(), &sfs))
142 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
150 //Open a file for reading, set up a buffer
151 static inline PHYSFS_file *PHYSFSX_openReadBuffered(char *filename)
154 PHYSFS_uint64 bufSize;
156 if (filename[0] == '\x01')
158 //FIXME: don't look in dir, only in hogfile
162 fp = PHYSFS_openRead(filename);
166 bufSize = PHYSFS_fileLength(fp);
167 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
168 bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times
173 //Open a file for writing, set up a buffer
174 static inline PHYSFS_file *PHYSFSX_openWriteBuffered(char *filename)
177 PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size.
179 fp = PHYSFS_openWrite(filename);
183 while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
189 #endif /* PHYSFSX_H */