]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/fs_path.h
0a8ab0de2ffb0b18d63a445105648ef2fa053d08
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / fs_path.h
1 // Class filesystem::path -*- C++ -*-
2
3 // Copyright (C) 2014-2019 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 <utility>
36 #include <type_traits>
37 #include <locale>
38 #include <iosfwd>
39 #include <iomanip>
40 #include <codecvt>
41 #include <string_view>
42 #include <system_error>
43 #include <bits/stl_algobase.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 defined(_WIN32) && !defined(__CYGWIN__)
50 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
51 # include <algorithm>
52 #endif
53
54 namespace std _GLIBCXX_VISIBILITY(default)
55 {
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57
58 namespace filesystem
59 {
60 _GLIBCXX_BEGIN_NAMESPACE_CXX11
61
62 /** @addtogroup filesystem
63 * @{
64 */
65
66 /// A filesystem path.
67 class path
68 {
69 template<typename _CharT, typename _Ch = remove_const_t<_CharT>>
70 using __is_encoded_char
71 = __or_<is_same<_Ch, char>,
72 #ifdef _GLIBCXX_USE_CHAR8_T
73 is_same<_Ch, char8_t>,
74 #endif
75 is_same<_Ch, wchar_t>,
76 is_same<_Ch, char16_t>,
77 is_same<_Ch, char32_t>>;
78
79 template<typename _Iter,
80 typename _Iter_traits = std::iterator_traits<_Iter>>
81 using __is_path_iter_src
82 = __and_<__is_encoded_char<typename _Iter_traits::value_type>,
83 std::is_base_of<std::input_iterator_tag,
84 typename _Iter_traits::iterator_category>>;
85
86 template<typename _Iter>
87 static __is_path_iter_src<_Iter>
88 __is_path_src(_Iter, int);
89
90 template<typename _CharT, typename _Traits, typename _Alloc>
91 static __is_encoded_char<_CharT>
92 __is_path_src(const basic_string<_CharT, _Traits, _Alloc>&, int);
93
94 template<typename _CharT, typename _Traits>
95 static __is_encoded_char<_CharT>
96 __is_path_src(const basic_string_view<_CharT, _Traits>&, int);
97
98 template<typename _Unknown>
99 static std::false_type
100 __is_path_src(const _Unknown&, ...);
101
102 template<typename _Tp1, typename _Tp2>
103 struct __constructible_from;
104
105 template<typename _Iter>
106 struct __constructible_from<_Iter, _Iter>
107 : __is_path_iter_src<_Iter>
108 { };
109
110 template<typename _Source>
111 struct __constructible_from<_Source, void>
112 : decltype(__is_path_src(std::declval<_Source>(), 0))
113 { };
114
115 template<typename _Tp1, typename _Tp2 = void>
116 using _Path = typename
117 std::enable_if<__and_<__not_<is_same<remove_cv_t<_Tp1>, path>>,
118 __not_<is_void<remove_pointer_t<_Tp1>>>,
119 __constructible_from<_Tp1, _Tp2>>::value,
120 path>::type;
121
122 template<typename _Source>
123 static _Source
124 _S_range_begin(_Source __begin) { return __begin; }
125
126 struct __null_terminated { };
127
128 template<typename _Source>
129 static __null_terminated
130 _S_range_end(_Source) { return {}; }
131
132 template<typename _CharT, typename _Traits, typename _Alloc>
133 static const _CharT*
134 _S_range_begin(const basic_string<_CharT, _Traits, _Alloc>& __str)
135 { return __str.data(); }
136
137 template<typename _CharT, typename _Traits, typename _Alloc>
138 static const _CharT*
139 _S_range_end(const basic_string<_CharT, _Traits, _Alloc>& __str)
140 { return __str.data() + __str.size(); }
141
142 template<typename _CharT, typename _Traits>
143 static const _CharT*
144 _S_range_begin(const basic_string_view<_CharT, _Traits>& __str)
145 { return __str.data(); }
146
147 template<typename _CharT, typename _Traits>
148 static const _CharT*
149 _S_range_end(const basic_string_view<_CharT, _Traits>& __str)
150 { return __str.data() + __str.size(); }
151
152 template<typename _Tp,
153 typename _Iter = decltype(_S_range_begin(std::declval<_Tp>())),
154 typename _Val = typename std::iterator_traits<_Iter>::value_type>
155 using __value_type_is_char
156 = std::enable_if_t<std::is_same_v<std::remove_const_t<_Val>, char>>;
157
158 public:
159 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
160 using value_type = wchar_t;
161 static constexpr value_type preferred_separator = L'\\';
162 #else
163 # ifdef _GLIBCXX_DOXYGEN
164 /// Windows uses wchar_t for path::value_type, POSIX uses char.
165 using value_type = __os_dependent__;
166 # else
167 using value_type = char;
168 # endif
169 static constexpr value_type preferred_separator = '/';
170 #endif
171 using string_type = std::basic_string<value_type>;
172
173 /// path::format is ignored in this implementation
174 enum format : unsigned char { native_format, generic_format, auto_format };
175
176 // constructors and destructor
177
178 path() noexcept { }
179
180 path(const path& __p) = default;
181
182 path(path&& __p)
183 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_FULLY_DYNAMIC_STRING == 0
184 noexcept
185 #endif
186 : _M_pathname(std::move(__p._M_pathname)),
187 _M_cmpts(std::move(__p._M_cmpts))
188 { __p.clear(); }
189
190 path(string_type&& __source, format = auto_format)
191 : _M_pathname(std::move(__source))
192 { _M_split_cmpts(); }
193
194 template<typename _Source,
195 typename _Require = _Path<_Source>>
196 path(_Source const& __source, format = auto_format)
197 : _M_pathname(_S_convert(_S_range_begin(__source),
198 _S_range_end(__source)))
199 { _M_split_cmpts(); }
200
201 template<typename _InputIterator,
202 typename _Require = _Path<_InputIterator, _InputIterator>>
203 path(_InputIterator __first, _InputIterator __last, format = auto_format)
204 : _M_pathname(_S_convert(__first, __last))
205 { _M_split_cmpts(); }
206
207 template<typename _Source,
208 typename _Require = _Path<_Source>,
209 typename _Require2 = __value_type_is_char<_Source>>
210 path(_Source const& __source, const locale& __loc, format = auto_format)
211 : _M_pathname(_S_convert_loc(_S_range_begin(__source),
212 _S_range_end(__source), __loc))
213 { _M_split_cmpts(); }
214
215 template<typename _InputIterator,
216 typename _Require = _Path<_InputIterator, _InputIterator>,
217 typename _Require2 = __value_type_is_char<_InputIterator>>
218 path(_InputIterator __first, _InputIterator __last, const locale& __loc,
219 format = auto_format)
220 : _M_pathname(_S_convert_loc(__first, __last, __loc))
221 { _M_split_cmpts(); }
222
223 ~path() = default;
224
225 // assignments
226
227 path& operator=(const path&);
228 path& operator=(path&&) noexcept;
229 path& operator=(string_type&& __source);
230 path& assign(string_type&& __source);
231
232 template<typename _Source>
233 _Path<_Source>&
234 operator=(_Source const& __source)
235 { return *this = path(__source); }
236
237 template<typename _Source>
238 _Path<_Source>&
239 assign(_Source const& __source)
240 { return *this = path(__source); }
241
242 template<typename _InputIterator>
243 _Path<_InputIterator, _InputIterator>&
244 assign(_InputIterator __first, _InputIterator __last)
245 { return *this = path(__first, __last); }
246
247 // appends
248
249 path& operator/=(const path& __p);
250
251 template <class _Source>
252 _Path<_Source>&
253 operator/=(_Source const& __source)
254 {
255 _M_append(_S_convert(_S_range_begin(__source), _S_range_end(__source)));
256 return *this;
257 }
258
259 template<typename _Source>
260 _Path<_Source>&
261 append(_Source const& __source)
262 {
263 _M_append(_S_convert(_S_range_begin(__source), _S_range_end(__source)));
264 return *this;
265 }
266
267 template<typename _InputIterator>
268 _Path<_InputIterator, _InputIterator>&
269 append(_InputIterator __first, _InputIterator __last)
270 {
271 _M_append(_S_convert(__first, __last));
272 return *this;
273 }
274
275 // concatenation
276
277 path& operator+=(const path& __x);
278 path& operator+=(const string_type& __x);
279 path& operator+=(const value_type* __x);
280 path& operator+=(value_type __x);
281 path& operator+=(basic_string_view<value_type> __x);
282
283 template<typename _Source>
284 _Path<_Source>&
285 operator+=(_Source const& __x) { return concat(__x); }
286
287 template<typename _CharT>
288 _Path<_CharT*, _CharT*>&
289 operator+=(_CharT __x);
290
291 template<typename _Source>
292 _Path<_Source>&
293 concat(_Source const& __x)
294 {
295 _M_concat(_S_convert(_S_range_begin(__x), _S_range_end(__x)));
296 return *this;
297 }
298
299 template<typename _InputIterator>
300 _Path<_InputIterator, _InputIterator>&
301 concat(_InputIterator __first, _InputIterator __last)
302 {
303 _M_concat(_S_convert(__first, __last));
304 return *this;
305 }
306
307 // modifiers
308
309 void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); }
310
311 path& make_preferred();
312 path& remove_filename();
313 path& replace_filename(const path& __replacement);
314 path& replace_extension(const path& __replacement = path());
315
316 void swap(path& __rhs) noexcept;
317
318 // native format observers
319
320 const string_type& native() const noexcept { return _M_pathname; }
321 const value_type* c_str() const noexcept { return _M_pathname.c_str(); }
322 operator string_type() const { return _M_pathname; }
323
324 template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
325 typename _Allocator = std::allocator<_CharT>>
326 std::basic_string<_CharT, _Traits, _Allocator>
327 string(const _Allocator& __a = _Allocator()) const;
328
329 std::string string() const;
330 #if _GLIBCXX_USE_WCHAR_T
331 std::wstring wstring() const;
332 #endif
333 #ifdef _GLIBCXX_USE_CHAR8_T
334 __attribute__((__abi_tag__("__u8")))
335 std::u8string u8string() const;
336 #else
337 std::string u8string() const;
338 #endif // _GLIBCXX_USE_CHAR8_T
339 std::u16string u16string() const;
340 std::u32string u32string() const;
341
342 // generic format observers
343 template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
344 typename _Allocator = std::allocator<_CharT>>
345 std::basic_string<_CharT, _Traits, _Allocator>
346 generic_string(const _Allocator& __a = _Allocator()) const;
347
348 std::string generic_string() const;
349 #if _GLIBCXX_USE_WCHAR_T
350 std::wstring generic_wstring() const;
351 #endif
352 #ifdef _GLIBCXX_USE_CHAR8_T
353 __attribute__((__abi_tag__("__u8")))
354 std::u8string generic_u8string() const;
355 #else
356 std::string generic_u8string() const;
357 #endif // _GLIBCXX_USE_CHAR8_T
358 std::u16string generic_u16string() const;
359 std::u32string generic_u32string() const;
360
361 // compare
362
363 int compare(const path& __p) const noexcept;
364 int compare(const string_type& __s) const noexcept;
365 int compare(const value_type* __s) const noexcept;
366 int compare(basic_string_view<value_type> __s) const noexcept;
367
368 // decomposition
369
370 path root_name() const;
371 path root_directory() const;
372 path root_path() const;
373 path relative_path() const;
374 path parent_path() const;
375 path filename() const;
376 path stem() const;
377 path extension() const;
378
379 // query
380
381 [[nodiscard]] bool empty() const noexcept { return _M_pathname.empty(); }
382 bool has_root_name() const noexcept;
383 bool has_root_directory() const noexcept;
384 bool has_root_path() const noexcept;
385 bool has_relative_path() const noexcept;
386 bool has_parent_path() const noexcept;
387 bool has_filename() const noexcept;
388 bool has_stem() const noexcept;
389 bool has_extension() const noexcept;
390 bool is_absolute() const noexcept;
391 bool is_relative() const noexcept { return !is_absolute(); }
392
393 // generation
394 path lexically_normal() const;
395 path lexically_relative(const path& base) const;
396 path lexically_proximate(const path& base) const;
397
398 // iterators
399 class iterator;
400 using const_iterator = iterator;
401
402 iterator begin() const;
403 iterator end() const;
404
405 /// Write a path to a stream
406 template<typename _CharT, typename _Traits>
407 friend std::basic_ostream<_CharT, _Traits>&
408 operator<<(std::basic_ostream<_CharT, _Traits>& __os, const path& __p)
409 {
410 __os << std::quoted(__p.string<_CharT, _Traits>());
411 return __os;
412 }
413
414 /// Read a path from a stream
415 template<typename _CharT, typename _Traits>
416 friend std::basic_istream<_CharT, _Traits>&
417 operator>>(std::basic_istream<_CharT, _Traits>& __is, path& __p)
418 {
419 std::basic_string<_CharT, _Traits> __tmp;
420 if (__is >> std::quoted(__tmp))
421 __p = std::move(__tmp);
422 return __is;
423 }
424
425 // non-member operators
426
427 /// Compare paths
428 friend bool operator<(const path& __lhs, const path& __rhs) noexcept
429 { return __lhs.compare(__rhs) < 0; }
430
431 /// Compare paths
432 friend bool operator<=(const path& __lhs, const path& __rhs) noexcept
433 { return !(__rhs < __lhs); }
434
435 /// Compare paths
436 friend bool operator>(const path& __lhs, const path& __rhs) noexcept
437 { return __rhs < __lhs; }
438
439 /// Compare paths
440 friend bool operator>=(const path& __lhs, const path& __rhs) noexcept
441 { return !(__lhs < __rhs); }
442
443 /// Compare paths
444 friend bool operator==(const path& __lhs, const path& __rhs) noexcept
445 { return __lhs.compare(__rhs) == 0; }
446
447 /// Compare paths
448 friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
449 { return !(__lhs == __rhs); }
450
451 /// Append one path to another
452 friend path operator/(const path& __lhs, const path& __rhs)
453 {
454 path __result(__lhs);
455 __result /= __rhs;
456 return __result;
457 }
458
459 /// @cond undocumented
460 // Create a basic_string by reading until a null character.
461 template<typename _InputIterator,
462 typename _Traits = std::iterator_traits<_InputIterator>,
463 typename _CharT
464 = typename std::remove_cv_t<typename _Traits::value_type>>
465 static std::basic_string<_CharT>
466 _S_string_from_iter(_InputIterator __source)
467 {
468 std::basic_string<_CharT> __str;
469 for (_CharT __ch = *__source; __ch != _CharT(); __ch = *++__source)
470 __str.push_back(__ch);
471 return __str;
472 }
473 /// @endcond
474
475 private:
476 enum class _Type : unsigned char {
477 _Multi = 0, _Root_name, _Root_dir, _Filename
478 };
479
480 path(basic_string_view<value_type> __str, _Type __type)
481 : _M_pathname(__str)
482 {
483 __glibcxx_assert(__type != _Type::_Multi);
484 _M_cmpts.type(__type);
485 }
486
487 enum class _Split { _Stem, _Extension };
488
489 void _M_append(basic_string_view<value_type>);
490 void _M_concat(basic_string_view<value_type>);
491
492 pair<const string_type*, size_t> _M_find_extension() const noexcept;
493
494 template<typename _CharT>
495 struct _Cvt;
496
497 static basic_string_view<value_type>
498 _S_convert(value_type* __src, __null_terminated)
499 { return __src; }
500
501 static basic_string_view<value_type>
502 _S_convert(const value_type* __src, __null_terminated)
503 { return __src; }
504
505 static basic_string_view<value_type>
506 _S_convert(value_type* __first, value_type* __last)
507 { return {__first, __last - __first}; }
508
509 static basic_string_view<value_type>
510 _S_convert(const value_type* __first, const value_type* __last)
511 { return {__first, __last - __first}; }
512
513 template<typename _Iter>
514 static string_type
515 _S_convert(_Iter __first, _Iter __last)
516 {
517 using __value_type = typename std::iterator_traits<_Iter>::value_type;
518 return _Cvt<typename remove_cv<__value_type>::type>::
519 _S_convert(__first, __last);
520 }
521
522 template<typename _InputIterator>
523 static string_type
524 _S_convert(_InputIterator __src, __null_terminated)
525 {
526 // Read from iterator into basic_string until a null value is seen:
527 auto __s = _S_string_from_iter(__src);
528 // Convert (if needed) from iterator's value type to path::value_type:
529 return string_type(_S_convert(__s.data(), __s.data() + __s.size()));
530 }
531
532 static string_type
533 _S_convert_loc(const char* __first, const char* __last,
534 const std::locale& __loc);
535
536 template<typename _Iter>
537 static string_type
538 _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc)
539 {
540 const std::string __str(__first, __last);
541 return _S_convert_loc(__str.data(), __str.data()+__str.size(), __loc);
542 }
543
544 template<typename _InputIterator>
545 static string_type
546 _S_convert_loc(_InputIterator __src, __null_terminated,
547 const std::locale& __loc)
548 {
549 const std::string __s = _S_string_from_iter(__src);
550 return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
551 }
552
553 template<typename _CharT, typename _Traits, typename _Allocator>
554 static basic_string<_CharT, _Traits, _Allocator>
555 _S_str_convert(const string_type&, const _Allocator& __a);
556
557 void _M_split_cmpts();
558
559 _Type _M_type() const noexcept { return _M_cmpts.type(); }
560
561 string_type _M_pathname;
562
563 struct _Cmpt;
564
565 struct _List
566 {
567 using value_type = _Cmpt;
568 using iterator = value_type*;
569 using const_iterator = const value_type*;
570
571 _List();
572 _List(const _List&);
573 _List(_List&&) = default;
574 _List& operator=(const _List&);
575 _List& operator=(_List&&) = default;
576 ~_List() = default;
577
578 _Type type() const noexcept
579 { return _Type{reinterpret_cast<uintptr_t>(_M_impl.get()) & 0x3}; }
580
581 void type(_Type) noexcept;
582
583 int size() const noexcept; // zero unless type() == _Type::_Multi
584 bool empty() const noexcept; // true unless type() == _Type::_Multi
585 void clear();
586 void swap(_List& __l) noexcept { _M_impl.swap(__l._M_impl); }
587 int capacity() const noexcept;
588 void reserve(int, bool); ///< @pre type() == _Type::_Multi
589
590 // All the member functions below here have a precondition !empty()
591 // (and they should only be called from within the library).
592
593 iterator begin();
594 iterator end();
595 const_iterator begin() const;
596 const_iterator end() const;
597
598 value_type& front() noexcept;
599 value_type& back() noexcept;
600 const value_type& front() const noexcept;
601 const value_type& back() const noexcept;
602
603 void pop_back();
604 void _M_erase_from(const_iterator __pos); // erases [__pos,end())
605
606 struct _Impl;
607 struct _Impl_deleter
608 {
609 void operator()(_Impl*) const noexcept;
610 };
611 unique_ptr<_Impl, _Impl_deleter> _M_impl;
612 };
613 _List _M_cmpts;
614
615 struct _Parser;
616 };
617
618 /// @relates std::filesystem::path @{
619
620 inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); }
621
622 size_t hash_value(const path& __p) noexcept;
623
624 /// Create a path from a UTF-8-encoded sequence of char
625 template<typename _InputIterator>
626 inline auto
627 u8path(_InputIterator __first, _InputIterator __last)
628 -> decltype(filesystem::path(__first, __last, std::locale::classic()))
629 {
630 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
631 // XXX This assumes native wide encoding is UTF-16.
632 std::codecvt_utf8_utf16<path::value_type> __cvt;
633 path::string_type __tmp;
634 if constexpr (is_pointer_v<_InputIterator>)
635 {
636 if (__str_codecvt_in_all(__first, __last, __tmp, __cvt))
637 return path{ __tmp };
638 }
639 else
640 {
641 const std::string __u8str{__first, __last};
642 const char* const __ptr = __u8str.data();
643 if (__str_codecvt_in_all(__ptr, __ptr + __u8str.size(), __tmp, __cvt))
644 return path{ __tmp };
645 }
646 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
647 "Cannot convert character sequence",
648 std::make_error_code(errc::illegal_byte_sequence)));
649 #else
650 // This assumes native normal encoding is UTF-8.
651 return path{ __first, __last };
652 #endif
653 }
654
655 /// Create a path from a UTF-8-encoded sequence of char
656 template<typename _Source>
657 inline auto
658 u8path(const _Source& __source)
659 -> decltype(filesystem::path(__source, std::locale::classic()))
660 {
661 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
662 if constexpr (is_convertible_v<const _Source&, std::string_view>)
663 {
664 const std::string_view __s = __source;
665 return filesystem::u8path(__s.data(), __s.data() + __s.size());
666 }
667 else
668 {
669 std::string __s = path::_S_string_from_iter(__source);
670 return filesystem::u8path(__s.data(), __s.data() + __s.size());
671 }
672 #else
673 return path{ __source };
674 #endif
675 }
676
677 /// @}
678
679 /// Exception type thrown by the Filesystem library
680 class filesystem_error : public std::system_error
681 {
682 public:
683 filesystem_error(const string& __what_arg, error_code __ec);
684
685 filesystem_error(const string& __what_arg, const path& __p1,
686 error_code __ec);
687
688 filesystem_error(const string& __what_arg, const path& __p1,
689 const path& __p2, error_code __ec);
690
691 filesystem_error(const filesystem_error&) = default;
692 filesystem_error& operator=(const filesystem_error&) = default;
693
694 // No move constructor or assignment operator.
695 // Copy rvalues instead, so that _M_impl is not left empty.
696
697 ~filesystem_error();
698
699 const path& path1() const noexcept;
700 const path& path2() const noexcept;
701 const char* what() const noexcept;
702
703 private:
704 struct _Impl;
705 std::__shared_ptr<const _Impl> _M_impl;
706 };
707
708 /// @cond undocumented
709
710 struct path::_Cmpt : path
711 {
712 _Cmpt(basic_string_view<value_type> __s, _Type __t, size_t __pos)
713 : path(__s, __t), _M_pos(__pos) { }
714
715 _Cmpt() : _M_pos(-1) { }
716
717 size_t _M_pos;
718 };
719
720 // specialize _Cvt for degenerate 'noconv' case
721 template<>
722 struct path::_Cvt<path::value_type>
723 {
724 template<typename _Iter>
725 static string_type
726 _S_convert(_Iter __first, _Iter __last)
727 { return string_type{__first, __last}; }
728 };
729
730 #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
731 // For POSIX converting from char8_t to char is also 'noconv'
732 template<>
733 struct path::_Cvt<char8_t>
734 {
735 template<typename _Iter>
736 static string_type
737 _S_convert(_Iter __first, _Iter __last)
738 { return string_type(__first, __last); }
739 };
740 #endif
741
742 template<typename _CharT>
743 struct path::_Cvt
744 {
745 static string_type
746 _S_convert(const _CharT* __f, const _CharT* __l)
747 {
748 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
749 std::wstring __wstr;
750 if constexpr (is_same_v<_CharT, char>)
751 {
752 struct _UCvt : std::codecvt<wchar_t, char, std::mbstate_t>
753 { } __cvt;
754 if (__str_codecvt_in_all(__f, __l, __wstr, __cvt))
755 return __wstr;
756 }
757 #ifdef _GLIBCXX_USE_CHAR8_T
758 else if constexpr (is_same_v<_CharT, char8_t>)
759 {
760 const char* __f2 = (const char*)__f;
761 const char* __l2 = (const char*)__l;
762 std::codecvt_utf8_utf16<wchar_t> __wcvt;
763 if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt))
764 return __wstr;
765 }
766 #endif
767 else // char16_t or char32_t
768 {
769 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t>
770 { } __cvt;
771 std::string __str;
772 if (__str_codecvt_out_all(__f, __l, __str, __cvt))
773 {
774 const char* __f2 = __str.data();
775 const char* __l2 = __f2 + __str.size();
776 std::codecvt_utf8_utf16<wchar_t> __wcvt;
777 if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt))
778 return __wstr;
779 }
780 }
781 #else // ! windows
782 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t>
783 { } __cvt;
784 std::string __str;
785 if (__str_codecvt_out_all(__f, __l, __str, __cvt))
786 return __str;
787 #endif
788 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
789 "Cannot convert character sequence",
790 std::make_error_code(errc::illegal_byte_sequence)));
791 }
792
793 static string_type
794 _S_convert(_CharT* __f, _CharT* __l)
795 {
796 return _S_convert(const_cast<const _CharT*>(__f),
797 const_cast<const _CharT*>(__l));
798 }
799
800 template<typename _Iter>
801 static string_type
802 _S_convert(_Iter __first, _Iter __last)
803 {
804 const std::basic_string<_CharT> __str(__first, __last);
805 return _S_convert(__str.data(), __str.data() + __str.size());
806 }
807
808 template<typename _Iter, typename _Cont>
809 static string_type
810 _S_convert(__gnu_cxx::__normal_iterator<_Iter, _Cont> __first,
811 __gnu_cxx::__normal_iterator<_Iter, _Cont> __last)
812 { return _S_convert(__first.base(), __last.base()); }
813 };
814
815 /// @endcond
816
817 /// An iterator for the components of a path
818 class path::iterator
819 {
820 public:
821 using difference_type = std::ptrdiff_t;
822 using value_type = path;
823 using reference = const path&;
824 using pointer = const path*;
825 using iterator_category = std::bidirectional_iterator_tag;
826
827 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
828
829 iterator(const iterator&) = default;
830 iterator& operator=(const iterator&) = default;
831
832 reference operator*() const;
833 pointer operator->() const { return std::__addressof(**this); }
834
835 iterator& operator++();
836 iterator operator++(int) { auto __tmp = *this; ++*this; return __tmp; }
837
838 iterator& operator--();
839 iterator operator--(int) { auto __tmp = *this; --*this; return __tmp; }
840
841 friend bool operator==(const iterator& __lhs, const iterator& __rhs)
842 { return __lhs._M_equals(__rhs); }
843
844 friend bool operator!=(const iterator& __lhs, const iterator& __rhs)
845 { return !__lhs._M_equals(__rhs); }
846
847 private:
848 friend class path;
849
850 bool _M_is_multi() const { return _M_path->_M_type() == _Type::_Multi; }
851
852 friend difference_type
853 __path_iter_distance(const iterator& __first, const iterator& __last)
854 {
855 __glibcxx_assert(__first._M_path != nullptr);
856 __glibcxx_assert(__first._M_path == __last._M_path);
857 if (__first._M_is_multi())
858 return std::distance(__first._M_cur, __last._M_cur);
859 else if (__first._M_at_end == __last._M_at_end)
860 return 0;
861 else
862 return __first._M_at_end ? -1 : 1;
863 }
864
865 friend void
866 __path_iter_advance(iterator& __i, difference_type __n)
867 {
868 if (__n == 1)
869 ++__i;
870 else if (__n == -1)
871 --__i;
872 else if (__n != 0)
873 {
874 __glibcxx_assert(__i._M_path != nullptr);
875 __glibcxx_assert(__i._M_is_multi());
876 // __glibcxx_assert(__i._M_path->_M_cmpts.end() - __i._M_cur >= __n);
877 __i._M_cur += __n;
878 }
879 }
880
881 iterator(const path* __path, path::_List::const_iterator __iter)
882 : _M_path(__path), _M_cur(__iter), _M_at_end()
883 { }
884
885 iterator(const path* __path, bool __at_end)
886 : _M_path(__path), _M_cur(), _M_at_end(__at_end)
887 { }
888
889 bool _M_equals(iterator) const;
890
891 const path* _M_path;
892 path::_List::const_iterator _M_cur;
893 bool _M_at_end; // only used when type != _Multi
894 };
895
896
897 inline path&
898 path::operator=(path&& __p) noexcept
899 {
900 if (&__p == this) [[__unlikely__]]
901 return *this;
902
903 _M_pathname = std::move(__p._M_pathname);
904 _M_cmpts = std::move(__p._M_cmpts);
905 __p.clear();
906 return *this;
907 }
908
909 inline path&
910 path::operator=(string_type&& __source)
911 { return *this = path(std::move(__source)); }
912
913 inline path&
914 path::assign(string_type&& __source)
915 { return *this = path(std::move(__source)); }
916
917 inline path&
918 path::operator+=(const string_type& __x)
919 {
920 _M_concat(__x);
921 return *this;
922 }
923
924 inline path&
925 path::operator+=(const value_type* __x)
926 {
927 _M_concat(__x);
928 return *this;
929 }
930
931 inline path&
932 path::operator+=(value_type __x)
933 {
934 _M_concat(basic_string_view<value_type>(&__x, 1));
935 return *this;
936 }
937
938 inline path&
939 path::operator+=(basic_string_view<value_type> __x)
940 {
941 _M_concat(__x);
942 return *this;
943 }
944
945 template<typename _CharT>
946 inline path::_Path<_CharT*, _CharT*>&
947 path::operator+=(_CharT __x)
948 {
949 auto* __addr = std::__addressof(__x);
950 return concat(__addr, __addr + 1);
951 }
952
953 inline path&
954 path::make_preferred()
955 {
956 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
957 std::replace(_M_pathname.begin(), _M_pathname.end(), L'/',
958 preferred_separator);
959 #endif
960 return *this;
961 }
962
963 inline void path::swap(path& __rhs) noexcept
964 {
965 _M_pathname.swap(__rhs._M_pathname);
966 _M_cmpts.swap(__rhs._M_cmpts);
967 }
968
969 /// @cond undocumented
970 template<typename _CharT, typename _Traits, typename _Allocator>
971 std::basic_string<_CharT, _Traits, _Allocator>
972 path::_S_str_convert(const string_type& __str, const _Allocator& __a)
973 {
974 static_assert(!is_same_v<_CharT, value_type>);
975
976 using _WString = basic_string<_CharT, _Traits, _Allocator>;
977
978 if (__str.size() == 0)
979 return _WString(__a);
980
981 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
982 // First convert native string from UTF-16 to to UTF-8.
983 // XXX This assumes that the execution wide-character set is UTF-16.
984 std::codecvt_utf8_utf16<value_type> __cvt;
985
986 using _CharAlloc = __alloc_rebind<_Allocator, char>;
987 using _String = basic_string<char, char_traits<char>, _CharAlloc>;
988 _String __u8str{_CharAlloc{__a}};
989 const value_type* __wfirst = __str.data();
990 const value_type* __wlast = __wfirst + __str.size();
991 if (__str_codecvt_out_all(__wfirst, __wlast, __u8str, __cvt)) {
992 if constexpr (is_same_v<_CharT, char>)
993 return __u8str; // XXX assumes native ordinary encoding is UTF-8.
994 else {
995
996 const char* __first = __u8str.data();
997 const char* __last = __first + __u8str.size();
998 #else
999 const value_type* __first = __str.data();
1000 const value_type* __last = __first + __str.size();
1001 #endif
1002
1003 // Convert UTF-8 string to requested format.
1004 #ifdef _GLIBCXX_USE_CHAR8_T
1005 if constexpr (is_same_v<_CharT, char8_t>)
1006 return _WString(__first, __last, __a);
1007 else
1008 #endif
1009 {
1010 // Convert UTF-8 to wide string.
1011 _WString __wstr(__a);
1012 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> { } __cvt;
1013 if (__str_codecvt_in_all(__first, __last, __wstr, __cvt))
1014 return __wstr;
1015 }
1016
1017 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1018 } }
1019 #endif
1020 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1021 "Cannot convert character sequence",
1022 std::make_error_code(errc::illegal_byte_sequence)));
1023 }
1024 /// @endcond
1025
1026 template<typename _CharT, typename _Traits, typename _Allocator>
1027 inline basic_string<_CharT, _Traits, _Allocator>
1028 path::string(const _Allocator& __a) const
1029 {
1030 if constexpr (is_same_v<_CharT, value_type>)
1031 return { _M_pathname.c_str(), _M_pathname.length(), __a };
1032 else
1033 return _S_str_convert<_CharT, _Traits>(_M_pathname, __a);
1034 }
1035
1036 inline std::string
1037 path::string() const { return string<char>(); }
1038
1039 #if _GLIBCXX_USE_WCHAR_T
1040 inline std::wstring
1041 path::wstring() const { return string<wchar_t>(); }
1042 #endif
1043
1044 #ifdef _GLIBCXX_USE_CHAR8_T
1045 inline std::u8string
1046 path::u8string() const { return string<char8_t>(); }
1047 #else
1048 inline std::string
1049 path::u8string() const
1050 {
1051 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1052 std::string __str;
1053 // convert from native wide encoding (assumed to be UTF-16) to UTF-8
1054 std::codecvt_utf8_utf16<value_type> __cvt;
1055 const value_type* __first = _M_pathname.data();
1056 const value_type* __last = __first + _M_pathname.size();
1057 if (__str_codecvt_out_all(__first, __last, __str, __cvt))
1058 return __str;
1059 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1060 "Cannot convert character sequence",
1061 std::make_error_code(errc::illegal_byte_sequence)));
1062 #else
1063 return _M_pathname;
1064 #endif
1065 }
1066 #endif // _GLIBCXX_USE_CHAR8_T
1067
1068 inline std::u16string
1069 path::u16string() const { return string<char16_t>(); }
1070
1071 inline std::u32string
1072 path::u32string() const { return string<char32_t>(); }
1073
1074 template<typename _CharT, typename _Traits, typename _Allocator>
1075 inline std::basic_string<_CharT, _Traits, _Allocator>
1076 path::generic_string(const _Allocator& __a) const
1077 {
1078 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1079 const value_type __slash = L'/';
1080 #else
1081 const value_type __slash = '/';
1082 #endif
1083 string_type __str(__a);
1084
1085 if (_M_type() == _Type::_Root_dir)
1086 __str.assign(1, __slash);
1087 else
1088 {
1089 __str.reserve(_M_pathname.size());
1090 bool __add_slash = false;
1091 for (auto& __elem : *this)
1092 {
1093 if (__add_slash)
1094 __str += __slash;
1095 __str += __elem._M_pathname;
1096 __add_slash = __elem._M_type() == _Type::_Filename;
1097 }
1098 }
1099
1100 if constexpr (is_same_v<_CharT, value_type>)
1101 return __str;
1102 else
1103 return _S_str_convert<_CharT, _Traits>(__str, __a);
1104 }
1105
1106 inline std::string
1107 path::generic_string() const
1108 { return generic_string<char>(); }
1109
1110 #if _GLIBCXX_USE_WCHAR_T
1111 inline std::wstring
1112 path::generic_wstring() const
1113 { return generic_string<wchar_t>(); }
1114 #endif
1115
1116 #ifdef _GLIBCXX_USE_CHAR8_T
1117 inline std::u8string
1118 path::generic_u8string() const
1119 { return generic_string<char8_t>(); }
1120 #else
1121 inline std::string
1122 path::generic_u8string() const
1123 { return generic_string(); }
1124 #endif
1125
1126 inline std::u16string
1127 path::generic_u16string() const
1128 { return generic_string<char16_t>(); }
1129
1130 inline std::u32string
1131 path::generic_u32string() const
1132 { return generic_string<char32_t>(); }
1133
1134 inline int
1135 path::compare(const string_type& __s) const noexcept
1136 { return compare(basic_string_view<value_type>(__s)); }
1137
1138 inline int
1139 path::compare(const value_type* __s) const noexcept
1140 { return compare(basic_string_view<value_type>(__s)); }
1141
1142 inline path
1143 path::filename() const
1144 {
1145 if (empty())
1146 return {};
1147 else if (_M_type() == _Type::_Filename)
1148 return *this;
1149 else if (_M_type() == _Type::_Multi)
1150 {
1151 if (_M_pathname.back() == preferred_separator)
1152 return {};
1153 auto& __last = *--end();
1154 if (__last._M_type() == _Type::_Filename)
1155 return __last;
1156 }
1157 return {};
1158 }
1159
1160 inline path
1161 path::stem() const
1162 {
1163 auto ext = _M_find_extension();
1164 if (ext.first && ext.second != 0)
1165 return path{ext.first->substr(0, ext.second)};
1166 return {};
1167 }
1168
1169 inline path
1170 path::extension() const
1171 {
1172 auto ext = _M_find_extension();
1173 if (ext.first && ext.second != string_type::npos)
1174 return path{ext.first->substr(ext.second)};
1175 return {};
1176 }
1177
1178 inline bool
1179 path::has_stem() const noexcept
1180 {
1181 auto ext = _M_find_extension();
1182 return ext.first && ext.second != 0;
1183 }
1184
1185 inline bool
1186 path::has_extension() const noexcept
1187 {
1188 auto ext = _M_find_extension();
1189 return ext.first && ext.second != string_type::npos;
1190 }
1191
1192 inline bool
1193 path::is_absolute() const noexcept
1194 {
1195 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1196 return has_root_name() && has_root_directory();
1197 #else
1198 return has_root_directory();
1199 #endif
1200 }
1201
1202 inline path::iterator
1203 path::begin() const
1204 {
1205 if (_M_type() == _Type::_Multi)
1206 return iterator(this, _M_cmpts.begin());
1207 return iterator(this, empty());
1208 }
1209
1210 inline path::iterator
1211 path::end() const
1212 {
1213 if (_M_type() == _Type::_Multi)
1214 return iterator(this, _M_cmpts.end());
1215 return iterator(this, true);
1216 }
1217
1218 inline path::iterator&
1219 path::iterator::operator++()
1220 {
1221 __glibcxx_assert(_M_path != nullptr);
1222 if (_M_path->_M_type() == _Type::_Multi)
1223 {
1224 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1225 ++_M_cur;
1226 }
1227 else
1228 {
1229 __glibcxx_assert(!_M_at_end);
1230 _M_at_end = true;
1231 }
1232 return *this;
1233 }
1234
1235 inline path::iterator&
1236 path::iterator::operator--()
1237 {
1238 __glibcxx_assert(_M_path != nullptr);
1239 if (_M_path->_M_type() == _Type::_Multi)
1240 {
1241 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
1242 --_M_cur;
1243 }
1244 else
1245 {
1246 __glibcxx_assert(_M_at_end);
1247 _M_at_end = false;
1248 }
1249 return *this;
1250 }
1251
1252 inline path::iterator::reference
1253 path::iterator::operator*() const
1254 {
1255 __glibcxx_assert(_M_path != nullptr);
1256 if (_M_path->_M_type() == _Type::_Multi)
1257 {
1258 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1259 return *_M_cur;
1260 }
1261 return *_M_path;
1262 }
1263
1264 inline bool
1265 path::iterator::_M_equals(iterator __rhs) const
1266 {
1267 if (_M_path != __rhs._M_path)
1268 return false;
1269 if (_M_path == nullptr)
1270 return true;
1271 if (_M_path->_M_type() == path::_Type::_Multi)
1272 return _M_cur == __rhs._M_cur;
1273 return _M_at_end == __rhs._M_at_end;
1274 }
1275
1276 // @} group filesystem
1277 _GLIBCXX_END_NAMESPACE_CXX11
1278 } // namespace filesystem
1279
1280 inline ptrdiff_t
1281 distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
1282 { return __path_iter_distance(__first, __last); }
1283
1284 template<typename _InputIterator, typename _Distance>
1285 void
1286 advance(filesystem::path::iterator& __i, _Distance __n)
1287 { __path_iter_advance(__i, static_cast<ptrdiff_t>(__n)); }
1288
1289 extern template class __shared_ptr<const filesystem::filesystem_error::_Impl>;
1290
1291 _GLIBCXX_END_NAMESPACE_VERSION
1292 } // namespace std
1293
1294 #endif // C++17
1295
1296 #endif // _GLIBCXX_FS_PATH_H