| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file LICENSE.rst or https://cmake.org/licensing for details. */ |
| 3 |
|
| 4 |
#include <cm/filesystem> // IWYU pragma: associated |
| 5 |
|
| 6 |
#if !defined(CMake_HAVE_CXX_FILESYSTEM) |
| 7 |
|
| 8 |
# include <algorithm> |
| 9 |
# include <cassert> |
| 10 |
# include <cstddef> |
| 11 |
# include <cstdlib> |
| 12 |
# include <functional> |
| 13 |
# include <string> |
| 14 |
# include <utility> |
| 15 |
# include <vector> |
| 16 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 17 |
# include <cctype> |
| 18 |
# endif |
| 19 |
# if defined(_WIN32) || defined(__CYGWIN__) |
| 20 |
# include <iterator> |
| 21 |
# endif |
| 22 |
|
| 23 |
# include <cm/memory> |
| 24 |
# include <cm/string_view> |
| 25 |
# include <cmext/string_view> |
| 26 |
|
| 27 |
#endif |
| 28 |
|
| 29 |
namespace cm { |
| 30 |
namespace filesystem { |
| 31 |
namespace internals { |
| 32 |
|
| 33 |
// class unicode_helper |
| 34 |
unicode_helper::utf8_state unicode_helper::decode(utf8_state const state, |
| 35 |
std::uint8_t const fragment, |
| 36 |
std::uint32_t& codepoint) |
| 37 |
{ |
| 38 |
std::uint32_t const utf8_state_info[] = { |
| 39 |
// encoded states |
| 40 |
0x11111111u, 0x11111111u, 0x77777777u, 0x77777777u, 0x88888888u, |
| 41 |
0x88888888u, 0x88888888u, 0x88888888u, 0x22222299u, 0x22222222u, |
| 42 |
0x22222222u, 0x22222222u, 0x3333333au, 0x33433333u, 0x9995666bu, |
| 43 |
0x99999999u, 0x88888880u, 0x22818108u, 0x88888881u, 0x88888882u, |
| 44 |
0x88888884u, 0x88888887u, 0x88888886u, 0x82218108u, 0x82281108u, |
| 45 |
0x88888888u, 0x88888883u, 0x88888885u, 0u, 0u, |
| 46 |
0u, 0u, |
| 47 |
}; |
| 48 |
std::uint8_t category = fragment < 128 |
| 49 |
? 0 |
| 50 |
: (utf8_state_info[(fragment >> 3) & 0xf] >> ((fragment & 7) << 2)) & 0xf; |
| 51 |
codepoint = (state ? (codepoint << 6) | (fragment & 0x3fu) |
| 52 |
: (0xffu >> category) & fragment); |
| 53 |
return state == s_reject |
| 54 |
? s_reject |
| 55 |
: static_cast<utf8_state>( |
| 56 |
(utf8_state_info[category + 16] >> (state << 2)) & 0xf); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
#if !defined(CMake_HAVE_CXX_FILESYSTEM) |
| 62 |
|
| 63 |
namespace filesystem { |
| 64 |
namespace internals { |
| 65 |
|
| 66 |
class path_parser |
| 67 |
{ |
| 68 |
# if defined(__SUNPRO_CC) && defined(__sparc) |
| 69 |
// Oracle DeveloperStudio C++ compiler generates wrong code if enum size |
| 70 |
// is different than the default. |
| 71 |
using enum_size = int; |
| 72 |
# else |
| 73 |
using enum_size = unsigned char; |
| 74 |
# endif |
| 75 |
|
| 76 |
enum class state : enum_size |
| 77 |
{ |
| 78 |
before_begin, |
| 79 |
in_root_name, |
| 80 |
in_root_dir, |
| 81 |
in_filename, |
| 82 |
trailing_separator, |
| 83 |
at_end |
| 84 |
}; |
| 85 |
|
| 86 |
using pointer = char const*; |
| 87 |
|
| 88 |
public: |
| 89 |
enum class seek_position : enum_size |
| 90 |
{ |
| 91 |
root_name = static_cast<enum_size>(state::in_root_name), |
| 92 |
root_directory = static_cast<enum_size>(state::in_root_dir) |
| 93 |
}; |
| 94 |
enum class peek_fragment : enum_size |
| 95 |
{ |
| 96 |
remainder, |
| 97 |
path |
| 98 |
}; |
| 99 |
|
| 100 |
path_parser(cm::string_view path, bool set_at_end = false) |
| 101 |
: State(set_at_end ? state::at_end : state::before_begin) |
| 102 |
, Path(path) |
| 103 |
{ |
| 104 |
} |
| 105 |
|
| 106 |
path_parser(path_parser const&) = default; |
| 107 |
|
| 108 |
~path_parser() = default; |
| 109 |
|
| 110 |
void reset() noexcept { this->set_state(state::before_begin); } |
| 111 |
|
| 112 |
void increment() noexcept |
| 113 |
{ |
| 114 |
pointer const start = this->next_token(); |
| 115 |
pointer const end = this->after_end(); |
| 116 |
|
| 117 |
if (start == end) { |
| 118 |
this->set_state(state::at_end); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
switch (this->State) { |
| 123 |
case state::before_begin: { |
| 124 |
auto pos = this->consume_root_name(start, end); |
| 125 |
if (pos) { |
| 126 |
this->set_state(state::in_root_name); |
| 127 |
} else { |
| 128 |
pos = this->consume_separator(start, end); |
| 129 |
if (pos) { |
| 130 |
this->set_state(state::in_root_dir); |
| 131 |
} else { |
| 132 |
this->consume_filename(start, end); |
| 133 |
this->set_state(state::in_filename); |
| 134 |
} |
| 135 |
} |
| 136 |
break; |
| 137 |
} |
| 138 |
case state::in_root_name: { |
| 139 |
auto pos = this->consume_separator(start, end); |
| 140 |
if (pos) { |
| 141 |
this->set_state(state::in_root_dir); |
| 142 |
} else { |
| 143 |
this->consume_filename(start, end); |
| 144 |
this->set_state(state::in_filename); |
| 145 |
} |
| 146 |
break; |
| 147 |
} |
| 148 |
case state::in_root_dir: { |
| 149 |
this->consume_filename(start, end); |
| 150 |
this->set_state(state::in_filename); |
| 151 |
break; |
| 152 |
} |
| 153 |
case state::in_filename: { |
| 154 |
auto posSep = this->consume_separator(start, end); |
| 155 |
if (posSep != end) { |
| 156 |
auto pos = this->consume_filename(posSep, end); |
| 157 |
if (pos) { |
| 158 |
return; |
| 159 |
} |
| 160 |
} |
| 161 |
set_state(state::trailing_separator); |
| 162 |
break; |
| 163 |
} |
| 164 |
case state::trailing_separator: { |
| 165 |
this->set_state(state::at_end); |
| 166 |
break; |
| 167 |
} |
| 168 |
case state::at_end: |
| 169 |
// unreachable |
| 170 |
std::abort(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void decrement() noexcept |
| 175 |
{ |
| 176 |
pointer const rstart = this->current_token() - 1; |
| 177 |
pointer const rend = this->before_start(); |
| 178 |
|
| 179 |
if (rstart == rend) { |
| 180 |
this->set_state(state::before_begin); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
switch (this->State) { |
| 185 |
case state::at_end: { |
| 186 |
auto posSep = this->consume_separator(rstart, rend); |
| 187 |
if (posSep) { |
| 188 |
if (posSep == rend) { |
| 189 |
this->set_state(state::in_root_dir); |
| 190 |
} else { |
| 191 |
auto pos = this->consume_root_name(posSep, rend, true); |
| 192 |
if (pos == rend) { |
| 193 |
this->set_state(state::in_root_dir); |
| 194 |
} else { |
| 195 |
this->set_state(state::trailing_separator); |
| 196 |
} |
| 197 |
} |
| 198 |
} else { |
| 199 |
auto pos = this->consume_root_name(rstart, rend); |
| 200 |
if (pos == rend) { |
| 201 |
this->set_state(state::in_root_name); |
| 202 |
} else { |
| 203 |
this->consume_filename(rstart, rend); |
| 204 |
this->set_state(state::in_filename); |
| 205 |
} |
| 206 |
} |
| 207 |
break; |
| 208 |
} |
| 209 |
case state::trailing_separator: { |
| 210 |
this->consume_filename(rstart, rend); |
| 211 |
this->set_state(state::in_filename); |
| 212 |
break; |
| 213 |
} |
| 214 |
case state::in_filename: { |
| 215 |
auto posSep = this->consume_separator(rstart, rend); |
| 216 |
if (posSep == rend) { |
| 217 |
this->set_state(state::in_root_dir); |
| 218 |
} else { |
| 219 |
auto pos = this->consume_root_name(posSep ? posSep : rstart, rend, |
| 220 |
posSep != nullptr); |
| 221 |
if (pos == rend) { |
| 222 |
this->set_state(posSep ? state::in_root_dir : state::in_root_name); |
| 223 |
} else { |
| 224 |
this->consume_filename(posSep, rend); |
| 225 |
this->set_state(state::in_filename); |
| 226 |
} |
| 227 |
} |
| 228 |
break; |
| 229 |
} |
| 230 |
case state::in_root_dir: { |
| 231 |
auto pos = this->consume_root_name(rstart, rend); |
| 232 |
if (pos) { |
| 233 |
this->set_state(state::in_root_name); |
| 234 |
} |
| 235 |
break; |
| 236 |
} |
| 237 |
case state::in_root_name: |
| 238 |
case state::before_begin: { |
| 239 |
// unreachable |
| 240 |
std::abort(); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
path_parser& operator++() noexcept |
| 246 |
{ |
| 247 |
this->increment(); |
| 248 |
return *this; |
| 249 |
} |
| 250 |
|
| 251 |
path_parser& operator--() noexcept |
| 252 |
{ |
| 253 |
this->decrement(); |
| 254 |
return *this; |
| 255 |
} |
| 256 |
|
| 257 |
cm::string_view operator*() const noexcept |
| 258 |
{ |
| 259 |
switch (this->State) { |
| 260 |
case state::before_begin: |
| 261 |
case state::at_end: |
| 262 |
return cm::string_view(); |
| 263 |
case state::trailing_separator: |
| 264 |
return ""; |
| 265 |
case state::in_root_dir: |
| 266 |
case state::in_root_name: |
| 267 |
case state::in_filename: |
| 268 |
return this->Entry; |
| 269 |
default: |
| 270 |
// unreachable |
| 271 |
std::abort(); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
void seek(seek_position position) |
| 276 |
{ |
| 277 |
state s = static_cast<state>(static_cast<int>(position)); |
| 278 |
|
| 279 |
while (this->State <= s) { |
| 280 |
this->increment(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
cm::string_view peek(peek_fragment fragment) |
| 285 |
{ |
| 286 |
if (fragment == peek_fragment::remainder) { |
| 287 |
// peek-up remain part of the initial path |
| 288 |
return { this->Entry.data(), |
| 289 |
std::size_t(&this->Path.back() - this->Entry.data() + 1) }; |
| 290 |
} |
| 291 |
if (fragment == peek_fragment::path) { |
| 292 |
// peek-up full path until current position |
| 293 |
return { this->Path.data(), |
| 294 |
std::size_t(&this->Entry.back() - this->Path.data() + 1) }; |
| 295 |
} |
| 296 |
return {}; |
| 297 |
} |
| 298 |
|
| 299 |
bool in_root_name() const { return this->State == state::in_root_name; } |
| 300 |
bool in_root_directory() const { return this->State == state::in_root_dir; } |
| 301 |
bool at_end() const { return this->State == state::at_end; } |
| 302 |
|
| 303 |
bool at_start() const { return this->Entry.data() == this->Path.data(); } |
| 304 |
|
| 305 |
private: |
| 306 |
void set_state(state newState) noexcept |
| 307 |
{ |
| 308 |
this->State = newState; |
| 309 |
if (newState == state::before_begin || newState == state::at_end) { |
| 310 |
this->Entry = {}; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
pointer before_start() const noexcept { return this->Path.data() - 1; } |
| 315 |
pointer after_end() const noexcept |
| 316 |
{ |
| 317 |
return this->Path.data() + this->Path.size(); |
| 318 |
} |
| 319 |
|
| 320 |
pointer current_token() const noexcept |
| 321 |
{ |
| 322 |
switch (this->State) { |
| 323 |
case state::before_begin: |
| 324 |
case state::in_root_name: |
| 325 |
return &this->Path.front(); |
| 326 |
case state::in_root_dir: |
| 327 |
case state::in_filename: |
| 328 |
case state::trailing_separator: |
| 329 |
return &this->Entry.front(); |
| 330 |
case state::at_end: |
| 331 |
return &this->Path.back() + 1; |
| 332 |
default: |
| 333 |
// unreachable |
| 334 |
std::abort(); |
| 335 |
} |
| 336 |
} |
| 337 |
pointer next_token() const noexcept |
| 338 |
{ |
| 339 |
switch (this->State) { |
| 340 |
case state::before_begin: |
| 341 |
return this->Path.data(); |
| 342 |
case state::in_root_name: |
| 343 |
case state::in_root_dir: |
| 344 |
case state::in_filename: |
| 345 |
return &this->Entry.back() + 1; |
| 346 |
case state::trailing_separator: |
| 347 |
case state::at_end: |
| 348 |
return after_end(); |
| 349 |
default: |
| 350 |
// unreachable |
| 351 |
std::abort(); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
pointer consume_separator(pointer ptr, pointer end) noexcept |
| 356 |
{ |
| 357 |
if (ptr == end || |
| 358 |
(*ptr != '/' |
| 359 |
# if defined(_WIN32) |
| 360 |
&& *ptr != '\\' |
| 361 |
# endif |
| 362 |
)) { |
| 363 |
return nullptr; |
| 364 |
} |
| 365 |
auto const step = ptr < end ? 1 : -1; |
| 366 |
ptr += step; |
| 367 |
while (ptr != end && |
| 368 |
(*ptr == '/' |
| 369 |
# if defined(_WIN32) |
| 370 |
|| *ptr == '\\' |
| 371 |
# endif |
| 372 |
)) { |
| 373 |
ptr += step; |
| 374 |
} |
| 375 |
if (step == 1) { |
| 376 |
this->Entry = cm::string_view(ptr - 1, 1); |
| 377 |
} else { |
| 378 |
this->Entry = cm::string_view(ptr + 1, 1); |
| 379 |
} |
| 380 |
|
| 381 |
return ptr; |
| 382 |
} |
| 383 |
|
| 384 |
pointer consume_filename(pointer ptr, pointer end) noexcept |
| 385 |
{ |
| 386 |
auto start = ptr; |
| 387 |
|
| 388 |
if (ptr == end || *ptr == '/' |
| 389 |
# if defined(_WIN32) |
| 390 |
|| *ptr == '\\' |
| 391 |
# endif |
| 392 |
) { |
| 393 |
return nullptr; |
| 394 |
} |
| 395 |
auto const step = ptr < end ? 1 : -1; |
| 396 |
ptr += step; |
| 397 |
while (ptr != end && *ptr != '/' |
| 398 |
# if defined(_WIN32) |
| 399 |
&& *ptr != '\\' |
| 400 |
# endif |
| 401 |
) { |
| 402 |
ptr += step; |
| 403 |
} |
| 404 |
|
| 405 |
# if defined(_WIN32) |
| 406 |
if (step == -1 && (start - ptr) >= 2 && ptr == end) { |
| 407 |
// rollback drive name consumption, if any |
| 408 |
if (this->is_drive_name(ptr + 1)) { |
| 409 |
ptr += 2; |
| 410 |
} |
| 411 |
if (ptr == start) { |
| 412 |
return nullptr; |
| 413 |
} |
| 414 |
} |
| 415 |
# endif |
| 416 |
|
| 417 |
if (step == 1) { |
| 418 |
this->Entry = cm::string_view(start, ptr - start); |
| 419 |
} else { |
| 420 |
this->Entry = cm::string_view(ptr + 1, start - ptr); |
| 421 |
} |
| 422 |
|
| 423 |
return ptr; |
| 424 |
} |
| 425 |
|
| 426 |
# if defined(_WIN32) |
| 427 |
bool is_drive_name(pointer ptr) |
| 428 |
{ |
| 429 |
auto ptr0 = |
| 430 |
static_cast<char>(std::toupper(static_cast<unsigned char>(ptr[0]))); |
| 431 |
return ptr0 >= 'A' && ptr0 <= 'Z' && ptr[1] == ':'; |
| 432 |
} |
| 433 |
# endif |
| 434 |
|
| 435 |
pointer consume_root_name(pointer ptr, pointer end, |
| 436 |
bool check_only = false) noexcept |
| 437 |
{ |
| 438 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 439 |
if (ptr < end) { |
| 440 |
if ((end - ptr) >= 2 && this->is_drive_name(ptr)) { |
| 441 |
// Drive letter (X:) is a root name |
| 442 |
if (!check_only) { |
| 443 |
this->Entry = cm::string_view(ptr, 2); |
| 444 |
} |
| 445 |
return ptr + 2; |
| 446 |
} |
| 447 |
if ((end - ptr) > 2 && (ptr[0] == '/' || ptr[0] == '\\') && |
| 448 |
(ptr[1] == '/' || ptr[1] == '\\') && |
| 449 |
(ptr[2] != '/' && ptr[2] != '\\')) { |
| 450 |
// server name (//server) is a root name |
| 451 |
auto pos = std::find_if(ptr + 2, end, |
| 452 |
[](char c) { return c == '/' || c == '\\'; }); |
| 453 |
if (!check_only) { |
| 454 |
this->Entry = cm::string_view(ptr, pos - ptr); |
| 455 |
} |
| 456 |
return pos; |
| 457 |
} |
| 458 |
} else { |
| 459 |
if ((ptr - end) >= 2 && this->is_drive_name(ptr - 1)) { |
| 460 |
// Drive letter (X:) is a root name |
| 461 |
if (!check_only) { |
| 462 |
this->Entry = cm::string_view(ptr - 1, 2); |
| 463 |
} |
| 464 |
return ptr - 2; |
| 465 |
} |
| 466 |
if ((ptr - end) > 2 && (ptr[0] != '/' && ptr[0] != '\\')) { |
| 467 |
std::reverse_iterator<pointer> start(ptr); |
| 468 |
std::reverse_iterator<pointer> stop(end); |
| 469 |
auto res = std::find_if(start, stop, |
| 470 |
[](char c) { return c == '/' || c == '\\'; }); |
| 471 |
pointer pos = res.base() - 1; |
| 472 |
if ((pos - 1) > end && (pos[-1] == '/' || pos[-1] == '\\')) { |
| 473 |
// server name (//server) is a root name |
| 474 |
if (!check_only) { |
| 475 |
this->Entry = cm::string_view(pos - 1, ptr - pos + 2); |
| 476 |
} |
| 477 |
return pos - 2; |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
# elif defined(__CYGWIN__) |
| 482 |
if (ptr < end) { |
| 483 |
if ((end - ptr) > 2 && ptr[0] == '/' && ptr[1] == '/' && ptr[2] != '/') { |
| 484 |
// server name (//server) is a root name |
| 485 |
auto pos = std::find(ptr + 2, end, '/'); |
| 486 |
if (!check_only) { |
| 487 |
this->Entry = cm::string_view(ptr, pos - ptr); |
| 488 |
} |
| 489 |
return pos; |
| 490 |
} |
| 491 |
} else { |
| 492 |
if ((ptr - end) > 2 && ptr[0] != '/') { |
| 493 |
std::reverse_iterator<pointer> start(ptr); |
| 494 |
std::reverse_iterator<pointer> stop(end); |
| 495 |
auto res = std::find(start, stop, '/'); |
| 496 |
pointer pos = res.base() - 1; |
| 497 |
if ((pos - 1) > end && pos[-1] == '/') { |
| 498 |
// server name (//server) is a root name |
| 499 |
if (!check_only) { |
| 500 |
this->Entry = cm::string_view(pos - 1, ptr - pos + 2); |
| 501 |
} |
| 502 |
return pos - 2; |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
# else |
| 507 |
(void)ptr; |
| 508 |
(void)end; |
| 509 |
(void)check_only; |
| 510 |
# endif |
| 511 |
return nullptr; |
| 512 |
} |
| 513 |
|
| 514 |
state State; |
| 515 |
cm::string_view const Path; |
| 516 |
cm::string_view Entry; |
| 517 |
}; |
| 518 |
|
| 519 |
} // internals |
| 520 |
|
| 521 |
// Class path |
| 522 |
path& path::operator/=(path const& p) |
| 523 |
{ |
| 524 |
if (p.is_absolute() || |
| 525 |
(p.has_root_name() && p.get_root_name() != this->get_root_name())) { |
| 526 |
this->path_ = p.path_; |
| 527 |
return *this; |
| 528 |
} |
| 529 |
if (p.has_root_directory()) { |
| 530 |
this->path_ = static_cast<std::string>(this->get_root_name()); |
| 531 |
this->path_ += static_cast<std::string>(p.get_root_directory()); |
| 532 |
} else if (this->has_filename()) { |
| 533 |
this->path_ += this->preferred_separator; |
| 534 |
# if defined(_WIN32) || defined(__CYGWIN__) |
| 535 |
// special case: "//host" / "b" => "//host/b" |
| 536 |
} else if (this->has_root_name() && !this->has_root_directory()) { |
| 537 |
if (this->path_.length() >= 3 && |
| 538 |
(this->path_[0] == '/' |
| 539 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 540 |
|| this->path_[0] == '\\' |
| 541 |
# endif |
| 542 |
) && |
| 543 |
(this->path_[1] == '/' |
| 544 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 545 |
|| this->path_[1] == '\\' |
| 546 |
# endif |
| 547 |
) && |
| 548 |
(this->path_[2] != '/' |
| 549 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 550 |
&& this->path_[2] != '\\' |
| 551 |
# endif |
| 552 |
)) { |
| 553 |
this->path_ += this->preferred_separator; |
| 554 |
} |
| 555 |
# endif |
| 556 |
} |
| 557 |
|
| 558 |
this->path_ += p.get_relative_path(); |
| 559 |
return *this; |
| 560 |
} |
| 561 |
|
| 562 |
path path::lexically_normal() const |
| 563 |
{ |
| 564 |
if (this->path_.empty()) { |
| 565 |
return *this; |
| 566 |
} |
| 567 |
|
| 568 |
cm::string_view const dot = "."_s; |
| 569 |
cm::string_view const dotdot = ".."_s; |
| 570 |
|
| 571 |
std::vector<cm::string_view> root_parts; |
| 572 |
std::vector<cm::string_view> parts; |
| 573 |
bool root_directory_defined = false; |
| 574 |
bool need_final_separator = false; |
| 575 |
std::size_t path_size = 0; |
| 576 |
|
| 577 |
internals::path_parser parser(this->path_); |
| 578 |
++parser; |
| 579 |
while (!parser.at_end()) { |
| 580 |
auto part = *parser; |
| 581 |
|
| 582 |
if (parser.in_root_name() || parser.in_root_directory()) { |
| 583 |
if (parser.in_root_directory()) { |
| 584 |
root_directory_defined = true; |
| 585 |
} |
| 586 |
root_parts.push_back(part); |
| 587 |
path_size += part.size(); |
| 588 |
} else if (part == dotdot) { |
| 589 |
if (!parts.empty() && parts.back() != dotdot) { |
| 590 |
need_final_separator = true; |
| 591 |
path_size -= parts.back().size(); |
| 592 |
parts.pop_back(); |
| 593 |
} else if ((parts.empty() || parts.back() == dotdot) && |
| 594 |
!root_directory_defined) { |
| 595 |
parts.push_back(dotdot); |
| 596 |
path_size += 2; |
| 597 |
} |
| 598 |
|
| 599 |
} else if (part == dot || part.empty()) { |
| 600 |
need_final_separator = true; |
| 601 |
if (part.empty()) { |
| 602 |
parts.push_back(part); |
| 603 |
} |
| 604 |
} else { |
| 605 |
// filename |
| 606 |
need_final_separator = false; |
| 607 |
parts.push_back(part); |
| 608 |
path_size += part.size(); |
| 609 |
} |
| 610 |
++parser; |
| 611 |
} |
| 612 |
|
| 613 |
// no final separator if last element of path is ".." |
| 614 |
need_final_separator = |
| 615 |
need_final_separator && !parts.empty() && parts.back() != dotdot; |
| 616 |
|
| 617 |
// build final path |
| 618 |
//// compute final size of path |
| 619 |
path_size += parts.size() + (need_final_separator ? 1 : 0); |
| 620 |
|
| 621 |
std::string np; |
| 622 |
np.reserve(path_size); |
| 623 |
for (auto const& p : root_parts) { |
| 624 |
np += p; |
| 625 |
} |
| 626 |
// convert any slash to the preferred_separator |
| 627 |
if (static_cast<std::string::value_type>(this->preferred_separator) != '/') { |
| 628 |
std::replace( |
| 629 |
np.begin(), np.end(), '/', |
| 630 |
static_cast<std::string::value_type>(this->preferred_separator)); |
| 631 |
} |
| 632 |
for (auto const& p : parts) { |
| 633 |
if (!p.empty()) { |
| 634 |
np += p; |
| 635 |
np += static_cast<std::string::value_type>(this->preferred_separator); |
| 636 |
} |
| 637 |
} |
| 638 |
if (!parts.empty() && !need_final_separator) { |
| 639 |
// remove extra separator |
| 640 |
np.pop_back(); |
| 641 |
} |
| 642 |
if (np.empty()) { |
| 643 |
np.assign(1, '.'); |
| 644 |
} |
| 645 |
|
| 646 |
return path(std::move(np)); |
| 647 |
} |
| 648 |
|
| 649 |
path path::lexically_relative(path const& base) const |
| 650 |
{ |
| 651 |
internals::path_parser parser(this->path_); |
| 652 |
++parser; |
| 653 |
internals::path_parser parserbase(base.path_); |
| 654 |
++parserbase; |
| 655 |
cm::string_view this_root_name, base_root_name; |
| 656 |
cm::string_view this_root_dir, base_root_dir; |
| 657 |
|
| 658 |
if (parser.in_root_name()) { |
| 659 |
this_root_name = *parser; |
| 660 |
++parser; |
| 661 |
} |
| 662 |
if (parser.in_root_directory()) { |
| 663 |
this_root_dir = *parser; |
| 664 |
++parser; |
| 665 |
} |
| 666 |
if (parserbase.in_root_name()) { |
| 667 |
base_root_name = *parserbase; |
| 668 |
++parserbase; |
| 669 |
} |
| 670 |
if (parserbase.in_root_directory()) { |
| 671 |
base_root_dir = *parserbase; |
| 672 |
++parserbase; |
| 673 |
} |
| 674 |
|
| 675 |
auto is_path_absolute = [](cm::string_view rn, cm::string_view rd) -> bool { |
| 676 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 677 |
return !rn.empty() && !rd.empty(); |
| 678 |
# else |
| 679 |
(void)rn; |
| 680 |
return !rd.empty(); |
| 681 |
# endif |
| 682 |
}; |
| 683 |
|
| 684 |
if (this_root_name != base_root_name || |
| 685 |
is_path_absolute(this_root_name, this_root_dir) != |
| 686 |
is_path_absolute(base_root_name, base_root_dir) || |
| 687 |
(this_root_dir.empty() && !base_root_dir.empty())) { |
| 688 |
return path(); |
| 689 |
} |
| 690 |
|
| 691 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 692 |
// LWG3070 handle special case: filename can also be a root-name |
| 693 |
auto is_drive_name = [](cm::string_view item) -> bool { |
| 694 |
return item.length() == 2 && item[1] == ':'; |
| 695 |
}; |
| 696 |
parser.reset(); |
| 697 |
parser.seek(internals::path_parser::seek_position::root_directory); |
| 698 |
while (!parser.at_end()) { |
| 699 |
if (is_drive_name(*parser)) { |
| 700 |
return path(); |
| 701 |
} |
| 702 |
++parser; |
| 703 |
} |
| 704 |
parserbase.reset(); |
| 705 |
parserbase.seek(internals::path_parser::seek_position::root_directory); |
| 706 |
while (!parserbase.at_end()) { |
| 707 |
if (is_drive_name(*parserbase)) { |
| 708 |
return path(); |
| 709 |
} |
| 710 |
++parserbase; |
| 711 |
} |
| 712 |
# endif |
| 713 |
|
| 714 |
cm::string_view const dot = "."_s; |
| 715 |
cm::string_view const dotdot = ".."_s; |
| 716 |
|
| 717 |
auto a = this->begin(), aend = this->end(); |
| 718 |
auto b = base.begin(), bend = base.end(); |
| 719 |
while (a != aend && b != bend && a->string() == b->string()) { |
| 720 |
++a; |
| 721 |
++b; |
| 722 |
} |
| 723 |
|
| 724 |
int count = 0; |
| 725 |
for (; b != bend; ++b) { |
| 726 |
auto part = *b; |
| 727 |
if (part == dotdot) { |
| 728 |
--count; |
| 729 |
} else if (part.string() != dot && !part.empty()) { |
| 730 |
++count; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
if (count == 0 && (a == this->end() || a->empty())) { |
| 735 |
return path(dot); |
| 736 |
} |
| 737 |
if (count >= 0) { |
| 738 |
path result; |
| 739 |
path p_dotdot(dotdot); |
| 740 |
for (int i = 0; i < count; ++i) { |
| 741 |
result /= p_dotdot; |
| 742 |
} |
| 743 |
for (; a != aend; ++a) { |
| 744 |
result /= *a; |
| 745 |
} |
| 746 |
return result; |
| 747 |
} |
| 748 |
// count < 0 |
| 749 |
return path(); |
| 750 |
} |
| 751 |
|
| 752 |
path::path_type path::get_generic() const |
| 753 |
{ |
| 754 |
auto gen_path = this->path_; |
| 755 |
auto start = gen_path.begin(); |
| 756 |
# if defined(_WIN32) && !defined(__CYGWIN__) |
| 757 |
std::replace(gen_path.begin(), gen_path.end(), '\\', '/'); |
| 758 |
// preserve special syntax for root_name ('//server' or '//?') |
| 759 |
if (gen_path.length() > 2 && gen_path[2] != '/') { |
| 760 |
start += 2; |
| 761 |
} |
| 762 |
# endif |
| 763 |
// remove duplicate separators |
| 764 |
auto new_end = std::unique(start, gen_path.end(), [](char lhs, char rhs) { |
| 765 |
return lhs == rhs && lhs == '/'; |
| 766 |
}); |
| 767 |
gen_path.erase(new_end, gen_path.end()); |
| 768 |
return gen_path; |
| 769 |
} |
| 770 |
|
| 771 |
cm::string_view path::get_root_name() const |
| 772 |
{ |
| 773 |
internals::path_parser parser(this->path_); |
| 774 |
++parser; |
| 775 |
if (parser.in_root_name()) { |
| 776 |
return *parser; |
| 777 |
} |
| 778 |
return {}; |
| 779 |
} |
| 780 |
|
| 781 |
cm::string_view path::get_root_directory() const |
| 782 |
{ |
| 783 |
internals::path_parser parser(this->path_); |
| 784 |
++parser; |
| 785 |
if (parser.in_root_name()) { |
| 786 |
++parser; |
| 787 |
} |
| 788 |
if (parser.in_root_directory()) { |
| 789 |
return *parser; |
| 790 |
} |
| 791 |
return {}; |
| 792 |
} |
| 793 |
|
| 794 |
cm::string_view path::get_relative_path() const |
| 795 |
{ |
| 796 |
internals::path_parser parser(this->path_); |
| 797 |
parser.seek(internals::path_parser::seek_position::root_directory); |
| 798 |
if (parser.at_end()) { |
| 799 |
return {}; |
| 800 |
} |
| 801 |
return parser.peek(internals::path_parser::peek_fragment::remainder); |
| 802 |
} |
| 803 |
|
| 804 |
cm::string_view path::get_parent_path() const |
| 805 |
{ |
| 806 |
if (!this->has_relative_path()) { |
| 807 |
return this->path_; |
| 808 |
} |
| 809 |
|
| 810 |
// peek-up full path minus last element |
| 811 |
internals::path_parser parser(this->path_, true); |
| 812 |
--parser; |
| 813 |
if (parser.at_start()) { |
| 814 |
return {}; |
| 815 |
} |
| 816 |
--parser; |
| 817 |
return parser.peek(internals::path_parser::peek_fragment::path); |
| 818 |
} |
| 819 |
|
| 820 |
cm::string_view path::get_filename() const |
| 821 |
{ |
| 822 |
{ |
| 823 |
internals::path_parser parser(this->path_); |
| 824 |
parser.seek(internals::path_parser::seek_position::root_directory); |
| 825 |
if (parser.at_end()) { |
| 826 |
return {}; |
| 827 |
} |
| 828 |
} |
| 829 |
{ |
| 830 |
internals::path_parser parser(this->path_, true); |
| 831 |
return *(--parser); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
cm::string_view path::get_filename_fragment(filename_fragment fragment) const |
| 836 |
{ |
| 837 |
auto file = this->get_filename(); |
| 838 |
|
| 839 |
if (file.empty() || file == "." || file == "..") { |
| 840 |
return fragment == filename_fragment::stem ? file : cm::string_view{}; |
| 841 |
} |
| 842 |
|
| 843 |
auto pos = file.find_last_of('.'); |
| 844 |
if (pos == cm::string_view::npos || pos == 0) { |
| 845 |
return fragment == filename_fragment::stem ? file : cm::string_view{}; |
| 846 |
} |
| 847 |
return fragment == filename_fragment::stem ? file.substr(0, pos) |
| 848 |
: file.substr(pos); |
| 849 |
} |
| 850 |
|
| 851 |
int path::compare_path(cm::string_view str) const |
| 852 |
{ |
| 853 |
internals::path_parser this_pp(this->path_); |
| 854 |
++this_pp; |
| 855 |
internals::path_parser other_pp(str); |
| 856 |
++other_pp; |
| 857 |
|
| 858 |
// compare root_name part |
| 859 |
{ |
| 860 |
bool compare_root_names = false; |
| 861 |
cm::string_view this_root_name, other_root_name; |
| 862 |
int res; |
| 863 |
|
| 864 |
if (this_pp.in_root_name()) { |
| 865 |
compare_root_names = true; |
| 866 |
this_root_name = *this_pp; |
| 867 |
++this_pp; |
| 868 |
} |
| 869 |
if (other_pp.in_root_name()) { |
| 870 |
compare_root_names = true; |
| 871 |
other_root_name = *other_pp; |
| 872 |
++other_pp; |
| 873 |
} |
| 874 |
if (compare_root_names && |
| 875 |
(res = this_root_name.compare(other_root_name) != 0)) { |
| 876 |
return res; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
// compare root_directory part |
| 881 |
{ |
| 882 |
if (!this_pp.in_root_directory() && other_pp.in_root_directory()) { |
| 883 |
return -1; |
| 884 |
} else if (this_pp.in_root_directory() && !other_pp.in_root_directory()) { |
| 885 |
return 1; |
| 886 |
} |
| 887 |
if (this_pp.in_root_directory()) { |
| 888 |
++this_pp; |
| 889 |
} |
| 890 |
if (other_pp.in_root_directory()) { |
| 891 |
++other_pp; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
// compare various parts of the paths |
| 896 |
while (!this_pp.at_end() && !other_pp.at_end()) { |
| 897 |
int res; |
| 898 |
if ((res = (*this_pp).compare(*other_pp)) != 0) { |
| 899 |
return res; |
| 900 |
} |
| 901 |
++this_pp; |
| 902 |
++other_pp; |
| 903 |
} |
| 904 |
|
| 905 |
// final step |
| 906 |
if (this_pp.at_end() && !other_pp.at_end()) { |
| 907 |
return -1; |
| 908 |
} else if (!this_pp.at_end() && other_pp.at_end()) { |
| 909 |
return 1; |
| 910 |
} |
| 911 |
|
| 912 |
return 0; |
| 913 |
} |
| 914 |
|
| 915 |
// Class path::iterator |
| 916 |
path::iterator::iterator() |
| 917 |
: path_(nullptr) |
| 918 |
{ |
| 919 |
} |
| 920 |
path::iterator::iterator(iterator const& other) |
| 921 |
{ |
| 922 |
this->path_ = other.path_; |
| 923 |
if (other.parser_) { |
| 924 |
this->parser_ = cm::make_unique<internals::path_parser>(*other.parser_); |
| 925 |
this->path_element_ = path(**this->parser_); |
| 926 |
} |
| 927 |
} |
| 928 |
path::iterator::iterator(path const* p, bool at_end) |
| 929 |
: path_(p) |
| 930 |
, parser_(cm::make_unique<internals::path_parser>(p->path_, at_end)) |
| 931 |
{ |
| 932 |
if (!at_end) { |
| 933 |
++(*this->parser_); |
| 934 |
this->path_element_ = path(**this->parser_); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
path::iterator::~iterator() = default; |
| 939 |
|
| 940 |
path::iterator& path::iterator::operator=(iterator const& other) |
| 941 |
{ |
| 942 |
this->path_ = other.path_; |
| 943 |
if (other.parser_) { |
| 944 |
this->parser_ = cm::make_unique<internals::path_parser>(*other.parser_); |
| 945 |
this->path_element_ = path(**this->parser_); |
| 946 |
} |
| 947 |
|
| 948 |
return *this; |
| 949 |
} |
| 950 |
|
| 951 |
path::iterator& path::iterator::operator++() |
| 952 |
{ |
| 953 |
assert(this->parser_); |
| 954 |
|
| 955 |
if (this->parser_) { |
| 956 |
assert(!this->parser_->at_end()); |
| 957 |
|
| 958 |
if (!this->parser_->at_end()) { |
| 959 |
++(*this->parser_); |
| 960 |
if (this->parser_->at_end()) { |
| 961 |
this->path_element_ = path(); |
| 962 |
} else { |
| 963 |
this->path_element_ = path(**this->parser_); |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
return *this; |
| 969 |
} |
| 970 |
|
| 971 |
path::iterator& path::iterator::operator--() |
| 972 |
{ |
| 973 |
assert(this->parser_); |
| 974 |
|
| 975 |
if (this->parser_) { |
| 976 |
assert(!this->parser_->at_start()); |
| 977 |
|
| 978 |
if (!this->parser_->at_start()) { |
| 979 |
--(*this->parser_); |
| 980 |
this->path_element_ = path(**this->parser_); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
return *this; |
| 985 |
} |
| 986 |
|
| 987 |
bool operator==(path::iterator const& lhs, path::iterator const& rhs) |
| 988 |
{ |
| 989 |
return lhs.path_ == rhs.path_ && lhs.parser_ != nullptr && |
| 990 |
((lhs.parser_->at_end() && rhs.parser_->at_end()) || |
| 991 |
(lhs.parser_->at_start() && rhs.parser_->at_start()) || |
| 992 |
((**lhs.parser_).data() == (**rhs.parser_).data())); |
| 993 |
} |
| 994 |
|
| 995 |
std::size_t hash_value(path const& p) noexcept |
| 996 |
{ |
| 997 |
internals::path_parser parser(p.path_); |
| 998 |
std::hash<cm::string_view> hasher; |
| 999 |
std::size_t value = 0; |
| 1000 |
|
| 1001 |
while (!parser.at_end()) { |
| 1002 |
value = hasher(*parser) + 0x9e3779b9 + (value << 6) + (value >> 2); |
| 1003 |
++parser; |
| 1004 |
} |
| 1005 |
|
| 1006 |
return value; |
| 1007 |
} |
| 1008 |
} // filesystem |
| 1009 |
#endif |
| 1010 |
} // cm |