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>
46 #include <ext/concurrence.h>
47 #include <bits/shared_ptr.h>
49 #if defined(_WIN32) && !defined(__CYGWIN__)
50 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
54 namespace std
_GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 _GLIBCXX_BEGIN_NAMESPACE_CXX11
67 /// A filesystem path.
70 template<typename _CharT
, typename _Ch
= remove_const_t
<_CharT
>>
71 using __is_encoded_char
72 = __or_
<is_same
<_Ch
, char>, is_same
<_Ch
, wchar_t>,
73 is_same
<_Ch
, char16_t
>, is_same
<_Ch
, char32_t
>>;
75 template<typename _Iter
,
76 typename _Iter_traits
= std::iterator_traits
<_Iter
>>
77 using __is_path_iter_src
78 = __and_
<__is_encoded_char
<typename
_Iter_traits::value_type
>,
79 std::is_base_of
<std::input_iterator_tag
,
80 typename
_Iter_traits::iterator_category
>>;
82 template<typename _Iter
>
83 static __is_path_iter_src
<_Iter
>
84 __is_path_src(_Iter
, int);
86 template<typename _CharT
, typename _Traits
, typename _Alloc
>
87 static __is_encoded_char
<_CharT
>
88 __is_path_src(const basic_string
<_CharT
, _Traits
, _Alloc
>&, int);
90 template<typename _CharT
, typename _Traits
>
91 static __is_encoded_char
<_CharT
>
92 __is_path_src(const basic_string_view
<_CharT
, _Traits
>&, int);
94 template<typename _Unknown
>
95 static std::false_type
96 __is_path_src(const _Unknown
&, ...);
98 template<typename _Tp1
, typename _Tp2
>
99 struct __constructible_from
;
101 template<typename _Iter
>
102 struct __constructible_from
<_Iter
, _Iter
>
103 : __is_path_iter_src
<_Iter
>
106 template<typename _Source
>
107 struct __constructible_from
<_Source
, void>
108 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
111 template<typename _Tp1
, typename _Tp2
= void>
112 using _Path
= typename
113 std::enable_if
<__and_
<__not_
<is_same
<_Tp1
, path
>>,
114 __constructible_from
<_Tp1
, _Tp2
>>::value
,
117 template<typename _Source
>
119 _S_range_begin(_Source __begin
) { return __begin
; }
121 struct __null_terminated
{ };
123 template<typename _Source
>
124 static __null_terminated
125 _S_range_end(_Source
) { return {}; }
127 template<typename _CharT
, typename _Traits
, typename _Alloc
>
129 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
130 { return __str
.data(); }
132 template<typename _CharT
, typename _Traits
, typename _Alloc
>
134 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
135 { return __str
.data() + __str
.size(); }
137 template<typename _CharT
, typename _Traits
>
139 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
140 { return __str
.data(); }
142 template<typename _CharT
, typename _Traits
>
144 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
145 { return __str
.data() + __str
.size(); }
147 template<typename _Tp
,
148 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
149 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
150 using __value_type_is_char
151 = std::enable_if_t
<std::is_same_v
<std::remove_const_t
<_Val
>, char>>;
154 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
155 typedef wchar_t value_type
;
156 static constexpr value_type preferred_separator
= L
'\\';
158 typedef char value_type
;
159 static constexpr value_type preferred_separator
= '/';
161 typedef std::basic_string
<value_type
> string_type
;
163 enum format
{ native_format
, generic_format
, auto_format
};
165 // constructors and destructor
169 path(const path
& __p
) = default;
171 path(path
&& __p
) noexcept
172 : _M_pathname(std::move(__p
._M_pathname
)), _M_type(__p
._M_type
)
178 path(string_type
&& __source
, format
= auto_format
)
179 : _M_pathname(std::move(__source
))
180 { _M_split_cmpts(); }
182 template<typename _Source
,
183 typename _Require
= _Path
<_Source
>>
184 path(_Source
const& __source
, format
= auto_format
)
185 : _M_pathname(_S_convert(_S_range_begin(__source
),
186 _S_range_end(__source
)))
187 { _M_split_cmpts(); }
189 template<typename _InputIterator
,
190 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
191 path(_InputIterator __first
, _InputIterator __last
, format
= auto_format
)
192 : _M_pathname(_S_convert(__first
, __last
))
193 { _M_split_cmpts(); }
195 template<typename _Source
,
196 typename _Require
= _Path
<_Source
>,
197 typename _Require2
= __value_type_is_char
<_Source
>>
198 path(_Source
const& __source
, const locale
& __loc
, format
= auto_format
)
199 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
200 _S_range_end(__source
), __loc
))
201 { _M_split_cmpts(); }
203 template<typename _InputIterator
,
204 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
205 typename _Require2
= __value_type_is_char
<_InputIterator
>>
206 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
,
207 format
= auto_format
)
208 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
209 { _M_split_cmpts(); }
215 path
& operator=(const path
& __p
) = default;
216 path
& operator=(path
&& __p
) noexcept
;
217 path
& operator=(string_type
&& __source
);
218 path
& assign(string_type
&& __source
);
220 template<typename _Source
>
222 operator=(_Source
const& __source
)
223 { return *this = path(__source
); }
225 template<typename _Source
>
227 assign(_Source
const& __source
)
228 { return *this = path(__source
); }
230 template<typename _InputIterator
>
231 _Path
<_InputIterator
, _InputIterator
>&
232 assign(_InputIterator __first
, _InputIterator __last
)
233 { return *this = path(__first
, __last
); }
237 path
& operator/=(const path
& __p
);
239 template <class _Source
>
241 operator/=(_Source
const& __source
)
242 { return _M_append(path(__source
)); }
244 template<typename _Source
>
246 append(_Source
const& __source
)
247 { return _M_append(path(__source
)); }
249 template<typename _InputIterator
>
250 _Path
<_InputIterator
, _InputIterator
>&
251 append(_InputIterator __first
, _InputIterator __last
)
252 { return _M_append(path(__first
, __last
)); }
256 path
& operator+=(const path
& __x
);
257 path
& operator+=(const string_type
& __x
);
258 path
& operator+=(const value_type
* __x
);
259 path
& operator+=(value_type __x
);
260 path
& operator+=(basic_string_view
<value_type
> __x
);
262 template<typename _Source
>
264 operator+=(_Source
const& __x
) { return concat(__x
); }
266 template<typename _CharT
>
267 _Path
<_CharT
*, _CharT
*>&
268 operator+=(_CharT __x
);
270 template<typename _Source
>
272 concat(_Source
const& __x
)
273 { return *this += _S_convert(_S_range_begin(__x
), _S_range_end(__x
)); }
275 template<typename _InputIterator
>
276 _Path
<_InputIterator
, _InputIterator
>&
277 concat(_InputIterator __first
, _InputIterator __last
)
278 { return *this += _S_convert(__first
, __last
); }
282 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
284 path
& make_preferred();
285 path
& remove_filename();
286 path
& replace_filename(const path
& __replacement
);
287 path
& replace_extension(const path
& __replacement
= path());
289 void swap(path
& __rhs
) noexcept
;
291 // native format observers
293 const string_type
& native() const noexcept
{ return _M_pathname
; }
294 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
295 operator string_type() const { return _M_pathname
; }
297 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
298 typename _Allocator
= std::allocator
<_CharT
>>
299 std::basic_string
<_CharT
, _Traits
, _Allocator
>
300 string(const _Allocator
& __a
= _Allocator()) const;
302 std::string
string() const;
303 #if _GLIBCXX_USE_WCHAR_T
304 std::wstring
wstring() const;
306 std::string
u8string() const;
307 std::u16string
u16string() const;
308 std::u32string
u32string() const;
310 // generic format observers
311 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
312 typename _Allocator
= std::allocator
<_CharT
>>
313 std::basic_string
<_CharT
, _Traits
, _Allocator
>
314 generic_string(const _Allocator
& __a
= _Allocator()) const;
316 std::string
generic_string() const;
317 #if _GLIBCXX_USE_WCHAR_T
318 std::wstring
generic_wstring() const;
320 std::string
generic_u8string() const;
321 std::u16string
generic_u16string() const;
322 std::u32string
generic_u32string() const;
326 int compare(const path
& __p
) const noexcept
;
327 int compare(const string_type
& __s
) const;
328 int compare(const value_type
* __s
) const;
329 int compare(const basic_string_view
<value_type
> __s
) const;
333 path
root_name() const;
334 path
root_directory() const;
335 path
root_path() const;
336 path
relative_path() const;
337 path
parent_path() const;
338 path
filename() const;
340 path
extension() const;
344 [[nodiscard
]] bool empty() const noexcept
{ return _M_pathname
.empty(); }
345 bool has_root_name() const;
346 bool has_root_directory() const;
347 bool has_root_path() const;
348 bool has_relative_path() const;
349 bool has_parent_path() const;
350 bool has_filename() const;
351 bool has_stem() const;
352 bool has_extension() const;
353 bool is_absolute() const;
354 bool is_relative() const { return !is_absolute(); }
357 path
lexically_normal() const;
358 path
lexically_relative(const path
& base
) const;
359 path
lexically_proximate(const path
& base
) const;
363 typedef iterator const_iterator
;
365 iterator
begin() const;
366 iterator
end() const;
368 /// Write a path to a stream
369 template<typename _CharT
, typename _Traits
>
370 friend std::basic_ostream
<_CharT
, _Traits
>&
371 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
373 __os
<< std::quoted(__p
.string
<_CharT
, _Traits
>());
377 /// Read a path from a stream
378 template<typename _CharT
, typename _Traits
>
379 friend std::basic_istream
<_CharT
, _Traits
>&
380 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
382 std::basic_string
<_CharT
, _Traits
> __tmp
;
383 if (__is
>> std::quoted(__tmp
))
384 __p
= std::move(__tmp
);
388 // Create a basic_string by reading until a null character.
389 template<typename _InputIterator
,
390 typename _Traits
= std::iterator_traits
<_InputIterator
>,
392 = typename
std::remove_cv_t
<typename
_Traits::value_type
>>
393 static std::basic_string
<_CharT
>
394 _S_string_from_iter(_InputIterator __source
)
396 std::basic_string
<_CharT
> __str
;
397 for (_CharT __ch
= *__source
; __ch
!= _CharT(); __ch
= *++__source
)
398 __str
.push_back(__ch
);
403 enum class _Type
: unsigned char {
404 _Multi
, _Root_name
, _Root_dir
, _Filename
407 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
409 __glibcxx_assert(_M_type
!= _Type::_Multi
);
412 enum class _Split
{ _Stem
, _Extension
};
414 path
& _M_append(path __p
);
416 pair
<const string_type
*, size_t> _M_find_extension() const;
418 template<typename _CharT
>
422 _S_convert(value_type
* __src
, __null_terminated
)
423 { return string_type(__src
); }
426 _S_convert(const value_type
* __src
, __null_terminated
)
427 { return string_type(__src
); }
429 template<typename _Iter
>
431 _S_convert(_Iter __first
, _Iter __last
)
433 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
434 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
435 _S_convert(__first
, __last
);
438 template<typename _InputIterator
>
440 _S_convert(_InputIterator __src
, __null_terminated
)
442 auto __s
= _S_string_from_iter(__src
);
443 return _S_convert(__s
.c_str(), __s
.c_str() + __s
.size());
447 _S_convert_loc(const char* __first
, const char* __last
,
448 const std::locale
& __loc
);
450 template<typename _Iter
>
452 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
454 const std::string
__str(__first
, __last
);
455 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
458 template<typename _InputIterator
>
460 _S_convert_loc(_InputIterator __src
, __null_terminated
,
461 const std::locale
& __loc
)
463 std::string __s
= _S_string_from_iter(__src
);
464 return _S_convert_loc(__s
.data(), __s
.data() + __s
.size(), __loc
);
467 template<typename _CharT
, typename _Traits
, typename _Allocator
>
468 static basic_string
<_CharT
, _Traits
, _Allocator
>
469 _S_str_convert(const string_type
&, const _Allocator
& __a
);
471 bool _S_is_dir_sep(value_type __ch
)
473 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
474 return __ch
== L
'/' || __ch
== preferred_separator
;
480 void _M_split_cmpts();
482 void _M_add_root_name(size_t __n
);
483 void _M_add_root_dir(size_t __pos
);
484 void _M_add_filename(size_t __pos
, size_t __n
);
486 string_type _M_pathname
;
489 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
490 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
491 _Type _M_type
= _Type::_Filename
;
494 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
496 size_t hash_value(const path
& __p
) noexcept
;
499 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
500 { return __lhs
.compare(__rhs
) < 0; }
503 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
504 { return !(__rhs
< __lhs
); }
507 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
508 { return __rhs
< __lhs
; }
511 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
512 { return !(__lhs
< __rhs
); }
515 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
516 { return __lhs
.compare(__rhs
) == 0; }
519 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
520 { return !(__lhs
== __rhs
); }
522 /// Append one path to another
523 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
525 path
__result(__lhs
);
530 template<typename _InputIterator
>
532 u8path(_InputIterator __first
, _InputIterator __last
)
533 -> decltype(filesystem::path(__first
, __last
, std::locale::classic()))
535 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
536 codecvt_utf8
<path::value_type
> __cvt
;
537 path::string_type __tmp
;
538 if constexpr (is_pointer_v
<_InputIterator
>)
540 if (__str_codecvt_in(__first
, __last
, __tmp
, __cvt
))
541 return path
{ __tmp
};
545 const std::string __u8str
{__first
, __last
};
546 const char* const __ptr
= __u8str
.data();
547 if (__str_codecvt_in(__ptr
, __ptr
+ __u8str
.size(), __tmp
, __cvt
))
548 return path
{ __tmp
};
552 return path
{ __first
, __last
};
556 template<typename _Source
>
558 u8path(const _Source
& __source
)
559 -> decltype(filesystem::path(__source
, std::locale::classic()))
561 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
562 if constexpr (is_convertible_v
<const _Source
&, std::string_view
>)
564 const std::string_view __s
= __source
;
565 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
569 std::string __s
= path::_S_string_from_iter(__source
);
570 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
573 return path
{ __source
};
577 class filesystem_error
: public std::system_error
580 filesystem_error(const string
& __what_arg
, error_code __ec
);
582 filesystem_error(const string
& __what_arg
, const path
& __p1
,
585 filesystem_error(const string
& __what_arg
, const path
& __p1
,
586 const path
& __p2
, error_code __ec
);
588 filesystem_error(const filesystem_error
&) = default;
589 filesystem_error
& operator=(const filesystem_error
&) = default;
591 // No move constructor or assignment operator.
592 // Copy rvalues instead, so that _M_impl is not left empty.
596 const path
& path1() const noexcept
;
597 const path
& path2() const noexcept
;
598 const char* what() const noexcept
;
602 std::__shared_ptr
<const _Impl
> _M_impl
;
605 struct path::_Cmpt
: path
607 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
608 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
610 _Cmpt() : _M_pos(-1) { }
615 // specialize _Cvt for degenerate 'noconv' case
617 struct path::_Cvt
<path::value_type
>
619 template<typename _Iter
>
621 _S_convert(_Iter __first
, _Iter __last
)
622 { return string_type
{__first
, __last
}; }
625 template<typename _CharT
>
628 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
630 _S_wconvert(const char* __f
, const char* __l
, true_type
)
632 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
633 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
635 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
637 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
638 "Cannot convert character sequence",
639 std::make_error_code(errc::illegal_byte_sequence
)));
643 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
645 std::codecvt_utf8
<_CharT
> __cvt
;
647 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
649 const char* __f2
= __str
.data();
650 const char* __l2
= __f2
+ __str
.size();
651 std::codecvt_utf8
<wchar_t> __wcvt
;
653 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
656 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
657 "Cannot convert character sequence",
658 std::make_error_code(errc::illegal_byte_sequence
)));
662 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
664 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
668 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
670 std::codecvt_utf8
<_CharT
> __cvt
;
672 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
674 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
675 "Cannot convert character sequence",
676 std::make_error_code(errc::illegal_byte_sequence
)));
681 _S_convert(_CharT
* __f
, _CharT
* __l
)
683 return _S_convert(const_cast<const _CharT
*>(__f
),
684 const_cast<const _CharT
*>(__l
));
687 template<typename _Iter
>
689 _S_convert(_Iter __first
, _Iter __last
)
691 const std::basic_string
<_CharT
> __str(__first
, __last
);
692 return _S_convert(__str
.data(), __str
.data() + __str
.size());
695 template<typename _Iter
, typename _Cont
>
697 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
698 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
699 { return _S_convert(__first
.base(), __last
.base()); }
702 /// An iterator for the components of a path
706 using difference_type
= std::ptrdiff_t;
707 using value_type
= path
;
708 using reference
= const path
&;
709 using pointer
= const path
*;
710 using iterator_category
= std::bidirectional_iterator_tag
;
712 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
714 iterator(const iterator
&) = default;
715 iterator
& operator=(const iterator
&) = default;
717 reference
operator*() const;
718 pointer
operator->() const { return std::__addressof(**this); }
720 iterator
& operator++();
721 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
723 iterator
& operator--();
724 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
726 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
727 { return __lhs
._M_equals(__rhs
); }
729 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
730 { return !__lhs
._M_equals(__rhs
); }
735 iterator(const path
* __path
, path::_List::const_iterator __iter
)
736 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
739 iterator(const path
* __path
, bool __at_end
)
740 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
743 bool _M_equals(iterator
) const;
746 path::_List::const_iterator _M_cur
;
747 bool _M_at_end
; // only used when type != _Multi
752 path::operator=(path
&& __p
) noexcept
754 _M_pathname
= std::move(__p
._M_pathname
);
755 _M_cmpts
= std::move(__p
._M_cmpts
);
756 _M_type
= __p
._M_type
;
762 path::operator=(string_type
&& __source
)
763 { return *this = path(std::move(__source
)); }
766 path::assign(string_type
&& __source
)
767 { return *this = path(std::move(__source
)); }
770 path::operator+=(const path
& __p
)
772 return operator+=(__p
.native());
776 path::operator+=(const string_type
& __x
)
784 path::operator+=(const value_type
* __x
)
792 path::operator+=(value_type __x
)
800 path::operator+=(basic_string_view
<value_type
> __x
)
802 _M_pathname
.append(__x
.data(), __x
.size());
807 template<typename _CharT
>
808 inline path::_Path
<_CharT
*, _CharT
*>&
809 path::operator+=(_CharT __x
)
811 auto* __addr
= std::__addressof(__x
);
812 return concat(__addr
, __addr
+ 1);
816 path::make_preferred()
818 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
819 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
820 preferred_separator
);
825 inline void path::swap(path
& __rhs
) noexcept
827 _M_pathname
.swap(__rhs
._M_pathname
);
828 _M_cmpts
.swap(__rhs
._M_cmpts
);
829 std::swap(_M_type
, __rhs
._M_type
);
832 template<typename _CharT
, typename _Traits
, typename _Allocator
>
833 std::basic_string
<_CharT
, _Traits
, _Allocator
>
834 path::_S_str_convert(const string_type
& __str
, const _Allocator
& __a
)
836 if (__str
.size() == 0)
837 return std::basic_string
<_CharT
, _Traits
, _Allocator
>(__a
);
839 const value_type
* __first
= __str
.data();
840 const value_type
* __last
= __first
+ __str
.size();
842 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
843 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
844 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
845 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
847 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
848 codecvt_utf8
<value_type
> __cvt
;
849 _String __u8str
{_CharAlloc
{__a
}};
850 if (__str_codecvt_out(__first
, __last
, __u8str
, __cvt
))
852 if constexpr (is_same_v
<_CharT
, char>)
857 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
858 codecvt_utf8
<_CharT
> __cvt
;
859 const char* __f
= __u8str
.data();
860 const char* __l
= __f
+ __u8str
.size();
861 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
866 codecvt_utf8
<_CharT
> __cvt
;
867 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
868 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
871 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
872 "Cannot convert character sequence",
873 std::make_error_code(errc::illegal_byte_sequence
)));
876 template<typename _CharT
, typename _Traits
, typename _Allocator
>
877 inline basic_string
<_CharT
, _Traits
, _Allocator
>
878 path::string(const _Allocator
& __a
) const
880 if constexpr (is_same_v
<_CharT
, value_type
>)
882 #if _GLIBCXX_USE_CXX11_ABI
883 return { _M_pathname
, __a
};
885 if constexpr (is_same_v
<_Allocator
, string_type::allocator_type
>)
888 return { _M_pathname
, string_type::size_type(0), __a
};
892 return _S_str_convert
<_CharT
, _Traits
>(_M_pathname
, __a
);
896 path::string() const { return string
<char>(); }
898 #if _GLIBCXX_USE_WCHAR_T
900 path::wstring() const { return string
<wchar_t>(); }
904 path::u8string() const
906 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
908 // convert from native encoding to UTF-8
909 codecvt_utf8
<value_type
> __cvt
;
910 const value_type
* __first
= _M_pathname
.data();
911 const value_type
* __last
= __first
+ _M_pathname
.size();
912 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
914 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
915 "Cannot convert character sequence",
916 std::make_error_code(errc::illegal_byte_sequence
)));
922 inline std::u16string
923 path::u16string() const { return string
<char16_t
>(); }
925 inline std::u32string
926 path::u32string() const { return string
<char32_t
>(); }
928 template<typename _CharT
, typename _Traits
, typename _Allocator
>
929 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
930 path::generic_string(const _Allocator
& __a
) const
932 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
933 const value_type __slash
= L
'/';
935 const value_type __slash
= '/';
937 string_type
__str(__a
);
939 if (_M_type
== _Type::_Root_dir
)
940 __str
.assign(1, __slash
);
943 __str
.reserve(_M_pathname
.size());
944 bool __add_slash
= false;
945 for (auto& __elem
: *this)
949 __str
+= __elem
._M_pathname
;
950 __add_slash
= __elem
._M_type
== _Type::_Filename
;
954 if constexpr (is_same_v
<_CharT
, value_type
>)
957 return _S_str_convert
<_CharT
, _Traits
>(__str
, __a
);
961 path::generic_string() const
962 { return generic_string
<char>(); }
964 #if _GLIBCXX_USE_WCHAR_T
966 path::generic_wstring() const
967 { return generic_string
<wchar_t>(); }
971 path::generic_u8string() const
972 { return generic_string(); }
974 inline std::u16string
975 path::generic_u16string() const
976 { return generic_string
<char16_t
>(); }
978 inline std::u32string
979 path::generic_u32string() const
980 { return generic_string
<char32_t
>(); }
983 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
986 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
989 path::compare(basic_string_view
<value_type
> __s
) const
990 { return compare(path(__s
)); }
993 path::filename() const
997 else if (_M_type
== _Type::_Filename
)
999 else if (_M_type
== _Type::_Multi
)
1001 if (_M_pathname
.back() == preferred_separator
)
1003 auto& __last
= *--end();
1004 if (__last
._M_type
== _Type::_Filename
)
1013 auto ext
= _M_find_extension();
1014 if (ext
.first
&& ext
.second
!= 0)
1015 return path
{ext
.first
->substr(0, ext
.second
)};
1020 path::extension() const
1022 auto ext
= _M_find_extension();
1023 if (ext
.first
&& ext
.second
!= string_type::npos
)
1024 return path
{ext
.first
->substr(ext
.second
)};
1029 path::has_stem() const
1031 auto ext
= _M_find_extension();
1032 return ext
.first
&& ext
.second
!= 0;
1036 path::has_extension() const
1038 auto ext
= _M_find_extension();
1039 return ext
.first
&& ext
.second
!= string_type::npos
;
1043 path::is_absolute() const
1045 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1046 return has_root_name() && has_root_directory();
1048 return has_root_directory();
1052 inline path::iterator
1055 if (_M_type
== _Type::_Multi
)
1056 return iterator(this, _M_cmpts
.begin());
1057 return iterator(this, empty());
1060 inline path::iterator
1063 if (_M_type
== _Type::_Multi
)
1064 return iterator(this, _M_cmpts
.end());
1065 return iterator(this, true);
1068 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1069 inline path
& path::operator/=(const path
& __p
)
1071 // Much simpler than the specification in the standard,
1072 // as any path with root-name or root-dir is absolute.
1073 if (__p
.is_absolute())
1077 if (has_filename() || (_M_type
== _Type::_Root_name
))
1078 _M_pathname
+= preferred_separator
;
1079 _M_pathname
+= __p
.native();
1087 path::_M_append(path __p
)
1089 if (__p
.is_absolute())
1090 operator=(std::move(__p
));
1091 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1092 else if (__p
.has_root_name() && __p
.root_name() != root_name())
1093 operator=(std::move(__p
));
1096 operator/=(const_cast<const path
&>(__p
));
1100 inline path::iterator
&
1101 path::iterator::operator++()
1103 __glibcxx_assert(_M_path
!= nullptr);
1104 if (_M_path
->_M_type
== _Type::_Multi
)
1106 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1111 __glibcxx_assert(!_M_at_end
);
1117 inline path::iterator
&
1118 path::iterator::operator--()
1120 __glibcxx_assert(_M_path
!= nullptr);
1121 if (_M_path
->_M_type
== _Type::_Multi
)
1123 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1128 __glibcxx_assert(_M_at_end
);
1134 inline path::iterator::reference
1135 path::iterator::operator*() const
1137 __glibcxx_assert(_M_path
!= nullptr);
1138 if (_M_path
->_M_type
== _Type::_Multi
)
1140 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1147 path::iterator::_M_equals(iterator __rhs
) const
1149 if (_M_path
!= __rhs
._M_path
)
1151 if (_M_path
== nullptr)
1153 if (_M_path
->_M_type
== path::_Type::_Multi
)
1154 return _M_cur
== __rhs
._M_cur
;
1155 return _M_at_end
== __rhs
._M_at_end
;
1158 // @} group filesystem
1159 _GLIBCXX_END_NAMESPACE_CXX11
1160 } // namespace filesystem
1162 extern template class __shared_ptr
<const filesystem::filesystem_error::_Impl
>;
1164 _GLIBCXX_END_NAMESPACE_VERSION
1169 #endif // _GLIBCXX_FS_PATH_H