1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___FILESYSTEM_PATH_H 11 #define _LIBCPP___FILESYSTEM_PATH_H 12 13 #include <__algorithm/replace.h> 14 #include <__algorithm/replace_copy.h> 15 #include <__availability> 16 #include <__config> 17 #include <__iterator/back_insert_iterator.h> 18 #include <__iterator/iterator_traits.h> 19 #include <cstddef> 20 #include <string> 21 #include <string_view> 22 #include <type_traits> 23 24 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 25 # include <iomanip> // for quoted 26 # include <locale> 27 #endif 28 29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 30 # pragma GCC system_header 31 #endif 32 33 #ifndef _LIBCPP_CXX03_LANG 34 35 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM 36 37 _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH 38 39 template <class _Tp> 40 struct __can_convert_char { 41 static const bool value = false; 42 }; 43 template <class _Tp> 44 struct __can_convert_char<const _Tp> : public __can_convert_char<_Tp> {}; 45 template <> 46 struct __can_convert_char<char> { 47 static const bool value = true; 48 using __char_type = char; 49 }; 50 template <> 51 struct __can_convert_char<wchar_t> { 52 static const bool value = true; 53 using __char_type = wchar_t; 54 }; 55 #ifndef _LIBCPP_HAS_NO_CHAR8_T 56 template <> 57 struct __can_convert_char<char8_t> { 58 static const bool value = true; 59 using __char_type = char8_t; 60 }; 61 #endif 62 template <> 63 struct __can_convert_char<char16_t> { 64 static const bool value = true; 65 using __char_type = char16_t; 66 }; 67 template <> 68 struct __can_convert_char<char32_t> { 69 static const bool value = true; 70 using __char_type = char32_t; 71 }; 72 73 template <class _ECharT> 74 typename enable_if<__can_convert_char<_ECharT>::value, bool>::type 75 __is_separator(_ECharT __e) { 76 #if defined(_LIBCPP_WIN32API) 77 return __e == _ECharT('/') || __e == _ECharT('\\'); 78 #else 79 return __e == _ECharT('/'); 80 #endif 81 } 82 83 #ifndef _LIBCPP_HAS_NO_CHAR8_T 84 typedef u8string __u8_string; 85 #else 86 typedef string __u8_string; 87 #endif 88 89 struct _NullSentinel {}; 90 91 template <class _Tp> 92 using _Void = void; 93 94 template <class _Tp, class = void> 95 struct __is_pathable_string : public false_type {}; 96 97 template <class _ECharT, class _Traits, class _Alloc> 98 struct __is_pathable_string< 99 basic_string<_ECharT, _Traits, _Alloc>, 100 _Void<typename __can_convert_char<_ECharT>::__char_type> > 101 : public __can_convert_char<_ECharT> { 102 using _Str = basic_string<_ECharT, _Traits, _Alloc>; 103 using _Base = __can_convert_char<_ECharT>; 104 static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); } 105 static _ECharT const* __range_end(_Str const& __s) { 106 return __s.data() + __s.length(); 107 } 108 static _ECharT __first_or_null(_Str const& __s) { 109 return __s.empty() ? _ECharT{} : __s[0]; 110 } 111 }; 112 113 template <class _ECharT, class _Traits> 114 struct __is_pathable_string< 115 basic_string_view<_ECharT, _Traits>, 116 _Void<typename __can_convert_char<_ECharT>::__char_type> > 117 : public __can_convert_char<_ECharT> { 118 using _Str = basic_string_view<_ECharT, _Traits>; 119 using _Base = __can_convert_char<_ECharT>; 120 static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); } 121 static _ECharT const* __range_end(_Str const& __s) { 122 return __s.data() + __s.length(); 123 } 124 static _ECharT __first_or_null(_Str const& __s) { 125 return __s.empty() ? _ECharT{} : __s[0]; 126 } 127 }; 128 129 template <class _Source, class _DS = typename decay<_Source>::type, 130 class _UnqualPtrType = 131 typename remove_const<typename remove_pointer<_DS>::type>::type, 132 bool _IsCharPtr = is_pointer<_DS>::value&& 133 __can_convert_char<_UnqualPtrType>::value> 134 struct __is_pathable_char_array : false_type {}; 135 136 template <class _Source, class _ECharT, class _UPtr> 137 struct __is_pathable_char_array<_Source, _ECharT*, _UPtr, true> 138 : __can_convert_char<typename remove_const<_ECharT>::type> { 139 using _Base = __can_convert_char<typename remove_const<_ECharT>::type>; 140 141 static _ECharT const* __range_begin(const _ECharT* __b) { return __b; } 142 static _ECharT const* __range_end(const _ECharT* __b) { 143 using _Iter = const _ECharT*; 144 const _ECharT __sentinel = _ECharT{}; 145 _Iter __e = __b; 146 for (; *__e != __sentinel; ++__e) 147 ; 148 return __e; 149 } 150 151 static _ECharT __first_or_null(const _ECharT* __b) { return *__b; } 152 }; 153 154 template <class _Iter, bool _IsIt = __is_cpp17_input_iterator<_Iter>::value, 155 class = void> 156 struct __is_pathable_iter : false_type {}; 157 158 template <class _Iter> 159 struct __is_pathable_iter< 160 _Iter, true, 161 _Void<typename __can_convert_char< 162 typename iterator_traits<_Iter>::value_type>::__char_type> > 163 : __can_convert_char<typename iterator_traits<_Iter>::value_type> { 164 using _ECharT = typename iterator_traits<_Iter>::value_type; 165 using _Base = __can_convert_char<_ECharT>; 166 167 static _Iter __range_begin(_Iter __b) { return __b; } 168 static _NullSentinel __range_end(_Iter) { return _NullSentinel{}; } 169 170 static _ECharT __first_or_null(_Iter __b) { return *__b; } 171 }; 172 173 template <class _Tp, bool _IsStringT = __is_pathable_string<_Tp>::value, 174 bool _IsCharIterT = __is_pathable_char_array<_Tp>::value, 175 bool _IsIterT = !_IsCharIterT && __is_pathable_iter<_Tp>::value> 176 struct __is_pathable : false_type { 177 static_assert(!_IsStringT && !_IsCharIterT && !_IsIterT, "Must all be false"); 178 }; 179 180 template <class _Tp> 181 struct __is_pathable<_Tp, true, false, false> : __is_pathable_string<_Tp> {}; 182 183 template <class _Tp> 184 struct __is_pathable<_Tp, false, true, false> : __is_pathable_char_array<_Tp> { 185 }; 186 187 template <class _Tp> 188 struct __is_pathable<_Tp, false, false, true> : __is_pathable_iter<_Tp> {}; 189 190 #if defined(_LIBCPP_WIN32API) 191 typedef wstring __path_string; 192 typedef wchar_t __path_value; 193 #else 194 typedef string __path_string; 195 typedef char __path_value; 196 #endif 197 198 #if defined(_LIBCPP_WIN32API) 199 _LIBCPP_FUNC_VIS 200 size_t __wide_to_char(const wstring&, char*, size_t); 201 _LIBCPP_FUNC_VIS 202 size_t __char_to_wide(const string&, wchar_t*, size_t); 203 #endif 204 205 template <class _ECharT> 206 struct _PathCVT; 207 208 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 209 template <class _ECharT> 210 struct _PathCVT { 211 static_assert(__can_convert_char<_ECharT>::value, 212 "Char type not convertible"); 213 214 typedef __narrow_to_utf8<sizeof(_ECharT) * __CHAR_BIT__> _Narrower; 215 #if defined(_LIBCPP_WIN32API) 216 typedef __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__> _Widener; 217 #endif 218 219 static void __append_range(__path_string& __dest, _ECharT const* __b, 220 _ECharT const* __e) { 221 #if defined(_LIBCPP_WIN32API) 222 string __utf8; 223 _Narrower()(back_inserter(__utf8), __b, __e); 224 _Widener()(back_inserter(__dest), __utf8.data(), __utf8.data() + __utf8.size()); 225 #else 226 _Narrower()(back_inserter(__dest), __b, __e); 227 #endif 228 } 229 230 template <class _Iter> 231 static void __append_range(__path_string& __dest, _Iter __b, _Iter __e) { 232 static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload"); 233 if (__b == __e) 234 return; 235 basic_string<_ECharT> __tmp(__b, __e); 236 #if defined(_LIBCPP_WIN32API) 237 string __utf8; 238 _Narrower()(back_inserter(__utf8), __tmp.data(), 239 __tmp.data() + __tmp.length()); 240 _Widener()(back_inserter(__dest), __utf8.data(), __utf8.data() + __utf8.size()); 241 #else 242 _Narrower()(back_inserter(__dest), __tmp.data(), 243 __tmp.data() + __tmp.length()); 244 #endif 245 } 246 247 template <class _Iter> 248 static void __append_range(__path_string& __dest, _Iter __b, _NullSentinel) { 249 static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload"); 250 const _ECharT __sentinel = _ECharT{}; 251 if (*__b == __sentinel) 252 return; 253 basic_string<_ECharT> __tmp; 254 for (; *__b != __sentinel; ++__b) 255 __tmp.push_back(*__b); 256 #if defined(_LIBCPP_WIN32API) 257 string __utf8; 258 _Narrower()(back_inserter(__utf8), __tmp.data(), 259 __tmp.data() + __tmp.length()); 260 _Widener()(back_inserter(__dest), __utf8.data(), __utf8.data() + __utf8.size()); 261 #else 262 _Narrower()(back_inserter(__dest), __tmp.data(), 263 __tmp.data() + __tmp.length()); 264 #endif 265 } 266 267 template <class _Source> 268 static void __append_source(__path_string& __dest, _Source const& __s) { 269 using _Traits = __is_pathable<_Source>; 270 __append_range(__dest, _Traits::__range_begin(__s), 271 _Traits::__range_end(__s)); 272 } 273 }; 274 #endif // !_LIBCPP_HAS_NO_LOCALIZATION 275 276 template <> 277 struct _PathCVT<__path_value> { 278 279 template <class _Iter> 280 static typename enable_if<__is_exactly_cpp17_input_iterator<_Iter>::value>::type 281 __append_range(__path_string& __dest, _Iter __b, _Iter __e) { 282 for (; __b != __e; ++__b) 283 __dest.push_back(*__b); 284 } 285 286 template <class _Iter> 287 static typename enable_if<__is_cpp17_forward_iterator<_Iter>::value>::type 288 __append_range(__path_string& __dest, _Iter __b, _Iter __e) { 289 __dest.append(__b, __e); 290 } 291 292 template <class _Iter> 293 static void __append_range(__path_string& __dest, _Iter __b, _NullSentinel) { 294 const char __sentinel = char{}; 295 for (; *__b != __sentinel; ++__b) 296 __dest.push_back(*__b); 297 } 298 299 template <class _Source> 300 static void __append_source(__path_string& __dest, _Source const& __s) { 301 using _Traits = __is_pathable<_Source>; 302 __append_range(__dest, _Traits::__range_begin(__s), 303 _Traits::__range_end(__s)); 304 } 305 }; 306 307 #if defined(_LIBCPP_WIN32API) 308 template <> 309 struct _PathCVT<char> { 310 311 static void 312 __append_string(__path_string& __dest, const basic_string<char> &__str) { 313 size_t __size = __char_to_wide(__str, nullptr, 0); 314 size_t __pos = __dest.size(); 315 __dest.resize(__pos + __size); 316 __char_to_wide(__str, const_cast<__path_value*>(__dest.data()) + __pos, __size); 317 } 318 319 template <class _Iter> 320 static typename enable_if<__is_exactly_cpp17_input_iterator<_Iter>::value>::type 321 __append_range(__path_string& __dest, _Iter __b, _Iter __e) { 322 basic_string<char> __tmp(__b, __e); 323 __append_string(__dest, __tmp); 324 } 325 326 template <class _Iter> 327 static typename enable_if<__is_cpp17_forward_iterator<_Iter>::value>::type 328 __append_range(__path_string& __dest, _Iter __b, _Iter __e) { 329 basic_string<char> __tmp(__b, __e); 330 __append_string(__dest, __tmp); 331 } 332 333 template <class _Iter> 334 static void __append_range(__path_string& __dest, _Iter __b, _NullSentinel) { 335 const char __sentinel = char{}; 336 basic_string<char> __tmp; 337 for (; *__b != __sentinel; ++__b) 338 __tmp.push_back(*__b); 339 __append_string(__dest, __tmp); 340 } 341 342 template <class _Source> 343 static void __append_source(__path_string& __dest, _Source const& __s) { 344 using _Traits = __is_pathable<_Source>; 345 __append_range(__dest, _Traits::__range_begin(__s), 346 _Traits::__range_end(__s)); 347 } 348 }; 349 350 template <class _ECharT> 351 struct _PathExport { 352 typedef __narrow_to_utf8<sizeof(wchar_t) * __CHAR_BIT__> _Narrower; 353 typedef __widen_from_utf8<sizeof(_ECharT) * __CHAR_BIT__> _Widener; 354 355 template <class _Str> 356 static void __append(_Str& __dest, const __path_string& __src) { 357 string __utf8; 358 _Narrower()(back_inserter(__utf8), __src.data(), __src.data() + __src.size()); 359 _Widener()(back_inserter(__dest), __utf8.data(), __utf8.data() + __utf8.size()); 360 } 361 }; 362 363 template <> 364 struct _PathExport<char> { 365 template <class _Str> 366 static void __append(_Str& __dest, const __path_string& __src) { 367 size_t __size = __wide_to_char(__src, nullptr, 0); 368 size_t __pos = __dest.size(); 369 __dest.resize(__size); 370 __wide_to_char(__src, const_cast<char*>(__dest.data()) + __pos, __size); 371 } 372 }; 373 374 template <> 375 struct _PathExport<wchar_t> { 376 template <class _Str> 377 static void __append(_Str& __dest, const __path_string& __src) { 378 __dest.append(__src.begin(), __src.end()); 379 } 380 }; 381 382 template <> 383 struct _PathExport<char16_t> { 384 template <class _Str> 385 static void __append(_Str& __dest, const __path_string& __src) { 386 __dest.append(__src.begin(), __src.end()); 387 } 388 }; 389 390 #ifndef _LIBCPP_HAS_NO_CHAR8_T 391 template <> 392 struct _PathExport<char8_t> { 393 typedef __narrow_to_utf8<sizeof(wchar_t) * __CHAR_BIT__> _Narrower; 394 395 template <class _Str> 396 static void __append(_Str& __dest, const __path_string& __src) { 397 _Narrower()(back_inserter(__dest), __src.data(), __src.data() + __src.size()); 398 } 399 }; 400 #endif /* !_LIBCPP_HAS_NO_CHAR8_T */ 401 #endif /* _LIBCPP_WIN32API */ 402 403 class _LIBCPP_TYPE_VIS path { 404 template <class _SourceOrIter, class _Tp = path&> 405 using _EnableIfPathable = 406 typename enable_if<__is_pathable<_SourceOrIter>::value, _Tp>::type; 407 408 template <class _Tp> 409 using _SourceChar = typename __is_pathable<_Tp>::__char_type; 410 411 template <class _Tp> 412 using _SourceCVT = _PathCVT<_SourceChar<_Tp> >; 413 414 public: 415 #if defined(_LIBCPP_WIN32API) 416 typedef wchar_t value_type; 417 static constexpr value_type preferred_separator = L'\\'; 418 #else 419 typedef char value_type; 420 static constexpr value_type preferred_separator = '/'; 421 #endif 422 typedef basic_string<value_type> string_type; 423 typedef basic_string_view<value_type> __string_view; 424 425 enum _LIBCPP_ENUM_VIS format : unsigned char { 426 auto_format, 427 native_format, 428 generic_format 429 }; 430 431 // constructors and destructor 432 _LIBCPP_INLINE_VISIBILITY path() noexcept {} 433 _LIBCPP_INLINE_VISIBILITY path(const path& __p) : __pn_(__p.__pn_) {} 434 _LIBCPP_INLINE_VISIBILITY path(path&& __p) noexcept 435 : __pn_(_VSTD::move(__p.__pn_)) {} 436 437 _LIBCPP_INLINE_VISIBILITY 438 path(string_type&& __s, format = format::auto_format) noexcept 439 : __pn_(_VSTD::move(__s)) {} 440 441 template <class _Source, class = _EnableIfPathable<_Source, void> > 442 path(const _Source& __src, format = format::auto_format) { 443 _SourceCVT<_Source>::__append_source(__pn_, __src); 444 } 445 446 template <class _InputIt> 447 path(_InputIt __first, _InputIt __last, format = format::auto_format) { 448 typedef typename iterator_traits<_InputIt>::value_type _ItVal; 449 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last); 450 } 451 452 /* 453 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 454 // TODO Implement locale conversions. 455 template <class _Source, class = _EnableIfPathable<_Source, void> > 456 path(const _Source& __src, const locale& __loc, format = format::auto_format); 457 template <class _InputIt> 458 path(_InputIt __first, _InputIt _last, const locale& __loc, 459 format = format::auto_format); 460 #endif 461 */ 462 463 _LIBCPP_INLINE_VISIBILITY 464 ~path() = default; 465 466 // assignments 467 _LIBCPP_INLINE_VISIBILITY 468 path& operator=(const path& __p) { 469 __pn_ = __p.__pn_; 470 return *this; 471 } 472 473 _LIBCPP_INLINE_VISIBILITY 474 path& operator=(path&& __p) noexcept { 475 __pn_ = _VSTD::move(__p.__pn_); 476 return *this; 477 } 478 479 _LIBCPP_INLINE_VISIBILITY 480 path& operator=(string_type&& __s) noexcept { 481 __pn_ = _VSTD::move(__s); 482 return *this; 483 } 484 485 _LIBCPP_INLINE_VISIBILITY 486 path& assign(string_type&& __s) noexcept { 487 __pn_ = _VSTD::move(__s); 488 return *this; 489 } 490 491 template <class _Source> 492 _LIBCPP_INLINE_VISIBILITY _EnableIfPathable<_Source> 493 operator=(const _Source& __src) { 494 return this->assign(__src); 495 } 496 497 template <class _Source> 498 _EnableIfPathable<_Source> assign(const _Source& __src) { 499 __pn_.clear(); 500 _SourceCVT<_Source>::__append_source(__pn_, __src); 501 return *this; 502 } 503 504 template <class _InputIt> 505 path& assign(_InputIt __first, _InputIt __last) { 506 typedef typename iterator_traits<_InputIt>::value_type _ItVal; 507 __pn_.clear(); 508 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last); 509 return *this; 510 } 511 512 public: 513 // appends 514 #if defined(_LIBCPP_WIN32API) 515 path& operator/=(const path& __p) { 516 auto __p_root_name = __p.__root_name(); 517 auto __p_root_name_size = __p_root_name.size(); 518 if (__p.is_absolute() || 519 (!__p_root_name.empty() && __p_root_name != __string_view(root_name().__pn_))) { 520 __pn_ = __p.__pn_; 521 return *this; 522 } 523 if (__p.has_root_directory()) { 524 path __root_name_str = root_name(); 525 __pn_ = __root_name_str.native(); 526 __pn_ += __string_view(__p.__pn_).substr(__p_root_name_size); 527 return *this; 528 } 529 if (has_filename() || (!has_root_directory() && is_absolute())) 530 __pn_ += preferred_separator; 531 __pn_ += __string_view(__p.__pn_).substr(__p_root_name_size); 532 return *this; 533 } 534 template <class _Source> 535 _LIBCPP_INLINE_VISIBILITY _EnableIfPathable<_Source> 536 operator/=(const _Source& __src) { 537 return operator/=(path(__src)); 538 } 539 540 template <class _Source> 541 _EnableIfPathable<_Source> append(const _Source& __src) { 542 return operator/=(path(__src)); 543 } 544 545 template <class _InputIt> 546 path& append(_InputIt __first, _InputIt __last) { 547 return operator/=(path(__first, __last)); 548 } 549 #else 550 path& operator/=(const path& __p) { 551 if (__p.is_absolute()) { 552 __pn_ = __p.__pn_; 553 return *this; 554 } 555 if (has_filename()) 556 __pn_ += preferred_separator; 557 __pn_ += __p.native(); 558 return *this; 559 } 560 561 // FIXME: Use _LIBCPP_DIAGNOSE_WARNING to produce a diagnostic when __src 562 // is known at compile time to be "/' since the user almost certainly intended 563 // to append a separator instead of overwriting the path with "/" 564 template <class _Source> 565 _LIBCPP_INLINE_VISIBILITY _EnableIfPathable<_Source> 566 operator/=(const _Source& __src) { 567 return this->append(__src); 568 } 569 570 template <class _Source> 571 _EnableIfPathable<_Source> append(const _Source& __src) { 572 using _Traits = __is_pathable<_Source>; 573 using _CVT = _PathCVT<_SourceChar<_Source> >; 574 bool __source_is_absolute = __is_separator(_Traits::__first_or_null(__src)); 575 if (__source_is_absolute) 576 __pn_.clear(); 577 else if (has_filename()) 578 __pn_ += preferred_separator; 579 _CVT::__append_source(__pn_, __src); 580 return *this; 581 } 582 583 template <class _InputIt> 584 path& append(_InputIt __first, _InputIt __last) { 585 typedef typename iterator_traits<_InputIt>::value_type _ItVal; 586 static_assert(__can_convert_char<_ItVal>::value, "Must convertible"); 587 using _CVT = _PathCVT<_ItVal>; 588 if (__first != __last && __is_separator(*__first)) 589 __pn_.clear(); 590 else if (has_filename()) 591 __pn_ += preferred_separator; 592 _CVT::__append_range(__pn_, __first, __last); 593 return *this; 594 } 595 #endif 596 597 // concatenation 598 _LIBCPP_INLINE_VISIBILITY 599 path& operator+=(const path& __x) { 600 __pn_ += __x.__pn_; 601 return *this; 602 } 603 604 _LIBCPP_INLINE_VISIBILITY 605 path& operator+=(const string_type& __x) { 606 __pn_ += __x; 607 return *this; 608 } 609 610 _LIBCPP_INLINE_VISIBILITY 611 path& operator+=(__string_view __x) { 612 __pn_ += __x; 613 return *this; 614 } 615 616 _LIBCPP_INLINE_VISIBILITY 617 path& operator+=(const value_type* __x) { 618 __pn_ += __x; 619 return *this; 620 } 621 622 _LIBCPP_INLINE_VISIBILITY 623 path& operator+=(value_type __x) { 624 __pn_ += __x; 625 return *this; 626 } 627 628 template <class _ECharT> 629 typename enable_if<__can_convert_char<_ECharT>::value, path&>::type 630 operator+=(_ECharT __x) { 631 _PathCVT<_ECharT>::__append_source(__pn_, 632 basic_string_view<_ECharT>(&__x, 1)); 633 return *this; 634 } 635 636 template <class _Source> 637 _EnableIfPathable<_Source> operator+=(const _Source& __x) { 638 return this->concat(__x); 639 } 640 641 template <class _Source> 642 _EnableIfPathable<_Source> concat(const _Source& __x) { 643 _SourceCVT<_Source>::__append_source(__pn_, __x); 644 return *this; 645 } 646 647 template <class _InputIt> 648 path& concat(_InputIt __first, _InputIt __last) { 649 typedef typename iterator_traits<_InputIt>::value_type _ItVal; 650 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last); 651 return *this; 652 } 653 654 // modifiers 655 _LIBCPP_INLINE_VISIBILITY 656 void clear() noexcept { __pn_.clear(); } 657 658 path& make_preferred() { 659 #if defined(_LIBCPP_WIN32API) 660 _VSTD::replace(__pn_.begin(), __pn_.end(), L'/', L'\\'); 661 #endif 662 return *this; 663 } 664 665 _LIBCPP_INLINE_VISIBILITY 666 path& remove_filename() { 667 auto __fname = __filename(); 668 if (!__fname.empty()) 669 __pn_.erase(__fname.data() - __pn_.data()); 670 return *this; 671 } 672 673 path& replace_filename(const path& __replacement) { 674 remove_filename(); 675 return (*this /= __replacement); 676 } 677 678 path& replace_extension(const path& __replacement = path()); 679 680 _LIBCPP_INLINE_VISIBILITY 681 void swap(path& __rhs) noexcept { __pn_.swap(__rhs.__pn_); } 682 683 // private helper to allow reserving memory in the path 684 _LIBCPP_INLINE_VISIBILITY 685 void __reserve(size_t __s) { __pn_.reserve(__s); } 686 687 // native format observers 688 _LIBCPP_INLINE_VISIBILITY 689 const string_type& native() const noexcept { return __pn_; } 690 691 _LIBCPP_INLINE_VISIBILITY 692 const value_type* c_str() const noexcept { return __pn_.c_str(); } 693 694 _LIBCPP_INLINE_VISIBILITY operator string_type() const { return __pn_; } 695 696 #if defined(_LIBCPP_WIN32API) 697 _LIBCPP_INLINE_VISIBILITY _VSTD::wstring wstring() const { return __pn_; } 698 699 _VSTD::wstring generic_wstring() const { 700 _VSTD::wstring __s; 701 __s.resize(__pn_.size()); 702 _VSTD::replace_copy(__pn_.begin(), __pn_.end(), __s.begin(), '\\', '/'); 703 return __s; 704 } 705 706 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 707 template <class _ECharT, class _Traits = char_traits<_ECharT>, 708 class _Allocator = allocator<_ECharT> > 709 basic_string<_ECharT, _Traits, _Allocator> 710 string(const _Allocator& __a = _Allocator()) const { 711 using _Str = basic_string<_ECharT, _Traits, _Allocator>; 712 _Str __s(__a); 713 __s.reserve(__pn_.size()); 714 _PathExport<_ECharT>::__append(__s, __pn_); 715 return __s; 716 } 717 718 _LIBCPP_INLINE_VISIBILITY _VSTD::string string() const { 719 return string<char>(); 720 } 721 _LIBCPP_INLINE_VISIBILITY __u8_string u8string() const { 722 using _CVT = __narrow_to_utf8<sizeof(wchar_t) * __CHAR_BIT__>; 723 __u8_string __s; 724 __s.reserve(__pn_.size()); 725 _CVT()(back_inserter(__s), __pn_.data(), __pn_.data() + __pn_.size()); 726 return __s; 727 } 728 729 _LIBCPP_INLINE_VISIBILITY _VSTD::u16string u16string() const { 730 return string<char16_t>(); 731 } 732 _LIBCPP_INLINE_VISIBILITY _VSTD::u32string u32string() const { 733 return string<char32_t>(); 734 } 735 736 // generic format observers 737 template <class _ECharT, class _Traits = char_traits<_ECharT>, 738 class _Allocator = allocator<_ECharT> > 739 basic_string<_ECharT, _Traits, _Allocator> 740 generic_string(const _Allocator& __a = _Allocator()) const { 741 using _Str = basic_string<_ECharT, _Traits, _Allocator>; 742 _Str __s = string<_ECharT, _Traits, _Allocator>(__a); 743 // Note: This (and generic_u8string below) is slightly suboptimal as 744 // it iterates twice over the string; once to convert it to the right 745 // character type, and once to replace path delimiters. 746 _VSTD::replace(__s.begin(), __s.end(), 747 static_cast<_ECharT>('\\'), static_cast<_ECharT>('/')); 748 return __s; 749 } 750 751 _VSTD::string generic_string() const { return generic_string<char>(); } 752 _VSTD::u16string generic_u16string() const { return generic_string<char16_t>(); } 753 _VSTD::u32string generic_u32string() const { return generic_string<char32_t>(); } 754 __u8_string generic_u8string() const { 755 __u8_string __s = u8string(); 756 _VSTD::replace(__s.begin(), __s.end(), '\\', '/'); 757 return __s; 758 } 759 #endif /* !_LIBCPP_HAS_NO_LOCALIZATION */ 760 #else /* _LIBCPP_WIN32API */ 761 762 _LIBCPP_INLINE_VISIBILITY _VSTD::string string() const { return __pn_; } 763 #ifndef _LIBCPP_HAS_NO_CHAR8_T 764 _LIBCPP_INLINE_VISIBILITY _VSTD::u8string u8string() const { return _VSTD::u8string(__pn_.begin(), __pn_.end()); } 765 #else 766 _LIBCPP_INLINE_VISIBILITY _VSTD::string u8string() const { return __pn_; } 767 #endif 768 769 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 770 template <class _ECharT, class _Traits = char_traits<_ECharT>, 771 class _Allocator = allocator<_ECharT> > 772 basic_string<_ECharT, _Traits, _Allocator> 773 string(const _Allocator& __a = _Allocator()) const { 774 using _CVT = __widen_from_utf8<sizeof(_ECharT) * __CHAR_BIT__>; 775 using _Str = basic_string<_ECharT, _Traits, _Allocator>; 776 _Str __s(__a); 777 __s.reserve(__pn_.size()); 778 _CVT()(back_inserter(__s), __pn_.data(), __pn_.data() + __pn_.size()); 779 return __s; 780 } 781 782 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 783 _LIBCPP_INLINE_VISIBILITY _VSTD::wstring wstring() const { 784 return string<wchar_t>(); 785 } 786 #endif 787 _LIBCPP_INLINE_VISIBILITY _VSTD::u16string u16string() const { 788 return string<char16_t>(); 789 } 790 _LIBCPP_INLINE_VISIBILITY _VSTD::u32string u32string() const { 791 return string<char32_t>(); 792 } 793 #endif /* !_LIBCPP_HAS_NO_LOCALIZATION */ 794 795 // generic format observers 796 _VSTD::string generic_string() const { return __pn_; } 797 #ifndef _LIBCPP_HAS_NO_CHAR8_T 798 _VSTD::u8string generic_u8string() const { return _VSTD::u8string(__pn_.begin(), __pn_.end()); } 799 #else 800 _VSTD::string generic_u8string() const { return __pn_; } 801 #endif 802 803 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 804 template <class _ECharT, class _Traits = char_traits<_ECharT>, 805 class _Allocator = allocator<_ECharT> > 806 basic_string<_ECharT, _Traits, _Allocator> 807 generic_string(const _Allocator& __a = _Allocator()) const { 808 return string<_ECharT, _Traits, _Allocator>(__a); 809 } 810 811 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 812 _VSTD::wstring generic_wstring() const { return string<wchar_t>(); } 813 #endif 814 _VSTD::u16string generic_u16string() const { return string<char16_t>(); } 815 _VSTD::u32string generic_u32string() const { return string<char32_t>(); } 816 #endif /* !_LIBCPP_HAS_NO_LOCALIZATION */ 817 #endif /* !_LIBCPP_WIN32API */ 818 819 private: 820 int __compare(__string_view) const; 821 __string_view __root_name() const; 822 __string_view __root_directory() const; 823 __string_view __root_path_raw() const; 824 __string_view __relative_path() const; 825 __string_view __parent_path() const; 826 __string_view __filename() const; 827 __string_view __stem() const; 828 __string_view __extension() const; 829 830 public: 831 // compare 832 _LIBCPP_INLINE_VISIBILITY int compare(const path& __p) const noexcept { 833 return __compare(__p.__pn_); 834 } 835 _LIBCPP_INLINE_VISIBILITY int compare(const string_type& __s) const { 836 return __compare(__s); 837 } 838 _LIBCPP_INLINE_VISIBILITY int compare(__string_view __s) const { 839 return __compare(__s); 840 } 841 _LIBCPP_INLINE_VISIBILITY int compare(const value_type* __s) const { 842 return __compare(__s); 843 } 844 845 // decomposition 846 _LIBCPP_INLINE_VISIBILITY path root_name() const { 847 return string_type(__root_name()); 848 } 849 _LIBCPP_INLINE_VISIBILITY path root_directory() const { 850 return string_type(__root_directory()); 851 } 852 _LIBCPP_INLINE_VISIBILITY path root_path() const { 853 #if defined(_LIBCPP_WIN32API) 854 return string_type(__root_path_raw()); 855 #else 856 return root_name().append(string_type(__root_directory())); 857 #endif 858 } 859 _LIBCPP_INLINE_VISIBILITY path relative_path() const { 860 return string_type(__relative_path()); 861 } 862 _LIBCPP_INLINE_VISIBILITY path parent_path() const { 863 return string_type(__parent_path()); 864 } 865 _LIBCPP_INLINE_VISIBILITY path filename() const { 866 return string_type(__filename()); 867 } 868 _LIBCPP_INLINE_VISIBILITY path stem() const { return string_type(__stem()); } 869 _LIBCPP_INLINE_VISIBILITY path extension() const { 870 return string_type(__extension()); 871 } 872 873 // query 874 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY bool 875 empty() const noexcept { 876 return __pn_.empty(); 877 } 878 879 _LIBCPP_INLINE_VISIBILITY bool has_root_name() const { 880 return !__root_name().empty(); 881 } 882 _LIBCPP_INLINE_VISIBILITY bool has_root_directory() const { 883 return !__root_directory().empty(); 884 } 885 _LIBCPP_INLINE_VISIBILITY bool has_root_path() const { 886 return !__root_path_raw().empty(); 887 } 888 _LIBCPP_INLINE_VISIBILITY bool has_relative_path() const { 889 return !__relative_path().empty(); 890 } 891 _LIBCPP_INLINE_VISIBILITY bool has_parent_path() const { 892 return !__parent_path().empty(); 893 } 894 _LIBCPP_INLINE_VISIBILITY bool has_filename() const { 895 return !__filename().empty(); 896 } 897 _LIBCPP_INLINE_VISIBILITY bool has_stem() const { return !__stem().empty(); } 898 _LIBCPP_INLINE_VISIBILITY bool has_extension() const { 899 return !__extension().empty(); 900 } 901 902 _LIBCPP_INLINE_VISIBILITY bool is_absolute() const { 903 #if defined(_LIBCPP_WIN32API) 904 __string_view __root_name_str = __root_name(); 905 __string_view __root_dir = __root_directory(); 906 if (__root_name_str.size() == 2 && __root_name_str[1] == ':') { 907 // A drive letter with no root directory is relative, e.g. x:example. 908 return !__root_dir.empty(); 909 } 910 // If no root name, it's relative, e.g. \example is relative to the current drive 911 if (__root_name_str.empty()) 912 return false; 913 if (__root_name_str.size() < 3) 914 return false; 915 // A server root name, like \\server, is always absolute 916 if (__root_name_str[0] != '/' && __root_name_str[0] != '\\') 917 return false; 918 if (__root_name_str[1] != '/' && __root_name_str[1] != '\\') 919 return false; 920 // Seems to be a server root name 921 return true; 922 #else 923 return has_root_directory(); 924 #endif 925 } 926 _LIBCPP_INLINE_VISIBILITY bool is_relative() const { return !is_absolute(); } 927 928 // relative paths 929 path lexically_normal() const; 930 path lexically_relative(const path& __base) const; 931 932 _LIBCPP_INLINE_VISIBILITY path lexically_proximate(const path& __base) const { 933 path __result = this->lexically_relative(__base); 934 if (__result.native().empty()) 935 return *this; 936 return __result; 937 } 938 939 // iterators 940 class _LIBCPP_TYPE_VIS iterator; 941 typedef iterator const_iterator; 942 943 iterator begin() const; 944 iterator end() const; 945 946 #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 947 template <class _CharT, class _Traits> 948 _LIBCPP_INLINE_VISIBILITY friend 949 typename enable_if<is_same<_CharT, value_type>::value && 950 is_same<_Traits, char_traits<value_type> >::value, 951 basic_ostream<_CharT, _Traits>&>::type 952 operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) { 953 __os << _VSTD::__quoted(__p.native()); 954 return __os; 955 } 956 957 template <class _CharT, class _Traits> 958 _LIBCPP_INLINE_VISIBILITY friend 959 typename enable_if<!is_same<_CharT, value_type>::value || 960 !is_same<_Traits, char_traits<value_type> >::value, 961 basic_ostream<_CharT, _Traits>&>::type 962 operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) { 963 __os << _VSTD::__quoted(__p.string<_CharT, _Traits>()); 964 return __os; 965 } 966 967 template <class _CharT, class _Traits> 968 _LIBCPP_INLINE_VISIBILITY friend basic_istream<_CharT, _Traits>& 969 operator>>(basic_istream<_CharT, _Traits>& __is, path& __p) { 970 basic_string<_CharT, _Traits> __tmp; 971 __is >> _VSTD::__quoted(__tmp); 972 __p = __tmp; 973 return __is; 974 } 975 #endif // !_LIBCPP_HAS_NO_LOCALIZATION 976 977 friend _LIBCPP_INLINE_VISIBILITY bool operator==(const path& __lhs, const path& __rhs) noexcept { 978 return __lhs.__compare(__rhs.__pn_) == 0; 979 } 980 friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const path& __lhs, const path& __rhs) noexcept { 981 return __lhs.__compare(__rhs.__pn_) != 0; 982 } 983 friend _LIBCPP_INLINE_VISIBILITY bool operator<(const path& __lhs, const path& __rhs) noexcept { 984 return __lhs.__compare(__rhs.__pn_) < 0; 985 } 986 friend _LIBCPP_INLINE_VISIBILITY bool operator<=(const path& __lhs, const path& __rhs) noexcept { 987 return __lhs.__compare(__rhs.__pn_) <= 0; 988 } 989 friend _LIBCPP_INLINE_VISIBILITY bool operator>(const path& __lhs, const path& __rhs) noexcept { 990 return __lhs.__compare(__rhs.__pn_) > 0; 991 } 992 friend _LIBCPP_INLINE_VISIBILITY bool operator>=(const path& __lhs, const path& __rhs) noexcept { 993 return __lhs.__compare(__rhs.__pn_) >= 0; 994 } 995 996 friend _LIBCPP_INLINE_VISIBILITY path operator/(const path& __lhs, 997 const path& __rhs) { 998 path __result(__lhs); 999 __result /= __rhs; 1000 return __result; 1001 } 1002 private: 1003 inline _LIBCPP_INLINE_VISIBILITY path& 1004 __assign_view(__string_view const& __s) noexcept { 1005 __pn_ = string_type(__s); 1006 return *this; 1007 } 1008 string_type __pn_; 1009 }; 1010 1011 inline _LIBCPP_INLINE_VISIBILITY void swap(path& __lhs, path& __rhs) noexcept { 1012 __lhs.swap(__rhs); 1013 } 1014 1015 _LIBCPP_FUNC_VIS 1016 size_t hash_value(const path& __p) noexcept; 1017 1018 _LIBCPP_AVAILABILITY_FILESYSTEM_POP 1019 1020 _LIBCPP_END_NAMESPACE_FILESYSTEM 1021 1022 #endif // _LIBCPP_CXX03_LANG 1023 1024 #endif // _LIBCPP___FILESYSTEM_PATH_H 1025