]> icculus.org git repositories - btb/d2x.git/blob - include/physfsx.h
merged physfs branch
[btb/d2x.git] / include / physfsx.h
1 /* $Id: physfsx.h,v 1.2 2004-12-01 12:48:13 btb Exp $ */
2
3 /*
4  *
5  * Some simple physfs extensions
6  *
7  */
8
9 #ifndef PHYSFSX_H
10 #define PHYSFSX_H
11
12 #ifndef macintosh
13 #include <sys/param.h>
14 #endif
15 #if defined(__linux__)
16 #include <sys/vfs.h>
17 #elif defined(__MACH__) && defined(__APPLE__)
18 #include <sys/mount.h>
19 #endif
20 #include <string.h>
21
22 #include <physfs.h>
23
24 static inline int PHYSFSX_readString(PHYSFS_file *file, char *s)
25 {
26         char *ptr = s;
27
28         if (PHYSFS_eof(file))
29                 *ptr = 0;
30         else
31                 do
32                         PHYSFS_read(file, ptr, 1, 1);
33                 while (!PHYSFS_eof(file) && *ptr++ != 0);
34
35         return strlen(s);
36 }
37
38 static inline int PHYSFSX_gets(PHYSFS_file *file, char *s)
39 {
40         char *ptr = s;
41
42         if (PHYSFS_eof(file))
43                 *ptr = 0;
44         else
45                 do
46                         PHYSFS_read(file, ptr, 1, 1);
47                 while (!PHYSFS_eof(file) && *ptr++ != '\n');
48
49         return strlen(s);
50 }
51
52 static inline int PHYSFSX_writeU8(PHYSFS_file *file, PHYSFS_uint8 val)
53 {
54         return PHYSFS_write(file, &val, 1, 1);
55 }
56
57 static inline int PHYSFSX_writeString(PHYSFS_file *file, char *s)
58 {
59         return PHYSFS_write(file, s, 1, strlen(s) + 1);
60 }
61
62 static inline int PHYSFSX_puts(PHYSFS_file *file, char *s)
63 {
64         return PHYSFS_write(file, s, 1, strlen(s));
65 }
66
67 static inline int PHYSFSX_putc(PHYSFS_file *file, int c)
68 {
69         unsigned char ch = (unsigned char)c;
70
71         if (PHYSFS_write(file, &ch, 1, 1) < 1)
72                 return -1;
73         else
74                 return (int)c;
75 }
76
77 static inline int PHYSFSX_getRealPath(char *stdPath, char *realPath)
78 {
79         const char *realDir = PHYSFS_getRealDir(stdPath);
80         char *p;
81         char sep = *PHYSFS_getDirSeparator();
82
83         if (!realDir)
84                 return 0;
85         
86         strncpy(realPath, realDir, PATH_MAX);   // some compilers (like MPW) don't have snprintf
87         p = realPath + strlen(realPath);
88         strncat(realPath, stdPath, PATH_MAX - strlen(realPath));
89         while ((p = strchr(p, '/')))
90                 *p++ = sep;
91
92         return 1;
93 }
94
95 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
96 {
97         char old[PATH_MAX], new[PATH_MAX];
98
99         PHYSFSX_getRealPath(oldpath, old);
100         PHYSFSX_getRealPath(newpath, new);
101         return (rename(old, new) == 0);
102 }
103
104
105 // returns -1 if error
106 // Gets bytes free in current write dir
107 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
108 {
109 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
110         struct statfs sfs;
111
112         if (!statfs(PHYSFS_getWriteDir(), &sfs))
113                 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
114
115         return -1;
116 #else
117         return 0x7FFFFFFF;
118 #endif
119 }
120
121 #endif /* PHYSFSX_H */