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