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>
42 #include <string_view>
43 #include <system_error>
44 #include <bits/stl_algobase.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
, typename _Ch
= remove_const_t
<_CharT
>>
69 using __is_encoded_char
70 = __or_
<is_same
<_Ch
, char>, is_same
<_Ch
, wchar_t>,
71 is_same
<_Ch
, char16_t
>, is_same
<_Ch
, char32_t
>>;
73 template<typename _Iter
,
74 typename _Iter_traits
= std::iterator_traits
<_Iter
>>
75 using __is_path_iter_src
76 = __and_
<__is_encoded_char
<typename
_Iter_traits::value_type
>,
77 std::is_base_of
<std::input_iterator_tag
,
78 typename
_Iter_traits::iterator_category
>>;
80 template<typename _Iter
>
81 static __is_path_iter_src
<_Iter
>
82 __is_path_src(_Iter
, int);
84 template<typename _CharT
, typename _Traits
, typename _Alloc
>
85 static __is_encoded_char
<_CharT
>
86 __is_path_src(const basic_string
<_CharT
, _Traits
, _Alloc
>&, int);
88 template<typename _CharT
, typename _Traits
>
89 static __is_encoded_char
<_CharT
>
90 __is_path_src(const basic_string_view
<_CharT
, _Traits
>&, int);
92 template<typename _Unknown
>
93 static std::false_type
94 __is_path_src(const _Unknown
&, ...);
96 template<typename _Tp1
, typename _Tp2
>
97 struct __constructible_from
;
99 template<typename _Iter
>
100 struct __constructible_from
<_Iter
, _Iter
>
101 : __is_path_iter_src
<_Iter
>
104 template<typename _Source
>
105 struct __constructible_from
<_Source
, void>
106 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
109 template<typename _Tp1
, typename _Tp2
= void>
110 using _Path
= typename
111 std::enable_if
<__and_
<__not_
<is_same
<_Tp1
, path
>>,
112 __constructible_from
<_Tp1
, _Tp2
>>::value
,
115 template<typename _Source
>
117 _S_range_begin(_Source __begin
) { return __begin
; }
119 struct __null_terminated
{ };
121 template<typename _Source
>
122 static __null_terminated
123 _S_range_end(_Source
) { return {}; }
125 template<typename _CharT
, typename _Traits
, typename _Alloc
>
127 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
128 { return __str
.data(); }
130 template<typename _CharT
, typename _Traits
, typename _Alloc
>
132 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
133 { return __str
.data() + __str
.size(); }
135 template<typename _CharT
, typename _Traits
>
137 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
138 { return __str
.data(); }
140 template<typename _CharT
, typename _Traits
>
142 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
143 { return __str
.data() + __str
.size(); }
145 template<typename _Tp
,
146 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
147 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
148 using __value_type_is_char
149 = std::enable_if_t
<std::is_same_v
<std::remove_const_t
<_Val
>, char>>;
152 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
153 typedef wchar_t value_type
;
154 static constexpr value_type preferred_separator
= L
'\\';
156 typedef char value_type
;
157 static constexpr value_type preferred_separator
= '/';
159 typedef std::basic_string
<value_type
> string_type
;
161 enum format
{ native_format
, generic_format
, auto_format
};
163 // constructors and destructor
167 path(const path
& __p
) = default;
169 path(path
&& __p
) noexcept
170 : _M_pathname(std::move(__p
._M_pathname
)), _M_type(__p
._M_type
)
176 path(string_type
&& __source
, format
= auto_format
)
177 : _M_pathname(std::move(__source
))
178 { _M_split_cmpts(); }
180 template<typename _Source
,
181 typename _Require
= _Path
<_Source
>>
182 path(_Source
const& __source
, format
= auto_format
)
183 : _M_pathname(_S_convert(_S_range_begin(__source
),
184 _S_range_end(__source
)))
185 { _M_split_cmpts(); }
187 template<typename _InputIterator
,
188 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
189 path(_InputIterator __first
, _InputIterator __last
, format
= auto_format
)
190 : _M_pathname(_S_convert(__first
, __last
))
191 { _M_split_cmpts(); }
193 template<typename _Source
,
194 typename _Require
= _Path
<_Source
>,
195 typename _Require2
= __value_type_is_char
<_Source
>>
196 path(_Source
const& __source
, const locale
& __loc
, format
= auto_format
)
197 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
198 _S_range_end(__source
), __loc
))
199 { _M_split_cmpts(); }
201 template<typename _InputIterator
,
202 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
203 typename _Require2
= __value_type_is_char
<_InputIterator
>>
204 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
,
205 format
= auto_format
)
206 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
207 { _M_split_cmpts(); }
213 path
& operator=(const path
& __p
) = default;
214 path
& operator=(path
&& __p
) noexcept
;
215 path
& operator=(string_type
&& __source
);
216 path
& assign(string_type
&& __source
);
218 template<typename _Source
>
220 operator=(_Source
const& __source
)
221 { return *this = path(__source
); }
223 template<typename _Source
>
225 assign(_Source
const& __source
)
226 { return *this = path(__source
); }
228 template<typename _InputIterator
>
229 _Path
<_InputIterator
, _InputIterator
>&
230 assign(_InputIterator __first
, _InputIterator __last
)
231 { return *this = path(__first
, __last
); }
235 path
& operator/=(const path
& __p
);
237 template <class _Source
>
239 operator/=(_Source
const& __source
)
240 { return _M_append(path(__source
)); }
242 template<typename _Source
>
244 append(_Source
const& __source
)
245 { return _M_append(path(__source
)); }
247 template<typename _InputIterator
>
248 _Path
<_InputIterator
, _InputIterator
>&
249 append(_InputIterator __first
, _InputIterator __last
)
250 { return _M_append(path(__first
, __last
)); }
254 path
& operator+=(const path
& __x
);
255 path
& operator+=(const string_type
& __x
);
256 path
& operator+=(const value_type
* __x
);
257 path
& operator+=(value_type __x
);
258 path
& operator+=(basic_string_view
<value_type
> __x
);
260 template<typename _Source
>
262 operator+=(_Source
const& __x
) { return concat(__x
); }
264 template<typename _CharT
>
265 _Path
<_CharT
*, _CharT
*>&
266 operator+=(_CharT __x
);
268 template<typename _Source
>
270 concat(_Source
const& __x
)
271 { return *this += _S_convert(_S_range_begin(__x
), _S_range_end(__x
)); }
273 template<typename _InputIterator
>
274 _Path
<_InputIterator
, _InputIterator
>&
275 concat(_InputIterator __first
, _InputIterator __last
)
276 { return *this += _S_convert(__first
, __last
); }
280 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
282 path
& make_preferred();
283 path
& remove_filename();
284 path
& replace_filename(const path
& __replacement
);
285 path
& replace_extension(const path
& __replacement
= path());
287 void swap(path
& __rhs
) noexcept
;
289 // native format observers
291 const string_type
& native() const noexcept
{ return _M_pathname
; }
292 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
293 operator string_type() const { return _M_pathname
; }
295 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
296 typename _Allocator
= std::allocator
<_CharT
>>
297 std::basic_string
<_CharT
, _Traits
, _Allocator
>
298 string(const _Allocator
& __a
= _Allocator()) const;
300 std::string
string() const;
301 #if _GLIBCXX_USE_WCHAR_T
302 std::wstring
wstring() const;
304 std::string
u8string() const;
305 std::u16string
u16string() const;
306 std::u32string
u32string() const;
308 // generic format observers
309 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
310 typename _Allocator
= std::allocator
<_CharT
>>
311 std::basic_string
<_CharT
, _Traits
, _Allocator
>
312 generic_string(const _Allocator
& __a
= _Allocator()) const;
314 std::string
generic_string() const;
315 #if _GLIBCXX_USE_WCHAR_T
316 std::wstring
generic_wstring() const;
318 std::string
generic_u8string() const;
319 std::u16string
generic_u16string() const;
320 std::u32string
generic_u32string() const;
324 int compare(const path
& __p
) const noexcept
;
325 int compare(const string_type
& __s
) const;
326 int compare(const value_type
* __s
) const;
327 int compare(const basic_string_view
<value_type
> __s
) const;
331 path
root_name() const;
332 path
root_directory() const;
333 path
root_path() const;
334 path
relative_path() const;
335 path
parent_path() const;
336 path
filename() const;
338 path
extension() const;
342 [[nodiscard
]] bool empty() const noexcept
{ return _M_pathname
.empty(); }
343 bool has_root_name() const;
344 bool has_root_directory() const;
345 bool has_root_path() const;
346 bool has_relative_path() const;
347 bool has_parent_path() const;
348 bool has_filename() const;
349 bool has_stem() const;
350 bool has_extension() const;
351 bool is_absolute() const;
352 bool is_relative() const { return !is_absolute(); }
355 path
lexically_normal() const;
356 path
lexically_relative(const path
& base
) const;
357 path
lexically_proximate(const path
& base
) const;
361 typedef iterator const_iterator
;
363 iterator
begin() const;
364 iterator
end() const;
366 /// Write a path to a stream
367 template<typename _CharT
, typename _Traits
>
368 friend std::basic_ostream
<_CharT
, _Traits
>&
369 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
371 __os
<< std::quoted(__p
.string
<_CharT
, _Traits
>());
375 /// Read a path from a stream
376 template<typename _CharT
, typename _Traits
>
377 friend std::basic_istream
<_CharT
, _Traits
>&
378 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
380 std::basic_string
<_CharT
, _Traits
> __tmp
;
381 if (__is
>> std::quoted(__tmp
))
382 __p
= std::move(__tmp
);
386 // Create a basic_string by reading until a null character.
387 template<typename _InputIterator
,
388 typename _Traits
= std::iterator_traits
<_InputIterator
>,
390 = typename
std::remove_cv_t
<typename
_Traits::value_type
>>
391 static std::basic_string
<_CharT
>
392 _S_string_from_iter(_InputIterator __source
)
394 std::basic_string
<_CharT
> __str
;
395 for (_CharT __ch
= *__source
; __ch
!= _CharT(); __ch
= *++__source
)
396 __str
.push_back(__ch
);
401 enum class _Type
: unsigned char {
402 _Multi
, _Root_name
, _Root_dir
, _Filename
405 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
407 __glibcxx_assert(_M_type
!= _Type::_Multi
);
410 enum class _Split
{ _Stem
, _Extension
};
412 path
& _M_append(path __p
);
414 pair
<const string_type
*, size_t> _M_find_extension() const;
416 template<typename _CharT
>
420 _S_convert(value_type
* __src
, __null_terminated
)
421 { return string_type(__src
); }
424 _S_convert(const value_type
* __src
, __null_terminated
)
425 { return string_type(__src
); }
427 template<typename _Iter
>
429 _S_convert(_Iter __first
, _Iter __last
)
431 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
432 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
433 _S_convert(__first
, __last
);
436 template<typename _InputIterator
>
438 _S_convert(_InputIterator __src
, __null_terminated
)
440 auto __s
= _S_string_from_iter(__src
);
441 return _S_convert(__s
.c_str(), __s
.c_str() + __s
.size());
445 _S_convert_loc(const char* __first
, const char* __last
,
446 const std::locale
& __loc
);
448 template<typename _Iter
>
450 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
452 const std::string
__str(__first
, __last
);
453 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
456 template<typename _InputIterator
>
458 _S_convert_loc(_InputIterator __src
, __null_terminated
,
459 const std::locale
& __loc
)
461 std::string __s
= _S_string_from_iter(__src
);
462 return _S_convert_loc(__s
.data(), __s
.data() + __s
.size(), __loc
);
465 template<typename _CharT
, typename _Traits
, typename _Allocator
>
466 static basic_string
<_CharT
, _Traits
, _Allocator
>
467 _S_str_convert(const string_type
&, const _Allocator
& __a
);
469 bool _S_is_dir_sep(value_type __ch
)
471 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
472 return __ch
== L
'/' || __ch
== preferred_separator
;
478 void _M_split_cmpts();
480 void _M_add_root_name(size_t __n
);
481 void _M_add_root_dir(size_t __pos
);
482 void _M_add_filename(size_t __pos
, size_t __n
);
484 string_type _M_pathname
;
487 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
488 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
489 _Type _M_type
= _Type::_Filename
;
492 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
494 size_t hash_value(const path
& __p
) noexcept
;
497 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
498 { return __lhs
.compare(__rhs
) < 0; }
501 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
502 { return !(__rhs
< __lhs
); }
505 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
506 { return __rhs
< __lhs
; }
509 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
510 { return !(__lhs
< __rhs
); }
513 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
514 { return __lhs
.compare(__rhs
) == 0; }
517 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
518 { return !(__lhs
== __rhs
); }
520 /// Append one path to another
521 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
523 path
__result(__lhs
);
528 template<typename _InputIterator
>
530 u8path(_InputIterator __first
, _InputIterator __last
)
531 -> decltype(filesystem::path(__first
, __last
, std::locale::classic()))
533 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
534 codecvt_utf8
<path::value_type
> __cvt
;
535 path::string_type __tmp
;
536 if constexpr (is_pointer_v
<_InputIterator
>)
538 if (__str_codecvt_in(__first
, __last
, __tmp
, __cvt
))
539 return path
{ __tmp
};
543 const std::string __u8str
{__first
, __last
};
544 const char* const __ptr
= __u8str
.data();
545 if (__str_codecvt_in(__ptr
, __ptr
+ __u8str
.size(), __tmp
, __cvt
))
546 return path
{ __tmp
};
550 return path
{ __first
, __last
};
554 template<typename _Source
>
556 u8path(const _Source
& __source
)
557 -> decltype(filesystem::path(__source
, std::locale::classic()))
559 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
560 if constexpr (is_convertible_v
<const _Source
&, std::string_view
>)
562 const std::string_view __s
= __source
;
563 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
567 std::string __s
= path::_S_string_from_iter(__source
);
568 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
571 return path
{ __source
};
575 class filesystem_error
: public std::system_error
578 filesystem_error(const string
& __what_arg
, error_code __ec
)
579 : system_error(__ec
, __what_arg
) { }
581 filesystem_error(const string
& __what_arg
, const path
& __p1
,
583 : system_error(__ec
, __what_arg
), _M_path1(__p1
) { }
585 filesystem_error(const string
& __what_arg
, const path
& __p1
,
586 const path
& __p2
, error_code __ec
)
587 : system_error(__ec
, __what_arg
), _M_path1(__p1
), _M_path2(__p2
)
592 const path
& path1() const noexcept
{ return _M_path1
; }
593 const path
& path2() const noexcept
{ return _M_path2
; }
594 const char* what() const noexcept
{ return _M_what
.c_str(); }
597 std::string
_M_gen_what();
601 std::string _M_what
= _M_gen_what();
604 struct path::_Cmpt
: path
606 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
607 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
609 _Cmpt() : _M_pos(-1) { }
614 // specialize _Cvt for degenerate 'noconv' case
616 struct path::_Cvt
<path::value_type
>
618 template<typename _Iter
>
620 _S_convert(_Iter __first
, _Iter __last
)
621 { return string_type
{__first
, __last
}; }
624 template<typename _CharT
>
627 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
629 _S_wconvert(const char* __f
, const char* __l
, true_type
)
631 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
632 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
634 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
636 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
637 "Cannot convert character sequence",
638 std::make_error_code(errc::illegal_byte_sequence
)));
642 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
644 std::codecvt_utf8
<_CharT
> __cvt
;
646 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
648 const char* __f2
= __str
.data();
649 const char* __l2
= __f2
+ __str
.size();
650 std::codecvt_utf8
<wchar_t> __wcvt
;
652 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
655 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
656 "Cannot convert character sequence",
657 std::make_error_code(errc::illegal_byte_sequence
)));
661 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
663 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
667 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
669 std::codecvt_utf8
<_CharT
> __cvt
;
671 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
673 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
674 "Cannot convert character sequence",
675 std::make_error_code(errc::illegal_byte_sequence
)));
680 _S_convert(_CharT
* __f
, _CharT
* __l
)
682 return _S_convert(const_cast<const _CharT
*>(__f
),
683 const_cast<const _CharT
*>(__l
));
686 template<typename _Iter
>
688 _S_convert(_Iter __first
, _Iter __last
)
690 const std::basic_string
<_CharT
> __str(__first
, __last
);
691 return _S_convert(__str
.data(), __str
.data() + __str
.size());
694 template<typename _Iter
, typename _Cont
>
696 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
697 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
698 { return _S_convert(__first
.base(), __last
.base()); }
701 /// An iterator for the components of a path
705 using difference_type
= std::ptrdiff_t;
706 using value_type
= path
;
707 using reference
= const path
&;
708 using pointer
= const path
*;
709 using iterator_category
= std::bidirectional_iterator_tag
;
711 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
713 iterator(const iterator
&) = default;
714 iterator
& operator=(const iterator
&) = default;
716 reference
operator*() const;
717 pointer
operator->() const { return std::__addressof(**this); }
719 iterator
& operator++();
720 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
722 iterator
& operator--();
723 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
725 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
726 { return __lhs
._M_equals(__rhs
); }
728 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
729 { return !__lhs
._M_equals(__rhs
); }
734 iterator(const path
* __path
, path::_List::const_iterator __iter
)
735 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
738 iterator(const path
* __path
, bool __at_end
)
739 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
742 bool _M_equals(iterator
) const;
745 path::_List::const_iterator _M_cur
;
746 bool _M_at_end
; // only used when type != _Multi
751 path::operator=(path
&& __p
) noexcept
753 _M_pathname
= std::move(__p
._M_pathname
);
754 _M_cmpts
= std::move(__p
._M_cmpts
);
755 _M_type
= __p
._M_type
;
761 path::operator=(string_type
&& __source
)
762 { return *this = path(std::move(__source
)); }
765 path::assign(string_type
&& __source
)
766 { return *this = path(std::move(__source
)); }
769 path::operator+=(const path
& __p
)
771 return operator+=(__p
.native());
775 path::operator+=(const string_type
& __x
)
783 path::operator+=(const value_type
* __x
)
791 path::operator+=(value_type __x
)
799 path::operator+=(basic_string_view
<value_type
> __x
)
801 _M_pathname
.append(__x
.data(), __x
.size());
806 template<typename _CharT
>
807 inline path::_Path
<_CharT
*, _CharT
*>&
808 path::operator+=(_CharT __x
)
810 auto* __addr
= std::__addressof(__x
);
811 return concat(__addr
, __addr
+ 1);
815 path::make_preferred()
817 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
818 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
819 preferred_separator
);
824 inline void path::swap(path
& __rhs
) noexcept
826 _M_pathname
.swap(__rhs
._M_pathname
);
827 _M_cmpts
.swap(__rhs
._M_cmpts
);
828 std::swap(_M_type
, __rhs
._M_type
);
831 template<typename _CharT
, typename _Traits
, typename _Allocator
>
832 std::basic_string
<_CharT
, _Traits
, _Allocator
>
833 path::_S_str_convert(const string_type
& __str
, const _Allocator
& __a
)
835 if (__str
.size() == 0)
836 return std::basic_string
<_CharT
, _Traits
, _Allocator
>(__a
);
838 const value_type
* __first
= __str
.data();
839 const value_type
* __last
= __first
+ __str
.size();
841 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
842 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
843 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
844 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
846 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
847 codecvt_utf8
<value_type
> __cvt
;
848 _String __u8str
{_CharAlloc
{__a
}};
849 if (__str_codecvt_out(__first
, __last
, __u8str
, __cvt
))
851 if constexpr (is_same_v
<_CharT
, char>)
856 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
857 codecvt_utf8
<_CharT
> __cvt
;
858 const char* __f
= __u8str
.data();
859 const char* __l
= __f
+ __u8str
.size();
860 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
865 codecvt_utf8
<_CharT
> __cvt
;
866 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
867 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
870 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
871 "Cannot convert character sequence",
872 std::make_error_code(errc::illegal_byte_sequence
)));
875 template<typename _CharT
, typename _Traits
, typename _Allocator
>
876 inline basic_string
<_CharT
, _Traits
, _Allocator
>
877 path::string(const _Allocator
& __a
) const
879 if constexpr (is_same_v
<_CharT
, value_type
>)
881 #if _GLIBCXX_USE_CXX11_ABI
882 return { _M_pathname
, __a
};
884 if constexpr (is_same_v
<_Allocator
, string_type::allocator_type
>)
887 return { _M_pathname
, string_type::size_type(0), __a
};
891 return _S_str_convert
<_CharT
, _Traits
>(_M_pathname
, __a
);
895 path::string() const { return string
<char>(); }
897 #if _GLIBCXX_USE_WCHAR_T
899 path::wstring() const { return string
<wchar_t>(); }
903 path::u8string() const
905 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
907 // convert from native encoding to UTF-8
908 codecvt_utf8
<value_type
> __cvt
;
909 const value_type
* __first
= _M_pathname
.data();
910 const value_type
* __last
= __first
+ _M_pathname
.size();
911 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
913 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
914 "Cannot convert character sequence",
915 std::make_error_code(errc::illegal_byte_sequence
)));
921 inline std::u16string
922 path::u16string() const { return string
<char16_t
>(); }
924 inline std::u32string
925 path::u32string() const { return string
<char32_t
>(); }
927 template<typename _CharT
, typename _Traits
, typename _Allocator
>
928 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
929 path::generic_string(const _Allocator
& __a
) const
931 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
932 const value_type __slash
= L
'/';
934 const value_type __slash
= '/';
936 string_type
__str(__a
);
938 if (_M_type
== _Type::_Root_dir
)
939 __str
.assign(1, __slash
);
942 __str
.reserve(_M_pathname
.size());
943 bool __add_slash
= false;
944 for (auto& __elem
: *this)
948 __str
+= __elem
._M_pathname
;
949 __add_slash
= __elem
._M_type
== _Type::_Filename
;
953 if constexpr (is_same_v
<_CharT
, value_type
>)
956 return _S_str_convert
<_CharT
, _Traits
>(__str
, __a
);
960 path::generic_string() const
961 { return generic_string
<char>(); }
963 #if _GLIBCXX_USE_WCHAR_T
965 path::generic_wstring() const
966 { return generic_string
<wchar_t>(); }
970 path::generic_u8string() const
971 { return generic_string(); }
973 inline std::u16string
974 path::generic_u16string() const
975 { return generic_string
<char16_t
>(); }
977 inline std::u32string
978 path::generic_u32string() const
979 { return generic_string
<char32_t
>(); }
982 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
985 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
988 path::compare(basic_string_view
<value_type
> __s
) const
989 { return compare(path(__s
)); }
992 path::filename() const
996 else if (_M_type
== _Type::_Filename
)
998 else if (_M_type
== _Type::_Multi
)
1000 if (_M_pathname
.back() == preferred_separator
)
1002 auto& __last
= *--end();
1003 if (__last
._M_type
== _Type::_Filename
)
1012 auto ext
= _M_find_extension();
1013 if (ext
.first
&& ext
.second
!= 0)
1014 return path
{ext
.first
->substr(0, ext
.second
)};
1019 path::extension() const
1021 auto ext
= _M_find_extension();
1022 if (ext
.first
&& ext
.second
!= string_type::npos
)
1023 return path
{ext
.first
->substr(ext
.second
)};
1028 path::has_stem() const
1030 auto ext
= _M_find_extension();
1031 return ext
.first
&& ext
.second
!= 0;
1035 path::has_extension() const
1037 auto ext
= _M_find_extension();
1038 return ext
.first
&& ext
.second
!= string_type::npos
;
1042 path::is_absolute() const
1044 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1045 return has_root_name() && has_root_directory();
1047 return has_root_directory();
1051 inline path::iterator
1054 if (_M_type
== _Type::_Multi
)
1055 return iterator(this, _M_cmpts
.begin());
1056 return iterator(this, empty());
1059 inline path::iterator
1062 if (_M_type
== _Type::_Multi
)
1063 return iterator(this, _M_cmpts
.end());
1064 return iterator(this, true);
1067 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1068 inline path
& path::operator/=(const path
& __p
)
1070 // Much simpler than the specification in the standard,
1071 // as any path with root-name or root-dir is absolute.
1072 if (__p
.is_absolute())
1076 if (has_filename() || (_M_type
== _Type::_Root_name
))
1077 _M_pathname
+= preferred_separator
;
1078 _M_pathname
+= __p
.native();
1086 path::_M_append(path __p
)
1088 if (__p
.is_absolute())
1089 operator=(std::move(__p
));
1090 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1091 else if (__p
.has_root_name() && __p
.root_name() != root_name())
1092 operator=(std::move(__p
));
1095 operator/=(const_cast<const path
&>(__p
));
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