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