forked from spoutn1k/mcmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesystem.cpp
276 lines (262 loc) · 6.35 KB
/
filesystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "filesystem.h"
#ifdef MSVCP
#include <direct.h>
// See http://en.wikipedia.org/wiki/Stdint.h#External_links
#include <stdint.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#endif
#include <cstdarg>
#include <cstdlib>
#include <cstring>
#define CCEND ((char*)0)
#ifdef MSVCP
// For UTF8 conversion
#define LOWER_6_BIT(u) ((u) & 0x003f)
#define LOWER_7_BIT(u) ((u) & 0x007f)
#define BIT7(a) ((a) & 0x80)
#define BIT6(a) ((a) & 0x40)
#define BIT5(a) ((a) & 0x20)
#define BIT54(a) ((a) & 0x30)
#define BIT543(a) ((a) & 0x38)
#define BIT2(a) ((a) & 0x04)
#define BIT1(a) ((a) & 0x02)
#define BIT0(a) ((a) & 0x01)
// Return: -1 = at least one nullpointer, 1 = success, 0 = outbuffer too small
static int Utf8ToWideChar(char *pUTF8, size_t cchSrc, wchar_t *pWCHAR, size_t cchDest, bool *isvalid)
{
if (!pUTF8 || !pWCHAR) {
return -1; // Valid pointers?
}
int nTB = 0; // Number of bytes left for current char
wchar_t *pDestEnd = pWCHAR + cchDest;
char *pSrcEnd = pUTF8 + cchSrc;
char UTF8;
if (isvalid != NULL) {
*isvalid = true;
}
while ((pUTF8 < pSrcEnd) && (pWCHAR < pDestEnd)) {
if (BIT7(*pUTF8) == 0) { // normal ASCII
if (nTB) { // last mulibyte char not complete, insert '?'
nTB = 0;
*pWCHAR++ = 63;
if (isvalid != NULL) {
*isvalid = false;
}
} else { // just convert
*pWCHAR++ = (wchar_t)*pUTF8++;
}
} else if (BIT6(*pUTF8) == 0) { // utf8 sequence byte (not first)
if (nTB != 0) {
*pWCHAR <<= 6;
*pWCHAR |= LOWER_6_BIT(*pUTF8);
if (--nTB == 0) {
++pWCHAR;
}
} else { // No more trailing bytes expected, insert '?'
*pWCHAR++ = 63;
if (isvalid != NULL) {
*isvalid = false;
}
}
++pUTF8;
} else { // No ASCII and no trailing byte
if (nTB) { // but last char was multibyte and not complete yet, insert '?'
nTB = 0;
*pWCHAR++ = 63;
if (isvalid != NULL) {
*isvalid = false;
}
} else { // OK, check how many bytes
UTF8 = *pUTF8;
while (BIT7(UTF8) != 0) { // count number of bytes for this char
UTF8 <<= 1;
nTB++;
}
if (nTB > 4) { // too long, utf8 specs allow only up to 4 bytes per char
nTB = 0;
*pWCHAR++ = 63; // time for a '?'
if (isvalid != NULL) {
*isvalid = false;
}
} else { // just shift bits back and assign
*pWCHAR = UTF8 >> nTB--;
}
}
++pUTF8;
}
}
if (nTB != 0 && isvalid != NULL) {
*isvalid = false;
}
if (pWCHAR < pDestEnd) {
*pWCHAR = 0;
return 1;
}
*(pWCHAR-1) = 0;
return 0;
}
// Return: -1 = at least one nullpointer, 1 = success, 0 = outbuffer too small
template <class T>
int WideCharToUtf8(T pWCHAR, size_t cchSrc, uint8_t *pUTF8, size_t cchDest)
{
if (!pUTF8 || !pWCHAR) {
return -1; // Valid pointers?
}
uint8_t *pDestEnd = pUTF8 + cchDest;
T pSrcEnd = pWCHAR + cchSrc;
uint8_t UTF8[4];
while ((pWCHAR < pSrcEnd) && (pUTF8 < pDestEnd)) {
if (LOWER_7_BIT(*pWCHAR) == *pWCHAR) { // normal ASCII
*pUTF8++ = (uint8_t)*pWCHAR++;
} else { // utf8 encode!
int i;
for (i = 0; i < 4; ++i) {
UTF8[i] = LOWER_6_BIT(*pWCHAR) | 0x80;
*pWCHAR >>= 6;
if (*pWCHAR == 0) {
break;
}
}
bool exp = false;
if (i == 1 && BIT5(UTF8[1])) {
exp = true;
} else if (i == 2 && BIT54(UTF8[2])) {
exp = true;
} else if (i == 3 && BIT543(UTF8[3])) {
exp = true;
}
if (exp) {
++i;
UTF8[i] = (0xff) << (7 - i);
} else if (i == 1) {
UTF8[1] |= 0xc0;
} else if (i == 2) {
UTF8[2] |= 0xe0;
} else if (i == 3) {
UTF8[3] |= 0xf0;
}
do {
*pUTF8++ = UTF8[i];
if (pUTF8 >= pDestEnd) {
*(pUTF8-1) = '\0';
return 0;
}
} while (i-- > 0);
++pWCHAR;
}
}
if (pUTF8 >= pDestEnd) {
*(pUTF8-1) = '\0';
return 0;
}
*pUTF8 = 0;
return 1;
}
#endif
static size_t concat(char *buffer, const size_t len, char *source, ...)
{
if (len <= 0) {
return 0;
}
va_list parg;
size_t count = 0;
va_start(parg, source);
if (source != CCEND) do {
while (*source != 0) {
*buffer++ = *source++;
if (++count >= len) {
*(buffer-1) = 0;
va_end(parg); /* End variable argument process */
return count;
}
}
} while((source = va_arg(parg, char *)) != CCEND);
va_end(parg); /* End variable argument process */
*buffer = 0;
return count;
}
namespace Dir
{
DIRHANDLE open(char *path, myFile &file)
{
if (path == NULL) {
return NULL;
}
#ifdef MSVCP
char buffer[1000];
wchar_t wbuffer[1000];
_WIN32_FIND_DATAW ffd;
concat(buffer, 1000, path, "/*", CCEND);
bool b;
Utf8ToWideChar(buffer, strlen(buffer), wbuffer, 1000, &b);
HANDLE h = FindFirstFileW(wbuffer, &ffd);
if (h == INVALID_HANDLE_VALUE) {
return NULL;
}
WideCharToUtf8(ffd.cFileName, wcslen(ffd.cFileName), (uint8_t *)file.name, sizeof(file.name));
file.isdir = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
file.size = ffd.nFileSizeLow;
#else
DIR *h = opendir(path);
if (h == NULL) {
return NULL;
}
dirent *dirp = readdir(h);
if (dirp == NULL) {
closedir(h);
return NULL;
}
char buffer[1000];
concat(buffer, 1000, path, "/", dirp->d_name, CCEND);
struct stat stDirInfo;
if (stat(buffer, &stDirInfo) < 0) {
closedir(h);
return NULL;
}
strncpy(file.name, dirp->d_name, sizeof(file.name));
file.isdir = S_ISDIR(stDirInfo.st_mode);
file.size = stDirInfo.st_size;
#endif
return h;
}
bool next(DIRHANDLE handle, char *path, myFile &file)
{
#ifdef MSVCP
_WIN32_FIND_DATAW ffd;
bool ret = FindNextFileW(handle, &ffd) == TRUE;
if (!ret) {
return false;
}
WideCharToUtf8(ffd.cFileName, wcslen(ffd.cFileName), (uint8_t *)file.name, sizeof(file.name));
file.isdir = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
file.size = ffd.nFileSizeLow;
#else
dirent *dirp = readdir(handle);
if (dirp == NULL) {
return false;
}
char buffer[1000];
concat(buffer, 1000, path, "/", dirp->d_name, CCEND);
struct stat stDirInfo;
if (stat(buffer, &stDirInfo) < 0) {
return false;
}
strncpy(file.name, dirp->d_name, sizeof(file.name));
file.isdir = S_ISDIR(stDirInfo.st_mode);
file.size = stDirInfo.st_size;
#endif
return true;
}
void close(DIRHANDLE handle)
{
#ifdef MSVCP
FindClose(handle);
#else
closedir(handle);
#endif
}
}