]> icculus.org git repositories - btb/d2x.git/blob - include/physfsx.h
implement removeext, use it in dsload instead of splitpath
[btb/d2x.git] / include / physfsx.h
1 /* $Id: physfsx.h,v 1.7 2004-12-05 10:56:32 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         {
87                 realDir = PHYSFS_getWriteDir();
88                 if (!realDir)
89                         return 0;
90         }
91
92         strncpy(realPath, realDir, PATH_MAX - 1);
93         if (strlen(realPath) >= strlen(sep))
94         {
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));
98         }
99
100         if (strlen(stdPath) >= 1)
101                 if (*stdPath == '/')
102                         stdPath++;
103
104         while (*stdPath)
105         {
106                 if (*stdPath == '/')
107                         strncat(realPath, sep, PATH_MAX - 1 - strlen(realPath));
108                 else
109                 {
110                         if (strlen(realPath) < PATH_MAX - 2)
111                         {
112                                 p = realPath + strlen(realPath);
113                                 p[0] = *stdPath;
114                                 p[1] = '\0';
115                         }
116                 }
117                 stdPath++;
118         }
119
120         return 1;
121 }
122
123 static inline int PHYSFSX_rename(char *oldpath, char *newpath)
124 {
125         char old[PATH_MAX], new[PATH_MAX];
126
127         PHYSFSX_getRealPath(oldpath, old);
128         PHYSFSX_getRealPath(newpath, new);
129         return (rename(old, new) == 0);
130 }
131
132
133 // returns -1 if error
134 // Gets bytes free in current write dir
135 static inline PHYSFS_sint64 PHYSFSX_getFreeDiskSpace()
136 {
137 #if defined(__linux__) || (defined(__MACH__) && defined(__APPLE__))
138         struct statfs sfs;
139
140         if (!statfs(PHYSFS_getWriteDir(), &sfs))
141                 return (PHYSFS_sint64)(sfs.f_bavail * sfs.f_bsize);
142
143         return -1;
144 #else
145         return 0x7FFFFFFF;
146 #endif
147 }
148
149 #endif /* PHYSFSX_H */