1 /* $Id: physfsx.h,v 1.7 2004-12-05 10:56:32 btb 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 #endif /* PHYSFSX_H */