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 * @addtogroup 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 typename _Tp1_nocv
= typename remove_cv
<_Tp1
>::type
,
133 typename _Tp1_noptr
= typename remove_pointer
<_Tp1
>::type
>
134 using _Path
= typename
135 std::enable_if
<__and_
<__not_
<is_same
<_Tp1_nocv
, path
>>,
136 __not_
<is_void
<_Tp1_noptr
>>,
137 __constructible_from
<_Tp1
, _Tp2
>>::value
,
140 template<typename _Source
>
142 _S_range_begin(_Source __begin
) { return __begin
; }
144 struct __null_terminated
{ };
146 template<typename _Source
>
147 static __null_terminated
148 _S_range_end(_Source
) { return {}; }
150 template<typename _CharT
, typename _Traits
, typename _Alloc
>
152 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
153 { return __str
.data(); }
155 template<typename _CharT
, typename _Traits
, typename _Alloc
>
157 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
158 { return __str
.data() + __str
.size(); }
160 #if __cplusplus >= 201402L
161 template<typename _CharT
, typename _Traits
>
163 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
164 { return __str
.data(); }
166 template<typename _CharT
, typename _Traits
>
168 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
169 { return __str
.data() + __str
.size(); }
172 template<typename _Tp
,
173 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
174 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
175 using __value_type_is_char
= typename
std::enable_if
<
176 std::is_same
<typename
std::remove_const
<_Val
>::type
, char>::value
180 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
181 typedef wchar_t value_type
;
182 static constexpr value_type preferred_separator
= L
'\\';
184 typedef char value_type
;
185 static constexpr value_type preferred_separator
= '/';
187 typedef std::basic_string
<value_type
> string_type
;
189 // constructors and destructor
193 path(const path
& __p
) = default;
195 path(path
&& __p
) noexcept
196 : _M_pathname(std::move(__p
._M_pathname
)), _M_type(__p
._M_type
)
202 path(string_type
&& __source
)
203 : _M_pathname(std::move(__source
))
204 { _M_split_cmpts(); }
206 template<typename _Source
,
207 typename _Require
= _Path
<_Source
>>
208 path(_Source
const& __source
)
209 : _M_pathname(_S_convert(_S_range_begin(__source
),
210 _S_range_end(__source
)))
211 { _M_split_cmpts(); }
213 template<typename _InputIterator
,
214 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
215 path(_InputIterator __first
, _InputIterator __last
)
216 : _M_pathname(_S_convert(__first
, __last
))
217 { _M_split_cmpts(); }
219 template<typename _Source
,
220 typename _Require
= _Path
<_Source
>,
221 typename _Require2
= __value_type_is_char
<_Source
>>
222 path(_Source
const& __source
, const locale
& __loc
)
223 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
224 _S_range_end(__source
), __loc
))
225 { _M_split_cmpts(); }
227 template<typename _InputIterator
,
228 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
229 typename _Require2
= __value_type_is_char
<_InputIterator
>>
230 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
)
231 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
232 { _M_split_cmpts(); }
238 path
& operator=(const path
& __p
) = default;
239 path
& operator=(path
&& __p
) noexcept
;
240 path
& operator=(string_type
&& __source
);
241 path
& assign(string_type
&& __source
);
243 template<typename _Source
>
245 operator=(_Source
const& __source
)
246 { return *this = path(__source
); }
248 template<typename _Source
>
250 assign(_Source
const& __source
)
251 { return *this = path(__source
); }
253 template<typename _InputIterator
>
254 _Path
<_InputIterator
, _InputIterator
>&
255 assign(_InputIterator __first
, _InputIterator __last
)
256 { return *this = path(__first
, __last
); }
260 path
& operator/=(const path
& __p
) { return _M_append(__p
._M_pathname
); }
262 template <class _Source
>
264 operator/=(_Source
const& __source
)
265 { return append(__source
); }
267 template<typename _Source
>
269 append(_Source
const& __source
)
271 return _M_append(_S_convert(_S_range_begin(__source
),
272 _S_range_end(__source
)));
275 template<typename _InputIterator
>
276 _Path
<_InputIterator
, _InputIterator
>&
277 append(_InputIterator __first
, _InputIterator __last
)
278 { return _M_append(_S_convert(__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 #if __cplusplus >= 201402L
287 path
& operator+=(basic_string_view
<value_type
> __x
);
290 template<typename _Source
>
292 operator+=(_Source
const& __x
) { return concat(__x
); }
294 template<typename _CharT
>
295 _Path
<_CharT
*, _CharT
*>&
296 operator+=(_CharT __x
);
298 template<typename _Source
>
300 concat(_Source
const& __x
)
301 { return *this += _S_convert(_S_range_begin(__x
), _S_range_end(__x
)); }
303 template<typename _InputIterator
>
304 _Path
<_InputIterator
, _InputIterator
>&
305 concat(_InputIterator __first
, _InputIterator __last
)
306 { return *this += _S_convert(__first
, __last
); }
310 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
312 path
& make_preferred();
313 path
& remove_filename();
314 path
& replace_filename(const path
& __replacement
);
315 path
& replace_extension(const path
& __replacement
= path());
317 void swap(path
& __rhs
) noexcept
;
319 // native format observers
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
; }
325 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
326 typename _Allocator
= std::allocator
<_CharT
>>
327 std::basic_string
<_CharT
, _Traits
, _Allocator
>
328 string(const _Allocator
& __a
= _Allocator()) const;
330 std::string
string() const;
331 #if _GLIBCXX_USE_WCHAR_T
332 std::wstring
wstring() const;
334 #ifdef _GLIBCXX_USE_CHAR8_T
335 __attribute__((__abi_tag__("__u8")))
336 std::u8string
u8string() const;
338 std::string
u8string() const;
339 #endif // _GLIBCXX_USE_CHAR8_T
340 std::u16string
u16string() const;
341 std::u32string
u32string() const;
343 // generic format observers
344 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
345 typename _Allocator
= std::allocator
<_CharT
>>
346 std::basic_string
<_CharT
, _Traits
, _Allocator
>
347 generic_string(const _Allocator
& __a
= _Allocator()) const;
349 std::string
generic_string() const;
350 #if _GLIBCXX_USE_WCHAR_T
351 std::wstring
generic_wstring() const;
353 #ifdef _GLIBCXX_USE_CHAR8_T
354 __attribute__((__abi_tag__("__u8")))
355 std::u8string
generic_u8string() const;
357 std::string
generic_u8string() const;
358 #endif // _GLIBCXX_USE_CHAR8_T
359 std::u16string
generic_u16string() const;
360 std::u32string
generic_u32string() const;
364 int compare(const path
& __p
) const noexcept
;
365 int compare(const string_type
& __s
) const;
366 int compare(const value_type
* __s
) const;
367 #if __cplusplus >= 201402L
368 int compare(const basic_string_view
<value_type
> __s
) const;
373 path
root_name() const;
374 path
root_directory() const;
375 path
root_path() const;
376 path
relative_path() const;
377 path
parent_path() const;
378 path
filename() const;
380 path
extension() const;
384 _GLIBCXX_NODISCARD
bool empty() const noexcept
{ return _M_pathname
.empty(); }
385 bool has_root_name() const;
386 bool has_root_directory() const;
387 bool has_root_path() const;
388 bool has_relative_path() const;
389 bool has_parent_path() const;
390 bool has_filename() const;
391 bool has_stem() const;
392 bool has_extension() const;
393 bool is_absolute() const;
394 bool is_relative() const { return !is_absolute(); }
398 typedef iterator const_iterator
;
400 iterator
begin() const;
401 iterator
end() const;
403 /// @cond undocumented
404 // Create a basic_string by reading until a null character.
405 template<typename _InputIterator
,
406 typename _Traits
= std::iterator_traits
<_InputIterator
>,
408 = typename
std::remove_cv
<typename
_Traits::value_type
>::type
>
409 static std::basic_string
<_CharT
>
410 _S_string_from_iter(_InputIterator __source
)
412 std::basic_string
<_CharT
> __str
;
413 for (_CharT __ch
= *__source
; __ch
!= _CharT(); __ch
= *++__source
)
414 __str
.push_back(__ch
);
420 enum class _Type
: unsigned char {
421 _Multi
, _Root_name
, _Root_dir
, _Filename
424 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
426 __glibcxx_assert(!empty());
427 __glibcxx_assert(_M_type
!= _Type::_Multi
);
430 enum class _Split
{ _Stem
, _Extension
};
432 path
& _M_append(const string_type
& __str
)
434 if (!_M_pathname
.empty() && !_S_is_dir_sep(_M_pathname
.back())
435 && !__str
.empty() && !_S_is_dir_sep(__str
.front()))
436 _M_pathname
+= preferred_separator
;
437 _M_pathname
+= __str
;
442 pair
<const string_type
*, size_t> _M_find_extension() const;
444 template<typename _CharT
>
448 _S_convert(value_type
* __src
, __null_terminated
)
449 { return string_type(__src
); }
452 _S_convert(const value_type
* __src
, __null_terminated
)
453 { return string_type(__src
); }
455 template<typename _Iter
>
457 _S_convert(_Iter __first
, _Iter __last
)
459 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
460 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
461 _S_convert(__first
, __last
);
464 template<typename _InputIterator
>
466 _S_convert(_InputIterator __src
, __null_terminated
)
468 auto __s
= _S_string_from_iter(__src
);
469 return _S_convert(__s
.c_str(), __s
.c_str() + __s
.size());
473 _S_convert_loc(const char* __first
, const char* __last
,
474 const std::locale
& __loc
);
476 template<typename _Iter
>
478 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
480 const std::string
__str(__first
, __last
);
481 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
484 template<typename _InputIterator
>
486 _S_convert_loc(_InputIterator __src
, __null_terminated
,
487 const std::locale
& __loc
)
489 const std::string __s
= _S_string_from_iter(__src
);
490 return _S_convert_loc(__s
.data(), __s
.data() + __s
.size(), __loc
);
493 bool _S_is_dir_sep(value_type __ch
)
495 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
496 return __ch
== L
'/' || __ch
== preferred_separator
;
502 void _M_split_cmpts();
504 void _M_add_root_name(size_t __n
);
505 void _M_add_root_dir(size_t __pos
);
506 void _M_add_filename(size_t __pos
, size_t __n
);
508 string_type _M_pathname
;
511 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
512 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
513 _Type _M_type
= _Type::_Multi
;
516 /// @relates std::experimental::filesystem::path @{
518 /// Swap overload for paths
519 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
521 /// Compute a hash value for a path
522 size_t hash_value(const path
& __p
) noexcept
;
525 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
526 { return __lhs
.compare(__rhs
) < 0; }
529 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
530 { return !(__rhs
< __lhs
); }
533 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
534 { return __rhs
< __lhs
; }
537 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
538 { return !(__lhs
< __rhs
); }
541 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
542 { return __lhs
.compare(__rhs
) == 0; }
545 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
546 { return !(__lhs
== __rhs
); }
548 /// Append one path to another
549 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
551 path
__result(__lhs
);
556 /// Write a path to a stream
557 template<typename _CharT
, typename _Traits
>
558 basic_ostream
<_CharT
, _Traits
>&
559 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
561 auto __tmp
= __p
.string
<_CharT
, _Traits
>();
562 using __quoted_string
563 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
564 __os
<< __quoted_string
{__tmp
, _CharT('"'), _CharT('\\')};
568 /// Read a path from a stream
569 template<typename _CharT
, typename _Traits
>
570 basic_istream
<_CharT
, _Traits
>&
571 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
573 basic_string
<_CharT
, _Traits
> __tmp
;
574 using __quoted_string
575 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
576 if (__is
>> __quoted_string
{ __tmp
, _CharT('"'), _CharT('\\') })
577 __p
= std::move(__tmp
);
581 /// Create a path from a UTF-8-encoded sequence of char
582 // TODO constrain with _Path<Source> and __value_type_is_char
583 template<typename _Source
>
585 u8path(const _Source
& __source
)
587 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
588 return path
{ path::string_type
{__source
} };
590 return path
{ __source
};
594 /// Create a path from a UTF-8-encoded sequence of char
595 // TODO constrain with _Path<InputIterator, InputIterator> and __value_type_is_char
596 template<typename _InputIterator
>
598 u8path(_InputIterator __first
, _InputIterator __last
)
600 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
601 return path
{ path::string_type
{__first
, __last
} };
603 return path
{ __first
, __last
};
609 /// Exception type thrown by the Filesystem TS library
610 class filesystem_error
: public std::system_error
613 filesystem_error(const string
& __what_arg
, error_code __ec
)
614 : system_error(__ec
, __what_arg
) { }
616 filesystem_error(const string
& __what_arg
, const path
& __p1
,
618 : system_error(__ec
, __what_arg
), _M_path1(__p1
) { }
620 filesystem_error(const string
& __what_arg
, const path
& __p1
,
621 const path
& __p2
, error_code __ec
)
622 : system_error(__ec
, __what_arg
), _M_path1(__p1
), _M_path2(__p2
)
627 const path
& path1() const noexcept
{ return _M_path1
; }
628 const path
& path2() const noexcept
{ return _M_path2
; }
629 const char* what() const noexcept
{ return _M_what
.c_str(); }
632 std::string
_M_gen_what();
636 std::string _M_what
= _M_gen_what();
639 /// @cond undocumented
640 struct path::_Cmpt
: path
642 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
643 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
645 _Cmpt() : _M_pos(-1) { }
650 // specialize _Cvt for degenerate 'noconv' case
652 struct path::_Cvt
<path::value_type
>
654 template<typename _Iter
>
656 _S_convert(_Iter __first
, _Iter __last
)
657 { return string_type
{__first
, __last
}; }
660 template<typename _CharT
>
663 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
665 _S_wconvert(const char* __f
, const char* __l
, true_type
)
667 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
668 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
670 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
672 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
673 "Cannot convert character sequence",
674 std::make_error_code(errc::illegal_byte_sequence
)));
678 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
680 std::codecvt_utf8
<_CharT
> __cvt
;
682 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
684 const char* __f2
= __str
.data();
685 const char* __l2
= __f2
+ __str
.size();
686 std::codecvt_utf8
<wchar_t> __wcvt
;
688 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
691 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
692 "Cannot convert character sequence",
693 std::make_error_code(errc::illegal_byte_sequence
)));
697 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
699 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
703 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
705 #ifdef _GLIBCXX_USE_CHAR8_T
706 if constexpr (is_same
<_CharT
, char8_t
>::value
)
708 string_type
__str(__f
, __l
);
714 std::codecvt_utf8
<_CharT
> __cvt
;
716 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
718 #ifdef _GLIBCXX_USE_CHAR8_T
721 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
722 "Cannot convert character sequence",
723 std::make_error_code(errc::illegal_byte_sequence
)));
728 _S_convert(_CharT
* __f
, _CharT
* __l
)
730 return _S_convert(const_cast<const _CharT
*>(__f
),
731 const_cast<const _CharT
*>(__l
));
734 template<typename _Iter
>
736 _S_convert(_Iter __first
, _Iter __last
)
738 const std::basic_string
<_CharT
> __str(__first
, __last
);
739 return _S_convert(__str
.data(), __str
.data() + __str
.size());
742 template<typename _Iter
, typename _Cont
>
744 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
745 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
746 { return _S_convert(__first
.base(), __last
.base()); }
750 /// An iterator for the components of a path
754 using difference_type
= std::ptrdiff_t;
755 using value_type
= path
;
756 using reference
= const path
&;
757 using pointer
= const path
*;
758 using iterator_category
= std::bidirectional_iterator_tag
;
760 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
762 iterator(const iterator
&) = default;
763 iterator
& operator=(const iterator
&) = default;
765 reference
operator*() const;
766 pointer
operator->() const { return std::__addressof(**this); }
768 iterator
& operator++();
769 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
771 iterator
& operator--();
772 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
774 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
775 { return __lhs
._M_equals(__rhs
); }
777 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
778 { return !__lhs
._M_equals(__rhs
); }
783 iterator(const path
* __path
, path::_List::const_iterator __iter
)
784 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
787 iterator(const path
* __path
, bool __at_end
)
788 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
791 bool _M_equals(iterator
) const;
794 path::_List::const_iterator _M_cur
;
795 bool _M_at_end
; // only used when type != _Multi
800 path::operator=(path
&& __p
) noexcept
802 _M_pathname
= std::move(__p
._M_pathname
);
803 _M_cmpts
= std::move(__p
._M_cmpts
);
804 _M_type
= __p
._M_type
;
810 path::operator=(string_type
&& __source
)
811 { return *this = path(std::move(__source
)); }
814 path::assign(string_type
&& __source
)
815 { return *this = path(std::move(__source
)); }
818 path::operator+=(const path
& __p
)
820 return operator+=(__p
.native());
824 path::operator+=(const string_type
& __x
)
832 path::operator+=(const value_type
* __x
)
840 path::operator+=(value_type __x
)
847 #if __cplusplus >= 201402L
849 path::operator+=(basic_string_view
<value_type
> __x
)
851 _M_pathname
.append(__x
.data(), __x
.size());
857 template<typename _CharT
>
858 inline path::_Path
<_CharT
*, _CharT
*>&
859 path::operator+=(_CharT __x
)
861 auto* __addr
= std::__addressof(__x
);
862 return concat(__addr
, __addr
+ 1);
866 path::make_preferred()
868 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
869 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
870 preferred_separator
);
875 inline void path::swap(path
& __rhs
) noexcept
877 _M_pathname
.swap(__rhs
._M_pathname
);
878 _M_cmpts
.swap(__rhs
._M_cmpts
);
879 std::swap(_M_type
, __rhs
._M_type
);
882 template<typename _CharT
, typename _Traits
, typename _Allocator
>
883 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
884 path::string(const _Allocator
& __a
) const
886 if (is_same
<_CharT
, value_type
>::value
)
887 return { _M_pathname
.begin(), _M_pathname
.end(), __a
};
889 const value_type
* __first
= _M_pathname
.data();
890 const value_type
* __last
= __first
+ _M_pathname
.size();
892 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
893 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
894 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
895 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
897 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
898 codecvt_utf8
<value_type
> __cvt
;
899 _String __u8str
{_CharAlloc
{__a
}};
900 if (__str_codecvt_out(__first
, __last
, __u8str
, __cvt
))
905 operator()(const _String
& __from
, _String
&, true_type
)
906 { return std::__addressof(__from
); }
909 operator()(const _String
& __from
, _WString
& __to
, false_type
)
911 #ifdef _GLIBCXX_USE_CHAR8_T
912 if constexpr (is_same
<_CharT
, char8_t
>::value
)
914 __to
.assign(__from
.begin(), __from
.end());
915 return std::__addressof(__to
);
920 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
921 codecvt_utf8
<_CharT
> __cvt
;
922 const char* __f
= __from
.data();
923 const char* __l
= __f
+ __from
.size();
924 if (__str_codecvt_in(__f
, __l
, __to
, __cvt
))
925 return std::__addressof(__to
);
926 #ifdef _GLIBCXX_USE_CHAR8_T
933 if (auto* __p
= __dispatch(__u8str
, __wstr
, is_same
<_CharT
, char>{}))
937 #ifdef _GLIBCXX_USE_CHAR8_T
938 if constexpr (is_same
<_CharT
, char8_t
>::value
)
940 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__first
, __last
, __a
};
946 codecvt_utf8
<_CharT
> __cvt
;
947 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
948 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
950 #ifdef _GLIBCXX_USE_CHAR8_T
954 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
955 "Cannot convert character sequence",
956 std::make_error_code(errc::illegal_byte_sequence
)));
960 path::string() const { return string
<char>(); }
962 #if _GLIBCXX_USE_WCHAR_T
964 path::wstring() const { return string
<wchar_t>(); }
967 #ifdef _GLIBCXX_USE_CHAR8_T
969 path::u8string() const { return string
<char8_t
>(); }
972 path::u8string() const
974 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
976 // convert from native encoding to UTF-8
977 codecvt_utf8
<value_type
> __cvt
;
978 const value_type
* __first
= _M_pathname
.data();
979 const value_type
* __last
= __first
+ _M_pathname
.size();
980 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
982 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
983 "Cannot convert character sequence",
984 std::make_error_code(errc::illegal_byte_sequence
)));
989 #endif // _GLIBCXX_USE_CHAR8_T
991 inline std::u16string
992 path::u16string() const { return string
<char16_t
>(); }
994 inline std::u32string
995 path::u32string() const { return string
<char32_t
>(); }
997 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
998 template<typename _CharT
, typename _Traits
, typename _Allocator
>
999 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
1000 path::generic_string(const _Allocator
& __a
) const
1001 { return string
<_CharT
, _Traits
, _Allocator
>(__a
); }
1004 path::generic_string() const { return string(); }
1006 #if _GLIBCXX_USE_WCHAR_T
1008 path::generic_wstring() const { return wstring(); }
1011 #ifdef _GLIBCXX_USE_CHAR8_T
1012 inline std::u8string
1013 path::generic_u8string() const { return u8string(); }
1016 path::generic_u8string() const { return u8string(); }
1019 inline std::u16string
1020 path::generic_u16string() const { return u16string(); }
1022 inline std::u32string
1023 path::generic_u32string() const { return u32string(); }
1027 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
1030 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
1032 #if __cplusplus >= 201402L
1034 path::compare(basic_string_view
<value_type
> __s
) const
1035 { return compare(path(__s
)); }
1039 path::filename() const { return empty() ? path() : *--end(); }
1044 auto ext
= _M_find_extension();
1045 if (ext
.first
&& ext
.second
!= 0)
1046 return path
{ext
.first
->substr(0, ext
.second
)};
1051 path::extension() const
1053 auto ext
= _M_find_extension();
1054 if (ext
.first
&& ext
.second
!= string_type::npos
)
1055 return path
{ext
.first
->substr(ext
.second
)};
1060 path::has_stem() const
1062 auto ext
= _M_find_extension();
1063 return ext
.first
&& ext
.second
!= 0;
1067 path::has_extension() const
1069 auto ext
= _M_find_extension();
1070 return ext
.first
&& ext
.second
!= string_type::npos
;
1074 path::is_absolute() const
1076 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1077 return has_root_name() && has_root_directory();
1079 return has_root_directory();
1083 inline path::iterator
1086 if (_M_type
== _Type::_Multi
)
1087 return iterator(this, _M_cmpts
.begin());
1088 return iterator(this, false);
1091 inline path::iterator
1094 if (_M_type
== _Type::_Multi
)
1095 return iterator(this, _M_cmpts
.end());
1096 return iterator(this, true);
1099 inline path::iterator
&
1100 path::iterator::operator++()
1102 __glibcxx_assert(_M_path
!= nullptr);
1103 if (_M_path
->_M_type
== _Type::_Multi
)
1105 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1110 __glibcxx_assert(!_M_at_end
);
1116 inline path::iterator
&
1117 path::iterator::operator--()
1119 __glibcxx_assert(_M_path
!= nullptr);
1120 if (_M_path
->_M_type
== _Type::_Multi
)
1122 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1127 __glibcxx_assert(_M_at_end
);
1133 inline path::iterator::reference
1134 path::iterator::operator*() const
1136 __glibcxx_assert(_M_path
!= nullptr);
1137 if (_M_path
->_M_type
== _Type::_Multi
)
1139 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1146 path::iterator::_M_equals(iterator __rhs
) const
1148 if (_M_path
!= __rhs
._M_path
)
1150 if (_M_path
== nullptr)
1152 if (_M_path
->_M_type
== path::_Type::_Multi
)
1153 return _M_cur
== __rhs
._M_cur
;
1154 return _M_at_end
== __rhs
._M_at_end
;
1157 // @} group filesystem-ts
1158 _GLIBCXX_END_NAMESPACE_CXX11
1160 } // namespace filesystem
1161 } // namespace experimental
1163 _GLIBCXX_END_NAMESPACE_VERSION
1168 #endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H