FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Fossilize/path.cpp at master · ValveSoftware/Fossilize · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ValveSoftware
/
Fossilize
Public
Notifications
You must be signed in to change notification settings
Fork
77
Star
769
Code
Issues
36
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Fossilize
/
path.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
434 lines (376 loc) · 9.34 KB
Breadcrumbs
Fossilize
/
path.cpp
Copy path
File metadata and controls
434 lines (376 loc) · 9.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
Copyright (c) 2017-2019 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#
include
"
path.hpp
"
#
include
<
algorithm
>
#
include
<
sstream
>
#
ifdef
_WIN32
#
define
WIN32_LEAN_AND_MEAN
#
include
<
windows.h
>
#
include
<
sys/stat.h
>
#
else
#
include
<
unistd.h
>
#
include
<
sys/stat.h
>
#
include
<
errno.h
>
#
include
<
fcntl.h
>
#
ifdef
__linux__
#
include
<
linux/limits.h
>
#
endif
#
endif
using
namespace
std
;
namespace
Util
{
namespace
inner
{
template
<
typename
T>
void
join_helper
(std::ostringstream &stream, T &&t)
{
stream << std::forward<T>(t);
}
template
<
typename
T,
typename
... Ts>
void
join_helper
(std::ostringstream &stream, T &&t, Ts &&... ts)
{
stream << std::forward<T>(t);
join_helper
(stream, std::forward<Ts>(ts)...);
}
}
template
<
typename
... Ts>
std::string
join
(Ts &&... ts)
{
std::ostringstream stream;
inner::join_helper
(stream, std::forward<Ts>(ts)...);
return
stream.
str
();
}
}
namespace
Path
{
static
vector<string>
split
(
const
string &str,
const
char
*delim,
bool
allow_empty)
{
if
(str.
empty
())
return
{};
vector<string> ret;
size_t
start_index =
0
;
size_t
index =
0
;
while
((index = str.
find_first_of
(delim, start_index)) != string::npos)
{
if
(allow_empty || index > start_index)
ret.
push_back
(str.
substr
(start_index, index - start_index));
start_index = index +
1
;
if
(allow_empty && (index == str.
size
() -
1
))
ret.
emplace_back
();
}
if
(start_index < str.
size
())
ret.
push_back
(str.
substr
(start_index));
return
ret;
}
vector<string>
split
(
const
string &str,
const
char
*delim)
{
return
split
(str, delim,
true
);
}
vector<string>
split_no_empty
(
const
string &str,
const
char
*delim)
{
return
split
(str, delim,
false
);
}
string
strip_whitespace
(
const
string &str)
{
string ret;
auto
index = str.
find_first_not_of
(
"
\t
"
);
if
(index == string::npos)
return
"
"
;
ret = str.
substr
(index, string::npos);
index = ret.
find_last_not_of
(
"
\t
"
);
if
(index != string::npos)
return
ret.
substr
(
0
, index +
1
);
else
return
ret;
}
string
enforce_protocol
(
const
string &path)
{
if
(path.
empty
())
return
"
"
;
auto
index = path.
find
(
"
://
"
);
if
(index == string::npos)
return
string
(
"
file://
"
) + path;
else
return
path;
}
string
canonicalize_path
(
const
string &path)
{
string transformed;
transformed.
resize
(path.
size
());
transform
(
begin
(path),
end
(path),
begin
(transformed), [](
char
c) ->
char
{
return
c ==
'
\\
'
?
'
/
'
: c; });
auto
data =
split_no_empty
(transformed,
"
/
"
);
vector<string> result;
for
(
auto
&i : data)
{
if
(i ==
"
..
"
)
{
if
(!result.
empty
())
result.
pop_back
();
}
else
result.
push_back
(
std::move
(i));
}
string res;
for
(
auto
&i : result)
{
if
(&i != result.
data
())
res +=
"
/
"
;
res += i;
}
return
res;
}
static
size_t
find_last_slash
(
const
string &str)
{
#
ifdef
_WIN32
auto
index = str.
find_last_of
(
"
/
\\
"
);
#
else
auto
index = str.
find_last_of
(
'
/
'
);
#
endif
return
index;
}
bool
is_abspath
(
const
string &path)
{
if
(path.
empty
())
return
false
;
if
(path.
front
() ==
'
/
'
)
return
true
;
#
ifdef
_WIN32
{
auto
index = (std::min)(path.
find
(
"
:/
"
), path.
find
(
"
:
\\
"
));
if
(index != string::npos)
return
true
;
}
#
endif
return
path.
find
(
"
://
"
) != string::npos;
}
bool
is_root_path
(
const
string &path)
{
if
(path.
empty
())
return
false
;
if
(path.
front
() ==
'
/
'
&& path.
size
() ==
1
)
return
true
;
#
ifdef
_WIN32
{
auto
index = (std::min)(path.
find
(
"
:/
"
), path.
find
(
"
:
\\
"
));
if
(index != string::npos && (index +
2
) == path.
size
())
return
true
;
}
#
endif
auto
index = path.
find
(
"
://
"
);
return
index != string::npos && (index +
3
) == path.
size
();
}
string
join
(
const
string &base,
const
string &path)
{
if
(base.
empty
())
return
path;
if
(path.
empty
())
return
base;
if
(
is_abspath
(path))
return
path;
auto
index =
find_last_slash
(base);
bool
need_slash = index != base.
size
() -
1
;
return
Util::join
(base, need_slash ?
"
/
"
:
"
"
, path);
}
string
basedir
(
const
string &path)
{
if
(path.
empty
())
return
"
"
;
if
(
is_root_path
(path))
return
path;
auto
index =
find_last_slash
(path);
if
(index == string::npos)
return
"
.
"
;
//
Preserve the first slash.
if
(index ==
0
&&
is_abspath
(path))
index++;
auto
ret = path.
substr
(
0
, index +
1
);
if
(!
is_root_path
(ret))
ret.
pop_back
();
return
ret;
}
string
basename
(
const
string &path)
{
if
(path.
empty
())
return
"
"
;
auto
index =
find_last_slash
(path);
if
(index == string::npos)
return
path;
auto
base = path.
substr
(index +
1
, string::npos);
return
base;
}
string
relpath
(
const
string &base,
const
string &path)
{
return
Path::join
(
basedir
(base), path);
}
string
ext
(
const
string &path)
{
auto
index = path.
find_last_of
(
'
.
'
);
if
(index == string::npos)
return
"
"
;
else
return
path.
substr
(index +
1
, string::npos);
}
pair<string, string>
split
(
const
string &path)
{
if
(path.
empty
())
return
make_pair
(
string
(
"
.
"
),
string
(
"
.
"
));
auto
index =
find_last_slash
(path);
if
(index == string::npos)
return
make_pair
(
string
(
"
.
"
), path);
auto
base = path.
substr
(index +
1
, string::npos);
return
make_pair
(path.
substr
(
0
, index), base);
}
pair<string, string>
protocol_split
(
const
string &path)
{
if
(path.
empty
())
return
make_pair
(
string
(
"
"
),
string
(
"
"
));
auto
index = path.
find
(
"
://
"
);
if
(index == string::npos)
return
make_pair
(
string
(
"
"
), path);
return
make_pair
(path.
substr
(
0
, index), path.
substr
(index +
3
, string::npos));
}
string
get_executable_path
()
{
#
ifdef
_WIN32
char
target[
4096
];
DWORD
ret =
GetModuleFileNameA
(
GetModuleHandle
(
nullptr
), target,
sizeof
(target));
target[ret] =
'
\0
'
;
return
canonicalize_path
(
string
(target));
#
else
static
const
char
*exts[] = {
"
exe
"
,
"
file
"
,
"
a.out
"
};
char
link_path[
PATH_MAX
];
char
target[
PATH_MAX
];
for
(
auto
*ext : exts)
{
snprintf
(link_path,
sizeof
(link_path),
"
/proc/self/%s
"
, ext);
ssize_t
ret =
readlink
(link_path, target,
sizeof
(target) -
1
);
if
(ret >=
0
)
{
target[ret] =
'
\0
'
;
return
string
(target);
}
}
return
"
"
;
#
endif
}
bool
mkdir
(
const
std::string &path)
{
#
ifdef
_WIN32
if
(!
CreateDirectoryA
(path.
c_str
(),
nullptr
))
{
if
(
GetLastError
() !=
ERROR_ALREADY_EXISTS
)
return
false
;
if
(!
is_directory
(path))
return
false
;
}
return
true
;
#
else
int
ret = ::
mkdir
(path.
c_str
(),
0750
);
if
(ret ==
0
)
return
true
;
if
(ret <
0
&& errno !=
EEXIST
)
return
false
;
//
Verify the path is actually a directory.
struct
stat
s = {};
return
::
stat
(path.
c_str
(), &s) ==
0
&& (s.
st_mode
&
S_IFDIR
) !=
0
;
#
endif
}
bool
touch
(
const
std::string &path)
{
#
ifdef
_WIN32
HANDLE
h =
CreateFileA
(path.
c_str
(),
GENERIC_WRITE
,
FILE_SHARE_WRITE
|
FILE_SHARE_READ
,
nullptr
,
CREATE_ALWAYS
,
FILE_ATTRIBUTE_NORMAL
,
INVALID_HANDLE_VALUE
);
if
(h ==
INVALID_HANDLE_VALUE
)
return
false
;
SYSTEMTIME
st;
FILETIME
ft;
GetSystemTime
(&st);
SystemTimeToFileTime
(&st, &ft);
bool
success =
SetFileTime
(h,
nullptr
,
nullptr
, &ft) !=
0
;
CloseHandle
(h);
return
success;
#
else
int
fd = ::
open
(path.
c_str
(),
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
0640
);
if
(fd <
0
)
return
false
;
const
struct
timespec
times[
2
] = {{
0
,
UTIME_NOW
}, {
0
,
UTIME_NOW
}};
bool
ret = ::
futimens
(fd, times) ==
0
;
::close
(fd);
return
ret;
#
endif
}
bool
is_file
(
const
std::string &path)
{
#
ifdef
_WIN32
struct
__stat64
s;
if
(
_stat64
(path.
c_str
(), &s) <
0
)
return
false
;
return
(s.
st_mode
& _S_IFREG) !=
0
;
#
else
struct
stat
s = {};
return
::
stat
(path.
c_str
(), &s) ==
0
&& (s.
st_mode
&
S_IFREG
) !=
0
;
#
endif
}
bool
is_directory
(
const
std::string &path)
{
#
ifdef
_WIN32
struct
__stat64
s;
if
(
_stat64
(path.
c_str
(), &s) <
0
)
return
false
;
return
(s.
st_mode
& _S_IFDIR) !=
0
;
#
else
struct
stat
s = {};
return
::
stat
(path.
c_str
(), &s) ==
0
&& (s.
st_mode
&
S_IFDIR
) !=
0
;
#
endif
}
bool
get_mtime_us
(
const
std::string &path,
uint64_t
&mtime_us)
{
#
ifdef
_WIN32
HANDLE
h =
CreateFileA
(path.
c_str
(),
GENERIC_READ
,
FILE_SHARE_READ
,
nullptr
,
OPEN_EXISTING
,
FILE_ATTRIBUTE_NORMAL
,
INVALID_HANDLE_VALUE
);
if
(h ==
INVALID_HANDLE_VALUE
)
return
false
;
FILETIME
ft;
if
(!
GetFileTime
(h,
nullptr
,
nullptr
, &ft))
{
CloseHandle
(h);
return
false
;
}
mtime_us = ((
uint64_t
(ft.
dwHighDateTime
) <<
32
) |
uint64_t
(ft.
dwLowDateTime
)) /
10
;
CloseHandle
(h);
return
true
;
#
else
struct
stat
s = {};
if
(::
stat
(path.
c_str
(), &s) !=
0
)
return
false
;
#
if
defined(__linux__)
mtime_us = s.
st_mtim
.
tv_sec
*
1000000ll
+ s.
st_mtim
.
tv_nsec
/
1000
;
#
else
//
Fallback.
mtime_us = s.
st_mtime
*
1000000ll
;
#
endif
return
true
;
#
endif
}
}
Back
|
FazBrowse Home
|
New Git URL