]> icculus.org git repositories - btb/d2x.git/blob - include/physfsx.h
use PATH_MAX for hog pathname, ensure correct translation of path separators
[btb/d2x.git] / include / physfsx.h
1 /* $Id: physfsx.h,v 1.6 2004-12-04 04:07:16 btb Exp $ */
2
3 /*
4  *
5  * Some simple physfs extensions
6  *
7  */
8
9 #ifndef PHYSFSX_H
10 #define PHYSFSX_H
11
12 #if !defined(macintosh) && !defined(_MSC_VER)
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 #include "error.h"
25
26 static inline int PHYSFSX_readString(PHYSFS_file *file, char *s)
27 {
28         char *ptr = s;
29
30         if (PHYSFS_eof(file))
31                 *ptr = 0;
32         else
33                 do
34                         PHYSFS_read(file, ptr, 1, 1);
35                 while (!PHYSFS_eof(file) && *ptr++ != 0);
36
37         return strlen(s);
38 }
39
40 static inline int PHYSFSX_gets(PHYSFS_file *file, char *s)
41 {
42         char *ptr = s;
43
44         if (PHYSFS_eof(file))
45                 *ptr = 0;
46         else
47                 do
48                         PHYSFS_read(file, ptr, 1, 1);
49                 while (!PHYSFS_eof(file) && *ptr++ != '\n');
50
51         return strlen(s);
52 }
53
54 static inline int PHYSFSX_writeU8(PHYSFS_file *file, PHYSFS_uint8 val)
55 {
56         return PHYSFS_write(file, &val, 1, 1);
57 }
58
59 static inline int PHYSFSX_writeString(PHYSFS_file *file, char *s)
60 {
61         return PHYSFS_write(file, s, 1, strlen(s) + 1);
62 }
63
64 static inline int PHYSFSX_puts(PHYSFS_file *file, char *s)
65 {
66         return PHYSFS_write(file, s, 1, strlen(s));
67 }
68
69 static inline int PHYSFSX_putc(PHYSFS_file *file, int c)
70 {
71         unsigned char ch = (unsigned char)c;
72
73         if (PHYSFS_write(file, &ch, 1, 1) < 1)
74                 return -1;
75         else
76                 return (int)c;
77 }
78
79 static inline int PHYSFSX_getRealPath(const char *stdPath, char *realPath)
80 {
81         const char *realDir = PHYSFS_getRealDir(stdPath);
82         const char *sep = PHYSFS_getDirSeparator();
83         char *p;
84
85         if (!realDir)
86                 return 0;
87
88         strncpy(realPath, realDir, PATH_MAX - 1);
89         if (strlen(realPath) >= strlen(sep))
90         {
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));
94         }
95
96         if (strlen(stdPath) >= 1)
97                 if (*stdPath == '/')
98                         stdPath++;
99
100         while (*stdPath)
101         {
102                 if (*stdPath == '/')
103                         strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
104                 else
105                 {
106                         if (strlen(realPath) < PATH_MAX - 2)
107                         {
108                                 p = realPath + strlen(realPath);
109                                 p[0] = *stdPath;
110                                 p[1] = '\0';
111                         }
112                 }
113                 stdPath++;
114         }
115
116         return 1;
117 }
118
119 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
120 {
121         char old[PATH_MAX], new[PATH_MAX];
122
123         PHYSFSX_getRealPath(oldpath, old);
124         PHYSFSX_getRealPath(newpath, new);
125         return (rename(old, new) == 0);
126 }
127
128
129 // returns -1 if error
130 // Gets bytes free in current write dir
131 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
132 {
133 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
134         struct statfs sfs;
135
136         if (!statfs(PHYSFS_getWriteDir(), &sfs))
137                 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
138
139         return -1;
140 #else
141         return 0x7FFFFFFF;
142 #endif
143 }
144
145 #endif /* PHYSFSX_H */