forked from bkaradzic/bx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilepath.cpp
407 lines (339 loc) · 7.03 KB
/
filepath.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright 2010-2022 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx/blob/master/LICENSE
*/
#include <bx/file.h>
#include <bx/os.h>
#include <bx/readerwriter.h>
#if !BX_CRT_NONE
# if BX_CRT_MSVC
# include <direct.h> // _getcwd
# else
# include <unistd.h> // getcwd
# endif // BX_CRT_MSVC
#endif // !BX_CRT_NONE
#if BX_PLATFORM_WINDOWS
extern "C" __declspec(dllimport) unsigned long __stdcall GetTempPathA(unsigned long _max, char* _ptr);
#endif // BX_PLATFORM_WINDOWS
namespace bx
{
static bool isPathSeparator(char _ch)
{
return false
|| '/' == _ch
|| '\\' == _ch
;
}
static int32_t normalizeFilePath(char* _dst, int32_t _dstSize, const char* _src, int32_t _num)
{
// Reference(s):
// - Lexical File Names in Plan 9 or Getting Dot-Dot Right
// https://web.archive.org/web/20180629044444/https://9p.io/sys/doc/lexnames.html
const int32_t num = strLen(_src, _num);
if (0 == num)
{
return strCopy(_dst, _dstSize, ".");
}
int32_t size = 0;
StaticMemoryBlockWriter writer(_dst, _dstSize);
Error err;
int32_t idx = 0;
int32_t dotdot = 0;
if (2 <= num
&& ':' == _src[1])
{
size += write(&writer, toUpper(_src[idx]), &err);
size += write(&writer, ':', &err);
idx += 2;
dotdot = size;
}
const int32_t slashIdx = idx;
bool rooted = isPathSeparator(_src[idx]);
if (rooted)
{
size += write(&writer, '/', &err);
++idx;
dotdot = size;
}
bool trailingSlash = false;
while (idx < num && err.isOk() )
{
switch (_src[idx])
{
case '/':
case '\\':
++idx;
trailingSlash = idx == num;
break;
case '.':
if (idx+1 == num
|| isPathSeparator(_src[idx+1]) )
{
++idx;
break;
}
if ('.' == _src[idx+1]
&& (idx+2 == num || isPathSeparator(_src[idx+2]) ) )
{
idx += 2;
if (dotdot < size)
{
for (--size
; dotdot < size && !isPathSeparator(_dst[size])
; --size)
{
}
seek(&writer, size, Whence::Begin);
}
else if (!rooted)
{
if (0 < size)
{
size += write(&writer, '/', &err);
}
size += write(&writer, "..", &err);
dotdot = size;
}
break;
}
BX_FALLTHROUGH;
default:
if ( ( rooted && slashIdx+1 != size)
|| (!rooted && 0 != size) )
{
size += write(&writer, '/', &err);
}
for (; idx < num && !isPathSeparator(_src[idx]); ++idx)
{
size += write(&writer, _src[idx], &err);
}
break;
}
}
if (0 == size)
{
size += write(&writer, '.', &err);
}
if (trailingSlash)
{
size += write(&writer, '/', &err);
}
write(&writer, '\0', &err);
return size;
}
static bool getEnv(char* _out, uint32_t* _inOutSize, const StringView& _name, FileType::Enum _type)
{
uint32_t len = *_inOutSize;
*_out = '\0';
if (getEnv(_out, &len, _name) )
{
FileInfo fi;
if (stat(fi, _out)
&& _type == fi.type)
{
*_inOutSize = len;
return true;
}
}
return false;
}
static char* pwd(char* _buffer, uint32_t _size)
{
#if BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT \
|| BX_CRT_NONE
BX_UNUSED(_buffer, _size);
return NULL;
#elif BX_CRT_MSVC
return ::_getcwd(_buffer, (int32_t)_size);
#else
return ::getcwd(_buffer, _size);
#endif // BX_COMPILER_
}
static bool getCurrentPath(char* _out, uint32_t* _inOutSize)
{
uint32_t len = *_inOutSize;
if (NULL != pwd(_out, len))
{
*_inOutSize = strLen(_out);
return true;
}
return false;
}
static bool getHomePath(char* _out, uint32_t* _inOutSize)
{
return false
#if BX_PLATFORM_WINDOWS
|| getEnv(_out, _inOutSize, "USERPROFILE", FileType::Dir)
#endif // BX_PLATFORM_WINDOWS
|| getEnv(_out, _inOutSize, "HOME", FileType::Dir)
;
}
static bool getTempPath(char* _out, uint32_t* _inOutSize)
{
#if BX_PLATFORM_WINDOWS
uint32_t len = ::GetTempPathA(*_inOutSize, _out);
bool result = len != 0 && len < *_inOutSize;
*_inOutSize = len;
return result;
#else
static const StringView s_tmp[] =
{
"TMPDIR",
"TMP",
"TEMP",
"TEMPDIR",
""
};
for (const StringView* tmp = s_tmp; !tmp->isEmpty(); ++tmp)
{
uint32_t len = *_inOutSize;
*_out = '\0';
bool ok = getEnv(_out, &len, *tmp, FileType::Dir);
if (ok
&& len != 0
&& len < *_inOutSize)
{
*_inOutSize = len;
return ok;
}
}
FileInfo fi;
if (stat(fi, "/tmp")
&& FileType::Dir == fi.type)
{
strCopy(_out, *_inOutSize, "/tmp");
*_inOutSize = 4;
return true;
}
return false;
#endif // BX_PLATFORM_*
}
FilePath::FilePath()
{
set("");
}
FilePath::FilePath(Dir::Enum _dir)
{
set(_dir);
}
FilePath::FilePath(const char* _rhs)
{
set(_rhs);
}
FilePath::FilePath(const StringView& _filePath)
{
set(_filePath);
}
FilePath& FilePath::operator=(const StringView& _rhs)
{
set(_rhs);
return *this;
}
void FilePath::clear()
{
if (!isEmpty() )
{
set("");
}
}
void FilePath::set(Dir::Enum _dir)
{
char tmp[kMaxFilePath];
uint32_t len = BX_COUNTOF(tmp);
switch (_dir)
{
case Dir::Current:
getCurrentPath(tmp, &len);
break;
case Dir::Temp:
getTempPath(tmp, &len);
break;
case Dir::Home:
getHomePath(tmp, &len);
break;
default:
len = 0;
break;
}
set(StringView(tmp, len) );
}
void FilePath::set(const StringView& _filePath)
{
normalizeFilePath(
m_filePath
, BX_COUNTOF(m_filePath)
, _filePath.getPtr()
, _filePath.getLength()
);
}
void FilePath::join(const StringView& _str)
{
char tmp[kMaxFilePath];
strCopy(tmp, BX_COUNTOF(tmp), m_filePath);
strCat(tmp, BX_COUNTOF(tmp), "/");
strCat(tmp, BX_COUNTOF(tmp), _str);
set(tmp);
}
FilePath::operator StringView() const
{
return StringView(m_filePath, strLen(m_filePath) );
}
const char* FilePath::getCPtr() const
{
return m_filePath;
}
StringView FilePath::getPath() const
{
StringView end = strRFind(m_filePath, '/');
if (!end.isEmpty() )
{
return StringView(m_filePath, end.getPtr()+1);
}
return StringView();
}
StringView FilePath::getFileName() const
{
StringView fileName = strRFind(m_filePath, '/');
if (!fileName.isEmpty() )
{
return StringView(fileName.getPtr()+1);
}
return getCPtr();
}
StringView FilePath::getBaseName() const
{
const StringView fileName = getFileName();
if (!fileName.isEmpty() )
{
StringView ext = strFind(fileName, '.');
if (!ext.isEmpty() )
{
return StringView(fileName.getPtr(), ext.getPtr() );
}
return fileName;
}
return StringView();
}
StringView FilePath::getExt() const
{
const StringView fileName = getFileName();
if (!fileName.isEmpty() )
{
const StringView dot = strFind(fileName, '.');
return StringView(dot.getPtr(), fileName.getTerm() );
}
return StringView();
}
bool FilePath::isAbsolute() const
{
return '/' == m_filePath[0] // no drive letter
|| (':' == m_filePath[1] && '/' == m_filePath[2]) // with drive letter
;
}
bool FilePath::isEmpty() const
{
return 0 == strCmp(m_filePath, ".");
}
} // namespace bx