1 /* $Id: physfsx.h,v 1.6 2004-12-04 04:07:16 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();
88 strncpy(realPath, realDir, PATH_MAX - 1);
89 if (strlen(realPath) >= strlen(sep))
91 p = realPath + strlen(realPath) - strlen(sep);
92 if (strcmp(p, sep)) // no sep at end of realPath
93 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
96 if (strlen(stdPath) >= 1)
103 strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
106 if (strlen(realPath) < PATH_MAX - 2)
108 p = realPath + strlen(realPath);
119 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
121 char old[PATH_MAX], new[PATH_MAX];
123 PHYSFSX_getRealPath(oldpath, old);
124 PHYSFSX_getRealPath(newpath, new);
125 return (rename(old, new) == 0);
129 // returns -1 if error
130 // Gets bytes free in current write dir
131 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
133 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
136 if (!statfs(PHYSFS_getWriteDir(), &sfs))
137 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
145 #endif /* PHYSFSX_H */