]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/fs_path.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / fs_path.h
1 // Class filesystem::path -*- C++ -*-
2
3 // Copyright (C) 2014-2022 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/bits/fs_path.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{filesystem}
28 */
29
30 #ifndef _GLIBCXX_FS_PATH_H
31 #define _GLIBCXX_FS_PATH_H 1
32
33 #if __cplusplus >= 201703L
34
35 #include <type_traits>
36 #include <locale>
37 #include <iosfwd>
38 #include <iomanip>
39 #include <codecvt>
40 #include <string_view>
41 #include <system_error>
42 #include <bits/stl_algobase.h>
43 #include <bits/stl_pair.h>
44 #include <bits/locale_conv.h>
45 #include <ext/concurrence.h>
46 #include <bits/shared_ptr.h>
47 #include <bits/unique_ptr.h>
48
49 #if __cplusplus > 201703L
50 # include <compare>
51 #endif
52
53 #if defined(_WIN32) && !defined(__CYGWIN__)
54 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
55 # include <algorithm>
56 #endif
57
58 namespace std _GLIBCXX_VISIBILITY(default)
59 {
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61
62 namespace filesystem
63 {
64 _GLIBCXX_BEGIN_NAMESPACE_CXX11
65
66 class path;
67
68 /// @cond undocumented
69 namespace __detail
70 {
71 /// @addtogroup filesystem
72 /// @{
73 template<typename _CharT>
74 inline constexpr bool __is_encoded_char = false;
75 template<>
76 inline constexpr bool __is_encoded_char<char> = true;
77 #ifdef _GLIBCXX_USE_CHAR8_T
78 template<>
79 inline constexpr bool __is_encoded_char<char8_t> = true;
80 #endif
81 #if _GLIBCXX_USE_WCHAR_T
82 template<>
83 inline constexpr bool __is_encoded_char<wchar_t> = true;
84 #endif
85 template<>
86 inline constexpr bool __is_encoded_char<char16_t> = true;
87 template<>
88 inline constexpr bool __is_encoded_char<char32_t> = true;
89
90 #if __cpp_concepts >= 201907L
91 template<typename _Iter>
92 using __safe_iterator_traits = std::iterator_traits<_Iter>;
93 #else
94 template<typename _Iter>
95 struct __safe_iterator_traits : std::iterator_traits<_Iter>
96 { };
97
98 // Protect against ill-formed iterator_traits specializations in C++17
99 template<> struct __safe_iterator_traits<void*> { };
100 template<> struct __safe_iterator_traits<const void*> { };
101 template<> struct __safe_iterator_traits<volatile void*> { };
102 template<> struct __safe_iterator_traits<const volatile void*> { };
103 #endif
104
105 template<typename _Iter_traits, typename = void>
106 struct __is_path_iter_src
107 : false_type
108 { };
109
110 template<typename _Iter_traits>
111 struct __is_path_iter_src<_Iter_traits,
112 void_t<typename _Iter_traits::value_type>>
113 : bool_constant<__is_encoded_char<typename _Iter_traits::value_type>>
114 { };
115
116 template<typename _Source>
117 inline constexpr bool __is_path_src
118 = __is_path_iter_src<iterator_traits<decay_t<_Source>>>::value;
119
120 template<>
121 inline constexpr bool __is_path_src<path> = false;
122
123 template<>
124 inline constexpr bool __is_path_src<volatile path> = false;
125
126 template<>
127 inline constexpr bool __is_path_src<void*> = false;
128
129 template<>
130 inline constexpr bool __is_path_src<const void*> = false;
131
132 template<>
133 inline constexpr bool __is_path_src<volatile void*> = false;
134
135 template<>
136 inline constexpr bool __is_path_src<const volatile void*> = false;
137
138 template<typename _CharT, typename _Traits, typename _Alloc>
139 inline constexpr bool
140 __is_path_src<basic_string<_CharT, _Traits, _Alloc>>
141 = __is_encoded_char<_CharT>;
142
143 template<typename _CharT, typename _Traits>
144 inline constexpr bool
145 __is_path_src<basic_string_view<_CharT, _Traits>>
146 = __is_encoded_char<_CharT>;
147
148 // SFINAE constraint for Source parameters as required by [fs.path.req].
149 template<typename _Tp>
150 using _Path = enable_if_t<__is_path_src<_Tp>, path>;
151
152 // SFINAE constraint for InputIterator parameters as required by [fs.req].
153 template<typename _Iter, typename _Tr = __safe_iterator_traits<_Iter>>
154 using _Path2 = enable_if_t<__is_path_iter_src<_Tr>::value, path>;
155
156 #if __cpp_lib_concepts
157 template<typename _Iter>
158 constexpr bool __is_contiguous = std::contiguous_iterator<_Iter>;
159 #else
160 template<typename _Iter>
161 constexpr bool __is_contiguous = false;
162 #endif
163
164 template<typename _Tp>
165 constexpr bool __is_contiguous<_Tp*> = true;
166
167 template<typename _Tp, typename _Seq>
168 constexpr bool
169 __is_contiguous<__gnu_cxx::__normal_iterator<_Tp*, _Seq>> = true;
170
171 #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
172 // For POSIX treat char8_t sequences as char without encoding conversions.
173 template<typename _EcharT>
174 using __unified_u8_t
175 = __conditional_t<is_same_v<_EcharT, char8_t>, char, _EcharT>;
176 #else
177 template<typename _EcharT>
178 using __unified_u8_t = _EcharT;
179 #endif
180
181 // The __effective_range overloads convert a Source parameter into
182 // either a basic_string_view<C> or basic_string<C> containing the
183 // effective range of the Source, as defined in [fs.path.req].
184
185 template<typename _CharT, typename _Traits, typename _Alloc>
186 inline basic_string_view<_CharT>
187 __effective_range(const basic_string<_CharT, _Traits, _Alloc>& __source)
188 noexcept
189 { return __source; }
190
191 template<typename _CharT, typename _Traits>
192 inline basic_string_view<_CharT>
193 __effective_range(const basic_string_view<_CharT, _Traits>& __source)
194 noexcept
195 { return __source; }
196
197 // Return the effective range of an NTCTS.
198 template<typename _Source>
199 auto
200 __effective_range(const _Source& __source)
201 {
202 // Remove a level of normal/safe iterator indirection, or decay an array.
203 using _Iter = decltype(std::__niter_base(__source));
204 using value_type = typename iterator_traits<_Iter>::value_type;
205
206 if constexpr (__is_contiguous<_Iter>)
207 return basic_string_view<value_type>{&*__source};
208 else
209 {
210 // _Source is an input iterator that iterates over an NTCTS.
211 // Create a basic_string by reading until the null character.
212 basic_string<__unified_u8_t<value_type>> __str;
213 _Source __it = __source;
214 for (value_type __ch = *__it; __ch != value_type(); __ch = *++__it)
215 __str.push_back(__ch);
216 return __str;
217 }
218 }
219
220 // The value type of a Source parameter's effective range.
221 template<typename _Source>
222 struct __source_value_type_impl
223 {
224 using type
225 = typename __safe_iterator_traits<decay_t<_Source>>::value_type;
226 };
227
228 template<typename _CharT, typename _Traits, typename _Alloc>
229 struct __source_value_type_impl<basic_string<_CharT, _Traits, _Alloc>>
230 {
231 using type = _CharT;
232 };
233
234 template<typename _CharT, typename _Traits>
235 struct __source_value_type_impl<basic_string_view<_CharT, _Traits>>
236 {
237 using type = _CharT;
238 };
239
240 // The value type of a Source parameter's effective range.
241 template<typename _Source>
242 using __source_value_t = typename __source_value_type_impl<_Source>::type;
243
244 // SFINAE helper to check that an effective range has value_type char,
245 // as required by path constructors taking a std::locale parameter.
246 // The type _Tp must have already been checked by _Path<Tp> or _Path2<_Tp>.
247 template<typename _Tp, typename _Val = __source_value_t<_Tp>>
248 using __value_type_is_char
249 = std::enable_if_t<std::is_same_v<_Val, char>, _Val>;
250
251 // As above, but also allows char8_t, as required by u8path
252 // C++20 [depr.fs.path.factory]
253 template<typename _Tp, typename _Val = __source_value_t<_Tp>>
254 using __value_type_is_char_or_char8_t
255 = std::enable_if_t<std::is_same_v<_Val, char>
256 #ifdef _GLIBCXX_USE_CHAR8_T
257 || std::is_same_v<_Val, char8_t>
258 #endif
259 , _Val>;
260
261 // Create a basic_string<C> or basic_string_view<C> from an iterator range.
262 template<typename _InputIterator>
263 inline auto
264 __string_from_range(_InputIterator __first, _InputIterator __last)
265 {
266 using _EcharT
267 = typename std::iterator_traits<_InputIterator>::value_type;
268 static_assert(__is_encoded_char<_EcharT>); // C++17 [fs.req]/3
269
270 if constexpr (__is_contiguous<_InputIterator>)
271 {
272 // For contiguous iterators we can just return a string view.
273 if (auto __len = __last - __first) [[__likely__]]
274 return basic_string_view<_EcharT>(&*__first, __len);
275 return basic_string_view<_EcharT>();
276 }
277 else
278 {
279 // Conversion requires contiguous characters, so create a string.
280 return basic_string<__unified_u8_t<_EcharT>>(__first, __last);
281 }
282 }
283
284 /// @} group filesystem
285 } // namespace __detail
286 /// @endcond
287
288 /// @addtogroup filesystem
289 /// @{
290
291 /// A filesystem path
292 /// @ingroup filesystem
293 class path
294 {
295 public:
296 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
297 using value_type = wchar_t;
298 static constexpr value_type preferred_separator = L'\\';
299 #else
300 # ifdef _GLIBCXX_DOXYGEN
301 /// Windows uses wchar_t for path::value_type, POSIX uses char.
302 using value_type = __os_dependent__;
303 # else
304 using value_type = char;
305 # endif
306 static constexpr value_type preferred_separator = '/';
307 #endif
308 using string_type = std::basic_string<value_type>;
309
310 /// path::format is ignored in this implementation
311 enum format : unsigned char { native_format, generic_format, auto_format };
312
313 // constructors and destructor
314
315 path() noexcept { }
316
317 path(const path& __p) = default;
318
319 path(path&& __p) noexcept
320 : _M_pathname(std::move(__p._M_pathname)),
321 _M_cmpts(std::move(__p._M_cmpts))
322 { __p.clear(); }
323
324 path(string_type&& __source, format = auto_format)
325 : _M_pathname(std::move(__source))
326 { _M_split_cmpts(); }
327
328 template<typename _Source,
329 typename _Require = __detail::_Path<_Source>>
330 path(_Source const& __source, format = auto_format)
331 : _M_pathname(_S_convert(__detail::__effective_range(__source)))
332 { _M_split_cmpts(); }
333
334 template<typename _InputIterator,
335 typename _Require = __detail::_Path2<_InputIterator>>
336 path(_InputIterator __first, _InputIterator __last, format = auto_format)
337 : _M_pathname(_S_convert(__detail::__string_from_range(__first, __last)))
338 { _M_split_cmpts(); }
339
340 template<typename _Source,
341 typename _Require = __detail::_Path<_Source>,
342 typename _Require2 = __detail::__value_type_is_char<_Source>>
343 path(_Source const& __src, const locale& __loc, format = auto_format)
344 : _M_pathname(_S_convert_loc(__detail::__effective_range(__src), __loc))
345 { _M_split_cmpts(); }
346
347 template<typename _InputIterator,
348 typename _Require = __detail::_Path2<_InputIterator>,
349 typename _Req2 = __detail::__value_type_is_char<_InputIterator>>
350 path(_InputIterator __first, _InputIterator __last, const locale& __loc,
351 format = auto_format)
352 : _M_pathname(_S_convert_loc(__first, __last, __loc))
353 { _M_split_cmpts(); }
354
355 ~path() = default;
356
357 // assignments
358
359 path& operator=(const path&);
360 path& operator=(path&&) noexcept;
361 path& operator=(string_type&& __source);
362 path& assign(string_type&& __source);
363
364 template<typename _Source>
365 __detail::_Path<_Source>&
366 operator=(_Source const& __source)
367 { return *this = path(__source); }
368
369 template<typename _Source>
370 __detail::_Path<_Source>&
371 assign(_Source const& __source)
372 { return *this = path(__source); }
373
374 template<typename _InputIterator>
375 __detail::_Path2<_InputIterator>&
376 assign(_InputIterator __first, _InputIterator __last)
377 { return *this = path(__first, __last); }
378
379 // appends
380
381 path& operator/=(const path& __p);
382
383 template<typename _Source>
384 __detail::_Path<_Source>&
385 operator/=(_Source const& __source)
386 {
387 _M_append(_S_convert(__detail::__effective_range(__source)));
388 return *this;
389 }
390
391 template<typename _Source>
392 __detail::_Path<_Source>&
393 append(_Source const& __source)
394 {
395 _M_append(_S_convert(__detail::__effective_range(__source)));
396 return *this;
397 }
398
399 template<typename _InputIterator>
400 __detail::_Path2<_InputIterator>&
401 append(_InputIterator __first, _InputIterator __last)
402 {
403 _M_append(_S_convert(__detail::__string_from_range(__first, __last)));
404 return *this;
405 }
406
407 // concatenation
408
409 path& operator+=(const path& __x);
410 path& operator+=(const string_type& __x);
411 path& operator+=(const value_type* __x);
412 path& operator+=(value_type __x);
413 path& operator+=(basic_string_view<value_type> __x);
414
415 template<typename _Source>
416 __detail::_Path<_Source>&
417 operator+=(_Source const& __x) { return concat(__x); }
418
419 template<typename _CharT>
420 __detail::_Path2<_CharT*>&
421 operator+=(_CharT __x);
422
423 template<typename _Source>
424 __detail::_Path<_Source>&
425 concat(_Source const& __x)
426 {
427 _M_concat(_S_convert(__detail::__effective_range(__x)));
428 return *this;
429 }
430
431 template<typename _InputIterator>
432 __detail::_Path2<_InputIterator>&
433 concat(_InputIterator __first, _InputIterator __last)
434 {
435 _M_concat(_S_convert(__detail::__string_from_range(__first, __last)));
436 return *this;
437 }
438
439 // modifiers
440
441 void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); }
442
443 path& make_preferred();
444 path& remove_filename();
445 path& replace_filename(const path& __replacement);
446 path& replace_extension(const path& __replacement = path());
447
448 void swap(path& __rhs) noexcept;
449
450 // native format observers
451
452 const string_type& native() const noexcept { return _M_pathname; }
453 const value_type* c_str() const noexcept { return _M_pathname.c_str(); }
454 operator string_type() const { return _M_pathname; }
455
456 template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
457 typename _Allocator = std::allocator<_CharT>>
458 std::basic_string<_CharT, _Traits, _Allocator>
459 string(const _Allocator& __a = _Allocator()) const;
460
461 std::string string() const;
462 #if _GLIBCXX_USE_WCHAR_T
463 std::wstring wstring() const;
464 #endif
465 #ifdef _GLIBCXX_USE_CHAR8_T
466 __attribute__((__abi_tag__("__u8")))
467 std::u8string u8string() const;
468 #else
469 std::string u8string() const;
470 #endif // _GLIBCXX_USE_CHAR8_T
471 std::u16string u16string() const;
472 std::u32string u32string() const;
473
474 // generic format observers
475 template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
476 typename _Allocator = std::allocator<_CharT>>
477 std::basic_string<_CharT, _Traits, _Allocator>
478 generic_string(const _Allocator& __a = _Allocator()) const;
479
480 std::string generic_string() const;
481 #if _GLIBCXX_USE_WCHAR_T
482 std::wstring generic_wstring() const;
483 #endif
484 #ifdef _GLIBCXX_USE_CHAR8_T
485 __attribute__((__abi_tag__("__u8")))
486 std::u8string generic_u8string() const;
487 #else
488 std::string generic_u8string() const;
489 #endif // _GLIBCXX_USE_CHAR8_T
490 std::u16string generic_u16string() const;
491 std::u32string generic_u32string() const;
492
493 // compare
494
495 int compare(const path& __p) const noexcept;
496 int compare(const string_type& __s) const noexcept;
497 int compare(const value_type* __s) const noexcept;
498 int compare(basic_string_view<value_type> __s) const noexcept;
499
500 // decomposition
501
502 path root_name() const;
503 path root_directory() const;
504 path root_path() const;
505 path relative_path() const;
506 path parent_path() const;
507 path filename() const;
508 path stem() const;
509 path extension() const;
510
511 // query
512
513 [[nodiscard]] bool empty() const noexcept { return _M_pathname.empty(); }
514 bool has_root_name() const noexcept;
515 bool has_root_directory() const noexcept;
516 bool has_root_path() const noexcept;
517 bool has_relative_path() const noexcept;
518 bool has_parent_path() const noexcept;
519 bool has_filename() const noexcept;
520 bool has_stem() const noexcept;
521 bool has_extension() const noexcept;
522 bool is_absolute() const noexcept;
523 bool is_relative() const noexcept { return !is_absolute(); }
524
525 // generation
526 path lexically_normal() const;
527 path lexically_relative(const path& base) const;
528 path lexically_proximate(const path& base) const;
529
530 // iterators
531 class iterator;
532 using const_iterator = iterator;
533
534 iterator begin() const noexcept;
535 iterator end() const noexcept;
536
537 /// Write a path to a stream
538 template<typename _CharT, typename _Traits>
539 friend std::basic_ostream<_CharT, _Traits>&
540 operator<<(std::basic_ostream<_CharT, _Traits>& __os, const path& __p)
541 {
542 __os << std::quoted(__p.string<_CharT, _Traits>());
543 return __os;
544 }
545
546 /// Read a path from a stream
547 template<typename _CharT, typename _Traits>
548 friend std::basic_istream<_CharT, _Traits>&
549 operator>>(std::basic_istream<_CharT, _Traits>& __is, path& __p)
550 {
551 std::basic_string<_CharT, _Traits> __tmp;
552 if (__is >> std::quoted(__tmp))
553 __p = std::move(__tmp);
554 return __is;
555 }
556
557 // non-member operators
558
559 /// Compare paths
560 friend bool operator==(const path& __lhs, const path& __rhs) noexcept
561 { return path::_S_compare(__lhs, __rhs) == 0; }
562
563 #if __cpp_lib_three_way_comparison
564 /// Compare paths
565 friend strong_ordering
566 operator<=>(const path& __lhs, const path& __rhs) noexcept
567 { return path::_S_compare(__lhs, __rhs) <=> 0; }
568 #else
569 /// Compare paths
570 friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
571 { return !(__lhs == __rhs); }
572
573 /// Compare paths
574 friend bool operator<(const path& __lhs, const path& __rhs) noexcept
575 { return __lhs.compare(__rhs) < 0; }
576
577 /// Compare paths
578 friend bool operator<=(const path& __lhs, const path& __rhs) noexcept
579 { return !(__rhs < __lhs); }
580
581 /// Compare paths
582 friend bool operator>(const path& __lhs, const path& __rhs) noexcept
583 { return __rhs < __lhs; }
584
585 /// Compare paths
586 friend bool operator>=(const path& __lhs, const path& __rhs) noexcept
587 { return !(__lhs < __rhs); }
588 #endif
589
590 /// Append one path to another
591 friend path operator/(const path& __lhs, const path& __rhs)
592 {
593 path __result(__lhs);
594 __result /= __rhs;
595 return __result;
596 }
597
598 private:
599 enum class _Type : unsigned char {
600 _Multi = 0, _Root_name, _Root_dir, _Filename
601 };
602
603 path(basic_string_view<value_type> __str, _Type __type)
604 : _M_pathname(__str)
605 {
606 __glibcxx_assert(__type != _Type::_Multi);
607 _M_cmpts.type(__type);
608 }
609
610 enum class _Split { _Stem, _Extension };
611
612 void _M_append(basic_string_view<value_type>);
613 void _M_concat(basic_string_view<value_type>);
614
615 pair<const string_type*, size_t> _M_find_extension() const noexcept;
616
617 // path::_S_convert creates a basic_string<value_type> or
618 // basic_string_view<value_type> from a basic_string<C> or
619 // basic_string_view<C>, for an encoded character type C,
620 // performing the conversions required by [fs.path.type.cvt].
621 template<typename _Tp>
622 static auto
623 _S_convert(_Tp __str)
624 noexcept(is_same_v<typename _Tp::value_type, value_type>)
625 {
626 if constexpr (is_same_v<typename _Tp::value_type, value_type>)
627 return __str; // No conversion needed.
628 #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
629 else if constexpr (is_same_v<_Tp, std::u8string>)
630 // Calling _S_convert<char8_t> will return a u8string_view that
631 // refers to __str and would dangle after this function returns.
632 // Return a string_type instead, to avoid dangling.
633 return string_type(_S_convert(__str.data(),
634 __str.data() + __str.size()));
635 #endif
636 else
637 return _S_convert(__str.data(), __str.data() + __str.size());
638 }
639
640 template<typename _EcharT>
641 static auto
642 _S_convert(const _EcharT* __first, const _EcharT* __last);
643
644 // _S_convert_loc converts a range of char to string_type, using the
645 // supplied locale for encoding conversions.
646
647 static string_type
648 _S_convert_loc(const char* __first, const char* __last,
649 const std::locale& __loc);
650
651 template<typename _Iter>
652 static string_type
653 _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc)
654 {
655 const auto __s = __detail::__string_from_range(__first, __last);
656 return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
657 }
658
659 template<typename _Tp>
660 static string_type
661 _S_convert_loc(const _Tp& __s, const std::locale& __loc)
662 {
663 return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
664 }
665
666 template<typename _CharT, typename _Traits, typename _Allocator>
667 static basic_string<_CharT, _Traits, _Allocator>
668 _S_str_convert(basic_string_view<value_type>, const _Allocator&);
669
670 // Returns lhs.compare(rhs), but defined after path::iterator is complete.
671 __attribute__((__always_inline__))
672 static int
673 _S_compare(const path& __lhs, const path& __rhs) noexcept;
674
675 void _M_split_cmpts();
676
677 _Type _M_type() const noexcept { return _M_cmpts.type(); }
678
679 string_type _M_pathname;
680
681 struct _Cmpt;
682
683 struct _List
684 {
685 using value_type = _Cmpt;
686 using iterator = value_type*;
687 using const_iterator = const value_type*;
688
689 _List();
690 _List(const _List&);
691 _List(_List&&) = default;
692 _List& operator=(const _List&);
693 _List& operator=(_List&&) = default;
694 ~_List() = default;
695
696 _Type type() const noexcept
697 { return _Type(reinterpret_cast<uintptr_t>(_M_impl.get()) & 0x3); }
698
699 void type(_Type) noexcept;
700
701 int size() const noexcept; // zero unless type() == _Type::_Multi
702 bool empty() const noexcept; // true unless type() == _Type::_Multi
703 void clear();
704 void swap(_List& __l) noexcept { _M_impl.swap(__l._M_impl); }
705 int capacity() const noexcept;
706 void reserve(int, bool); ///< @pre type() == _Type::_Multi
707
708 // All the member functions below here have a precondition !empty()
709 // (and they should only be called from within the library).
710
711 iterator begin() noexcept;
712 iterator end() noexcept;
713 const_iterator begin() const noexcept;
714 const_iterator end() const noexcept;
715
716 value_type& front() noexcept;
717 value_type& back() noexcept;
718 const value_type& front() const noexcept;
719 const value_type& back() const noexcept;
720
721 void pop_back();
722 void _M_erase_from(const_iterator __pos); // erases [__pos,end())
723
724 struct _Impl;
725 struct _Impl_deleter
726 {
727 void operator()(_Impl*) const noexcept;
728 };
729 unique_ptr<_Impl, _Impl_deleter> _M_impl;
730 };
731 _List _M_cmpts;
732
733 struct _Parser;
734 };
735
736 /// @{
737 /// @relates std::filesystem::path
738
739 inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); }
740
741 size_t hash_value(const path& __p) noexcept;
742
743 /// @}
744
745 /// Exception type thrown by the Filesystem library
746 class filesystem_error : public std::system_error
747 {
748 public:
749 filesystem_error(const string& __what_arg, error_code __ec);
750
751 filesystem_error(const string& __what_arg, const path& __p1,
752 error_code __ec);
753
754 filesystem_error(const string& __what_arg, const path& __p1,
755 const path& __p2, error_code __ec);
756
757 filesystem_error(const filesystem_error&) = default;
758 filesystem_error& operator=(const filesystem_error&) = default;
759
760 // No move constructor or assignment operator.
761 // Copy rvalues instead, so that _M_impl is not left empty.
762
763 ~filesystem_error();
764
765 const path& path1() const noexcept;
766 const path& path2() const noexcept;
767 const char* what() const noexcept;
768
769 private:
770 struct _Impl;
771 std::__shared_ptr<const _Impl> _M_impl;
772 };
773
774 /// @cond undocumented
775 namespace __detail
776 {
777 [[noreturn]] inline void
778 __throw_conversion_error()
779 {
780 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
781 "Cannot convert character sequence",
782 std::make_error_code(errc::illegal_byte_sequence)));
783 }
784
785 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
786 template<typename _Tp>
787 inline std::wstring
788 __wstr_from_utf8(const _Tp& __str)
789 {
790 static_assert(std::is_same_v<typename _Tp::value_type, char>);
791 std::wstring __wstr;
792 // XXX This assumes native wide encoding is UTF-16.
793 std::codecvt_utf8_utf16<wchar_t> __wcvt;
794 const auto __p = __str.data();
795 if (!__str_codecvt_in_all(__p, __p + __str.size(), __wstr, __wcvt))
796 __detail::__throw_conversion_error();
797 return __wstr;
798 }
799 #endif
800
801 } // namespace __detail
802 /// @endcond
803
804
805 /** Create a path from a UTF-8-encoded sequence of char
806 *
807 * @relates std::filesystem::path
808 */
809 template<typename _InputIterator,
810 typename _Require = __detail::_Path2<_InputIterator>,
811 typename _CharT
812 = __detail::__value_type_is_char_or_char8_t<_InputIterator>>
813 inline path
814 u8path(_InputIterator __first, _InputIterator __last)
815 {
816 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
817 if constexpr (is_same_v<_CharT, char>)
818 return path{ __detail::__wstr_from_utf8(
819 __detail::__string_from_range(__first, __last)) };
820 else
821 return path{ __first, __last }; // constructor handles char8_t
822 #else
823 // This assumes native normal encoding is UTF-8.
824 return path{ __first, __last };
825 #endif
826 }
827
828 /** Create a path from a UTF-8-encoded sequence of char
829 *
830 * @relates std::filesystem::path
831 */
832 template<typename _Source,
833 typename _Require = __detail::_Path<_Source>,
834 typename _CharT = __detail::__value_type_is_char_or_char8_t<_Source>>
835 inline path
836 u8path(const _Source& __source)
837 {
838 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
839 if constexpr (is_same_v<_CharT, char>)
840 return path{ __detail::__wstr_from_utf8(
841 __detail::__effective_range(__source)) };
842 else
843 return path{ __source }; // constructor handles char8_t
844 #else
845 // This assumes native normal encoding is UTF-8.
846 return path{ __source };
847 #endif
848 }
849
850 /// @cond undocumented
851
852 struct path::_Cmpt : path
853 {
854 _Cmpt(basic_string_view<value_type> __s, _Type __t, size_t __pos)
855 : path(__s, __t), _M_pos(__pos) { }
856
857 _Cmpt() : _M_pos(-1) { }
858
859 size_t _M_pos;
860 };
861
862 template<typename _EcharT>
863 auto
864 path::_S_convert(const _EcharT* __f, const _EcharT* __l)
865 {
866 static_assert(__detail::__is_encoded_char<_EcharT>);
867
868 if constexpr (is_same_v<_EcharT, value_type>)
869 return basic_string_view<value_type>(__f, __l - __f);
870 #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
871 else if constexpr (is_same_v<_EcharT, char8_t>)
872 // For POSIX converting from char8_t to char is also 'noconv'
873 return string_view(reinterpret_cast<const char*>(__f), __l - __f);
874 #endif
875 else
876 {
877 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
878 std::wstring __wstr;
879 if constexpr (is_same_v<_EcharT, char>)
880 {
881 struct _UCvt : std::codecvt<wchar_t, char, std::mbstate_t>
882 { } __cvt;
883 if (__str_codecvt_in_all(__f, __l, __wstr, __cvt))
884 return __wstr;
885 }
886 #ifdef _GLIBCXX_USE_CHAR8_T
887 else if constexpr (is_same_v<_EcharT, char8_t>)
888 {
889 const auto __f2 = reinterpret_cast<const char*>(__f);
890 return __detail::__wstr_from_utf8(string_view(__f2, __l - __f));
891 }
892 #endif
893 else // char16_t or char32_t
894 {
895 struct _UCvt : std::codecvt<_EcharT, char, std::mbstate_t>
896 { } __cvt;
897 std::string __str;
898 if (__str_codecvt_out_all(__f, __l, __str, __cvt))
899 return __detail::__wstr_from_utf8(__str);
900 }
901 #else // ! windows
902 struct _UCvt : std::codecvt<_EcharT, char, std::mbstate_t>
903 { } __cvt;
904 std::string __str;
905 if (__str_codecvt_out_all(__f, __l, __str, __cvt))
906 return __str;
907 #endif
908 __detail::__throw_conversion_error();
909 }
910 }
911
912 /// @endcond
913
914 /// An iterator for the components of a path
915 class path::iterator
916 {
917 public:
918 using difference_type = std::ptrdiff_t;
919 using value_type = path;
920 using reference = const path&;
921 using pointer = const path*;
922 using iterator_category = std::bidirectional_iterator_tag;
923
924 iterator() noexcept : _M_path(nullptr), _M_cur(), _M_at_end() { }
925
926 iterator(const iterator&) = default;
927 iterator& operator=(const iterator&) = default;
928
929 reference operator*() const noexcept;
930 pointer operator->() const noexcept { return std::__addressof(**this); }
931
932 iterator& operator++() noexcept;
933
934 iterator operator++(int) noexcept
935 { auto __tmp = *this; ++*this; return __tmp; }
936
937 iterator& operator--() noexcept;
938
939 iterator operator--(int) noexcept
940 { auto __tmp = *this; --*this; return __tmp; }
941
942 friend bool
943 operator==(const iterator& __lhs, const iterator& __rhs) noexcept
944 { return __lhs._M_equals(__rhs); }
945
946 friend bool
947 operator!=(const iterator& __lhs, const iterator& __rhs) noexcept
948 { return !__lhs._M_equals(__rhs); }
949
950 private:
951 friend class path;
952
953 bool
954 _M_is_multi() const noexcept
955 { return _M_path->_M_type() == _Type::_Multi; }
956
957 friend difference_type
958 __path_iter_distance(const iterator& __first, const iterator& __last)
959 noexcept
960 {
961 __glibcxx_assert(__first._M_path != nullptr);
962 __glibcxx_assert(__first._M_path == __last._M_path);
963 if (__first._M_is_multi())
964 return std::distance(__first._M_cur, __last._M_cur);
965 else if (__first._M_at_end == __last._M_at_end)
966 return 0;
967 else
968 return __first._M_at_end ? -1 : 1;
969 }
970
971 friend void
972 __path_iter_advance(iterator& __i, difference_type __n) noexcept
973 {
974 if (__n == 1)
975 ++__i;
976 else if (__n == -1)
977 --__i;
978 else if (__n != 0)
979 {
980 __glibcxx_assert(__i._M_path != nullptr);
981 __glibcxx_assert(__i._M_is_multi());
982 // __glibcxx_assert(__i._M_path->_M_cmpts.end() - __i._M_cur >= __n);
983 __i._M_cur += __n;
984 }
985 }
986
987 iterator(const path* __path, path::_List::const_iterator __iter) noexcept
988 : _M_path(__path), _M_cur(__iter), _M_at_end()
989 { }
990
991 iterator(const path* __path, bool __at_end) noexcept
992 : _M_path(__path), _M_cur(), _M_at_end(__at_end)
993 { }
994
995 bool _M_equals(iterator) const noexcept;
996
997 const path* _M_path;
998 path::_List::const_iterator _M_cur;
999 bool _M_at_end; // only used when type != _Multi
1000 };
1001
1002
1003 inline path&
1004 path::operator=(path&& __p) noexcept
1005 {
1006 if (&__p == this) [[__unlikely__]]
1007 return *this;
1008
1009 _M_pathname = std::move(__p._M_pathname);
1010 _M_cmpts = std::move(__p._M_cmpts);
1011 __p.clear();
1012 return *this;
1013 }
1014
1015 inline path&
1016 path::operator=(string_type&& __source)
1017 { return *this = path(std::move(__source)); }
1018
1019 inline path&
1020 path::assign(string_type&& __source)
1021 { return *this = path(std::move(__source)); }
1022
1023 inline path&
1024 path::operator+=(const string_type& __x)
1025 {
1026 _M_concat(__x);
1027 return *this;
1028 }
1029
1030 inline path&
1031 path::operator+=(const value_type* __x)
1032 {
1033 _M_concat(__x);
1034 return *this;
1035 }
1036
1037 inline path&
1038 path::operator+=(value_type __x)
1039 {
1040 _M_concat(basic_string_view<value_type>(&__x, 1));
1041 return *this;
1042 }
1043
1044 inline path&
1045 path::operator+=(basic_string_view<value_type> __x)
1046 {
1047 _M_concat(__x);
1048 return *this;
1049 }
1050
1051 template<typename _CharT>
1052 inline __detail::_Path2<_CharT*>&
1053 path::operator+=(const _CharT __x)
1054 {
1055 _M_concat(_S_convert(&__x, &__x + 1));
1056 return *this;
1057 }
1058
1059 inline path&
1060 path::make_preferred()
1061 {
1062 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1063 std::replace(_M_pathname.begin(), _M_pathname.end(), L'/',
1064 preferred_separator);
1065 #endif
1066 return *this;
1067 }
1068
1069 inline void path::swap(path& __rhs) noexcept
1070 {
1071 _M_pathname.swap(__rhs._M_pathname);
1072 _M_cmpts.swap(__rhs._M_cmpts);
1073 }
1074
1075 /// @cond undocumented
1076 template<typename _CharT, typename _Traits, typename _Allocator>
1077 std::basic_string<_CharT, _Traits, _Allocator>
1078 path::_S_str_convert(basic_string_view<value_type> __str,
1079 const _Allocator& __a)
1080 {
1081 static_assert(!is_same_v<_CharT, value_type>);
1082
1083 using _WString = basic_string<_CharT, _Traits, _Allocator>;
1084
1085 if (__str.size() == 0)
1086 return _WString(__a);
1087
1088 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1089 // First convert native string from UTF-16 to to UTF-8.
1090 // XXX This assumes that the execution wide-character set is UTF-16.
1091 std::codecvt_utf8_utf16<value_type> __cvt;
1092
1093 using _CharAlloc = __alloc_rebind<_Allocator, char>;
1094 using _String = basic_string<char, char_traits<char>, _CharAlloc>;
1095 _String __u8str{_CharAlloc{__a}};
1096 const value_type* __wfirst = __str.data();
1097 const value_type* __wlast = __wfirst + __str.size();
1098 if (__str_codecvt_out_all(__wfirst, __wlast, __u8str, __cvt)) {
1099 if constexpr (is_same_v<_CharT, char>)
1100 return __u8str; // XXX assumes native ordinary encoding is UTF-8.
1101 else {
1102
1103 const char* __first = __u8str.data();
1104 const char* __last = __first + __u8str.size();
1105 #else
1106 const value_type* __first = __str.data();
1107 const value_type* __last = __first + __str.size();
1108 #endif
1109
1110 // Convert UTF-8 string to requested format.
1111 #ifdef _GLIBCXX_USE_CHAR8_T
1112 if constexpr (is_same_v<_CharT, char8_t>)
1113 return _WString(__first, __last, __a);
1114 else
1115 #endif
1116 {
1117 // Convert UTF-8 to wide string.
1118 _WString __wstr(__a);
1119 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> { } __cvt;
1120 if (__str_codecvt_in_all(__first, __last, __wstr, __cvt))
1121 return __wstr;
1122 }
1123
1124 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1125 } }
1126 #endif
1127 __detail::__throw_conversion_error();
1128 }
1129 /// @endcond
1130
1131 template<typename _CharT, typename _Traits, typename _Allocator>
1132 inline basic_string<_CharT, _Traits, _Allocator>
1133 path::string(const _Allocator& __a) const
1134 {
1135 if constexpr (is_same_v<_CharT, value_type>)
1136 return { _M_pathname.c_str(), _M_pathname.length(), __a };
1137 else
1138 return _S_str_convert<_CharT, _Traits>(_M_pathname, __a);
1139 }
1140
1141 inline std::string
1142 path::string() const { return string<char>(); }
1143
1144 #if _GLIBCXX_USE_WCHAR_T
1145 inline std::wstring
1146 path::wstring() const { return string<wchar_t>(); }
1147 #endif
1148
1149 #ifdef _GLIBCXX_USE_CHAR8_T
1150 inline std::u8string
1151 path::u8string() const { return string<char8_t>(); }
1152 #else
1153 inline std::string
1154 path::u8string() const
1155 {
1156 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1157 std::string __str;
1158 // convert from native wide encoding (assumed to be UTF-16) to UTF-8
1159 std::codecvt_utf8_utf16<value_type> __cvt;
1160 const value_type* __first = _M_pathname.data();
1161 const value_type* __last = __first + _M_pathname.size();
1162 if (__str_codecvt_out_all(__first, __last, __str, __cvt))
1163 return __str;
1164 __detail::__throw_conversion_error();
1165 #else
1166 return _M_pathname;
1167 #endif
1168 }
1169 #endif // _GLIBCXX_USE_CHAR8_T
1170
1171 inline std::u16string
1172 path::u16string() const { return string<char16_t>(); }
1173
1174 inline std::u32string
1175 path::u32string() const { return string<char32_t>(); }
1176
1177 template<typename _CharT, typename _Traits, typename _Allocator>
1178 inline std::basic_string<_CharT, _Traits, _Allocator>
1179 path::generic_string(const _Allocator& __a) const
1180 {
1181 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1182 const value_type __slash = L'/';
1183 #else
1184 const value_type __slash = '/';
1185 #endif
1186 using _Alloc2 = typename allocator_traits<_Allocator>::template
1187 rebind_alloc<value_type>;
1188 basic_string<value_type, char_traits<value_type>, _Alloc2> __str(__a);
1189
1190 if (_M_type() == _Type::_Root_dir)
1191 __str.assign(1, __slash);
1192 else
1193 {
1194 __str.reserve(_M_pathname.size());
1195 bool __add_slash = false;
1196 for (auto& __elem : *this)
1197 {
1198 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1199 if (__elem._M_type() == _Type::_Root_dir)
1200 {
1201 __str += __slash;
1202 continue;
1203 }
1204 #endif
1205 if (__add_slash)
1206 __str += __slash;
1207 __str += basic_string_view<value_type>(__elem._M_pathname);
1208 __add_slash = __elem._M_type() == _Type::_Filename;
1209 }
1210 }
1211
1212 if constexpr (is_same_v<_CharT, value_type>)
1213 return __str;
1214 else
1215 return _S_str_convert<_CharT, _Traits>(__str, __a);
1216 }
1217
1218 inline std::string
1219 path::generic_string() const
1220 { return generic_string<char>(); }
1221
1222 #if _GLIBCXX_USE_WCHAR_T
1223 inline std::wstring
1224 path::generic_wstring() const
1225 { return generic_string<wchar_t>(); }
1226 #endif
1227
1228 #ifdef _GLIBCXX_USE_CHAR8_T
1229 inline std::u8string
1230 path::generic_u8string() const
1231 { return generic_string<char8_t>(); }
1232 #else
1233 inline std::string
1234 path::generic_u8string() const
1235 { return generic_string(); }
1236 #endif
1237
1238 inline std::u16string
1239 path::generic_u16string() const
1240 { return generic_string<char16_t>(); }
1241
1242 inline std::u32string
1243 path::generic_u32string() const
1244 { return generic_string<char32_t>(); }
1245
1246 inline int
1247 path::compare(const string_type& __s) const noexcept
1248 { return compare(basic_string_view<value_type>(__s)); }
1249
1250 inline int
1251 path::compare(const value_type* __s) const noexcept
1252 { return compare(basic_string_view<value_type>(__s)); }
1253
1254 inline path
1255 path::filename() const
1256 {
1257 if (empty())
1258 return {};
1259 else if (_M_type() == _Type::_Filename)
1260 return *this;
1261 else if (_M_type() == _Type::_Multi)
1262 {
1263 if (_M_pathname.back() == preferred_separator)
1264 return {};
1265 auto& __last = *--end();
1266 if (__last._M_type() == _Type::_Filename)
1267 return __last;
1268 }
1269 return {};
1270 }
1271
1272 inline path
1273 path::stem() const
1274 {
1275 auto ext = _M_find_extension();
1276 if (ext.first && ext.second != 0)
1277 return path{ext.first->substr(0, ext.second)};
1278 return {};
1279 }
1280
1281 inline path
1282 path::extension() const
1283 {
1284 auto ext = _M_find_extension();
1285 if (ext.first && ext.second != string_type::npos)
1286 return path{ext.first->substr(ext.second)};
1287 return {};
1288 }
1289
1290 inline bool
1291 path::has_stem() const noexcept
1292 {
1293 auto ext = _M_find_extension();
1294 return ext.first && ext.second != 0;
1295 }
1296
1297 inline bool
1298 path::has_extension() const noexcept
1299 {
1300 auto ext = _M_find_extension();
1301 return ext.first && ext.second != string_type::npos;
1302 }
1303
1304 inline bool
1305 path::is_absolute() const noexcept
1306 {
1307 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1308 return has_root_name() && has_root_directory();
1309 #else
1310 return has_root_directory();
1311 #endif
1312 }
1313
1314 inline path::iterator
1315 path::begin() const noexcept
1316 {
1317 if (_M_type() == _Type::_Multi)
1318 return iterator(this, _M_cmpts.begin());
1319 return iterator(this, empty());
1320 }
1321
1322 inline path::iterator
1323 path::end() const noexcept
1324 {
1325 if (_M_type() == _Type::_Multi)
1326 return iterator(this, _M_cmpts.end());
1327 return iterator(this, true);
1328 }
1329
1330 inline path::iterator&
1331 path::iterator::operator++() noexcept
1332 {
1333 __glibcxx_assert(_M_path != nullptr);
1334 if (_M_is_multi())
1335 {
1336 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1337 ++_M_cur;
1338 }
1339 else
1340 {
1341 __glibcxx_assert(!_M_at_end);
1342 _M_at_end = true;
1343 }
1344 return *this;
1345 }
1346
1347 inline path::iterator&
1348 path::iterator::operator--() noexcept
1349 {
1350 __glibcxx_assert(_M_path != nullptr);
1351 if (_M_is_multi())
1352 {
1353 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
1354 --_M_cur;
1355 }
1356 else
1357 {
1358 __glibcxx_assert(_M_at_end);
1359 _M_at_end = false;
1360 }
1361 return *this;
1362 }
1363
1364 inline path::iterator::reference
1365 path::iterator::operator*() const noexcept
1366 {
1367 __glibcxx_assert(_M_path != nullptr);
1368 if (_M_is_multi())
1369 {
1370 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1371 return *_M_cur;
1372 }
1373 return *_M_path;
1374 }
1375
1376 inline bool
1377 path::iterator::_M_equals(iterator __rhs) const noexcept
1378 {
1379 if (_M_path != __rhs._M_path)
1380 return false;
1381 if (_M_path == nullptr)
1382 return true;
1383 if (_M_is_multi())
1384 return _M_cur == __rhs._M_cur;
1385 return _M_at_end == __rhs._M_at_end;
1386 }
1387
1388 // Define this now that path and path::iterator are complete.
1389 // It needs to consider the string_view(Range&&) constructor during
1390 // overload resolution, which depends on whether range<path> is satisfied,
1391 // which depends on whether path::iterator is complete.
1392 inline int
1393 path::_S_compare(const path& __lhs, const path& __rhs) noexcept
1394 { return __lhs.compare(__rhs); }
1395
1396 /// @} group filesystem
1397 _GLIBCXX_END_NAMESPACE_CXX11
1398 } // namespace filesystem
1399
1400 /// @cond undocumented
1401
1402 inline ptrdiff_t
1403 distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
1404 noexcept
1405 { return __path_iter_distance(__first, __last); }
1406
1407 template<typename _Distance>
1408 inline void
1409 advance(filesystem::path::iterator& __i, _Distance __n) noexcept
1410 { __path_iter_advance(__i, static_cast<ptrdiff_t>(__n)); }
1411
1412 extern template class __shared_ptr<const filesystem::filesystem_error::_Impl>;
1413
1414 /// @endcond
1415
1416 _GLIBCXX_END_NAMESPACE_VERSION
1417 } // namespace std
1418
1419 #endif // C++17
1420
1421 #endif // _GLIBCXX_FS_PATH_H