-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstd_fs
More file actions
241 lines (190 loc) · 5.09 KB
/
std_fs
File metadata and controls
241 lines (190 loc) · 5.09 KB
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
#pragma once
//------------------------------------------------------------------------------
#if __has_include(<filesystem>)
#include <filesystem>
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
#else
#error Compiler does not have <filesystem> or <experimental/filesystem>
#endif
//------------------------------------------------------------------------------
#if defined(__GLIBCXX__)
#define STDFS_IS_LIBSTDCPP
#endif
#if defined(STDFWD_IS_LIBSTDCPP)
#define STDFS_NAMESPACE_VISIBILITY _GLIBCXX_VISIBILITY(default)
#else
#define STDFS_NAMESPACE_VISIBILITY
#endif
//------------------------------------------------------------------------------
#if __has_include(<filesystem>)
#ifdef __GNUC__
#if ( __GNUC__ == 8 )
#define STDFS_USE_ALTERNATIVE_COMAPRE
#endif
#endif
#else
#define STDFS_USE_ALTERNATIVE_COMAPRE
#endif
//------------------------------------------------------------------------------
namespace stdfs STDFS_NAMESPACE_VISIBILITY
{
#if __has_include(<filesystem>)
namespace fs_real = std::filesystem;
#elif __has_include(<experimental/filesystem>)
namespace fs_real = std::experimental::filesystem;
#endif
using path = fs_real::path;
using directory_iterator = fs_real::directory_iterator;
#ifndef _MSC_VER
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
static decltype( auto ) hash_value = &fs_real::hash_value;
static bool (*exists)( const path&, std::error_code & ) = &fs_real::exists;
static bool (*is_directory)( const path& ) = &fs_real::is_directory;
static path (*current_path)() = &fs_real::current_path;
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif
#if __has_include(<filesystem>)
static path lexically_relative( const path & _path, const path & _dir )
{
return _path.lexically_relative( _dir );
}
static path lexically_normal( const path & _path )
{
return _path.lexically_normal();
}
static path absolute( const path & _path )
{
return fs_real::absolute( _path );
}
static bool is_dir_filename( const path & _path )
{
return _path.filename().empty();
}
#elif __has_include(<experimental/filesystem>)
static path lexically_relative( const path & _path, const path & _dir )
{
fs_real::path path = fs_real::absolute( _path );
fs_real::path dir = fs_real::absolute( _dir );
if( path.root_path() != dir.root_path() )
return dir;
fs_real::path result;
auto itPath = path.begin();
auto itDir = dir.begin();
while(
itPath != path.end() &&
itDir != dir.end() &&
*itPath == *itDir
)
{
++itPath;
++itDir;
}
if( itDir != dir.end() )
{
++itDir;
while( itDir != dir.end() )
{
result /= "..";
++itDir;
}
}
while( itPath != path.end() )
{
result /= *itPath;
++itPath;
}
return result;
}
static path lexically_normal( const path & _path )
{
if( _path.empty() )
return _path;
std::vector< std::string > items{ _path.begin(), _path.end() };
std::string & lastItem = items.back();
// path { "/tmp/" } has ele,emts: { "/", "tmp", "." }
if( lastItem == "." )
lastItem = std::string{ path::preferred_separator };
std::vector< std::string > resultItems;
for( std::string s : items )
{
if (s == ".")
continue;
if (s == ".." && !resultItems.empty())
{
resultItems.pop_back();
continue;
}
resultItems.push_back( s );
}
path result;
for( std::string s : resultItems )
{
result /= s;
}
return result;
}
static path absolute( const path & _path )
{
return fs_real::absolute( _path, fs_real::current_path() );
}
static bool is_dir_filename( const path & _path )
{
if( _path.empty() )
return false;
const std::string pathStr = _path.string();
return pathStr.back() == path::preferred_separator || pathStr == ".";
}
#endif
#ifndef STDFS_USE_ALTERNATIVE_COMAPRE
static int compare( const path & _l, const path & _r )
{
return _l.compare( _r );
}
static bool less( const path & _l, const path & _r )
{
return _l < _r;
}
#else
static int compare( const path & _l, const path & _r )
{
int result = 1;
if( _l.has_root_directory() != _r.has_root_directory() )
{
if( !_l.has_root_directory() )
return +result;
else
return -result;
}
auto leftBegin = _l.begin();
auto leftEnd = _l.end();
auto rightBegin = _r.begin();
auto rightEnd = _r.end();
while( rightBegin != rightEnd && leftBegin != leftEnd )
{
if( rightBegin->native() < leftBegin->native() )
return -result;
if( rightBegin->native() > leftBegin->native() )
return +result;
++rightBegin;
++leftBegin;
++result;
}
if( rightBegin == rightEnd)
{
if (leftBegin == leftEnd)
return 0;
return -result;
}
return +result;
}
static bool less( const path & _l, const path & _r )
{
return compare( _l, _r ) > 0;
}
#endif
}
#undef STDFS_VARIABLE_IS_NOT_USED