1 // Class filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
30 #ifndef _GLIBCXX_FS_PATH_H
31 #define _GLIBCXX_FS_PATH_H 1
33 #if __cplusplus >= 201703L
36 #include <type_traits>
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>
47 #if defined(_WIN32) && !defined(__CYGWIN__)
48 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
52 namespace std
_GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
58 _GLIBCXX_BEGIN_NAMESPACE_CXX11
65 /// A filesystem path.
68 template<typename _CharT
>
69 struct __is_encoded_char
: std::false_type
{ };
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
>,
75 std::is_base_of
<std::input_iterator_tag
,
76 typename
_Iter_traits::iterator_category
>>;
78 template<typename _Iter
>
79 static __is_path_iter_src
<_Iter
>
80 __is_path_src(_Iter
, int);
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);
86 template<typename _CharT
, typename _Traits
>
87 static __is_encoded_char
<_CharT
>
88 __is_path_src(const basic_string_view
<_CharT
, _Traits
>&, int);
90 template<typename _Unknown
>
91 static std::false_type
92 __is_path_src(const _Unknown
&, ...);
94 template<typename _Tp1
, typename _Tp2
>
95 struct __constructible_from
;
97 template<typename _Iter
>
98 struct __constructible_from
<_Iter
, _Iter
>
99 : __is_path_iter_src
<_Iter
>
102 template<typename _Source
>
103 struct __constructible_from
<_Source
, void>
104 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
107 template<typename _Tp1
, typename _Tp2
= void>
108 using _Path
= typename
109 std::enable_if
<__and_
<__not_
<is_same
<_Tp1
, path
>>,
110 __constructible_from
<_Tp1
, _Tp2
>>::value
,
113 template<typename _Source
>
115 _S_range_begin(_Source __begin
) { return __begin
; }
117 struct __null_terminated
{ };
119 template<typename _Source
>
120 static __null_terminated
121 _S_range_end(_Source
) { return {}; }
123 template<typename _CharT
, typename _Traits
, typename _Alloc
>
125 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
126 { return __str
.data(); }
128 template<typename _CharT
, typename _Traits
, typename _Alloc
>
130 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
131 { return __str
.data() + __str
.size(); }
133 template<typename _CharT
, typename _Traits
>
135 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
136 { return __str
.data(); }
138 template<typename _CharT
, typename _Traits
>
140 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
141 { return __str
.data() + __str
.size(); }
143 template<typename _Tp
,
144 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
145 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
146 using __value_type_is_char
147 = typename
std::enable_if
<std::is_same
<_Val
, char>::value
>::type
;
150 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
151 typedef wchar_t value_type
;
152 static constexpr value_type preferred_separator
= L
'\\';
154 typedef char value_type
;
155 static constexpr value_type preferred_separator
= '/';
157 typedef std::basic_string
<value_type
> string_type
;
159 enum format
{ native_format
, generic_format
, auto_format
};
161 // constructors and destructor
165 path(const path
& __p
) = default;
167 path(path
&& __p
) noexcept
168 : _M_pathname(std::move(__p
._M_pathname
)), _M_type(__p
._M_type
)
174 path(string_type
&& __source
, format
= auto_format
)
175 : _M_pathname(std::move(__source
))
176 { _M_split_cmpts(); }
178 template<typename _Source
,
179 typename _Require
= _Path
<_Source
>>
180 path(_Source
const& __source
, format
= auto_format
)
181 : _M_pathname(_S_convert(_S_range_begin(__source
),
182 _S_range_end(__source
)))
183 { _M_split_cmpts(); }
185 template<typename _InputIterator
,
186 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
187 path(_InputIterator __first
, _InputIterator __last
, format
= auto_format
)
188 : _M_pathname(_S_convert(__first
, __last
))
189 { _M_split_cmpts(); }
191 template<typename _Source
,
192 typename _Require
= _Path
<_Source
>,
193 typename _Require2
= __value_type_is_char
<_Source
>>
194 path(_Source
const& __source
, const locale
& __loc
, format
= auto_format
)
195 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
196 _S_range_end(__source
), __loc
))
197 { _M_split_cmpts(); }
199 template<typename _InputIterator
,
200 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
201 typename _Require2
= __value_type_is_char
<_InputIterator
>>
202 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
,
203 format
= auto_format
)
204 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
205 { _M_split_cmpts(); }
211 path
& operator=(const path
& __p
) = default;
212 path
& operator=(path
&& __p
) noexcept
;
213 path
& operator=(string_type
&& __source
);
214 path
& assign(string_type
&& __source
);
216 template<typename _Source
>
218 operator=(_Source
const& __source
)
219 { return *this = path(__source
); }
221 template<typename _Source
>
223 assign(_Source
const& __source
)
224 { return *this = path(__source
); }
226 template<typename _InputIterator
>
227 _Path
<_InputIterator
, _InputIterator
>&
228 assign(_InputIterator __first
, _InputIterator __last
)
229 { return *this = path(__first
, __last
); }
233 path
& operator/=(const path
& __p
)
235 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
236 if (__p
.is_absolute()
237 || (__p
.has_root_name() && __p
.root_name() != root_name()))
241 string_type __pathname
;
242 if (__p
.has_root_directory())
243 __pathname
= root_name().native();
244 else if (has_filename() || (!has_root_directory() && is_absolute()))
245 __pathname
= _M_pathname
+ preferred_separator
;
246 __pathname
+= __p
.relative_path().native(); // XXX is this right?
247 _M_pathname
.swap(__pathname
);
251 // Much simpler, as any path with root-name or root-dir is absolute.
252 if (__p
.is_absolute())
256 if (has_filename() || (_M_type
== _Type::_Root_name
))
257 _M_pathname
+= preferred_separator
;
258 _M_pathname
+= __p
.native();
265 template <class _Source
>
267 operator/=(_Source
const& __source
)
268 { return _M_append(path(__source
)); }
270 template<typename _Source
>
272 append(_Source
const& __source
)
273 { return _M_append(path(__source
)); }
275 template<typename _InputIterator
>
276 _Path
<_InputIterator
, _InputIterator
>&
277 append(_InputIterator __first
, _InputIterator __last
)
278 { return _M_append(path(__first
, __last
)); }
282 path
& operator+=(const path
& __x
);
283 path
& operator+=(const string_type
& __x
);
284 path
& operator+=(const value_type
* __x
);
285 path
& operator+=(value_type __x
);
286 path
& operator+=(basic_string_view
<value_type
> __x
);
288 template<typename _Source
>
290 operator+=(_Source
const& __x
) { return concat(__x
); }
292 template<typename _CharT
>
293 _Path
<_CharT
*, _CharT
*>&
294 operator+=(_CharT __x
);
296 template<typename _Source
>
298 concat(_Source
const& __x
)
299 { return *this += _S_convert(_S_range_begin(__x
), _S_range_end(__x
)); }
301 template<typename _InputIterator
>
302 _Path
<_InputIterator
, _InputIterator
>&
303 concat(_InputIterator __first
, _InputIterator __last
)
304 { return *this += _S_convert(__first
, __last
); }
308 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
310 path
& make_preferred();
311 path
& remove_filename();
312 path
& replace_filename(const path
& __replacement
);
313 path
& replace_extension(const path
& __replacement
= path());
315 void swap(path
& __rhs
) noexcept
;
317 // native format observers
319 const string_type
& native() const noexcept
{ return _M_pathname
; }
320 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
321 operator string_type() const { return _M_pathname
; }
323 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
324 typename _Allocator
= std::allocator
<_CharT
>>
325 std::basic_string
<_CharT
, _Traits
, _Allocator
>
326 string(const _Allocator
& __a
= _Allocator()) const;
328 std::string
string() const;
329 #if _GLIBCXX_USE_WCHAR_T
330 std::wstring
wstring() const;
332 std::string
u8string() const;
333 std::u16string
u16string() const;
334 std::u32string
u32string() const;
336 // generic format observers
337 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
338 typename _Allocator
= std::allocator
<_CharT
>>
339 std::basic_string
<_CharT
, _Traits
, _Allocator
>
340 generic_string(const _Allocator
& __a
= _Allocator()) const;
342 std::string
generic_string() const;
343 #if _GLIBCXX_USE_WCHAR_T
344 std::wstring
generic_wstring() const;
346 std::string
generic_u8string() const;
347 std::u16string
generic_u16string() const;
348 std::u32string
generic_u32string() const;
352 int compare(const path
& __p
) const noexcept
;
353 int compare(const string_type
& __s
) const;
354 int compare(const value_type
* __s
) const;
355 int compare(const basic_string_view
<value_type
> __s
) const;
359 path
root_name() const;
360 path
root_directory() const;
361 path
root_path() const;
362 path
relative_path() const;
363 path
parent_path() const;
364 path
filename() const;
366 path
extension() const;
370 [[nodiscard
]] bool empty() const noexcept
{ return _M_pathname
.empty(); }
371 bool has_root_name() const;
372 bool has_root_directory() const;
373 bool has_root_path() const;
374 bool has_relative_path() const;
375 bool has_parent_path() const;
376 bool has_filename() const;
377 bool has_stem() const;
378 bool has_extension() const;
379 bool is_absolute() const { return has_root_directory(); }
380 bool is_relative() const { return !is_absolute(); }
383 path
lexically_normal() const;
384 path
lexically_relative(const path
& base
) const;
385 path
lexically_proximate(const path
& base
) const;
389 typedef iterator const_iterator
;
391 iterator
begin() const;
392 iterator
end() const;
395 enum class _Type
: unsigned char {
396 _Multi
, _Root_name
, _Root_dir
, _Filename
399 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
401 __glibcxx_assert(_M_type
!= _Type::_Multi
);
404 enum class _Split
{ _Stem
, _Extension
};
409 if (__p
.is_absolute())
410 operator=(std::move(__p
));
411 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
412 else if (__p
.has_root_name() && __p
.root_name() != root_name())
413 operator=(std::move(__p
));
416 operator/=(const_cast<const path
&>(__p
));
420 pair
<const string_type
*, size_t> _M_find_extension() const;
422 template<typename _CharT
>
426 _S_convert(value_type
* __src
, __null_terminated
)
427 { return string_type(__src
); }
430 _S_convert(const value_type
* __src
, __null_terminated
)
431 { return string_type(__src
); }
433 template<typename _Iter
>
435 _S_convert(_Iter __first
, _Iter __last
)
437 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
438 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
439 _S_convert(__first
, __last
);
442 template<typename _InputIterator
>
444 _S_convert(_InputIterator __src
, __null_terminated
)
446 using _Tp
= typename
std::iterator_traits
<_InputIterator
>::value_type
;
447 std::basic_string
<typename remove_cv
<_Tp
>::type
> __tmp
;
448 for (; *__src
!= _Tp
{}; ++__src
)
449 __tmp
.push_back(*__src
);
450 return _S_convert(__tmp
.c_str(), __tmp
.c_str() + __tmp
.size());
454 _S_convert_loc(const char* __first
, const char* __last
,
455 const std::locale
& __loc
);
457 template<typename _Iter
>
459 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
461 const std::string
__str(__first
, __last
);
462 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
465 template<typename _InputIterator
>
467 _S_convert_loc(_InputIterator __src
, __null_terminated
,
468 const std::locale
& __loc
)
471 while (*__src
!= '\0')
472 __tmp
.push_back(*__src
++);
473 return _S_convert_loc(__tmp
.data(), __tmp
.data()+__tmp
.size(), __loc
);
476 template<typename _CharT
, typename _Traits
, typename _Allocator
>
477 static basic_string
<_CharT
, _Traits
, _Allocator
>
478 _S_str_convert(const string_type
&, const _Allocator
& __a
);
480 bool _S_is_dir_sep(value_type __ch
)
482 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
483 return __ch
== L
'/' || __ch
== preferred_separator
;
489 void _M_split_cmpts();
491 void _M_add_root_name(size_t __n
);
492 void _M_add_root_dir(size_t __pos
);
493 void _M_add_filename(size_t __pos
, size_t __n
);
495 string_type _M_pathname
;
498 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
499 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
500 _Type _M_type
= _Type::_Filename
;
504 struct path::__is_encoded_char
<char> : std::true_type
505 { using value_type
= char; };
508 struct path::__is_encoded_char
<wchar_t> : std::true_type
509 { using value_type
= wchar_t; };
512 struct path::__is_encoded_char
<char16_t
> : std::true_type
513 { using value_type
= char16_t
; };
516 struct path::__is_encoded_char
<char32_t
> : std::true_type
517 { using value_type
= char32_t
; };
519 template<typename _Tp
>
520 struct path::__is_encoded_char
<const _Tp
> : __is_encoded_char
<_Tp
> { };
522 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
524 size_t hash_value(const path
& __p
) noexcept
;
527 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
528 { return __lhs
.compare(__rhs
) < 0; }
531 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
532 { return !(__rhs
< __lhs
); }
535 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
536 { return __rhs
< __lhs
; }
539 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
540 { return !(__lhs
< __rhs
); }
543 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
544 { return __lhs
.compare(__rhs
) == 0; }
547 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
548 { return !(__lhs
== __rhs
); }
550 /// Append one path to another
551 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
553 path
__result(__lhs
);
558 /// Write a path to a stream
559 template<typename _CharT
, typename _Traits
>
560 basic_ostream
<_CharT
, _Traits
>&
561 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
563 auto __tmp
= __p
.string
<_CharT
, _Traits
>();
564 using __quoted_string
565 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
566 __os
<< __quoted_string
{__tmp
, '"', '\\'};
570 /// Read a path from a stream
571 template<typename _CharT
, typename _Traits
>
572 basic_istream
<_CharT
, _Traits
>&
573 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
575 basic_string
<_CharT
, _Traits
> __tmp
;
576 using __quoted_string
577 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
578 if (__is
>> __quoted_string
{ __tmp
, '"', '\\' })
579 __p
= std::move(__tmp
);
583 template<typename _Source
>
585 u8path(const _Source
& __source
)
586 -> decltype(filesystem::path(__source
, std::locale::classic()))
588 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
589 const std::string __u8str
{__source
};
590 return std::filesystem::u8path(__u8str
.begin(), __u8str
.end());
592 return path
{ __source
};
596 template<typename _InputIterator
>
598 u8path(_InputIterator __first
, _InputIterator __last
)
599 -> decltype(filesystem::path(__first
, __last
, std::locale::classic()))
601 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
602 codecvt_utf8
<value_type
> __cvt
;
604 if (__str_codecvt_in(__first
, __last
, __tmp
, __cvt
))
605 return path
{ __tmp
};
609 return path
{ __first
, __last
};
613 class filesystem_error
: public std::system_error
616 filesystem_error(const string
& __what_arg
, error_code __ec
)
617 : system_error(__ec
, __what_arg
) { }
619 filesystem_error(const string
& __what_arg
, const path
& __p1
,
621 : system_error(__ec
, __what_arg
), _M_path1(__p1
) { }
623 filesystem_error(const string
& __what_arg
, const path
& __p1
,
624 const path
& __p2
, error_code __ec
)
625 : system_error(__ec
, __what_arg
), _M_path1(__p1
), _M_path2(__p2
)
630 const path
& path1() const noexcept
{ return _M_path1
; }
631 const path
& path2() const noexcept
{ return _M_path2
; }
632 const char* what() const noexcept
{ return _M_what
.c_str(); }
635 std::string
_M_gen_what();
639 std::string _M_what
= _M_gen_what();
642 struct path::_Cmpt
: path
644 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
645 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
647 _Cmpt() : _M_pos(-1) { }
652 // specialize _Cvt for degenerate 'noconv' case
654 struct path::_Cvt
<path::value_type
>
656 template<typename _Iter
>
658 _S_convert(_Iter __first
, _Iter __last
)
659 { return string_type
{__first
, __last
}; }
662 template<typename _CharT
>
665 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
667 _S_wconvert(const char* __f
, const char* __l
, true_type
)
669 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
670 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
672 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
674 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
675 "Cannot convert character sequence",
676 std::make_error_code(errc::illegal_byte_sequence
)));
680 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
682 std::codecvt_utf8
<_CharT
> __cvt
;
684 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
686 const char* __f2
= __str
.data();
687 const char* __l2
= __f2
+ __str
.size();
688 std::codecvt_utf8
<wchar_t> __wcvt
;
690 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
693 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
694 "Cannot convert character sequence",
695 std::make_error_code(errc::illegal_byte_sequence
)));
699 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
701 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
705 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
707 std::codecvt_utf8
<_CharT
> __cvt
;
709 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
711 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
712 "Cannot convert character sequence",
713 std::make_error_code(errc::illegal_byte_sequence
)));
718 _S_convert(_CharT
* __f
, _CharT
* __l
)
720 return _S_convert(const_cast<const _CharT
*>(__f
),
721 const_cast<const _CharT
*>(__l
));
724 template<typename _Iter
>
726 _S_convert(_Iter __first
, _Iter __last
)
728 const std::basic_string
<_CharT
> __str(__first
, __last
);
729 return _S_convert(__str
.data(), __str
.data() + __str
.size());
732 template<typename _Iter
, typename _Cont
>
734 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
735 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
736 { return _S_convert(__first
.base(), __last
.base()); }
739 /// An iterator for the components of a path
743 using difference_type
= std::ptrdiff_t;
744 using value_type
= path
;
745 using reference
= const path
&;
746 using pointer
= const path
*;
747 using iterator_category
= std::bidirectional_iterator_tag
;
749 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
751 iterator(const iterator
&) = default;
752 iterator
& operator=(const iterator
&) = default;
754 reference
operator*() const;
755 pointer
operator->() const { return std::__addressof(**this); }
757 iterator
& operator++();
758 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
760 iterator
& operator--();
761 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
763 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
764 { return __lhs
._M_equals(__rhs
); }
766 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
767 { return !__lhs
._M_equals(__rhs
); }
772 iterator(const path
* __path
, path::_List::const_iterator __iter
)
773 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
776 iterator(const path
* __path
, bool __at_end
)
777 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
780 bool _M_equals(iterator
) const;
783 path::_List::const_iterator _M_cur
;
784 bool _M_at_end
; // only used when type != _Multi
789 path::operator=(path
&& __p
) noexcept
791 _M_pathname
= std::move(__p
._M_pathname
);
792 _M_cmpts
= std::move(__p
._M_cmpts
);
793 _M_type
= __p
._M_type
;
799 path::operator=(string_type
&& __source
)
800 { return *this = path(std::move(__source
)); }
803 path::assign(string_type
&& __source
)
804 { return *this = path(std::move(__source
)); }
807 path::operator+=(const path
& __p
)
809 return operator+=(__p
.native());
813 path::operator+=(const string_type
& __x
)
821 path::operator+=(const value_type
* __x
)
829 path::operator+=(value_type __x
)
837 path::operator+=(basic_string_view
<value_type
> __x
)
839 _M_pathname
.append(__x
.data(), __x
.size());
844 template<typename _CharT
>
845 inline path::_Path
<_CharT
*, _CharT
*>&
846 path::operator+=(_CharT __x
)
848 auto* __addr
= std::__addressof(__x
);
849 return concat(__addr
, __addr
+ 1);
853 path::make_preferred()
855 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
856 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
857 preferred_separator
);
862 inline void path::swap(path
& __rhs
) noexcept
864 _M_pathname
.swap(__rhs
._M_pathname
);
865 _M_cmpts
.swap(__rhs
._M_cmpts
);
866 std::swap(_M_type
, __rhs
._M_type
);
869 template<typename _CharT
, typename _Traits
, typename _Allocator
>
870 std::basic_string
<_CharT
, _Traits
, _Allocator
>
871 path::_S_str_convert(const string_type
& __str
, const _Allocator
& __a
)
873 if (__str
.size() == 0)
874 return std::basic_string
<_CharT
, _Traits
, _Allocator
>(__a
);
876 const value_type
* __first
= __str
.data();
877 const value_type
* __last
= __first
+ __str
.size();
879 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
880 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
881 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
882 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
884 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
885 codecvt_utf8
<value_type
> __cvt
;
886 _String __u8str
{_CharAlloc
{__a
}};
887 if (__str_codecvt_out(__first
, __last
, __u8str
, __cvt
))
889 if constexpr (is_same_v
<_CharT
, char>)
894 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
895 codecvt_utf8
<_CharT
> __cvt
;
896 const char* __f
= __u8str
.data();
897 const char* __l
= __f
+ __u8str
.size();
898 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
903 codecvt_utf8
<_CharT
> __cvt
;
904 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
905 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
908 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
909 "Cannot convert character sequence",
910 std::make_error_code(errc::illegal_byte_sequence
)));
913 template<typename _CharT
, typename _Traits
, typename _Allocator
>
914 inline basic_string
<_CharT
, _Traits
, _Allocator
>
915 path::string(const _Allocator
& __a
) const
917 if constexpr (is_same_v
<_CharT
, value_type
>)
918 #if _GLIBCXX_USE_CXX11_ABI
919 return { _M_pathname
, __a
};
921 return { _M_pathname
, string_type::size_type(0), __a
};
924 return _S_str_convert
<_CharT
, _Traits
>(_M_pathname
, __a
);
928 path::string() const { return string
<char>(); }
930 #if _GLIBCXX_USE_WCHAR_T
932 path::wstring() const { return string
<wchar_t>(); }
936 path::u8string() const
938 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
940 // convert from native encoding to UTF-8
941 codecvt_utf8
<value_type
> __cvt
;
942 const value_type
* __first
= _M_pathname
.data();
943 const value_type
* __last
= __first
+ _M_pathname
.size();
944 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
946 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
947 "Cannot convert character sequence",
948 std::make_error_code(errc::illegal_byte_sequence
)));
954 inline std::u16string
955 path::u16string() const { return string
<char16_t
>(); }
957 inline std::u32string
958 path::u32string() const { return string
<char32_t
>(); }
960 template<typename _CharT
, typename _Traits
, typename _Allocator
>
961 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
962 path::generic_string(const _Allocator
& __a
) const
964 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
965 const value_type __slash
= L
'/';
967 const value_type __slash
= '/';
969 string_type
__str(__a
);
971 if (_M_type
== _Type::_Root_dir
)
972 __str
.assign(1, __slash
);
975 __str
.reserve(_M_pathname
.size());
976 bool __add_slash
= false;
977 for (auto& __elem
: *this)
981 __str
+= __elem
._M_pathname
;
982 __add_slash
= __elem
._M_type
== _Type::_Filename
;
986 if constexpr (is_same_v
<_CharT
, value_type
>)
989 return _S_str_convert
<_CharT
, _Traits
>(__str
, __a
);
993 path::generic_string() const
994 { return generic_string
<char>(); }
996 #if _GLIBCXX_USE_WCHAR_T
998 path::generic_wstring() const
999 { return generic_string
<wchar_t>(); }
1003 path::generic_u8string() const
1004 { return generic_string(); }
1006 inline std::u16string
1007 path::generic_u16string() const
1008 { return generic_string
<char16_t
>(); }
1010 inline std::u32string
1011 path::generic_u32string() const
1012 { return generic_string
<char32_t
>(); }
1015 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
1018 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
1021 path::compare(basic_string_view
<value_type
> __s
) const
1022 { return compare(path(__s
)); }
1025 path::filename() const
1029 else if (_M_type
== _Type::_Filename
)
1031 else if (_M_type
== _Type::_Multi
)
1033 if (_M_pathname
.back() == preferred_separator
)
1035 auto& __last
= *--end();
1036 if (__last
._M_type
== _Type::_Filename
)
1045 auto ext
= _M_find_extension();
1046 if (ext
.first
&& ext
.second
!= 0)
1047 return path
{ext
.first
->substr(0, ext
.second
)};
1052 path::extension() const
1054 auto ext
= _M_find_extension();
1055 if (ext
.first
&& ext
.second
!= string_type::npos
)
1056 return path
{ext
.first
->substr(ext
.second
)};
1061 path::has_stem() const
1063 auto ext
= _M_find_extension();
1064 return ext
.first
&& ext
.second
!= 0;
1068 path::has_extension() const
1070 auto ext
= _M_find_extension();
1071 return ext
.first
&& ext
.second
!= string_type::npos
;
1074 inline path::iterator
1077 if (_M_type
== _Type::_Multi
)
1078 return iterator(this, _M_cmpts
.begin());
1079 return iterator(this, empty());
1082 inline path::iterator
1085 if (_M_type
== _Type::_Multi
)
1086 return iterator(this, _M_cmpts
.end());
1087 return iterator(this, true);
1090 inline path::iterator
&
1091 path::iterator::operator++()
1093 __glibcxx_assert(_M_path
!= nullptr);
1094 if (_M_path
->_M_type
== _Type::_Multi
)
1096 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1101 __glibcxx_assert(!_M_at_end
);
1107 inline path::iterator
&
1108 path::iterator::operator--()
1110 __glibcxx_assert(_M_path
!= nullptr);
1111 if (_M_path
->_M_type
== _Type::_Multi
)
1113 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1118 __glibcxx_assert(_M_at_end
);
1124 inline path::iterator::reference
1125 path::iterator::operator*() const
1127 __glibcxx_assert(_M_path
!= nullptr);
1128 if (_M_path
->_M_type
== _Type::_Multi
)
1130 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1137 path::iterator::_M_equals(iterator __rhs
) const
1139 if (_M_path
!= __rhs
._M_path
)
1141 if (_M_path
== nullptr)
1143 if (_M_path
->_M_type
== path::_Type::_Multi
)
1144 return _M_cur
== __rhs
._M_cur
;
1145 return _M_at_end
== __rhs
._M_at_end
;
1148 // @} group filesystem
1149 _GLIBCXX_END_NAMESPACE_CXX11
1150 } // namespace filesystem
1152 _GLIBCXX_END_NAMESPACE_VERSION
1157 #endif // _GLIBCXX_FS_PATH_H