1 // Class filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2019 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 experimental/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{experimental/filesystem}
30 #ifndef _GLIBCXX_EXPERIMENTAL_FS_PATH_H
31 #define _GLIBCXX_EXPERIMENTAL_FS_PATH_H 1
33 #if __cplusplus < 201103L
34 # include <bits/c++0x_warning.h>
38 #include <type_traits>
43 #include <system_error>
44 #include <bits/stl_algobase.h>
45 #include <bits/quoted_string.h>
46 #include <bits/locale_conv.h>
47 #if __cplusplus == 201402L
48 # include <experimental/string_view>
51 #if defined(_WIN32) && !defined(__CYGWIN__)
52 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
56 namespace std
_GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 namespace experimental
66 _GLIBCXX_BEGIN_NAMESPACE_CXX11
68 #if __cplusplus == 201402L
69 using std::experimental::basic_string_view
;
70 #elif __cplusplus > 201402L
71 using std::basic_string_view
;
75 * @ingroup filesystem-ts
79 /// A filesystem path.
82 template<typename _CharT
,
83 typename _Ch
= typename remove_const
<_CharT
>::type
>
84 using __is_encoded_char
85 = __or_
<is_same
<_Ch
, char>,
86 is_same
<_Ch
, wchar_t>,
87 #ifdef _GLIBCXX_USE_CHAR8_T
88 is_same
<_Ch
, char8_t
>,
90 is_same
<_Ch
, char16_t
>,
91 is_same
<_Ch
, char32_t
>>;
93 template<typename _Iter
,
94 typename _Iter_traits
= std::iterator_traits
<_Iter
>>
95 using __is_path_iter_src
96 = __and_
<__is_encoded_char
<typename
_Iter_traits::value_type
>,
97 std::is_base_of
<std::input_iterator_tag
,
98 typename
_Iter_traits::iterator_category
>>;
100 template<typename _Iter
>
101 static __is_path_iter_src
<_Iter
>
102 __is_path_src(_Iter
, int);
104 template<typename _CharT
, typename _Traits
, typename _Alloc
>
105 static __is_encoded_char
<_CharT
>
106 __is_path_src(const basic_string
<_CharT
, _Traits
, _Alloc
>&, int);
108 #if __cplusplus >= 201402L
109 template<typename _CharT
, typename _Traits
>
110 static __is_encoded_char
<_CharT
>
111 __is_path_src(const basic_string_view
<_CharT
, _Traits
>&, int);
114 template<typename _Unknown
>
115 static std::false_type
116 __is_path_src(const _Unknown
&, ...);
118 template<typename _Tp1
, typename _Tp2
>
119 struct __constructible_from
;
121 template<typename _Iter
>
122 struct __constructible_from
<_Iter
, _Iter
>
123 : __is_path_iter_src
<_Iter
>
126 template<typename _Source
>
127 struct __constructible_from
<_Source
, void>
128 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
131 template<typename _Tp1
, typename _Tp2
= void>
132 using _Path
= typename
133 std::enable_if
<__and_
<__not_
<is_same
<typename remove_cv
<_Tp1
>::type
,
135 __not_
<is_void
<_Tp1
>>,
136 __constructible_from
<_Tp1
, _Tp2
>>::value
,
139 template<typename _Source
>
141 _S_range_begin(_Source __begin
) { return __begin
; }
143 struct __null_terminated
{ };
145 template<typename _Source
>
146 static __null_terminated
147 _S_range_end(_Source
) { return {}; }
149 template<typename _CharT
, typename _Traits
, typename _Alloc
>
151 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
152 { return __str
.data(); }
154 template<typename _CharT
, typename _Traits
, typename _Alloc
>
156 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
157 { return __str
.data() + __str
.size(); }
159 #if __cplusplus >= 201402L
160 template<typename _CharT
, typename _Traits
>
162 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
163 { return __str
.data(); }
165 template<typename _CharT
, typename _Traits
>
167 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
168 { return __str
.data() + __str
.size(); }
171 template<typename _Tp
,
172 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
173 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
174 using __value_type_is_char
= typename
std::enable_if
<
175 std::is_same
<typename
std::remove_const
<_Val
>::type
, char>::value
179 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
180 typedef wchar_t value_type
;
181 static constexpr value_type preferred_separator
= L
'\\';
183 typedef char value_type
;
184 static constexpr value_type preferred_separator
= '/';
186 typedef std::basic_string
<value_type
> string_type
;
188 // constructors and destructor
192 path(const path
& __p
) = default;
194 path(path
&& __p
) noexcept
195 : _M_pathname(std::move(__p
._M_pathname
)), _M_type(__p
._M_type
)
201 path(string_type
&& __source
)
202 : _M_pathname(std::move(__source
))
203 { _M_split_cmpts(); }
205 template<typename _Source
,
206 typename _Require
= _Path
<_Source
>>
207 path(_Source
const& __source
)
208 : _M_pathname(_S_convert(_S_range_begin(__source
),
209 _S_range_end(__source
)))
210 { _M_split_cmpts(); }
212 template<typename _InputIterator
,
213 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
214 path(_InputIterator __first
, _InputIterator __last
)
215 : _M_pathname(_S_convert(__first
, __last
))
216 { _M_split_cmpts(); }
218 template<typename _Source
,
219 typename _Require
= _Path
<_Source
>,
220 typename _Require2
= __value_type_is_char
<_Source
>>
221 path(_Source
const& __source
, const locale
& __loc
)
222 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
223 _S_range_end(__source
), __loc
))
224 { _M_split_cmpts(); }
226 template<typename _InputIterator
,
227 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
228 typename _Require2
= __value_type_is_char
<_InputIterator
>>
229 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
)
230 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
231 { _M_split_cmpts(); }
237 path
& operator=(const path
& __p
) = default;
238 path
& operator=(path
&& __p
) noexcept
;
239 path
& operator=(string_type
&& __source
);
240 path
& assign(string_type
&& __source
);
242 template<typename _Source
>
244 operator=(_Source
const& __source
)
245 { return *this = path(__source
); }
247 template<typename _Source
>
249 assign(_Source
const& __source
)
250 { return *this = path(__source
); }
252 template<typename _InputIterator
>
253 _Path
<_InputIterator
, _InputIterator
>&
254 assign(_InputIterator __first
, _InputIterator __last
)
255 { return *this = path(__first
, __last
); }
259 path
& operator/=(const path
& __p
) { return _M_append(__p
._M_pathname
); }
261 template <class _Source
>
263 operator/=(_Source
const& __source
)
264 { return append(__source
); }
266 template<typename _Source
>
268 append(_Source
const& __source
)
270 return _M_append(_S_convert(_S_range_begin(__source
),
271 _S_range_end(__source
)));
274 template<typename _InputIterator
>
275 _Path
<_InputIterator
, _InputIterator
>&
276 append(_InputIterator __first
, _InputIterator __last
)
277 { return _M_append(_S_convert(__first
, __last
)); }
281 path
& operator+=(const path
& __x
);
282 path
& operator+=(const string_type
& __x
);
283 path
& operator+=(const value_type
* __x
);
284 path
& operator+=(value_type __x
);
285 #if __cplusplus >= 201402L
286 path
& operator+=(basic_string_view
<value_type
> __x
);
289 template<typename _Source
>
291 operator+=(_Source
const& __x
) { return concat(__x
); }
293 template<typename _CharT
>
294 _Path
<_CharT
*, _CharT
*>&
295 operator+=(_CharT __x
);
297 template<typename _Source
>
299 concat(_Source
const& __x
)
300 { return *this += _S_convert(_S_range_begin(__x
), _S_range_end(__x
)); }
302 template<typename _InputIterator
>
303 _Path
<_InputIterator
, _InputIterator
>&
304 concat(_InputIterator __first
, _InputIterator __last
)
305 { return *this += _S_convert(__first
, __last
); }
309 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
311 path
& make_preferred();
312 path
& remove_filename();
313 path
& replace_filename(const path
& __replacement
);
314 path
& replace_extension(const path
& __replacement
= path());
316 void swap(path
& __rhs
) noexcept
;
318 // native format observers
320 const string_type
& native() const noexcept
{ return _M_pathname
; }
321 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
322 operator string_type() const { return _M_pathname
; }
324 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
325 typename _Allocator
= std::allocator
<_CharT
>>
326 std::basic_string
<_CharT
, _Traits
, _Allocator
>
327 string(const _Allocator
& __a
= _Allocator()) const;
329 std::string
string() const;
330 #if _GLIBCXX_USE_WCHAR_T
331 std::wstring
wstring() const;
333 #ifdef _GLIBCXX_USE_CHAR8_T
334 __attribute__((__abi_tag__("__u8")))
335 std::u8string
u8string() const;
337 std::string
u8string() const;
338 #endif // _GLIBCXX_USE_CHAR8_T
339 std::u16string
u16string() const;
340 std::u32string
u32string() const;
342 // generic format observers
343 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
344 typename _Allocator
= std::allocator
<_CharT
>>
345 std::basic_string
<_CharT
, _Traits
, _Allocator
>
346 generic_string(const _Allocator
& __a
= _Allocator()) const;
348 std::string
generic_string() const;
349 #if _GLIBCXX_USE_WCHAR_T
350 std::wstring
generic_wstring() const;
352 #ifdef _GLIBCXX_USE_CHAR8_T
353 __attribute__((__abi_tag__("__u8")))
354 std::u8string
generic_u8string() const;
356 std::string
generic_u8string() const;
357 #endif // _GLIBCXX_USE_CHAR8_T
358 std::u16string
generic_u16string() const;
359 std::u32string
generic_u32string() const;
363 int compare(const path
& __p
) const noexcept
;
364 int compare(const string_type
& __s
) const;
365 int compare(const value_type
* __s
) const;
366 #if __cplusplus >= 201402L
367 int compare(const basic_string_view
<value_type
> __s
) const;
372 path
root_name() const;
373 path
root_directory() const;
374 path
root_path() const;
375 path
relative_path() const;
376 path
parent_path() const;
377 path
filename() const;
379 path
extension() const;
383 _GLIBCXX_NODISCARD
bool empty() const noexcept
{ return _M_pathname
.empty(); }
384 bool has_root_name() const;
385 bool has_root_directory() const;
386 bool has_root_path() const;
387 bool has_relative_path() const;
388 bool has_parent_path() const;
389 bool has_filename() const;
390 bool has_stem() const;
391 bool has_extension() const;
392 bool is_absolute() const;
393 bool is_relative() const { return !is_absolute(); }
397 typedef iterator const_iterator
;
399 iterator
begin() const;
400 iterator
end() const;
402 // Create a basic_string by reading until a null character.
403 template<typename _InputIterator
,
404 typename _Traits
= std::iterator_traits
<_InputIterator
>,
406 = typename
std::remove_cv
<typename
_Traits::value_type
>::type
>
407 static std::basic_string
<_CharT
>
408 _S_string_from_iter(_InputIterator __source
)
410 std::basic_string
<_CharT
> __str
;
411 for (_CharT __ch
= *__source
; __ch
!= _CharT(); __ch
= *++__source
)
412 __str
.push_back(__ch
);
417 enum class _Type
: unsigned char {
418 _Multi
, _Root_name
, _Root_dir
, _Filename
421 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
423 __glibcxx_assert(!empty());
424 __glibcxx_assert(_M_type
!= _Type::_Multi
);
427 enum class _Split
{ _Stem
, _Extension
};
429 path
& _M_append(const string_type
& __str
)
431 if (!_M_pathname
.empty() && !_S_is_dir_sep(_M_pathname
.back())
432 && !__str
.empty() && !_S_is_dir_sep(__str
.front()))
433 _M_pathname
+= preferred_separator
;
434 _M_pathname
+= __str
;
439 pair
<const string_type
*, size_t> _M_find_extension() const;
441 template<typename _CharT
>
445 _S_convert(value_type
* __src
, __null_terminated
)
446 { return string_type(__src
); }
449 _S_convert(const value_type
* __src
, __null_terminated
)
450 { return string_type(__src
); }
452 template<typename _Iter
>
454 _S_convert(_Iter __first
, _Iter __last
)
456 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
457 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
458 _S_convert(__first
, __last
);
461 template<typename _InputIterator
>
463 _S_convert(_InputIterator __src
, __null_terminated
)
465 auto __s
= _S_string_from_iter(__src
);
466 return _S_convert(__s
.c_str(), __s
.c_str() + __s
.size());
470 _S_convert_loc(const char* __first
, const char* __last
,
471 const std::locale
& __loc
);
473 template<typename _Iter
>
475 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
477 const std::string
__str(__first
, __last
);
478 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
481 template<typename _InputIterator
>
483 _S_convert_loc(_InputIterator __src
, __null_terminated
,
484 const std::locale
& __loc
)
486 std::string __s
= _S_string_from_iter(__src
);
487 return _S_convert_loc(__s
.data(), __s
.data() + __s
.size(), __loc
);
490 bool _S_is_dir_sep(value_type __ch
)
492 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
493 return __ch
== L
'/' || __ch
== preferred_separator
;
499 void _M_split_cmpts();
501 void _M_add_root_name(size_t __n
);
502 void _M_add_root_dir(size_t __pos
);
503 void _M_add_filename(size_t __pos
, size_t __n
);
505 string_type _M_pathname
;
508 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
509 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
510 _Type _M_type
= _Type::_Multi
;
513 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
515 size_t hash_value(const path
& __p
) noexcept
;
518 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
519 { return __lhs
.compare(__rhs
) < 0; }
522 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
523 { return !(__rhs
< __lhs
); }
526 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
527 { return __rhs
< __lhs
; }
530 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
531 { return !(__lhs
< __rhs
); }
534 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
535 { return __lhs
.compare(__rhs
) == 0; }
538 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
539 { return !(__lhs
== __rhs
); }
541 /// Append one path to another
542 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
544 path
__result(__lhs
);
549 /// Write a path to a stream
550 template<typename _CharT
, typename _Traits
>
551 basic_ostream
<_CharT
, _Traits
>&
552 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
554 auto __tmp
= __p
.string
<_CharT
, _Traits
>();
555 using __quoted_string
556 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
557 __os
<< __quoted_string
{__tmp
, _CharT('"'), _CharT('\\')};
561 /// Read a path from a stream
562 template<typename _CharT
, typename _Traits
>
563 basic_istream
<_CharT
, _Traits
>&
564 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
566 basic_string
<_CharT
, _Traits
> __tmp
;
567 using __quoted_string
568 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
569 if (__is
>> __quoted_string
{ __tmp
, _CharT('"'), _CharT('\\') })
570 __p
= std::move(__tmp
);
574 // TODO constrain with _Path<Source> and __value_type_is_char
575 template<typename _Source
>
577 u8path(const _Source
& __source
)
579 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
580 return path
{ path::string_type
{__source
} };
582 return path
{ __source
};
586 // TODO constrain with _Path<InputIterator, InputIterator> and __value_type_is_char
587 template<typename _InputIterator
>
589 u8path(_InputIterator __first
, _InputIterator __last
)
591 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
592 return path
{ path::string_type
{__first
, __last
} };
594 return path
{ __first
, __last
};
598 class filesystem_error
: public std::system_error
601 filesystem_error(const string
& __what_arg
, error_code __ec
)
602 : system_error(__ec
, __what_arg
) { }
604 filesystem_error(const string
& __what_arg
, const path
& __p1
,
606 : system_error(__ec
, __what_arg
), _M_path1(__p1
) { }
608 filesystem_error(const string
& __what_arg
, const path
& __p1
,
609 const path
& __p2
, error_code __ec
)
610 : system_error(__ec
, __what_arg
), _M_path1(__p1
), _M_path2(__p2
)
615 const path
& path1() const noexcept
{ return _M_path1
; }
616 const path
& path2() const noexcept
{ return _M_path2
; }
617 const char* what() const noexcept
{ return _M_what
.c_str(); }
620 std::string
_M_gen_what();
624 std::string _M_what
= _M_gen_what();
627 struct path::_Cmpt
: path
629 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
630 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
632 _Cmpt() : _M_pos(-1) { }
637 // specialize _Cvt for degenerate 'noconv' case
639 struct path::_Cvt
<path::value_type
>
641 template<typename _Iter
>
643 _S_convert(_Iter __first
, _Iter __last
)
644 { return string_type
{__first
, __last
}; }
647 template<typename _CharT
>
650 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
652 _S_wconvert(const char* __f
, const char* __l
, true_type
)
654 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
655 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
657 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
659 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
660 "Cannot convert character sequence",
661 std::make_error_code(errc::illegal_byte_sequence
)));
665 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
667 std::codecvt_utf8
<_CharT
> __cvt
;
669 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
671 const char* __f2
= __str
.data();
672 const char* __l2
= __f2
+ __str
.size();
673 std::codecvt_utf8
<wchar_t> __wcvt
;
675 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
678 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
679 "Cannot convert character sequence",
680 std::make_error_code(errc::illegal_byte_sequence
)));
684 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
686 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
690 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
692 #ifdef _GLIBCXX_USE_CHAR8_T
693 if constexpr (is_same
<_CharT
, char8_t
>::value
)
695 string_type
__str(__f
, __l
);
701 std::codecvt_utf8
<_CharT
> __cvt
;
703 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
705 #ifdef _GLIBCXX_USE_CHAR8_T
708 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
709 "Cannot convert character sequence",
710 std::make_error_code(errc::illegal_byte_sequence
)));
715 _S_convert(_CharT
* __f
, _CharT
* __l
)
717 return _S_convert(const_cast<const _CharT
*>(__f
),
718 const_cast<const _CharT
*>(__l
));
721 template<typename _Iter
>
723 _S_convert(_Iter __first
, _Iter __last
)
725 const std::basic_string
<_CharT
> __str(__first
, __last
);
726 return _S_convert(__str
.data(), __str
.data() + __str
.size());
729 template<typename _Iter
, typename _Cont
>
731 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
732 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
733 { return _S_convert(__first
.base(), __last
.base()); }
736 /// An iterator for the components of a path
740 using difference_type
= std::ptrdiff_t;
741 using value_type
= path
;
742 using reference
= const path
&;
743 using pointer
= const path
*;
744 using iterator_category
= std::bidirectional_iterator_tag
;
746 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
748 iterator(const iterator
&) = default;
749 iterator
& operator=(const iterator
&) = default;
751 reference
operator*() const;
752 pointer
operator->() const { return std::__addressof(**this); }
754 iterator
& operator++();
755 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
757 iterator
& operator--();
758 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
760 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
761 { return __lhs
._M_equals(__rhs
); }
763 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
764 { return !__lhs
._M_equals(__rhs
); }
769 iterator(const path
* __path
, path::_List::const_iterator __iter
)
770 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
773 iterator(const path
* __path
, bool __at_end
)
774 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
777 bool _M_equals(iterator
) const;
780 path::_List::const_iterator _M_cur
;
781 bool _M_at_end
; // only used when type != _Multi
786 path::operator=(path
&& __p
) noexcept
788 _M_pathname
= std::move(__p
._M_pathname
);
789 _M_cmpts
= std::move(__p
._M_cmpts
);
790 _M_type
= __p
._M_type
;
796 path::operator=(string_type
&& __source
)
797 { return *this = path(std::move(__source
)); }
800 path::assign(string_type
&& __source
)
801 { return *this = path(std::move(__source
)); }
804 path::operator+=(const path
& __p
)
806 return operator+=(__p
.native());
810 path::operator+=(const string_type
& __x
)
818 path::operator+=(const value_type
* __x
)
826 path::operator+=(value_type __x
)
833 #if __cplusplus >= 201402L
835 path::operator+=(basic_string_view
<value_type
> __x
)
837 _M_pathname
.append(__x
.data(), __x
.size());
843 template<typename _CharT
>
844 inline path::_Path
<_CharT
*, _CharT
*>&
845 path::operator+=(_CharT __x
)
847 auto* __addr
= std::__addressof(__x
);
848 return concat(__addr
, __addr
+ 1);
852 path::make_preferred()
854 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
855 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
856 preferred_separator
);
861 inline void path::swap(path
& __rhs
) noexcept
863 _M_pathname
.swap(__rhs
._M_pathname
);
864 _M_cmpts
.swap(__rhs
._M_cmpts
);
865 std::swap(_M_type
, __rhs
._M_type
);
868 template<typename _CharT
, typename _Traits
, typename _Allocator
>
869 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
870 path::string(const _Allocator
& __a
) const
872 if (is_same
<_CharT
, value_type
>::value
)
873 return { _M_pathname
.begin(), _M_pathname
.end(), __a
};
875 const value_type
* __first
= _M_pathname
.data();
876 const value_type
* __last
= __first
+ _M_pathname
.size();
878 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
879 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
880 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
881 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
883 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
884 codecvt_utf8
<value_type
> __cvt
;
885 _String __u8str
{_CharAlloc
{__a
}};
886 if (__str_codecvt_out(__first
, __last
, __u8str
, __cvt
))
891 operator()(const _String
& __from
, _String
&, true_type
)
892 { return std::__addressof(__from
); }
895 operator()(const _String
& __from
, _WString
& __to
, false_type
)
897 #ifdef _GLIBCXX_USE_CHAR8_T
898 if constexpr (is_same
<_CharT
, char8_t
>::value
)
900 __to
.assign(__from
.begin(), __from
.end());
901 return std::__addressof(__to
);
906 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
907 codecvt_utf8
<_CharT
> __cvt
;
908 const char* __f
= __from
.data();
909 const char* __l
= __f
+ __from
.size();
910 if (__str_codecvt_in(__f
, __l
, __to
, __cvt
))
911 return std::__addressof(__to
);
912 #ifdef _GLIBCXX_USE_CHAR8_T
919 if (auto* __p
= __dispatch(__u8str
, __wstr
, is_same
<_CharT
, char>{}))
923 #ifdef _GLIBCXX_USE_CHAR8_T
924 if constexpr (is_same
<_CharT
, char8_t
>::value
)
926 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__first
, __last
, __a
};
932 codecvt_utf8
<_CharT
> __cvt
;
933 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
934 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
936 #ifdef _GLIBCXX_USE_CHAR8_T
940 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
941 "Cannot convert character sequence",
942 std::make_error_code(errc::illegal_byte_sequence
)));
946 path::string() const { return string
<char>(); }
948 #if _GLIBCXX_USE_WCHAR_T
950 path::wstring() const { return string
<wchar_t>(); }
953 #ifdef _GLIBCXX_USE_CHAR8_T
955 path::u8string() const { return string
<char8_t
>(); }
958 path::u8string() const
960 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
962 // convert from native encoding to UTF-8
963 codecvt_utf8
<value_type
> __cvt
;
964 const value_type
* __first
= _M_pathname
.data();
965 const value_type
* __last
= __first
+ _M_pathname
.size();
966 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
968 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
969 "Cannot convert character sequence",
970 std::make_error_code(errc::illegal_byte_sequence
)));
975 #endif // _GLIBCXX_USE_CHAR8_T
977 inline std::u16string
978 path::u16string() const { return string
<char16_t
>(); }
980 inline std::u32string
981 path::u32string() const { return string
<char32_t
>(); }
983 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
984 template<typename _CharT
, typename _Traits
, typename _Allocator
>
985 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
986 path::generic_string(const _Allocator
& __a
) const
987 { return string
<_CharT
, _Traits
, _Allocator
>(__a
); }
990 path::generic_string() const { return string(); }
992 #if _GLIBCXX_USE_WCHAR_T
994 path::generic_wstring() const { return wstring(); }
997 #ifdef _GLIBCXX_USE_CHAR8_T
999 path::generic_u8string() const { return u8string(); }
1002 path::generic_u8string() const { return u8string(); }
1005 inline std::u16string
1006 path::generic_u16string() const { return u16string(); }
1008 inline std::u32string
1009 path::generic_u32string() const { return u32string(); }
1013 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
1016 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
1018 #if __cplusplus >= 201402L
1020 path::compare(basic_string_view
<value_type
> __s
) const
1021 { return compare(path(__s
)); }
1025 path::filename() const { return empty() ? path() : *--end(); }
1030 auto ext
= _M_find_extension();
1031 if (ext
.first
&& ext
.second
!= 0)
1032 return path
{ext
.first
->substr(0, ext
.second
)};
1037 path::extension() const
1039 auto ext
= _M_find_extension();
1040 if (ext
.first
&& ext
.second
!= string_type::npos
)
1041 return path
{ext
.first
->substr(ext
.second
)};
1046 path::has_stem() const
1048 auto ext
= _M_find_extension();
1049 return ext
.first
&& ext
.second
!= 0;
1053 path::has_extension() const
1055 auto ext
= _M_find_extension();
1056 return ext
.first
&& ext
.second
!= string_type::npos
;
1060 path::is_absolute() const
1062 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1063 return has_root_name() && has_root_directory();
1065 return has_root_directory();
1069 inline path::iterator
1072 if (_M_type
== _Type::_Multi
)
1073 return iterator(this, _M_cmpts
.begin());
1074 return iterator(this, false);
1077 inline path::iterator
1080 if (_M_type
== _Type::_Multi
)
1081 return iterator(this, _M_cmpts
.end());
1082 return iterator(this, true);
1085 inline path::iterator
&
1086 path::iterator::operator++()
1088 __glibcxx_assert(_M_path
!= nullptr);
1089 if (_M_path
->_M_type
== _Type::_Multi
)
1091 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1096 __glibcxx_assert(!_M_at_end
);
1102 inline path::iterator
&
1103 path::iterator::operator--()
1105 __glibcxx_assert(_M_path
!= nullptr);
1106 if (_M_path
->_M_type
== _Type::_Multi
)
1108 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1113 __glibcxx_assert(_M_at_end
);
1119 inline path::iterator::reference
1120 path::iterator::operator*() const
1122 __glibcxx_assert(_M_path
!= nullptr);
1123 if (_M_path
->_M_type
== _Type::_Multi
)
1125 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1132 path::iterator::_M_equals(iterator __rhs
) const
1134 if (_M_path
!= __rhs
._M_path
)
1136 if (_M_path
== nullptr)
1138 if (_M_path
->_M_type
== path::_Type::_Multi
)
1139 return _M_cur
== __rhs
._M_cur
;
1140 return _M_at_end
== __rhs
._M_at_end
;
1143 // @} group filesystem-ts
1144 _GLIBCXX_END_NAMESPACE_CXX11
1146 } // namespace filesystem
1147 } // namespace experimental
1149 _GLIBCXX_END_NAMESPACE_VERSION
1154 #endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H