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 append(__source
); }
270 template<typename _Source
>
272 append(_Source
const& __source
)
274 return _M_append(_S_convert(_S_range_begin(__source
),
275 _S_range_end(__source
)));
278 template<typename _InputIterator
>
279 _Path
<_InputIterator
, _InputIterator
>&
280 append(_InputIterator __first
, _InputIterator __last
)
281 { return _M_append(_S_convert(__first
, __last
)); }
285 path
& operator+=(const path
& __x
);
286 path
& operator+=(const string_type
& __x
);
287 path
& operator+=(const value_type
* __x
);
288 path
& operator+=(value_type __x
);
289 path
& operator+=(basic_string_view
<value_type
> __x
);
291 template<typename _Source
>
293 operator+=(_Source
const& __x
) { return concat(__x
); }
295 template<typename _CharT
>
296 _Path
<_CharT
*, _CharT
*>&
297 operator+=(_CharT __x
);
299 template<typename _Source
>
301 concat(_Source
const& __x
)
302 { return *this += _S_convert(_S_range_begin(__x
), _S_range_end(__x
)); }
304 template<typename _InputIterator
>
305 _Path
<_InputIterator
, _InputIterator
>&
306 concat(_InputIterator __first
, _InputIterator __last
)
307 { return *this += _S_convert(__first
, __last
); }
311 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
313 path
& make_preferred();
314 path
& remove_filename();
315 path
& replace_filename(const path
& __replacement
);
316 path
& replace_extension(const path
& __replacement
= path());
318 void swap(path
& __rhs
) noexcept
;
320 // native format observers
322 const string_type
& native() const noexcept
{ return _M_pathname
; }
323 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
324 operator string_type() const { return _M_pathname
; }
326 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
327 typename _Allocator
= std::allocator
<_CharT
>>
328 std::basic_string
<_CharT
, _Traits
, _Allocator
>
329 string(const _Allocator
& __a
= _Allocator()) const;
331 std::string
string() const;
332 #if _GLIBCXX_USE_WCHAR_T
333 std::wstring
wstring() const;
335 std::string
u8string() const;
336 std::u16string
u16string() const;
337 std::u32string
u32string() const;
339 // generic format observers
340 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
341 typename _Allocator
= std::allocator
<_CharT
>>
342 std::basic_string
<_CharT
, _Traits
, _Allocator
>
343 generic_string(const _Allocator
& __a
= _Allocator()) const;
345 std::string
generic_string() const;
346 #if _GLIBCXX_USE_WCHAR_T
347 std::wstring
generic_wstring() const;
349 std::string
generic_u8string() const;
350 std::u16string
generic_u16string() const;
351 std::u32string
generic_u32string() const;
355 int compare(const path
& __p
) const noexcept
;
356 int compare(const string_type
& __s
) const;
357 int compare(const value_type
* __s
) const;
358 int compare(const basic_string_view
<value_type
> __s
) const;
362 path
root_name() const;
363 path
root_directory() const;
364 path
root_path() const;
365 path
relative_path() const;
366 path
parent_path() const;
367 path
filename() const;
369 path
extension() const;
373 [[nodiscard
]] bool empty() const noexcept
{ return _M_pathname
.empty(); }
374 bool has_root_name() const;
375 bool has_root_directory() const;
376 bool has_root_path() const;
377 bool has_relative_path() const;
378 bool has_parent_path() const;
379 bool has_filename() const;
380 bool has_stem() const;
381 bool has_extension() const;
382 bool is_absolute() const;
383 bool is_relative() const { return !is_absolute(); }
386 path
lexically_normal() const;
387 path
lexically_relative(const path
& base
) const;
388 path
lexically_proximate(const path
& base
) const;
392 typedef iterator const_iterator
;
394 iterator
begin() const;
395 iterator
end() const;
398 enum class _Type
: unsigned char {
399 _Multi
, _Root_name
, _Root_dir
, _Filename
402 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
404 __glibcxx_assert(_M_type
!= _Type::_Multi
);
407 enum class _Split
{ _Stem
, _Extension
};
409 path
& _M_append(string_type
&& __str
)
411 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
412 operator/=(path(std::move(__str
)));
414 if (!_M_pathname
.empty() && !_S_is_dir_sep(_M_pathname
.back())
415 && (__str
.empty() || !_S_is_dir_sep(__str
.front())))
416 _M_pathname
+= preferred_separator
;
417 _M_pathname
+= __str
;
423 pair
<const string_type
*, size_t> _M_find_extension() const;
425 template<typename _CharT
>
429 _S_convert(value_type
* __src
, __null_terminated
)
430 { return string_type(__src
); }
433 _S_convert(const value_type
* __src
, __null_terminated
)
434 { return string_type(__src
); }
436 template<typename _Iter
>
438 _S_convert(_Iter __first
, _Iter __last
)
440 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
441 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
442 _S_convert(__first
, __last
);
445 template<typename _InputIterator
>
447 _S_convert(_InputIterator __src
, __null_terminated
)
449 using _Tp
= typename
std::iterator_traits
<_InputIterator
>::value_type
;
450 std::basic_string
<typename remove_cv
<_Tp
>::type
> __tmp
;
451 for (; *__src
!= _Tp
{}; ++__src
)
452 __tmp
.push_back(*__src
);
453 return _S_convert(__tmp
.c_str(), __tmp
.c_str() + __tmp
.size());
457 _S_convert_loc(const char* __first
, const char* __last
,
458 const std::locale
& __loc
);
460 template<typename _Iter
>
462 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
464 const std::string
__str(__first
, __last
);
465 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
468 template<typename _InputIterator
>
470 _S_convert_loc(_InputIterator __src
, __null_terminated
,
471 const std::locale
& __loc
)
474 while (*__src
!= '\0')
475 __tmp
.push_back(*__src
++);
476 return _S_convert_loc(__tmp
.data(), __tmp
.data()+__tmp
.size(), __loc
);
479 template<typename _CharT
, typename _Traits
, typename _Allocator
>
480 static basic_string
<_CharT
, _Traits
, _Allocator
>
481 _S_str_convert(const string_type
&, const _Allocator
& __a
);
483 bool _S_is_dir_sep(value_type __ch
)
485 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
486 return __ch
== L
'/' || __ch
== preferred_separator
;
492 void _M_split_cmpts();
494 void _M_add_root_name(size_t __n
);
495 void _M_add_root_dir(size_t __pos
);
496 void _M_add_filename(size_t __pos
, size_t __n
);
498 string_type _M_pathname
;
501 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
502 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
503 _Type _M_type
= _Type::_Multi
;
507 struct path::__is_encoded_char
<char> : std::true_type
508 { using value_type
= char; };
511 struct path::__is_encoded_char
<wchar_t> : std::true_type
512 { using value_type
= wchar_t; };
515 struct path::__is_encoded_char
<char16_t
> : std::true_type
516 { using value_type
= char16_t
; };
519 struct path::__is_encoded_char
<char32_t
> : std::true_type
520 { using value_type
= char32_t
; };
522 template<typename _Tp
>
523 struct path::__is_encoded_char
<const _Tp
> : __is_encoded_char
<_Tp
> { };
525 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
527 size_t hash_value(const path
& __p
) noexcept
;
530 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
531 { return __lhs
.compare(__rhs
) < 0; }
534 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
535 { return !(__rhs
< __lhs
); }
538 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
539 { return __rhs
< __lhs
; }
542 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
543 { return !(__lhs
< __rhs
); }
546 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
547 { return __lhs
.compare(__rhs
) == 0; }
550 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
551 { return !(__lhs
== __rhs
); }
553 /// Append one path to another
554 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
555 { return path(__lhs
) /= __rhs
; }
557 /// Write a path to a stream
558 template<typename _CharT
, typename _Traits
>
559 basic_ostream
<_CharT
, _Traits
>&
560 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
562 auto __tmp
= __p
.string
<_CharT
, _Traits
>();
563 using __quoted_string
564 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
565 __os
<< __quoted_string
{__tmp
, '"', '\\'};
569 /// Read a path from a stream
570 template<typename _CharT
, typename _Traits
>
571 basic_istream
<_CharT
, _Traits
>&
572 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
574 basic_string
<_CharT
, _Traits
> __tmp
;
575 using __quoted_string
576 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
577 if (__is
>> __quoted_string
{ __tmp
, '"', '\\' })
578 __p
= std::move(__tmp
);
582 template<typename _Source
>
584 u8path(const _Source
& __source
)
585 -> decltype(filesystem::path(__source
, std::locale::classic()))
587 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
588 const std::string __u8str
{__source
};
589 return std::filesystem::u8path(__u8str
.begin(), __u8str
.end());
591 return path
{ __source
};
595 template<typename _InputIterator
>
597 u8path(_InputIterator __first
, _InputIterator __last
)
598 -> decltype(filesystem::path(__first
, __last
, std::locale::classic()))
600 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
601 codecvt_utf8
<value_type
> __cvt
;
603 if (__str_codecvt_in(__first
, __last
, __tmp
, __cvt
))
604 return path
{ __tmp
};
608 return path
{ __first
, __last
};
612 class filesystem_error
: public std::system_error
615 filesystem_error(const string
& __what_arg
, error_code __ec
)
616 : system_error(__ec
, __what_arg
) { }
618 filesystem_error(const string
& __what_arg
, const path
& __p1
,
620 : system_error(__ec
, __what_arg
), _M_path1(__p1
) { }
622 filesystem_error(const string
& __what_arg
, const path
& __p1
,
623 const path
& __p2
, error_code __ec
)
624 : system_error(__ec
, __what_arg
), _M_path1(__p1
), _M_path2(__p2
)
629 const path
& path1() const noexcept
{ return _M_path1
; }
630 const path
& path2() const noexcept
{ return _M_path2
; }
631 const char* what() const noexcept
{ return _M_what
.c_str(); }
634 std::string
_M_gen_what();
638 std::string _M_what
= _M_gen_what();
641 struct path::_Cmpt
: path
643 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
644 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
646 _Cmpt() : _M_pos(-1) { }
651 // specialize _Cvt for degenerate 'noconv' case
653 struct path::_Cvt
<path::value_type
>
655 template<typename _Iter
>
657 _S_convert(_Iter __first
, _Iter __last
)
658 { return string_type
{__first
, __last
}; }
661 template<typename _CharT
>
664 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
666 _S_wconvert(const char* __f
, const char* __l
, true_type
)
668 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
669 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
671 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
673 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
674 "Cannot convert character sequence",
675 std::make_error_code(errc::illegal_byte_sequence
)));
679 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
681 std::codecvt_utf8
<_CharT
> __cvt
;
683 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
685 const char* __f2
= __str
.data();
686 const char* __l2
= __f2
+ __str
.size();
687 std::codecvt_utf8
<wchar_t> __wcvt
;
689 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
692 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
693 "Cannot convert character sequence",
694 std::make_error_code(errc::illegal_byte_sequence
)));
698 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
700 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
704 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
706 std::codecvt_utf8
<_CharT
> __cvt
;
708 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
710 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
711 "Cannot convert character sequence",
712 std::make_error_code(errc::illegal_byte_sequence
)));
717 _S_convert(_CharT
* __f
, _CharT
* __l
)
719 return _S_convert(const_cast<const _CharT
*>(__f
),
720 const_cast<const _CharT
*>(__l
));
723 template<typename _Iter
>
725 _S_convert(_Iter __first
, _Iter __last
)
727 const std::basic_string
<_CharT
> __str(__first
, __last
);
728 return _S_convert(__str
.data(), __str
.data() + __str
.size());
731 template<typename _Iter
, typename _Cont
>
733 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
734 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
735 { return _S_convert(__first
.base(), __last
.base()); }
738 /// An iterator for the components of a path
742 using difference_type
= std::ptrdiff_t;
743 using value_type
= path
;
744 using reference
= const path
&;
745 using pointer
= const path
*;
746 using iterator_category
= std::bidirectional_iterator_tag
;
748 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
750 iterator(const iterator
&) = default;
751 iterator
& operator=(const iterator
&) = default;
753 reference
operator*() const;
754 pointer
operator->() const { return std::__addressof(**this); }
756 iterator
& operator++();
757 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
759 iterator
& operator--();
760 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
762 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
763 { return __lhs
._M_equals(__rhs
); }
765 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
766 { return !__lhs
._M_equals(__rhs
); }
771 iterator(const path
* __path
, path::_List::const_iterator __iter
)
772 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
775 iterator(const path
* __path
, bool __at_end
)
776 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
779 bool _M_equals(iterator
) const;
782 path::_List::const_iterator _M_cur
;
783 bool _M_at_end
; // only used when type != _Multi
788 path::operator=(path
&& __p
) noexcept
790 _M_pathname
= std::move(__p
._M_pathname
);
791 _M_cmpts
= std::move(__p
._M_cmpts
);
792 _M_type
= __p
._M_type
;
798 path::operator=(string_type
&& __source
)
799 { return *this = path(std::move(__source
)); }
802 path::assign(string_type
&& __source
)
803 { return *this = path(std::move(__source
)); }
806 path::operator+=(const path
& __p
)
808 return operator+=(__p
.native());
812 path::operator+=(const string_type
& __x
)
820 path::operator+=(const value_type
* __x
)
828 path::operator+=(value_type __x
)
836 path::operator+=(basic_string_view
<value_type
> __x
)
838 _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 std::basic_string
<_CharT
, _Traits
, _Allocator
>
870 path::_S_str_convert(const string_type
& __str
, const _Allocator
& __a
)
872 if (__str
.size() == 0)
873 return std::basic_string
<_CharT
, _Traits
, _Allocator
>(__a
);
875 const value_type
* __first
= __str
.data();
876 const value_type
* __last
= __first
+ __str
.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
))
888 if constexpr (is_same_v
<_CharT
, char>)
893 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
894 codecvt_utf8
<_CharT
> __cvt
;
895 const char* __f
= __u8str
.data();
896 const char* __l
= __f
+ __u8str
.size();
897 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
902 codecvt_utf8
<_CharT
> __cvt
;
903 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
904 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
907 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
908 "Cannot convert character sequence",
909 std::make_error_code(errc::illegal_byte_sequence
)));
912 template<typename _CharT
, typename _Traits
, typename _Allocator
>
913 inline basic_string
<_CharT
, _Traits
, _Allocator
>
914 path::string(const _Allocator
& __a
) const
916 if constexpr (is_same_v
<_CharT
, value_type
>)
917 #if _GLIBCXX_USE_CXX11_ABI
918 return { _M_pathname
, __a
};
920 return { _M_pathname
, string_type::size_type(0), __a
};
923 return _S_str_convert
<_CharT
, _Traits
>(_M_pathname
, __a
);
927 path::string() const { return string
<char>(); }
929 #if _GLIBCXX_USE_WCHAR_T
931 path::wstring() const { return string
<wchar_t>(); }
935 path::u8string() const
937 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
939 // convert from native encoding to UTF-8
940 codecvt_utf8
<value_type
> __cvt
;
941 const value_type
* __first
= _M_pathname
.data();
942 const value_type
* __last
= __first
+ _M_pathname
.size();
943 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
945 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
946 "Cannot convert character sequence",
947 std::make_error_code(errc::illegal_byte_sequence
)));
953 inline std::u16string
954 path::u16string() const { return string
<char16_t
>(); }
956 inline std::u32string
957 path::u32string() const { return string
<char32_t
>(); }
959 template<typename _CharT
, typename _Traits
, typename _Allocator
>
960 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
961 path::generic_string(const _Allocator
& __a
) const
963 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
964 const value_type __slash
= L
'/';
966 const value_type __slash
= '/';
968 string_type
__str(__a
);
970 if (_M_type
== _Type::_Root_dir
)
971 __str
.assign(1, __slash
);
974 __str
.reserve(_M_pathname
.size());
975 bool __add_slash
= false;
976 for (auto& __elem
: *this)
980 __str
+= __elem
._M_pathname
;
981 __add_slash
= __elem
._M_type
== _Type::_Filename
;
985 if constexpr (is_same_v
<_CharT
, value_type
>)
988 return _S_str_convert
<_CharT
, _Traits
>(__str
, __a
);
992 path::generic_string() const
993 { return generic_string
<char>(); }
995 #if _GLIBCXX_USE_WCHAR_T
997 path::generic_wstring() const
998 { return generic_string
<wchar_t>(); }
1002 path::generic_u8string() const
1003 { return generic_string(); }
1005 inline std::u16string
1006 path::generic_u16string() const
1007 { return generic_string
<char16_t
>(); }
1009 inline std::u32string
1010 path::generic_u32string() const
1011 { return generic_string
<char32_t
>(); }
1014 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
1017 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
1020 path::compare(basic_string_view
<value_type
> __s
) const
1021 { return compare(path(__s
)); }
1024 path::filename() const
1028 else if (_M_type
== _Type::_Filename
)
1030 else if (_M_type
== _Type::_Multi
)
1032 if (_M_pathname
.back() == preferred_separator
)
1034 auto& __last
= *--end();
1035 if (__last
._M_type
== _Type::_Filename
)
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();
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
1158 _GLIBCXX_END_NAMESPACE_CXX11
1159 } // namespace filesystem
1161 _GLIBCXX_END_NAMESPACE_VERSION
1166 #endif // _GLIBCXX_FS_PATH_H