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 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/locale_conv.h>
45 #include <ext/concurrence.h>
46 #include <bits/shared_ptr.h>
47 #include <bits/unique_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
62 /** @addtogroup filesystem
66 /// A filesystem path.
69 template<typename _CharT
, typename _Ch
= remove_const_t
<_CharT
>>
70 using __is_encoded_char
71 = __or_
<is_same
<_Ch
, char>,
72 #ifdef _GLIBCXX_USE_CHAR8_T
73 is_same
<_Ch
, char8_t
>,
75 is_same
<_Ch
, wchar_t>,
76 is_same
<_Ch
, char16_t
>,
77 is_same
<_Ch
, char32_t
>>;
79 template<typename _Iter
,
80 typename _Iter_traits
= std::iterator_traits
<_Iter
>>
81 using __is_path_iter_src
82 = __and_
<__is_encoded_char
<typename
_Iter_traits::value_type
>,
83 std::is_base_of
<std::input_iterator_tag
,
84 typename
_Iter_traits::iterator_category
>>;
86 template<typename _Iter
>
87 static __is_path_iter_src
<_Iter
>
88 __is_path_src(_Iter
, int);
90 template<typename _CharT
, typename _Traits
, typename _Alloc
>
91 static __is_encoded_char
<_CharT
>
92 __is_path_src(const basic_string
<_CharT
, _Traits
, _Alloc
>&, int);
94 template<typename _CharT
, typename _Traits
>
95 static __is_encoded_char
<_CharT
>
96 __is_path_src(const basic_string_view
<_CharT
, _Traits
>&, int);
98 template<typename _Unknown
>
99 static std::false_type
100 __is_path_src(const _Unknown
&, ...);
102 template<typename _Tp1
, typename _Tp2
>
103 struct __constructible_from
;
105 template<typename _Iter
>
106 struct __constructible_from
<_Iter
, _Iter
>
107 : __is_path_iter_src
<_Iter
>
110 template<typename _Source
>
111 struct __constructible_from
<_Source
, void>
112 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
115 template<typename _Tp1
, typename _Tp2
= void>
116 using _Path
= typename
117 std::enable_if
<__and_
<__not_
<is_same
<remove_cv_t
<_Tp1
>, path
>>,
118 __not_
<is_void
<_Tp1
>>,
119 __constructible_from
<_Tp1
, _Tp2
>>::value
,
122 template<typename _Source
>
124 _S_range_begin(_Source __begin
) { return __begin
; }
126 struct __null_terminated
{ };
128 template<typename _Source
>
129 static __null_terminated
130 _S_range_end(_Source
) { return {}; }
132 template<typename _CharT
, typename _Traits
, typename _Alloc
>
134 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
135 { return __str
.data(); }
137 template<typename _CharT
, typename _Traits
, typename _Alloc
>
139 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
140 { return __str
.data() + __str
.size(); }
142 template<typename _CharT
, typename _Traits
>
144 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
145 { return __str
.data(); }
147 template<typename _CharT
, typename _Traits
>
149 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
150 { return __str
.data() + __str
.size(); }
152 template<typename _Tp
,
153 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
154 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
155 using __value_type_is_char
156 = std::enable_if_t
<std::is_same_v
<std::remove_const_t
<_Val
>, char>>;
159 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
160 using value_type
= wchar_t;
161 static constexpr value_type preferred_separator
= L
'\\';
163 # ifdef _GLIBCXX_DOXYGEN
164 /// Windows uses wchar_t for path::value_type, POSIX uses char.
165 using value_type
= __os_dependent__
;
167 using value_type
= char;
169 static constexpr value_type preferred_separator
= '/';
171 using string_type
= std::basic_string
<value_type
>;
173 /// path::format is ignored in this implementation
174 enum format
: unsigned char { native_format
, generic_format
, auto_format
};
176 // constructors and destructor
180 path(const path
& __p
) = default;
183 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_FULLY_DYNAMIC_STRING == 0
186 : _M_pathname(std::move(__p
._M_pathname
)),
187 _M_cmpts(std::move(__p
._M_cmpts
))
190 path(string_type
&& __source
, format
= auto_format
)
191 : _M_pathname(std::move(__source
))
192 { _M_split_cmpts(); }
194 template<typename _Source
,
195 typename _Require
= _Path
<_Source
>>
196 path(_Source
const& __source
, format
= auto_format
)
197 : _M_pathname(_S_convert(_S_range_begin(__source
),
198 _S_range_end(__source
)))
199 { _M_split_cmpts(); }
201 template<typename _InputIterator
,
202 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
203 path(_InputIterator __first
, _InputIterator __last
, format
= auto_format
)
204 : _M_pathname(_S_convert(__first
, __last
))
205 { _M_split_cmpts(); }
207 template<typename _Source
,
208 typename _Require
= _Path
<_Source
>,
209 typename _Require2
= __value_type_is_char
<_Source
>>
210 path(_Source
const& __source
, const locale
& __loc
, format
= auto_format
)
211 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
212 _S_range_end(__source
), __loc
))
213 { _M_split_cmpts(); }
215 template<typename _InputIterator
,
216 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
217 typename _Require2
= __value_type_is_char
<_InputIterator
>>
218 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
,
219 format
= auto_format
)
220 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
221 { _M_split_cmpts(); }
227 path
& operator=(const path
&);
228 path
& operator=(path
&&) noexcept
;
229 path
& operator=(string_type
&& __source
);
230 path
& assign(string_type
&& __source
);
232 template<typename _Source
>
234 operator=(_Source
const& __source
)
235 { return *this = path(__source
); }
237 template<typename _Source
>
239 assign(_Source
const& __source
)
240 { return *this = path(__source
); }
242 template<typename _InputIterator
>
243 _Path
<_InputIterator
, _InputIterator
>&
244 assign(_InputIterator __first
, _InputIterator __last
)
245 { return *this = path(__first
, __last
); }
249 path
& operator/=(const path
& __p
);
251 template <class _Source
>
253 operator/=(_Source
const& __source
)
255 _M_append(_S_convert(_S_range_begin(__source
), _S_range_end(__source
)));
259 template<typename _Source
>
261 append(_Source
const& __source
)
263 _M_append(_S_convert(_S_range_begin(__source
), _S_range_end(__source
)));
267 template<typename _InputIterator
>
268 _Path
<_InputIterator
, _InputIterator
>&
269 append(_InputIterator __first
, _InputIterator __last
)
271 _M_append(_S_convert(__first
, __last
));
277 path
& operator+=(const path
& __x
);
278 path
& operator+=(const string_type
& __x
);
279 path
& operator+=(const value_type
* __x
);
280 path
& operator+=(value_type __x
);
281 path
& operator+=(basic_string_view
<value_type
> __x
);
283 template<typename _Source
>
285 operator+=(_Source
const& __x
) { return concat(__x
); }
287 template<typename _CharT
>
288 _Path
<_CharT
*, _CharT
*>&
289 operator+=(_CharT __x
);
291 template<typename _Source
>
293 concat(_Source
const& __x
)
295 _M_concat(_S_convert(_S_range_begin(__x
), _S_range_end(__x
)));
299 template<typename _InputIterator
>
300 _Path
<_InputIterator
, _InputIterator
>&
301 concat(_InputIterator __first
, _InputIterator __last
)
303 _M_concat(_S_convert(__first
, __last
));
309 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
311 path
& make_preferred();
312 path
& remove_filename();
313 path
& replace_filename(const path
& __replacement
);
314 path
& replace_extension(const path
& __replacement
= path());
316 void swap(path
& __rhs
) noexcept
;
318 // native format observers
320 const string_type
& native() const noexcept
{ return _M_pathname
; }
321 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
322 operator string_type() const { return _M_pathname
; }
324 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
325 typename _Allocator
= std::allocator
<_CharT
>>
326 std::basic_string
<_CharT
, _Traits
, _Allocator
>
327 string(const _Allocator
& __a
= _Allocator()) const;
329 std::string
string() const;
330 #if _GLIBCXX_USE_WCHAR_T
331 std::wstring
wstring() const;
333 #ifdef _GLIBCXX_USE_CHAR8_T
334 __attribute__((__abi_tag__("__u8")))
335 std::u8string
u8string() const;
337 std::string
u8string() const;
338 #endif // _GLIBCXX_USE_CHAR8_T
339 std::u16string
u16string() const;
340 std::u32string
u32string() const;
342 // generic format observers
343 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
344 typename _Allocator
= std::allocator
<_CharT
>>
345 std::basic_string
<_CharT
, _Traits
, _Allocator
>
346 generic_string(const _Allocator
& __a
= _Allocator()) const;
348 std::string
generic_string() const;
349 #if _GLIBCXX_USE_WCHAR_T
350 std::wstring
generic_wstring() const;
352 #ifdef _GLIBCXX_USE_CHAR8_T
353 __attribute__((__abi_tag__("__u8")))
354 std::u8string
generic_u8string() const;
356 std::string
generic_u8string() const;
357 #endif // _GLIBCXX_USE_CHAR8_T
358 std::u16string
generic_u16string() const;
359 std::u32string
generic_u32string() const;
363 int compare(const path
& __p
) const noexcept
;
364 int compare(const string_type
& __s
) const noexcept
;
365 int compare(const value_type
* __s
) const noexcept
;
366 int compare(basic_string_view
<value_type
> __s
) const noexcept
;
370 path
root_name() const;
371 path
root_directory() const;
372 path
root_path() const;
373 path
relative_path() const;
374 path
parent_path() const;
375 path
filename() const;
377 path
extension() const;
381 [[nodiscard
]] bool empty() const noexcept
{ return _M_pathname
.empty(); }
382 bool has_root_name() const noexcept
;
383 bool has_root_directory() const noexcept
;
384 bool has_root_path() const noexcept
;
385 bool has_relative_path() const noexcept
;
386 bool has_parent_path() const noexcept
;
387 bool has_filename() const noexcept
;
388 bool has_stem() const noexcept
;
389 bool has_extension() const noexcept
;
390 bool is_absolute() const noexcept
;
391 bool is_relative() const noexcept
{ return !is_absolute(); }
394 path
lexically_normal() const;
395 path
lexically_relative(const path
& base
) const;
396 path
lexically_proximate(const path
& base
) const;
400 using const_iterator
= iterator
;
402 iterator
begin() const;
403 iterator
end() const;
405 /// Write a path to a stream
406 template<typename _CharT
, typename _Traits
>
407 friend std::basic_ostream
<_CharT
, _Traits
>&
408 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
410 __os
<< std::quoted(__p
.string
<_CharT
, _Traits
>());
414 /// Read a path from a stream
415 template<typename _CharT
, typename _Traits
>
416 friend std::basic_istream
<_CharT
, _Traits
>&
417 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
419 std::basic_string
<_CharT
, _Traits
> __tmp
;
420 if (__is
>> std::quoted(__tmp
))
421 __p
= std::move(__tmp
);
425 // non-member operators
428 friend bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
429 { return __lhs
.compare(__rhs
) < 0; }
432 friend bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
433 { return !(__rhs
< __lhs
); }
436 friend bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
437 { return __rhs
< __lhs
; }
440 friend bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
441 { return !(__lhs
< __rhs
); }
444 friend bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
445 { return __lhs
.compare(__rhs
) == 0; }
448 friend bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
449 { return !(__lhs
== __rhs
); }
451 /// Append one path to another
452 friend path
operator/(const path
& __lhs
, const path
& __rhs
)
454 path
__result(__lhs
);
459 /// @cond undocumented
460 // Create a basic_string by reading until a null character.
461 template<typename _InputIterator
,
462 typename _Traits
= std::iterator_traits
<_InputIterator
>,
464 = typename
std::remove_cv_t
<typename
_Traits::value_type
>>
465 static std::basic_string
<_CharT
>
466 _S_string_from_iter(_InputIterator __source
)
468 std::basic_string
<_CharT
> __str
;
469 for (_CharT __ch
= *__source
; __ch
!= _CharT(); __ch
= *++__source
)
470 __str
.push_back(__ch
);
476 enum class _Type
: unsigned char {
477 _Multi
= 0, _Root_name
, _Root_dir
, _Filename
480 path(basic_string_view
<value_type
> __str
, _Type __type
)
483 __glibcxx_assert(__type
!= _Type::_Multi
);
484 _M_cmpts
.type(__type
);
487 enum class _Split
{ _Stem
, _Extension
};
489 void _M_append(basic_string_view
<value_type
>);
490 void _M_concat(basic_string_view
<value_type
>);
492 pair
<const string_type
*, size_t> _M_find_extension() const noexcept
;
494 template<typename _CharT
>
497 static basic_string_view
<value_type
>
498 _S_convert(value_type
* __src
, __null_terminated
)
501 static basic_string_view
<value_type
>
502 _S_convert(const value_type
* __src
, __null_terminated
)
505 static basic_string_view
<value_type
>
506 _S_convert(value_type
* __first
, value_type
* __last
)
507 { return {__first
, __last
- __first
}; }
509 static basic_string_view
<value_type
>
510 _S_convert(const value_type
* __first
, const value_type
* __last
)
511 { return {__first
, __last
- __first
}; }
513 template<typename _Iter
>
515 _S_convert(_Iter __first
, _Iter __last
)
517 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
518 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
519 _S_convert(__first
, __last
);
522 template<typename _InputIterator
>
524 _S_convert(_InputIterator __src
, __null_terminated
)
526 // Read from iterator into basic_string until a null value is seen:
527 auto __s
= _S_string_from_iter(__src
);
528 // Convert (if needed) from iterator's value type to path::value_type:
529 return string_type(_S_convert(__s
.data(), __s
.data() + __s
.size()));
533 _S_convert_loc(const char* __first
, const char* __last
,
534 const std::locale
& __loc
);
536 template<typename _Iter
>
538 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
540 const std::string
__str(__first
, __last
);
541 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
544 template<typename _InputIterator
>
546 _S_convert_loc(_InputIterator __src
, __null_terminated
,
547 const std::locale
& __loc
)
549 const std::string __s
= _S_string_from_iter(__src
);
550 return _S_convert_loc(__s
.data(), __s
.data() + __s
.size(), __loc
);
553 template<typename _CharT
, typename _Traits
, typename _Allocator
>
554 static basic_string
<_CharT
, _Traits
, _Allocator
>
555 _S_str_convert(const string_type
&, const _Allocator
& __a
);
557 void _M_split_cmpts();
559 _Type
_M_type() const noexcept
{ return _M_cmpts
.type(); }
561 string_type _M_pathname
;
567 using value_type
= _Cmpt
;
568 using iterator
= value_type
*;
569 using const_iterator
= const value_type
*;
573 _List(_List
&&) = default;
574 _List
& operator=(const _List
&);
575 _List
& operator=(_List
&&) = default;
578 _Type
type() const noexcept
579 { return _Type
{reinterpret_cast<uintptr_t>(_M_impl
.get()) & 0x3}; }
581 void type(_Type
) noexcept
;
583 int size() const noexcept
; // zero unless type() == _Type::_Multi
584 bool empty() const noexcept
; // true unless type() == _Type::_Multi
586 void swap(_List
& __l
) noexcept
{ _M_impl
.swap(__l
._M_impl
); }
587 int capacity() const noexcept
;
588 void reserve(int, bool); ///< @pre type() == _Type::_Multi
590 // All the member functions below here have a precondition !empty()
591 // (and they should only be called from within the library).
595 const_iterator
begin() const;
596 const_iterator
end() const;
598 value_type
& front() noexcept
;
599 value_type
& back() noexcept
;
600 const value_type
& front() const noexcept
;
601 const value_type
& back() const noexcept
;
604 void _M_erase_from(const_iterator __pos
); // erases [__pos,end())
609 void operator()(_Impl
*) const noexcept
;
611 unique_ptr
<_Impl
, _Impl_deleter
> _M_impl
;
618 /// @relates std::filesystem::path @{
620 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
622 size_t hash_value(const path
& __p
) noexcept
;
624 /// Create a path from a UTF-8-encoded sequence of char
625 template<typename _InputIterator
>
627 u8path(_InputIterator __first
, _InputIterator __last
)
628 -> decltype(filesystem::path(__first
, __last
, std::locale::classic()))
630 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
631 codecvt_utf8
<path::value_type
> __cvt
;
632 path::string_type __tmp
;
633 if constexpr (is_pointer_v
<_InputIterator
>)
635 if (__str_codecvt_in(__first
, __last
, __tmp
, __cvt
))
636 return path
{ __tmp
};
640 const std::string __u8str
{__first
, __last
};
641 const char* const __ptr
= __u8str
.data();
642 if (__str_codecvt_in(__ptr
, __ptr
+ __u8str
.size(), __tmp
, __cvt
))
643 return path
{ __tmp
};
647 return path
{ __first
, __last
};
651 /// Create a path from a UTF-8-encoded sequence of char
652 template<typename _Source
>
654 u8path(const _Source
& __source
)
655 -> decltype(filesystem::path(__source
, std::locale::classic()))
657 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
658 if constexpr (is_convertible_v
<const _Source
&, std::string_view
>)
660 const std::string_view __s
= __source
;
661 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
665 std::string __s
= path::_S_string_from_iter(__source
);
666 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
669 return path
{ __source
};
675 /// Exception type thrown by the Filesystem library
676 class filesystem_error
: public std::system_error
679 filesystem_error(const string
& __what_arg
, error_code __ec
);
681 filesystem_error(const string
& __what_arg
, const path
& __p1
,
684 filesystem_error(const string
& __what_arg
, const path
& __p1
,
685 const path
& __p2
, error_code __ec
);
687 filesystem_error(const filesystem_error
&) = default;
688 filesystem_error
& operator=(const filesystem_error
&) = default;
690 // No move constructor or assignment operator.
691 // Copy rvalues instead, so that _M_impl is not left empty.
695 const path
& path1() const noexcept
;
696 const path
& path2() const noexcept
;
697 const char* what() const noexcept
;
701 std::__shared_ptr
<const _Impl
> _M_impl
;
704 /// @cond undocumented
706 struct path::_Cmpt
: path
708 _Cmpt(basic_string_view
<value_type
> __s
, _Type __t
, size_t __pos
)
709 : path(__s
, __t
), _M_pos(__pos
) { }
711 _Cmpt() : _M_pos(-1) { }
716 // specialize _Cvt for degenerate 'noconv' case
718 struct path::_Cvt
<path::value_type
>
720 template<typename _Iter
>
722 _S_convert(_Iter __first
, _Iter __last
)
723 { return string_type
{__first
, __last
}; }
726 template<typename _CharT
>
729 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
731 _S_wconvert(const char* __f
, const char* __l
, true_type
)
733 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
734 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
736 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
738 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
739 "Cannot convert character sequence",
740 std::make_error_code(errc::illegal_byte_sequence
)));
744 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
746 std::codecvt_utf8
<_CharT
> __cvt
;
748 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
750 const char* __f2
= __str
.data();
751 const char* __l2
= __f2
+ __str
.size();
752 std::codecvt_utf8
<wchar_t> __wcvt
;
754 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
757 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
758 "Cannot convert character sequence",
759 std::make_error_code(errc::illegal_byte_sequence
)));
763 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
765 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
769 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
771 #ifdef _GLIBCXX_USE_CHAR8_T
772 if constexpr (is_same_v
<_CharT
, char8_t
>)
774 string_type
__str(__f
, __l
);
780 std::codecvt_utf8
<_CharT
> __cvt
;
782 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
784 #ifdef _GLIBCXX_USE_CHAR8_T
787 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
788 "Cannot convert character sequence",
789 std::make_error_code(errc::illegal_byte_sequence
)));
794 _S_convert(_CharT
* __f
, _CharT
* __l
)
796 return _S_convert(const_cast<const _CharT
*>(__f
),
797 const_cast<const _CharT
*>(__l
));
800 template<typename _Iter
>
802 _S_convert(_Iter __first
, _Iter __last
)
804 const std::basic_string
<_CharT
> __str(__first
, __last
);
805 return _S_convert(__str
.data(), __str
.data() + __str
.size());
808 template<typename _Iter
, typename _Cont
>
810 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
811 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
812 { return _S_convert(__first
.base(), __last
.base()); }
817 /// An iterator for the components of a path
821 using difference_type
= std::ptrdiff_t;
822 using value_type
= path
;
823 using reference
= const path
&;
824 using pointer
= const path
*;
825 using iterator_category
= std::bidirectional_iterator_tag
;
827 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
829 iterator(const iterator
&) = default;
830 iterator
& operator=(const iterator
&) = default;
832 reference
operator*() const;
833 pointer
operator->() const { return std::__addressof(**this); }
835 iterator
& operator++();
836 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
838 iterator
& operator--();
839 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
841 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
842 { return __lhs
._M_equals(__rhs
); }
844 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
845 { return !__lhs
._M_equals(__rhs
); }
850 bool _M_is_multi() const { return _M_path
->_M_type() == _Type::_Multi
; }
852 friend difference_type
853 __path_iter_distance(const iterator
& __first
, const iterator
& __last
)
855 __glibcxx_assert(__first
._M_path
!= nullptr);
856 __glibcxx_assert(__first
._M_path
== __last
._M_path
);
857 if (__first
._M_is_multi())
858 return std::distance(__first
._M_cur
, __last
._M_cur
);
859 else if (__first
._M_at_end
== __last
._M_at_end
)
862 return __first
._M_at_end
? -1 : 1;
866 __path_iter_advance(iterator
& __i
, difference_type __n
)
874 __glibcxx_assert(__i
._M_path
!= nullptr);
875 __glibcxx_assert(__i
._M_is_multi());
876 // __glibcxx_assert(__i._M_path->_M_cmpts.end() - __i._M_cur >= __n);
881 iterator(const path
* __path
, path::_List::const_iterator __iter
)
882 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
885 iterator(const path
* __path
, bool __at_end
)
886 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
889 bool _M_equals(iterator
) const;
892 path::_List::const_iterator _M_cur
;
893 bool _M_at_end
; // only used when type != _Multi
898 path::operator=(path
&& __p
) noexcept
900 if (&__p
== this) [[__unlikely__
]]
903 _M_pathname
= std::move(__p
._M_pathname
);
904 _M_cmpts
= std::move(__p
._M_cmpts
);
910 path::operator=(string_type
&& __source
)
911 { return *this = path(std::move(__source
)); }
914 path::assign(string_type
&& __source
)
915 { return *this = path(std::move(__source
)); }
918 path::operator+=(const string_type
& __x
)
925 path::operator+=(const value_type
* __x
)
932 path::operator+=(value_type __x
)
934 _M_concat(basic_string_view
<value_type
>(&__x
, 1));
939 path::operator+=(basic_string_view
<value_type
> __x
)
945 template<typename _CharT
>
946 inline path::_Path
<_CharT
*, _CharT
*>&
947 path::operator+=(_CharT __x
)
949 auto* __addr
= std::__addressof(__x
);
950 return concat(__addr
, __addr
+ 1);
954 path::make_preferred()
956 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
957 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
958 preferred_separator
);
963 inline void path::swap(path
& __rhs
) noexcept
965 _M_pathname
.swap(__rhs
._M_pathname
);
966 _M_cmpts
.swap(__rhs
._M_cmpts
);
969 /// @cond undocumented
970 template<typename _CharT
, typename _Traits
, typename _Allocator
>
971 std::basic_string
<_CharT
, _Traits
, _Allocator
>
972 path::_S_str_convert(const string_type
& __str
, const _Allocator
& __a
)
974 if (__str
.size() == 0)
975 return std::basic_string
<_CharT
, _Traits
, _Allocator
>(__a
);
977 const value_type
* __first
= __str
.data();
978 const value_type
* __last
= __first
+ __str
.size();
980 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
981 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
982 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
983 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
985 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
986 codecvt_utf8
<value_type
> __cvt
;
987 _String __u8str
{_CharAlloc
{__a
}};
988 if (__str_codecvt_out(__first
, __last
, __u8str
, __cvt
))
990 if constexpr (is_same_v
<_CharT
, char>)
992 #ifdef _GLIBCXX_USE_CHAR8_T
993 else if constexpr (is_same_v
<_CharT
, char8_t
>)
995 const char* __f
= __u8str
.data();
996 const char* __l
= __f
+ __u8str
.size();
997 _WString
__wstr(__f
, __l
);
1004 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
1005 codecvt_utf8
<_CharT
> __cvt
;
1006 const char* __f
= __u8str
.data();
1007 const char* __l
= __f
+ __u8str
.size();
1008 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
1013 #ifdef _GLIBCXX_USE_CHAR8_T
1014 if constexpr (is_same_v
<_CharT
, char8_t
>)
1016 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__first
, __last
, __a
};
1022 codecvt_utf8
<_CharT
> __cvt
;
1023 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
1024 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
1026 #ifdef _GLIBCXX_USE_CHAR8_T
1030 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1031 "Cannot convert character sequence",
1032 std::make_error_code(errc::illegal_byte_sequence
)));
1036 template<typename _CharT
, typename _Traits
, typename _Allocator
>
1037 inline basic_string
<_CharT
, _Traits
, _Allocator
>
1038 path::string(const _Allocator
& __a
) const
1040 if constexpr (is_same_v
<_CharT
, value_type
>)
1041 return { _M_pathname
, __a
};
1043 return _S_str_convert
<_CharT
, _Traits
>(_M_pathname
, __a
);
1047 path::string() const { return string
<char>(); }
1049 #if _GLIBCXX_USE_WCHAR_T
1051 path::wstring() const { return string
<wchar_t>(); }
1054 #ifdef _GLIBCXX_USE_CHAR8_T
1055 inline std::u8string
1056 path::u8string() const { return string
<char8_t
>(); }
1059 path::u8string() const
1061 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1063 // convert from native encoding to UTF-8
1064 codecvt_utf8
<value_type
> __cvt
;
1065 const value_type
* __first
= _M_pathname
.data();
1066 const value_type
* __last
= __first
+ _M_pathname
.size();
1067 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
1069 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1070 "Cannot convert character sequence",
1071 std::make_error_code(errc::illegal_byte_sequence
)));
1076 #endif // _GLIBCXX_USE_CHAR8_T
1078 inline std::u16string
1079 path::u16string() const { return string
<char16_t
>(); }
1081 inline std::u32string
1082 path::u32string() const { return string
<char32_t
>(); }
1084 template<typename _CharT
, typename _Traits
, typename _Allocator
>
1085 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
1086 path::generic_string(const _Allocator
& __a
) const
1088 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1089 const value_type __slash
= L
'/';
1091 const value_type __slash
= '/';
1093 string_type
__str(__a
);
1095 if (_M_type() == _Type::_Root_dir
)
1096 __str
.assign(1, __slash
);
1099 __str
.reserve(_M_pathname
.size());
1100 bool __add_slash
= false;
1101 for (auto& __elem
: *this)
1105 __str
+= __elem
._M_pathname
;
1106 __add_slash
= __elem
._M_type() == _Type::_Filename
;
1110 if constexpr (is_same_v
<_CharT
, value_type
>)
1113 return _S_str_convert
<_CharT
, _Traits
>(__str
, __a
);
1117 path::generic_string() const
1118 { return generic_string
<char>(); }
1120 #if _GLIBCXX_USE_WCHAR_T
1122 path::generic_wstring() const
1123 { return generic_string
<wchar_t>(); }
1126 #ifdef _GLIBCXX_USE_CHAR8_T
1127 inline std::u8string
1128 path::generic_u8string() const
1129 { return generic_string
<char8_t
>(); }
1132 path::generic_u8string() const
1133 { return generic_string(); }
1136 inline std::u16string
1137 path::generic_u16string() const
1138 { return generic_string
<char16_t
>(); }
1140 inline std::u32string
1141 path::generic_u32string() const
1142 { return generic_string
<char32_t
>(); }
1145 path::compare(const string_type
& __s
) const noexcept
1146 { return compare(basic_string_view
<value_type
>(__s
)); }
1149 path::compare(const value_type
* __s
) const noexcept
1150 { return compare(basic_string_view
<value_type
>(__s
)); }
1153 path::filename() const
1157 else if (_M_type() == _Type::_Filename
)
1159 else if (_M_type() == _Type::_Multi
)
1161 if (_M_pathname
.back() == preferred_separator
)
1163 auto& __last
= *--end();
1164 if (__last
._M_type() == _Type::_Filename
)
1173 auto ext
= _M_find_extension();
1174 if (ext
.first
&& ext
.second
!= 0)
1175 return path
{ext
.first
->substr(0, ext
.second
)};
1180 path::extension() const
1182 auto ext
= _M_find_extension();
1183 if (ext
.first
&& ext
.second
!= string_type::npos
)
1184 return path
{ext
.first
->substr(ext
.second
)};
1189 path::has_stem() const noexcept
1191 auto ext
= _M_find_extension();
1192 return ext
.first
&& ext
.second
!= 0;
1196 path::has_extension() const noexcept
1198 auto ext
= _M_find_extension();
1199 return ext
.first
&& ext
.second
!= string_type::npos
;
1203 path::is_absolute() const noexcept
1205 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1206 return has_root_name() && has_root_directory();
1208 return has_root_directory();
1212 inline path::iterator
1215 if (_M_type() == _Type::_Multi
)
1216 return iterator(this, _M_cmpts
.begin());
1217 return iterator(this, empty());
1220 inline path::iterator
1223 if (_M_type() == _Type::_Multi
)
1224 return iterator(this, _M_cmpts
.end());
1225 return iterator(this, true);
1228 inline path::iterator
&
1229 path::iterator::operator++()
1231 __glibcxx_assert(_M_path
!= nullptr);
1232 if (_M_path
->_M_type() == _Type::_Multi
)
1234 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1239 __glibcxx_assert(!_M_at_end
);
1245 inline path::iterator
&
1246 path::iterator::operator--()
1248 __glibcxx_assert(_M_path
!= nullptr);
1249 if (_M_path
->_M_type() == _Type::_Multi
)
1251 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1256 __glibcxx_assert(_M_at_end
);
1262 inline path::iterator::reference
1263 path::iterator::operator*() const
1265 __glibcxx_assert(_M_path
!= nullptr);
1266 if (_M_path
->_M_type() == _Type::_Multi
)
1268 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1275 path::iterator::_M_equals(iterator __rhs
) const
1277 if (_M_path
!= __rhs
._M_path
)
1279 if (_M_path
== nullptr)
1281 if (_M_path
->_M_type() == path::_Type::_Multi
)
1282 return _M_cur
== __rhs
._M_cur
;
1283 return _M_at_end
== __rhs
._M_at_end
;
1286 // @} group filesystem
1287 _GLIBCXX_END_NAMESPACE_CXX11
1288 } // namespace filesystem
1291 distance(filesystem::path::iterator __first
, filesystem::path::iterator __last
)
1292 { return __path_iter_distance(__first
, __last
); }
1294 template<typename _InputIterator
, typename _Distance
>
1296 advance(filesystem::path::iterator
& __i
, _Distance __n
)
1297 { __path_iter_advance(__i
, static_cast<ptrdiff_t>(__n
)); }
1299 extern template class __shared_ptr
<const filesystem::filesystem_error::_Impl
>;
1301 _GLIBCXX_END_NAMESPACE_VERSION
1306 #endif // _GLIBCXX_FS_PATH_H