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
>
70 using __is_encoded_char
= __is_one_of
<remove_const_t
<_CharT
>,
72 #ifdef _GLIBCXX_USE_CHAR8_T
75 #if _GLIBCXX_USE_WCHAR_T
80 template<typename _Iter
,
81 typename _Iter_traits
= std::iterator_traits
<_Iter
>>
82 using __is_path_iter_src
83 = __and_
<__is_encoded_char
<typename
_Iter_traits::value_type
>,
84 std::is_base_of
<std::input_iterator_tag
,
85 typename
_Iter_traits::iterator_category
>>;
87 template<typename _Iter
>
88 static __is_path_iter_src
<_Iter
>
89 __is_path_src(_Iter
, int);
91 template<typename _CharT
, typename _Traits
, typename _Alloc
>
92 static __is_encoded_char
<_CharT
>
93 __is_path_src(const basic_string
<_CharT
, _Traits
, _Alloc
>&, int);
95 template<typename _CharT
, typename _Traits
>
96 static __is_encoded_char
<_CharT
>
97 __is_path_src(const basic_string_view
<_CharT
, _Traits
>&, int);
99 template<typename _Unknown
>
100 static std::false_type
101 __is_path_src(const _Unknown
&, ...);
103 template<typename _Tp1
, typename _Tp2
>
104 struct __constructible_from
;
106 template<typename _Iter
>
107 struct __constructible_from
<_Iter
, _Iter
>
108 : __is_path_iter_src
<_Iter
>
111 template<typename _Source
>
112 struct __constructible_from
<_Source
, void>
113 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
116 template<typename _Tp1
, typename _Tp2
= void>
117 using _Path
= typename
118 std::enable_if
<__and_
<__not_
<is_same
<remove_cv_t
<_Tp1
>, path
>>,
119 __not_
<is_void
<remove_pointer_t
<_Tp1
>>>,
120 __constructible_from
<_Tp1
, _Tp2
>>::value
,
123 template<typename _Source
>
125 _S_range_begin(_Source __begin
) { return __begin
; }
127 struct __null_terminated
{ };
129 template<typename _Source
>
130 static __null_terminated
131 _S_range_end(_Source
) { return {}; }
133 template<typename _CharT
, typename _Traits
, typename _Alloc
>
135 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
136 { return __str
.data(); }
138 template<typename _CharT
, typename _Traits
, typename _Alloc
>
140 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
141 { return __str
.data() + __str
.size(); }
143 template<typename _CharT
, typename _Traits
>
145 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
146 { return __str
.data(); }
148 template<typename _CharT
, typename _Traits
>
150 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
151 { return __str
.data() + __str
.size(); }
153 template<typename _Tp
,
154 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
155 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
156 using __value_type_is_char
157 = std::enable_if_t
<std::is_same_v
<std::remove_const_t
<_Val
>, char>>;
160 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
161 using value_type
= wchar_t;
162 static constexpr value_type preferred_separator
= L
'\\';
164 # ifdef _GLIBCXX_DOXYGEN
165 /// Windows uses wchar_t for path::value_type, POSIX uses char.
166 using value_type
= __os_dependent__
;
168 using value_type
= char;
170 static constexpr value_type preferred_separator
= '/';
172 using string_type
= std::basic_string
<value_type
>;
174 /// path::format is ignored in this implementation
175 enum format
: unsigned char { native_format
, generic_format
, auto_format
};
177 // constructors and destructor
181 path(const path
& __p
) = default;
184 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_FULLY_DYNAMIC_STRING == 0
187 : _M_pathname(std::move(__p
._M_pathname
)),
188 _M_cmpts(std::move(__p
._M_cmpts
))
191 path(string_type
&& __source
, format
= auto_format
)
192 : _M_pathname(std::move(__source
))
193 { _M_split_cmpts(); }
195 template<typename _Source
,
196 typename _Require
= _Path
<_Source
>>
197 path(_Source
const& __source
, format
= auto_format
)
198 : _M_pathname(_S_convert(_S_range_begin(__source
),
199 _S_range_end(__source
)))
200 { _M_split_cmpts(); }
202 template<typename _InputIterator
,
203 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
204 path(_InputIterator __first
, _InputIterator __last
, format
= auto_format
)
205 : _M_pathname(_S_convert(__first
, __last
))
206 { _M_split_cmpts(); }
208 template<typename _Source
,
209 typename _Require
= _Path
<_Source
>,
210 typename _Require2
= __value_type_is_char
<_Source
>>
211 path(_Source
const& __source
, const locale
& __loc
, format
= auto_format
)
212 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
213 _S_range_end(__source
), __loc
))
214 { _M_split_cmpts(); }
216 template<typename _InputIterator
,
217 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
218 typename _Require2
= __value_type_is_char
<_InputIterator
>>
219 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
,
220 format
= auto_format
)
221 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
222 { _M_split_cmpts(); }
228 path
& operator=(const path
&);
229 path
& operator=(path
&&) noexcept
;
230 path
& operator=(string_type
&& __source
);
231 path
& assign(string_type
&& __source
);
233 template<typename _Source
>
235 operator=(_Source
const& __source
)
236 { return *this = path(__source
); }
238 template<typename _Source
>
240 assign(_Source
const& __source
)
241 { return *this = path(__source
); }
243 template<typename _InputIterator
>
244 _Path
<_InputIterator
, _InputIterator
>&
245 assign(_InputIterator __first
, _InputIterator __last
)
246 { return *this = path(__first
, __last
); }
250 path
& operator/=(const path
& __p
);
252 template <class _Source
>
254 operator/=(_Source
const& __source
)
256 _M_append(_S_convert(_S_range_begin(__source
), _S_range_end(__source
)));
260 template<typename _Source
>
262 append(_Source
const& __source
)
264 _M_append(_S_convert(_S_range_begin(__source
), _S_range_end(__source
)));
268 template<typename _InputIterator
>
269 _Path
<_InputIterator
, _InputIterator
>&
270 append(_InputIterator __first
, _InputIterator __last
)
272 _M_append(_S_convert(__first
, __last
));
278 path
& operator+=(const path
& __x
);
279 path
& operator+=(const string_type
& __x
);
280 path
& operator+=(const value_type
* __x
);
281 path
& operator+=(value_type __x
);
282 path
& operator+=(basic_string_view
<value_type
> __x
);
284 template<typename _Source
>
286 operator+=(_Source
const& __x
) { return concat(__x
); }
288 template<typename _CharT
>
289 _Path
<_CharT
*, _CharT
*>&
290 operator+=(_CharT __x
);
292 template<typename _Source
>
294 concat(_Source
const& __x
)
296 _M_concat(_S_convert(_S_range_begin(__x
), _S_range_end(__x
)));
300 template<typename _InputIterator
>
301 _Path
<_InputIterator
, _InputIterator
>&
302 concat(_InputIterator __first
, _InputIterator __last
)
304 _M_concat(_S_convert(__first
, __last
));
310 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
312 path
& make_preferred();
313 path
& remove_filename();
314 path
& replace_filename(const path
& __replacement
);
315 path
& replace_extension(const path
& __replacement
= path());
317 void swap(path
& __rhs
) noexcept
;
319 // native format observers
321 const string_type
& native() const noexcept
{ return _M_pathname
; }
322 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
323 operator string_type() const { return _M_pathname
; }
325 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
326 typename _Allocator
= std::allocator
<_CharT
>>
327 std::basic_string
<_CharT
, _Traits
, _Allocator
>
328 string(const _Allocator
& __a
= _Allocator()) const;
330 std::string
string() const;
331 #if _GLIBCXX_USE_WCHAR_T
332 std::wstring
wstring() const;
334 #ifdef _GLIBCXX_USE_CHAR8_T
335 __attribute__((__abi_tag__("__u8")))
336 std::u8string
u8string() const;
338 std::string
u8string() const;
339 #endif // _GLIBCXX_USE_CHAR8_T
340 std::u16string
u16string() const;
341 std::u32string
u32string() const;
343 // generic format observers
344 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
345 typename _Allocator
= std::allocator
<_CharT
>>
346 std::basic_string
<_CharT
, _Traits
, _Allocator
>
347 generic_string(const _Allocator
& __a
= _Allocator()) const;
349 std::string
generic_string() const;
350 #if _GLIBCXX_USE_WCHAR_T
351 std::wstring
generic_wstring() const;
353 #ifdef _GLIBCXX_USE_CHAR8_T
354 __attribute__((__abi_tag__("__u8")))
355 std::u8string
generic_u8string() const;
357 std::string
generic_u8string() const;
358 #endif // _GLIBCXX_USE_CHAR8_T
359 std::u16string
generic_u16string() const;
360 std::u32string
generic_u32string() const;
364 int compare(const path
& __p
) const noexcept
;
365 int compare(const string_type
& __s
) const noexcept
;
366 int compare(const value_type
* __s
) const noexcept
;
367 int compare(basic_string_view
<value_type
> __s
) const noexcept
;
371 path
root_name() const;
372 path
root_directory() const;
373 path
root_path() const;
374 path
relative_path() const;
375 path
parent_path() const;
376 path
filename() const;
378 path
extension() const;
382 [[nodiscard
]] bool empty() const noexcept
{ return _M_pathname
.empty(); }
383 bool has_root_name() const noexcept
;
384 bool has_root_directory() const noexcept
;
385 bool has_root_path() const noexcept
;
386 bool has_relative_path() const noexcept
;
387 bool has_parent_path() const noexcept
;
388 bool has_filename() const noexcept
;
389 bool has_stem() const noexcept
;
390 bool has_extension() const noexcept
;
391 bool is_absolute() const noexcept
;
392 bool is_relative() const noexcept
{ return !is_absolute(); }
395 path
lexically_normal() const;
396 path
lexically_relative(const path
& base
) const;
397 path
lexically_proximate(const path
& base
) const;
401 using const_iterator
= iterator
;
403 iterator
begin() const;
404 iterator
end() const;
406 /// Write a path to a stream
407 template<typename _CharT
, typename _Traits
>
408 friend std::basic_ostream
<_CharT
, _Traits
>&
409 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
411 __os
<< std::quoted(__p
.string
<_CharT
, _Traits
>());
415 /// Read a path from a stream
416 template<typename _CharT
, typename _Traits
>
417 friend std::basic_istream
<_CharT
, _Traits
>&
418 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
420 std::basic_string
<_CharT
, _Traits
> __tmp
;
421 if (__is
>> std::quoted(__tmp
))
422 __p
= std::move(__tmp
);
426 // non-member operators
429 friend bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
430 { return __lhs
.compare(__rhs
) < 0; }
433 friend bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
434 { return !(__rhs
< __lhs
); }
437 friend bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
438 { return __rhs
< __lhs
; }
441 friend bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
442 { return !(__lhs
< __rhs
); }
445 friend bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
446 { return __lhs
.compare(__rhs
) == 0; }
449 friend bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
450 { return !(__lhs
== __rhs
); }
452 /// Append one path to another
453 friend path
operator/(const path
& __lhs
, const path
& __rhs
)
455 path
__result(__lhs
);
460 /// @cond undocumented
461 // Create a basic_string by reading until a null character.
462 template<typename _InputIterator
,
463 typename _Traits
= std::iterator_traits
<_InputIterator
>,
465 = typename
std::remove_cv_t
<typename
_Traits::value_type
>>
466 static std::basic_string
<_CharT
>
467 _S_string_from_iter(_InputIterator __source
)
469 std::basic_string
<_CharT
> __str
;
470 for (_CharT __ch
= *__source
; __ch
!= _CharT(); __ch
= *++__source
)
471 __str
.push_back(__ch
);
477 enum class _Type
: unsigned char {
478 _Multi
= 0, _Root_name
, _Root_dir
, _Filename
481 path(basic_string_view
<value_type
> __str
, _Type __type
)
484 __glibcxx_assert(__type
!= _Type::_Multi
);
485 _M_cmpts
.type(__type
);
488 enum class _Split
{ _Stem
, _Extension
};
490 void _M_append(basic_string_view
<value_type
>);
491 void _M_concat(basic_string_view
<value_type
>);
493 pair
<const string_type
*, size_t> _M_find_extension() const noexcept
;
495 template<typename _CharT
>
498 static basic_string_view
<value_type
>
499 _S_convert(value_type
* __src
, __null_terminated
)
502 static basic_string_view
<value_type
>
503 _S_convert(const value_type
* __src
, __null_terminated
)
506 static basic_string_view
<value_type
>
507 _S_convert(value_type
* __first
, value_type
* __last
)
508 { return {__first
, __last
- __first
}; }
510 static basic_string_view
<value_type
>
511 _S_convert(const value_type
* __first
, const value_type
* __last
)
512 { return {__first
, __last
- __first
}; }
514 template<typename _Iter
>
516 _S_convert(_Iter __first
, _Iter __last
)
518 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
519 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
520 _S_convert(__first
, __last
);
523 template<typename _InputIterator
>
525 _S_convert(_InputIterator __src
, __null_terminated
)
527 // Read from iterator into basic_string until a null value is seen:
528 auto __s
= _S_string_from_iter(__src
);
529 // Convert (if needed) from iterator's value type to path::value_type:
530 return string_type(_S_convert(__s
.data(), __s
.data() + __s
.size()));
534 _S_convert_loc(const char* __first
, const char* __last
,
535 const std::locale
& __loc
);
537 template<typename _Iter
>
539 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
541 const std::string
__str(__first
, __last
);
542 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
545 template<typename _InputIterator
>
547 _S_convert_loc(_InputIterator __src
, __null_terminated
,
548 const std::locale
& __loc
)
550 const std::string __s
= _S_string_from_iter(__src
);
551 return _S_convert_loc(__s
.data(), __s
.data() + __s
.size(), __loc
);
554 template<typename _CharT
, typename _Traits
, typename _Allocator
>
555 static basic_string
<_CharT
, _Traits
, _Allocator
>
556 _S_str_convert(const string_type
&, const _Allocator
& __a
);
558 void _M_split_cmpts();
560 _Type
_M_type() const noexcept
{ return _M_cmpts
.type(); }
562 string_type _M_pathname
;
568 using value_type
= _Cmpt
;
569 using iterator
= value_type
*;
570 using const_iterator
= const value_type
*;
574 _List(_List
&&) = default;
575 _List
& operator=(const _List
&);
576 _List
& operator=(_List
&&) = default;
579 _Type
type() const noexcept
580 { return _Type
{reinterpret_cast<uintptr_t>(_M_impl
.get()) & 0x3}; }
582 void type(_Type
) noexcept
;
584 int size() const noexcept
; // zero unless type() == _Type::_Multi
585 bool empty() const noexcept
; // true unless type() == _Type::_Multi
587 void swap(_List
& __l
) noexcept
{ _M_impl
.swap(__l
._M_impl
); }
588 int capacity() const noexcept
;
589 void reserve(int, bool); ///< @pre type() == _Type::_Multi
591 // All the member functions below here have a precondition !empty()
592 // (and they should only be called from within the library).
596 const_iterator
begin() const;
597 const_iterator
end() const;
599 value_type
& front() noexcept
;
600 value_type
& back() noexcept
;
601 const value_type
& front() const noexcept
;
602 const value_type
& back() const noexcept
;
605 void _M_erase_from(const_iterator __pos
); // erases [__pos,end())
610 void operator()(_Impl
*) const noexcept
;
612 unique_ptr
<_Impl
, _Impl_deleter
> _M_impl
;
619 /// @relates std::filesystem::path @{
621 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
623 size_t hash_value(const path
& __p
) noexcept
;
625 /// Create a path from a UTF-8-encoded sequence of char
626 template<typename _InputIterator
>
628 u8path(_InputIterator __first
, _InputIterator __last
)
629 -> decltype(filesystem::path(__first
, __last
, std::locale::classic()))
631 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
632 // XXX This assumes native wide encoding is UTF-16.
633 std::codecvt_utf8_utf16
<path::value_type
> __cvt
;
634 path::string_type __tmp
;
635 if constexpr (is_pointer_v
<_InputIterator
>)
637 if (__str_codecvt_in_all(__first
, __last
, __tmp
, __cvt
))
638 return path
{ __tmp
};
642 const std::string __u8str
{__first
, __last
};
643 const char* const __ptr
= __u8str
.data();
644 if (__str_codecvt_in_all(__ptr
, __ptr
+ __u8str
.size(), __tmp
, __cvt
))
645 return path
{ __tmp
};
647 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
648 "Cannot convert character sequence",
649 std::make_error_code(errc::illegal_byte_sequence
)));
651 // This assumes native normal encoding is UTF-8.
652 return path
{ __first
, __last
};
656 /// Create a path from a UTF-8-encoded sequence of char
657 template<typename _Source
>
659 u8path(const _Source
& __source
)
660 -> decltype(filesystem::path(__source
, std::locale::classic()))
662 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
663 if constexpr (is_convertible_v
<const _Source
&, std::string_view
>)
665 const std::string_view __s
= __source
;
666 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
670 std::string __s
= path::_S_string_from_iter(__source
);
671 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
674 return path
{ __source
};
680 /// Exception type thrown by the Filesystem library
681 class filesystem_error
: public std::system_error
684 filesystem_error(const string
& __what_arg
, error_code __ec
);
686 filesystem_error(const string
& __what_arg
, const path
& __p1
,
689 filesystem_error(const string
& __what_arg
, const path
& __p1
,
690 const path
& __p2
, error_code __ec
);
692 filesystem_error(const filesystem_error
&) = default;
693 filesystem_error
& operator=(const filesystem_error
&) = default;
695 // No move constructor or assignment operator.
696 // Copy rvalues instead, so that _M_impl is not left empty.
700 const path
& path1() const noexcept
;
701 const path
& path2() const noexcept
;
702 const char* what() const noexcept
;
706 std::__shared_ptr
<const _Impl
> _M_impl
;
709 /// @cond undocumented
711 struct path::_Cmpt
: path
713 _Cmpt(basic_string_view
<value_type
> __s
, _Type __t
, size_t __pos
)
714 : path(__s
, __t
), _M_pos(__pos
) { }
716 _Cmpt() : _M_pos(-1) { }
721 // specialize _Cvt for degenerate 'noconv' case
723 struct path::_Cvt
<path::value_type
>
725 template<typename _Iter
>
727 _S_convert(_Iter __first
, _Iter __last
)
728 { return string_type
{__first
, __last
}; }
731 #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
732 // For POSIX converting from char8_t to char is also 'noconv'
734 struct path::_Cvt
<char8_t
>
736 template<typename _Iter
>
738 _S_convert(_Iter __first
, _Iter __last
)
739 { return string_type(__first
, __last
); }
743 template<typename _CharT
>
747 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
749 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
751 if constexpr (is_same_v
<_CharT
, char>)
753 struct _UCvt
: std::codecvt
<wchar_t, char, std::mbstate_t>
755 if (__str_codecvt_in_all(__f
, __l
, __wstr
, __cvt
))
758 #ifdef _GLIBCXX_USE_CHAR8_T
759 else if constexpr (is_same_v
<_CharT
, char8_t
>)
761 const char* __f2
= (const char*)__f
;
762 const char* __l2
= (const char*)__l
;
763 std::codecvt_utf8_utf16
<wchar_t> __wcvt
;
764 if (__str_codecvt_in_all(__f2
, __l2
, __wstr
, __wcvt
))
768 else // char16_t or char32_t
770 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t>
773 if (__str_codecvt_out_all(__f
, __l
, __str
, __cvt
))
775 const char* __f2
= __str
.data();
776 const char* __l2
= __f2
+ __str
.size();
777 std::codecvt_utf8_utf16
<wchar_t> __wcvt
;
778 if (__str_codecvt_in_all(__f2
, __l2
, __wstr
, __wcvt
))
783 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t>
786 if (__str_codecvt_out_all(__f
, __l
, __str
, __cvt
))
789 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
790 "Cannot convert character sequence",
791 std::make_error_code(errc::illegal_byte_sequence
)));
795 _S_convert(_CharT
* __f
, _CharT
* __l
)
797 return _S_convert(const_cast<const _CharT
*>(__f
),
798 const_cast<const _CharT
*>(__l
));
801 template<typename _Iter
>
803 _S_convert(_Iter __first
, _Iter __last
)
805 const std::basic_string
<_CharT
> __str(__first
, __last
);
806 return _S_convert(__str
.data(), __str
.data() + __str
.size());
809 template<typename _Iter
, typename _Cont
>
811 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
812 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
813 { return _S_convert(__first
.base(), __last
.base()); }
818 /// An iterator for the components of a path
822 using difference_type
= std::ptrdiff_t;
823 using value_type
= path
;
824 using reference
= const path
&;
825 using pointer
= const path
*;
826 using iterator_category
= std::bidirectional_iterator_tag
;
828 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
830 iterator(const iterator
&) = default;
831 iterator
& operator=(const iterator
&) = default;
833 reference
operator*() const;
834 pointer
operator->() const { return std::__addressof(**this); }
836 iterator
& operator++();
837 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
839 iterator
& operator--();
840 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
842 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
843 { return __lhs
._M_equals(__rhs
); }
845 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
846 { return !__lhs
._M_equals(__rhs
); }
851 bool _M_is_multi() const { return _M_path
->_M_type() == _Type::_Multi
; }
853 friend difference_type
854 __path_iter_distance(const iterator
& __first
, const iterator
& __last
)
856 __glibcxx_assert(__first
._M_path
!= nullptr);
857 __glibcxx_assert(__first
._M_path
== __last
._M_path
);
858 if (__first
._M_is_multi())
859 return std::distance(__first
._M_cur
, __last
._M_cur
);
860 else if (__first
._M_at_end
== __last
._M_at_end
)
863 return __first
._M_at_end
? -1 : 1;
867 __path_iter_advance(iterator
& __i
, difference_type __n
)
875 __glibcxx_assert(__i
._M_path
!= nullptr);
876 __glibcxx_assert(__i
._M_is_multi());
877 // __glibcxx_assert(__i._M_path->_M_cmpts.end() - __i._M_cur >= __n);
882 iterator(const path
* __path
, path::_List::const_iterator __iter
)
883 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
886 iterator(const path
* __path
, bool __at_end
)
887 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
890 bool _M_equals(iterator
) const;
893 path::_List::const_iterator _M_cur
;
894 bool _M_at_end
; // only used when type != _Multi
899 path::operator=(path
&& __p
) noexcept
901 if (&__p
== this) [[__unlikely__
]]
904 _M_pathname
= std::move(__p
._M_pathname
);
905 _M_cmpts
= std::move(__p
._M_cmpts
);
911 path::operator=(string_type
&& __source
)
912 { return *this = path(std::move(__source
)); }
915 path::assign(string_type
&& __source
)
916 { return *this = path(std::move(__source
)); }
919 path::operator+=(const string_type
& __x
)
926 path::operator+=(const value_type
* __x
)
933 path::operator+=(value_type __x
)
935 _M_concat(basic_string_view
<value_type
>(&__x
, 1));
940 path::operator+=(basic_string_view
<value_type
> __x
)
946 template<typename _CharT
>
947 inline path::_Path
<_CharT
*, _CharT
*>&
948 path::operator+=(_CharT __x
)
950 auto* __addr
= std::__addressof(__x
);
951 return concat(__addr
, __addr
+ 1);
955 path::make_preferred()
957 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
958 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
959 preferred_separator
);
964 inline void path::swap(path
& __rhs
) noexcept
966 _M_pathname
.swap(__rhs
._M_pathname
);
967 _M_cmpts
.swap(__rhs
._M_cmpts
);
970 /// @cond undocumented
971 template<typename _CharT
, typename _Traits
, typename _Allocator
>
972 std::basic_string
<_CharT
, _Traits
, _Allocator
>
973 path::_S_str_convert(const string_type
& __str
, const _Allocator
& __a
)
975 static_assert(!is_same_v
<_CharT
, value_type
>);
977 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
979 if (__str
.size() == 0)
980 return _WString(__a
);
982 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
983 // First convert native string from UTF-16 to to UTF-8.
984 // XXX This assumes that the execution wide-character set is UTF-16.
985 std::codecvt_utf8_utf16
<value_type
> __cvt
;
987 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
988 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
989 _String __u8str
{_CharAlloc
{__a
}};
990 const value_type
* __wfirst
= __str
.data();
991 const value_type
* __wlast
= __wfirst
+ __str
.size();
992 if (__str_codecvt_out_all(__wfirst
, __wlast
, __u8str
, __cvt
)) {
993 if constexpr (is_same_v
<_CharT
, char>)
994 return __u8str
; // XXX assumes native ordinary encoding is UTF-8.
997 const char* __first
= __u8str
.data();
998 const char* __last
= __first
+ __u8str
.size();
1000 const value_type
* __first
= __str
.data();
1001 const value_type
* __last
= __first
+ __str
.size();
1004 // Convert UTF-8 string to requested format.
1005 #ifdef _GLIBCXX_USE_CHAR8_T
1006 if constexpr (is_same_v
<_CharT
, char8_t
>)
1007 return _WString(__first
, __last
, __a
);
1011 // Convert UTF-8 to wide string.
1012 _WString
__wstr(__a
);
1013 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t> { } __cvt
;
1014 if (__str_codecvt_in_all(__first
, __last
, __wstr
, __cvt
))
1018 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1021 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1022 "Cannot convert character sequence",
1023 std::make_error_code(errc::illegal_byte_sequence
)));
1027 template<typename _CharT
, typename _Traits
, typename _Allocator
>
1028 inline basic_string
<_CharT
, _Traits
, _Allocator
>
1029 path::string(const _Allocator
& __a
) const
1031 if constexpr (is_same_v
<_CharT
, value_type
>)
1032 return { _M_pathname
.c_str(), _M_pathname
.length(), __a
};
1034 return _S_str_convert
<_CharT
, _Traits
>(_M_pathname
, __a
);
1038 path::string() const { return string
<char>(); }
1040 #if _GLIBCXX_USE_WCHAR_T
1042 path::wstring() const { return string
<wchar_t>(); }
1045 #ifdef _GLIBCXX_USE_CHAR8_T
1046 inline std::u8string
1047 path::u8string() const { return string
<char8_t
>(); }
1050 path::u8string() const
1052 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1054 // convert from native wide encoding (assumed to be UTF-16) to UTF-8
1055 std::codecvt_utf8_utf16
<value_type
> __cvt
;
1056 const value_type
* __first
= _M_pathname
.data();
1057 const value_type
* __last
= __first
+ _M_pathname
.size();
1058 if (__str_codecvt_out_all(__first
, __last
, __str
, __cvt
))
1060 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1061 "Cannot convert character sequence",
1062 std::make_error_code(errc::illegal_byte_sequence
)));
1067 #endif // _GLIBCXX_USE_CHAR8_T
1069 inline std::u16string
1070 path::u16string() const { return string
<char16_t
>(); }
1072 inline std::u32string
1073 path::u32string() const { return string
<char32_t
>(); }
1075 template<typename _CharT
, typename _Traits
, typename _Allocator
>
1076 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
1077 path::generic_string(const _Allocator
& __a
) const
1079 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1080 const value_type __slash
= L
'/';
1082 const value_type __slash
= '/';
1084 string_type
__str(__a
);
1086 if (_M_type() == _Type::_Root_dir
)
1087 __str
.assign(1, __slash
);
1090 __str
.reserve(_M_pathname
.size());
1091 bool __add_slash
= false;
1092 for (auto& __elem
: *this)
1096 __str
+= __elem
._M_pathname
;
1097 __add_slash
= __elem
._M_type() == _Type::_Filename
;
1101 if constexpr (is_same_v
<_CharT
, value_type
>)
1104 return _S_str_convert
<_CharT
, _Traits
>(__str
, __a
);
1108 path::generic_string() const
1109 { return generic_string
<char>(); }
1111 #if _GLIBCXX_USE_WCHAR_T
1113 path::generic_wstring() const
1114 { return generic_string
<wchar_t>(); }
1117 #ifdef _GLIBCXX_USE_CHAR8_T
1118 inline std::u8string
1119 path::generic_u8string() const
1120 { return generic_string
<char8_t
>(); }
1123 path::generic_u8string() const
1124 { return generic_string(); }
1127 inline std::u16string
1128 path::generic_u16string() const
1129 { return generic_string
<char16_t
>(); }
1131 inline std::u32string
1132 path::generic_u32string() const
1133 { return generic_string
<char32_t
>(); }
1136 path::compare(const string_type
& __s
) const noexcept
1137 { return compare(basic_string_view
<value_type
>(__s
)); }
1140 path::compare(const value_type
* __s
) const noexcept
1141 { return compare(basic_string_view
<value_type
>(__s
)); }
1144 path::filename() const
1148 else if (_M_type() == _Type::_Filename
)
1150 else if (_M_type() == _Type::_Multi
)
1152 if (_M_pathname
.back() == preferred_separator
)
1154 auto& __last
= *--end();
1155 if (__last
._M_type() == _Type::_Filename
)
1164 auto ext
= _M_find_extension();
1165 if (ext
.first
&& ext
.second
!= 0)
1166 return path
{ext
.first
->substr(0, ext
.second
)};
1171 path::extension() const
1173 auto ext
= _M_find_extension();
1174 if (ext
.first
&& ext
.second
!= string_type::npos
)
1175 return path
{ext
.first
->substr(ext
.second
)};
1180 path::has_stem() const noexcept
1182 auto ext
= _M_find_extension();
1183 return ext
.first
&& ext
.second
!= 0;
1187 path::has_extension() const noexcept
1189 auto ext
= _M_find_extension();
1190 return ext
.first
&& ext
.second
!= string_type::npos
;
1194 path::is_absolute() const noexcept
1196 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1197 return has_root_name() && has_root_directory();
1199 return has_root_directory();
1203 inline path::iterator
1206 if (_M_type() == _Type::_Multi
)
1207 return iterator(this, _M_cmpts
.begin());
1208 return iterator(this, empty());
1211 inline path::iterator
1214 if (_M_type() == _Type::_Multi
)
1215 return iterator(this, _M_cmpts
.end());
1216 return iterator(this, true);
1219 inline path::iterator
&
1220 path::iterator::operator++()
1222 __glibcxx_assert(_M_path
!= nullptr);
1223 if (_M_path
->_M_type() == _Type::_Multi
)
1225 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1230 __glibcxx_assert(!_M_at_end
);
1236 inline path::iterator
&
1237 path::iterator::operator--()
1239 __glibcxx_assert(_M_path
!= nullptr);
1240 if (_M_path
->_M_type() == _Type::_Multi
)
1242 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1247 __glibcxx_assert(_M_at_end
);
1253 inline path::iterator::reference
1254 path::iterator::operator*() const
1256 __glibcxx_assert(_M_path
!= nullptr);
1257 if (_M_path
->_M_type() == _Type::_Multi
)
1259 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1266 path::iterator::_M_equals(iterator __rhs
) const
1268 if (_M_path
!= __rhs
._M_path
)
1270 if (_M_path
== nullptr)
1272 if (_M_path
->_M_type() == path::_Type::_Multi
)
1273 return _M_cur
== __rhs
._M_cur
;
1274 return _M_at_end
== __rhs
._M_at_end
;
1277 // @} group filesystem
1278 _GLIBCXX_END_NAMESPACE_CXX11
1279 } // namespace filesystem
1282 distance(filesystem::path::iterator __first
, filesystem::path::iterator __last
)
1283 { return __path_iter_distance(__first
, __last
); }
1285 template<typename _InputIterator
, typename _Distance
>
1287 advance(filesystem::path::iterator
& __i
, _Distance __n
)
1288 { __path_iter_advance(__i
, static_cast<ptrdiff_t>(__n
)); }
1290 extern template class __shared_ptr
<const filesystem::filesystem_error::_Impl
>;
1292 _GLIBCXX_END_NAMESPACE_VERSION
1297 #endif // _GLIBCXX_FS_PATH_H