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
<remove_pointer_t
<_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 // XXX This assumes native wide encoding is UTF-16.
632 std::codecvt_utf8_utf16
<path::value_type
> __cvt
;
633 path::string_type __tmp
;
634 if constexpr (is_pointer_v
<_InputIterator
>)
636 if (__str_codecvt_in_all(__first
, __last
, __tmp
, __cvt
))
637 return path
{ __tmp
};
641 const std::string __u8str
{__first
, __last
};
642 const char* const __ptr
= __u8str
.data();
643 if (__str_codecvt_in_all(__ptr
, __ptr
+ __u8str
.size(), __tmp
, __cvt
))
644 return path
{ __tmp
};
646 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
647 "Cannot convert character sequence",
648 std::make_error_code(errc::illegal_byte_sequence
)));
650 // This assumes native normal encoding is UTF-8.
651 return path
{ __first
, __last
};
655 /// Create a path from a UTF-8-encoded sequence of char
656 template<typename _Source
>
658 u8path(const _Source
& __source
)
659 -> decltype(filesystem::path(__source
, std::locale::classic()))
661 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
662 if constexpr (is_convertible_v
<const _Source
&, std::string_view
>)
664 const std::string_view __s
= __source
;
665 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
669 std::string __s
= path::_S_string_from_iter(__source
);
670 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
673 return path
{ __source
};
679 /// Exception type thrown by the Filesystem library
680 class filesystem_error
: public std::system_error
683 filesystem_error(const string
& __what_arg
, error_code __ec
);
685 filesystem_error(const string
& __what_arg
, const path
& __p1
,
688 filesystem_error(const string
& __what_arg
, const path
& __p1
,
689 const path
& __p2
, error_code __ec
);
691 filesystem_error(const filesystem_error
&) = default;
692 filesystem_error
& operator=(const filesystem_error
&) = default;
694 // No move constructor or assignment operator.
695 // Copy rvalues instead, so that _M_impl is not left empty.
699 const path
& path1() const noexcept
;
700 const path
& path2() const noexcept
;
701 const char* what() const noexcept
;
705 std::__shared_ptr
<const _Impl
> _M_impl
;
708 /// @cond undocumented
710 struct path::_Cmpt
: path
712 _Cmpt(basic_string_view
<value_type
> __s
, _Type __t
, size_t __pos
)
713 : path(__s
, __t
), _M_pos(__pos
) { }
715 _Cmpt() : _M_pos(-1) { }
720 // specialize _Cvt for degenerate 'noconv' case
722 struct path::_Cvt
<path::value_type
>
724 template<typename _Iter
>
726 _S_convert(_Iter __first
, _Iter __last
)
727 { return string_type
{__first
, __last
}; }
730 #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
731 // For POSIX converting from char8_t to char is also 'noconv'
733 struct path::_Cvt
<char8_t
>
735 template<typename _Iter
>
737 _S_convert(_Iter __first
, _Iter __last
)
738 { return string_type(__first
, __last
); }
742 template<typename _CharT
>
746 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
748 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
750 if constexpr (is_same_v
<_CharT
, char>)
752 struct _UCvt
: std::codecvt
<wchar_t, char, std::mbstate_t>
754 if (__str_codecvt_in_all(__f
, __l
, __wstr
, __cvt
))
757 #ifdef _GLIBCXX_USE_CHAR8_T
758 else if constexpr (is_same_v
<_CharT
, char8_t
>)
760 const char* __f2
= (const char*)__f
;
761 const char* __l2
= (const char*)__l
;
762 std::codecvt_utf8_utf16
<wchar_t> __wcvt
;
763 if (__str_codecvt_in_all(__f2
, __l2
, __wstr
, __wcvt
))
767 else // char16_t or char32_t
769 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t>
772 if (__str_codecvt_out_all(__f
, __l
, __str
, __cvt
))
774 const char* __f2
= __str
.data();
775 const char* __l2
= __f2
+ __str
.size();
776 std::codecvt_utf8_utf16
<wchar_t> __wcvt
;
777 if (__str_codecvt_in_all(__f2
, __l2
, __wstr
, __wcvt
))
782 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t>
785 if (__str_codecvt_out_all(__f
, __l
, __str
, __cvt
))
788 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
789 "Cannot convert character sequence",
790 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 static_assert(!is_same_v
<_CharT
, value_type
>);
976 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
978 if (__str
.size() == 0)
979 return _WString(__a
);
981 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
982 // First convert native string from UTF-16 to to UTF-8.
983 // XXX This assumes that the execution wide-character set is UTF-16.
984 std::codecvt_utf8_utf16
<value_type
> __cvt
;
986 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
987 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
988 _String __u8str
{_CharAlloc
{__a
}};
989 const value_type
* __wfirst
= __str
.data();
990 const value_type
* __wlast
= __wfirst
+ __str
.size();
991 if (__str_codecvt_out_all(__wfirst
, __wlast
, __u8str
, __cvt
)) {
992 if constexpr (is_same_v
<_CharT
, char>)
993 return __u8str
; // XXX assumes native ordinary encoding is UTF-8.
996 const char* __first
= __u8str
.data();
997 const char* __last
= __first
+ __u8str
.size();
999 const value_type
* __first
= __str
.data();
1000 const value_type
* __last
= __first
+ __str
.size();
1003 // Convert UTF-8 string to requested format.
1004 #ifdef _GLIBCXX_USE_CHAR8_T
1005 if constexpr (is_same_v
<_CharT
, char8_t
>)
1006 return _WString(__first
, __last
, __a
);
1010 // Convert UTF-8 to wide string.
1011 _WString
__wstr(__a
);
1012 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t> { } __cvt
;
1013 if (__str_codecvt_in_all(__first
, __last
, __wstr
, __cvt
))
1017 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1020 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1021 "Cannot convert character sequence",
1022 std::make_error_code(errc::illegal_byte_sequence
)));
1026 template<typename _CharT
, typename _Traits
, typename _Allocator
>
1027 inline basic_string
<_CharT
, _Traits
, _Allocator
>
1028 path::string(const _Allocator
& __a
) const
1030 if constexpr (is_same_v
<_CharT
, value_type
>)
1031 return { _M_pathname
.c_str(), _M_pathname
.length(), __a
};
1033 return _S_str_convert
<_CharT
, _Traits
>(_M_pathname
, __a
);
1037 path::string() const { return string
<char>(); }
1039 #if _GLIBCXX_USE_WCHAR_T
1041 path::wstring() const { return string
<wchar_t>(); }
1044 #ifdef _GLIBCXX_USE_CHAR8_T
1045 inline std::u8string
1046 path::u8string() const { return string
<char8_t
>(); }
1049 path::u8string() const
1051 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1053 // convert from native wide encoding (assumed to be UTF-16) to UTF-8
1054 std::codecvt_utf8_utf16
<value_type
> __cvt
;
1055 const value_type
* __first
= _M_pathname
.data();
1056 const value_type
* __last
= __first
+ _M_pathname
.size();
1057 if (__str_codecvt_out_all(__first
, __last
, __str
, __cvt
))
1059 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1060 "Cannot convert character sequence",
1061 std::make_error_code(errc::illegal_byte_sequence
)));
1066 #endif // _GLIBCXX_USE_CHAR8_T
1068 inline std::u16string
1069 path::u16string() const { return string
<char16_t
>(); }
1071 inline std::u32string
1072 path::u32string() const { return string
<char32_t
>(); }
1074 template<typename _CharT
, typename _Traits
, typename _Allocator
>
1075 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
1076 path::generic_string(const _Allocator
& __a
) const
1078 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1079 const value_type __slash
= L
'/';
1081 const value_type __slash
= '/';
1083 string_type
__str(__a
);
1085 if (_M_type() == _Type::_Root_dir
)
1086 __str
.assign(1, __slash
);
1089 __str
.reserve(_M_pathname
.size());
1090 bool __add_slash
= false;
1091 for (auto& __elem
: *this)
1095 __str
+= __elem
._M_pathname
;
1096 __add_slash
= __elem
._M_type() == _Type::_Filename
;
1100 if constexpr (is_same_v
<_CharT
, value_type
>)
1103 return _S_str_convert
<_CharT
, _Traits
>(__str
, __a
);
1107 path::generic_string() const
1108 { return generic_string
<char>(); }
1110 #if _GLIBCXX_USE_WCHAR_T
1112 path::generic_wstring() const
1113 { return generic_string
<wchar_t>(); }
1116 #ifdef _GLIBCXX_USE_CHAR8_T
1117 inline std::u8string
1118 path::generic_u8string() const
1119 { return generic_string
<char8_t
>(); }
1122 path::generic_u8string() const
1123 { return generic_string(); }
1126 inline std::u16string
1127 path::generic_u16string() const
1128 { return generic_string
<char16_t
>(); }
1130 inline std::u32string
1131 path::generic_u32string() const
1132 { return generic_string
<char32_t
>(); }
1135 path::compare(const string_type
& __s
) const noexcept
1136 { return compare(basic_string_view
<value_type
>(__s
)); }
1139 path::compare(const value_type
* __s
) const noexcept
1140 { return compare(basic_string_view
<value_type
>(__s
)); }
1143 path::filename() const
1147 else if (_M_type() == _Type::_Filename
)
1149 else if (_M_type() == _Type::_Multi
)
1151 if (_M_pathname
.back() == preferred_separator
)
1153 auto& __last
= *--end();
1154 if (__last
._M_type() == _Type::_Filename
)
1163 auto ext
= _M_find_extension();
1164 if (ext
.first
&& ext
.second
!= 0)
1165 return path
{ext
.first
->substr(0, ext
.second
)};
1170 path::extension() const
1172 auto ext
= _M_find_extension();
1173 if (ext
.first
&& ext
.second
!= string_type::npos
)
1174 return path
{ext
.first
->substr(ext
.second
)};
1179 path::has_stem() const noexcept
1181 auto ext
= _M_find_extension();
1182 return ext
.first
&& ext
.second
!= 0;
1186 path::has_extension() const noexcept
1188 auto ext
= _M_find_extension();
1189 return ext
.first
&& ext
.second
!= string_type::npos
;
1193 path::is_absolute() const noexcept
1195 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1196 return has_root_name() && has_root_directory();
1198 return has_root_directory();
1202 inline path::iterator
1205 if (_M_type() == _Type::_Multi
)
1206 return iterator(this, _M_cmpts
.begin());
1207 return iterator(this, empty());
1210 inline path::iterator
1213 if (_M_type() == _Type::_Multi
)
1214 return iterator(this, _M_cmpts
.end());
1215 return iterator(this, true);
1218 inline path::iterator
&
1219 path::iterator::operator++()
1221 __glibcxx_assert(_M_path
!= nullptr);
1222 if (_M_path
->_M_type() == _Type::_Multi
)
1224 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1229 __glibcxx_assert(!_M_at_end
);
1235 inline path::iterator
&
1236 path::iterator::operator--()
1238 __glibcxx_assert(_M_path
!= nullptr);
1239 if (_M_path
->_M_type() == _Type::_Multi
)
1241 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1246 __glibcxx_assert(_M_at_end
);
1252 inline path::iterator::reference
1253 path::iterator::operator*() const
1255 __glibcxx_assert(_M_path
!= nullptr);
1256 if (_M_path
->_M_type() == _Type::_Multi
)
1258 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1265 path::iterator::_M_equals(iterator __rhs
) const
1267 if (_M_path
!= __rhs
._M_path
)
1269 if (_M_path
== nullptr)
1271 if (_M_path
->_M_type() == path::_Type::_Multi
)
1272 return _M_cur
== __rhs
._M_cur
;
1273 return _M_at_end
== __rhs
._M_at_end
;
1276 // @} group filesystem
1277 _GLIBCXX_END_NAMESPACE_CXX11
1278 } // namespace filesystem
1281 distance(filesystem::path::iterator __first
, filesystem::path::iterator __last
)
1282 { return __path_iter_distance(__first
, __last
); }
1284 template<typename _InputIterator
, typename _Distance
>
1286 advance(filesystem::path::iterator
& __i
, _Distance __n
)
1287 { __path_iter_advance(__i
, static_cast<ptrdiff_t>(__n
)); }
1289 extern template class __shared_ptr
<const filesystem::filesystem_error::_Impl
>;
1291 _GLIBCXX_END_NAMESPACE_VERSION
1296 #endif // _GLIBCXX_FS_PATH_H