1 // Class filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2021 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 experimental/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{experimental/filesystem}
30 #ifndef _GLIBCXX_EXPERIMENTAL_FS_PATH_H
31 #define _GLIBCXX_EXPERIMENTAL_FS_PATH_H 1
33 #if __cplusplus < 201103L
34 # include <bits/c++0x_warning.h>
38 #include <type_traits>
43 #include <system_error>
44 #include <bits/stl_algobase.h>
45 #include <bits/quoted_string.h>
46 #include <bits/locale_conv.h>
47 #if __cplusplus == 201402L
48 # include <experimental/string_view>
51 #if defined(_WIN32) && !defined(__CYGWIN__)
52 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
56 namespace std
_GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 namespace experimental
66 _GLIBCXX_BEGIN_NAMESPACE_CXX11
68 #if __cplusplus == 201402L
69 using std::experimental::basic_string_view
;
70 #elif __cplusplus > 201402L
71 using std::basic_string_view
;
74 /** @addtogroup filesystem-ts
78 /// @cond undocumented
81 template<typename _CharT
,
82 typename _Ch
= typename remove_const
<_CharT
>::type
>
83 using __is_encoded_char
84 = __or_
<is_same
<_Ch
, char>,
85 is_same
<_Ch
, wchar_t>,
86 #ifdef _GLIBCXX_USE_CHAR8_T
87 is_same
<_Ch
, char8_t
>,
89 is_same
<_Ch
, char16_t
>,
90 is_same
<_Ch
, char32_t
>>;
92 template<typename _Iter
,
93 typename _Iter_traits
= std::iterator_traits
<_Iter
>>
94 using __is_path_iter_src
95 = __and_
<__is_encoded_char
<typename
_Iter_traits::value_type
>,
96 std::is_base_of
<std::input_iterator_tag
,
97 typename
_Iter_traits::iterator_category
>>;
99 template<typename _Iter
>
100 static __is_path_iter_src
<_Iter
>
101 __is_path_src(_Iter
, int);
103 template<typename _CharT
, typename _Traits
, typename _Alloc
>
104 static __is_encoded_char
<_CharT
>
105 __is_path_src(const basic_string
<_CharT
, _Traits
, _Alloc
>&, int);
107 #if __cplusplus >= 201402L
108 template<typename _CharT
, typename _Traits
>
109 static __is_encoded_char
<_CharT
>
110 __is_path_src(const basic_string_view
<_CharT
, _Traits
>&, int);
113 template<typename _Unknown
>
114 static std::false_type
115 __is_path_src(const _Unknown
&, ...);
117 template<typename _Tp1
, typename _Tp2
>
118 struct __constructible_from
;
120 template<typename _Iter
>
121 struct __constructible_from
<_Iter
, _Iter
>
122 : __is_path_iter_src
<_Iter
>
125 template<typename _Source
>
126 struct __constructible_from
<_Source
, void>
127 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
130 template<typename _Tp1
, typename _Tp2
= void,
131 typename _Tp1_nocv
= typename remove_cv
<_Tp1
>::type
,
132 typename _Tp1_noptr
= typename remove_pointer
<_Tp1
>::type
>
133 using _Path
= typename
134 std::enable_if
<__and_
<__not_
<is_same
<_Tp1_nocv
, path
>>,
135 __not_
<is_void
<_Tp1_noptr
>>,
136 __constructible_from
<_Tp1
, _Tp2
>>::value
,
139 template<typename _Source
>
141 _S_range_begin(_Source __begin
) { return __begin
; }
143 struct __null_terminated
{ };
145 template<typename _Source
>
146 inline __null_terminated
147 _S_range_end(_Source
) { return {}; }
149 template<typename _CharT
, typename _Traits
, typename _Alloc
>
151 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
152 { return __str
.data(); }
154 template<typename _CharT
, typename _Traits
, typename _Alloc
>
156 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
157 { return __str
.data() + __str
.size(); }
159 #if __cplusplus >= 201402L
160 template<typename _CharT
, typename _Traits
>
162 _S_range_begin(const basic_string_view
<_CharT
, _Traits
>& __str
)
163 { return __str
.data(); }
165 template<typename _CharT
, typename _Traits
>
167 _S_range_end(const basic_string_view
<_CharT
, _Traits
>& __str
)
168 { return __str
.data() + __str
.size(); }
171 template<typename _Tp
,
172 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
173 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
,
174 typename _UnqualVal
= typename
std::remove_const
<_Val
>::type
>
175 using __value_type_is_char
= typename
std::enable_if
<
176 std::is_same
<_UnqualVal
, char>::value
,
179 template<typename _Tp
,
180 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
181 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
,
182 typename _UnqualVal
= typename
std::remove_const
<_Val
>::type
>
183 using __value_type_is_char_or_char8_t
= typename
std::enable_if
<
185 std::is_same
<_UnqualVal
, char>
186 #ifdef _GLIBCXX_USE_CHAR8_T
187 ,std::is_same
<_UnqualVal
, char8_t
>
189 >::value
, _UnqualVal
>::type
;
191 } // namespace __detail
194 /// A filesystem path.
198 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
199 typedef wchar_t value_type
;
200 static constexpr value_type preferred_separator
= L
'\\';
202 typedef char value_type
;
203 static constexpr value_type preferred_separator
= '/';
205 typedef std::basic_string
<value_type
> string_type
;
207 // constructors and destructor
211 path(const path
& __p
) = default;
213 path(path
&& __p
) noexcept
214 : _M_pathname(std::move(__p
._M_pathname
)), _M_type(__p
._M_type
)
216 if (_M_type
== _Type::_Multi
)
221 path(string_type
&& __source
)
222 : _M_pathname(std::move(__source
))
223 { _M_split_cmpts(); }
225 template<typename _Source
,
226 typename _Require
= __detail::_Path
<_Source
>>
227 path(_Source
const& __source
)
228 : _M_pathname(_S_convert(__detail::_S_range_begin(__source
),
229 __detail::_S_range_end(__source
)))
230 { _M_split_cmpts(); }
232 template<typename _InputIterator
,
233 typename _Require
= __detail::_Path
<_InputIterator
, _InputIterator
>>
234 path(_InputIterator __first
, _InputIterator __last
)
235 : _M_pathname(_S_convert(__first
, __last
))
236 { _M_split_cmpts(); }
238 template<typename _Source
,
239 typename _Require
= __detail::_Path
<_Source
>,
240 typename _Require2
= __detail::__value_type_is_char
<_Source
>>
241 path(_Source
const& __source
, const locale
& __loc
)
242 : _M_pathname(_S_convert_loc(__detail::_S_range_begin(__source
),
243 __detail::_S_range_end(__source
), __loc
))
244 { _M_split_cmpts(); }
246 template<typename _InputIterator
,
247 typename _Require
= __detail::_Path
<_InputIterator
, _InputIterator
>,
248 typename _Require2
= __detail::__value_type_is_char
<_InputIterator
>>
249 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
)
250 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
251 { _M_split_cmpts(); }
257 path
& operator=(const path
& __p
) = default;
258 path
& operator=(path
&& __p
) noexcept
;
259 path
& operator=(string_type
&& __source
);
260 path
& assign(string_type
&& __source
);
262 template<typename _Source
>
263 __detail::_Path
<_Source
>&
264 operator=(_Source
const& __source
)
265 { return *this = path(__source
); }
267 template<typename _Source
>
268 __detail::_Path
<_Source
>&
269 assign(_Source
const& __source
)
270 { return *this = path(__source
); }
272 template<typename _InputIterator
>
273 __detail::_Path
<_InputIterator
, _InputIterator
>&
274 assign(_InputIterator __first
, _InputIterator __last
)
275 { return *this = path(__first
, __last
); }
279 path
& operator/=(const path
& __p
) { return _M_append(__p
._M_pathname
); }
281 template<typename _Source
>
282 __detail::_Path
<_Source
>&
283 operator/=(_Source
const& __source
)
284 { return append(__source
); }
286 template<typename _Source
>
287 __detail::_Path
<_Source
>&
288 append(_Source
const& __source
)
290 return _M_append(_S_convert(__detail::_S_range_begin(__source
),
291 __detail::_S_range_end(__source
)));
294 template<typename _InputIterator
>
295 __detail::_Path
<_InputIterator
, _InputIterator
>&
296 append(_InputIterator __first
, _InputIterator __last
)
297 { return _M_append(_S_convert(__first
, __last
)); }
301 path
& operator+=(const path
& __x
);
302 path
& operator+=(const string_type
& __x
);
303 path
& operator+=(const value_type
* __x
);
304 path
& operator+=(value_type __x
);
305 #if __cplusplus >= 201402L
306 path
& operator+=(basic_string_view
<value_type
> __x
);
309 template<typename _Source
>
310 __detail::_Path
<_Source
>&
311 operator+=(_Source
const& __x
) { return concat(__x
); }
313 template<typename _CharT
>
314 __detail::_Path
<_CharT
*, _CharT
*>&
315 operator+=(_CharT __x
);
317 template<typename _Source
>
318 __detail::_Path
<_Source
>&
319 concat(_Source
const& __x
)
321 return *this += _S_convert(__detail::_S_range_begin(__x
),
322 __detail::_S_range_end(__x
));
325 template<typename _InputIterator
>
326 __detail::_Path
<_InputIterator
, _InputIterator
>&
327 concat(_InputIterator __first
, _InputIterator __last
)
328 { return *this += _S_convert(__first
, __last
); }
332 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
334 path
& make_preferred();
335 path
& remove_filename();
336 path
& replace_filename(const path
& __replacement
);
337 path
& replace_extension(const path
& __replacement
= path());
339 void swap(path
& __rhs
) noexcept
;
341 // native format observers
343 const string_type
& native() const noexcept
{ return _M_pathname
; }
344 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
345 operator string_type() const { return _M_pathname
; }
347 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
348 typename _Allocator
= std::allocator
<_CharT
>>
349 std::basic_string
<_CharT
, _Traits
, _Allocator
>
350 string(const _Allocator
& __a
= _Allocator()) const;
352 std::string
string() const;
353 #if _GLIBCXX_USE_WCHAR_T
354 std::wstring
wstring() const;
356 #ifdef _GLIBCXX_USE_CHAR8_T
357 __attribute__((__abi_tag__("__u8")))
358 std::u8string
u8string() const;
360 std::string
u8string() const;
361 #endif // _GLIBCXX_USE_CHAR8_T
362 std::u16string
u16string() const;
363 std::u32string
u32string() const;
365 // generic format observers
366 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
367 typename _Allocator
= std::allocator
<_CharT
>>
368 std::basic_string
<_CharT
, _Traits
, _Allocator
>
369 generic_string(const _Allocator
& __a
= _Allocator()) const;
371 std::string
generic_string() const;
372 #if _GLIBCXX_USE_WCHAR_T
373 std::wstring
generic_wstring() const;
375 #ifdef _GLIBCXX_USE_CHAR8_T
376 __attribute__((__abi_tag__("__u8")))
377 std::u8string
generic_u8string() const;
379 std::string
generic_u8string() const;
380 #endif // _GLIBCXX_USE_CHAR8_T
381 std::u16string
generic_u16string() const;
382 std::u32string
generic_u32string() const;
386 int compare(const path
& __p
) const noexcept
;
387 int compare(const string_type
& __s
) const;
388 int compare(const value_type
* __s
) const;
389 #if __cplusplus >= 201402L
390 int compare(const basic_string_view
<value_type
> __s
) const;
395 path
root_name() const;
396 path
root_directory() const;
397 path
root_path() const;
398 path
relative_path() const;
399 path
parent_path() const;
400 path
filename() const;
402 path
extension() const;
406 _GLIBCXX_NODISCARD
bool empty() const noexcept
{ return _M_pathname
.empty(); }
407 bool has_root_name() const;
408 bool has_root_directory() const;
409 bool has_root_path() const;
410 bool has_relative_path() const;
411 bool has_parent_path() const;
412 bool has_filename() const;
413 bool has_stem() const;
414 bool has_extension() const;
415 bool is_absolute() const;
416 bool is_relative() const { return !is_absolute(); }
420 typedef iterator const_iterator
;
422 iterator
begin() const;
423 iterator
end() const;
425 /// @cond undocumented
426 // Create a basic_string by reading until a null character.
427 template<typename _InputIterator
,
428 typename _Traits
= std::iterator_traits
<_InputIterator
>,
430 = typename
std::remove_cv
<typename
_Traits::value_type
>::type
>
431 static std::basic_string
<_CharT
>
432 _S_string_from_iter(_InputIterator __source
)
434 std::basic_string
<_CharT
> __str
;
435 for (_CharT __ch
= *__source
; __ch
!= _CharT(); __ch
= *++__source
)
436 __str
.push_back(__ch
);
442 enum class _Type
: unsigned char {
443 _Multi
, _Root_name
, _Root_dir
, _Filename
446 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
448 __glibcxx_assert(!empty());
449 __glibcxx_assert(_M_type
!= _Type::_Multi
);
452 enum class _Split
{ _Stem
, _Extension
};
454 path
& _M_append(const string_type
& __str
)
456 if (!_M_pathname
.empty() && !_S_is_dir_sep(_M_pathname
.back())
457 && !__str
.empty() && !_S_is_dir_sep(__str
.front()))
458 _M_pathname
+= preferred_separator
;
459 _M_pathname
+= __str
;
464 pair
<const string_type
*, size_t> _M_find_extension() const;
466 template<typename _CharT
>
470 _S_convert(value_type
* __src
, __detail::__null_terminated
)
471 { return string_type(__src
); }
474 _S_convert(const value_type
* __src
, __detail::__null_terminated
)
475 { return string_type(__src
); }
477 template<typename _Iter
>
479 _S_convert(_Iter __first
, _Iter __last
)
481 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
482 return _Cvt
<typename remove_cv
<__value_type
>::type
>::
483 _S_convert(__first
, __last
);
486 template<typename _InputIterator
>
488 _S_convert(_InputIterator __src
, __detail::__null_terminated
)
490 auto __s
= _S_string_from_iter(__src
);
491 return _S_convert(__s
.c_str(), __s
.c_str() + __s
.size());
495 _S_convert_loc(const char* __first
, const char* __last
,
496 const std::locale
& __loc
);
499 _S_convert_loc(char* __first
, char* __last
, const std::locale
& __loc
)
501 return _S_convert_loc(const_cast<const char*>(__first
),
502 const_cast<const char*>(__last
), __loc
);
505 template<typename _Iter
>
507 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
509 const std::string
__str(__first
, __last
);
510 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
513 template<typename _InputIterator
>
515 _S_convert_loc(_InputIterator __src
, __detail::__null_terminated
,
516 const std::locale
& __loc
)
518 const std::string __s
= _S_string_from_iter(__src
);
519 return _S_convert_loc(__s
.data(), __s
.data() + __s
.size(), __loc
);
522 static bool _S_is_dir_sep(value_type __ch
)
524 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
525 return __ch
== L
'/' || __ch
== preferred_separator
;
531 void _M_split_cmpts();
533 void _M_add_root_name(size_t __n
);
534 void _M_add_root_dir(size_t __pos
);
535 void _M_add_filename(size_t __pos
, size_t __n
);
537 string_type _M_pathname
;
540 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
541 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
542 _Type _M_type
= _Type::_Multi
;
545 /// @relates std::experimental::filesystem::path @{
547 /// Swap overload for paths
548 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
550 /// Compute a hash value for a path
551 size_t hash_value(const path
& __p
) noexcept
;
554 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
555 { return __lhs
.compare(__rhs
) < 0; }
558 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
559 { return !(__rhs
< __lhs
); }
562 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
563 { return __rhs
< __lhs
; }
566 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
567 { return !(__lhs
< __rhs
); }
570 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
571 { return __lhs
.compare(__rhs
) == 0; }
574 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
575 { return !(__lhs
== __rhs
); }
577 /// Append one path to another
578 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
580 path
__result(__lhs
);
585 /// Write a path to a stream
586 template<typename _CharT
, typename _Traits
>
587 basic_ostream
<_CharT
, _Traits
>&
588 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
590 auto __tmp
= __p
.string
<_CharT
, _Traits
>();
591 using __quoted_string
592 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
593 __os
<< __quoted_string
{__tmp
, _CharT('"'), _CharT('\\')};
597 /// Read a path from a stream
598 template<typename _CharT
, typename _Traits
>
599 basic_istream
<_CharT
, _Traits
>&
600 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
602 basic_string
<_CharT
, _Traits
> __tmp
;
603 using __quoted_string
604 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
605 if (__is
>> __quoted_string
{ __tmp
, _CharT('"'), _CharT('\\') })
606 __p
= std::move(__tmp
);
610 /// Create a path from a UTF-8-encoded sequence of char
611 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
612 template<typename _InputIterator
>
614 __u8path(_InputIterator __first
, _InputIterator __last
, char)
616 // XXX This assumes native wide encoding is UTF-16.
617 std::codecvt_utf8_utf16
<path::value_type
> __cvt
;
618 path::string_type __tmp
;
619 const std::string __u8str
{__first
, __last
};
620 const char* const __ptr
= __u8str
.data();
621 if (__str_codecvt_in_all(__ptr
, __ptr
+ __u8str
.size(), __tmp
, __cvt
))
622 return path
{ __tmp
};
623 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
624 "Cannot convert character sequence",
625 std::make_error_code(errc::illegal_byte_sequence
)));
628 #ifdef _GLIBCXX_USE_CHAR8_T
629 template<typename _InputIterator
>
631 __u8path(_InputIterator __first
, _InputIterator __last
, char8_t
)
633 return path
{ __first
, __last
};
635 #endif // _GLIBCXX_USE_CHAR8_T
636 #endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS
638 template<typename _InputIterator
,
639 typename _Require
= __detail::_Path
<_InputIterator
, _InputIterator
>,
641 __detail::__value_type_is_char_or_char8_t
<_InputIterator
>>
643 u8path(_InputIterator __first
, _InputIterator __last
)
645 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
646 return __u8path(__first
, __last
, _CharT
{});
648 return path
{ __first
, __last
};
652 /// Create a path from a UTF-8-encoded sequence of char
653 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
655 __u8path(const string
& __s
, char)
657 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
660 template<typename _Source
>
661 inline __enable_if_t
<is_convertible
<const _Source
&, string
>::value
, path
>
662 __u8path(const _Source
& __source
, char)
664 std::string __s
= __source
;
665 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
668 template<typename _Source
>
669 inline __enable_if_t
<!is_convertible
<const _Source
&, string
>::value
, path
>
670 __u8path(const _Source
& __source
, char)
672 std::string __s
= path::_S_string_from_iter(__source
);
673 return filesystem::u8path(__s
.data(), __s
.data() + __s
.size());
676 #ifdef _GLIBCXX_USE_CHAR8_T
677 template<typename _Source
>
679 __u8path(const _Source
& __source
, char8_t
)
681 return path
{ __source
};
683 #endif // _GLIBCXX_USE_CHAR8_T
684 #endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS
686 template<typename _Source
,
687 typename _Require
= __detail::_Path
<_Source
>,
689 __detail::__value_type_is_char_or_char8_t
<_Source
>>
691 u8path(const _Source
& __source
)
693 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
694 return __u8path(__source
, _CharT
{});
696 return path
{ __source
};
702 /// Exception type thrown by the Filesystem TS library
703 class filesystem_error
: public std::system_error
706 filesystem_error(const string
& __what_arg
, error_code __ec
)
707 : system_error(__ec
, __what_arg
) { }
709 filesystem_error(const string
& __what_arg
, const path
& __p1
,
711 : system_error(__ec
, __what_arg
), _M_path1(__p1
) { }
713 filesystem_error(const string
& __what_arg
, const path
& __p1
,
714 const path
& __p2
, error_code __ec
)
715 : system_error(__ec
, __what_arg
), _M_path1(__p1
), _M_path2(__p2
)
720 const path
& path1() const noexcept
{ return _M_path1
; }
721 const path
& path2() const noexcept
{ return _M_path2
; }
722 const char* what() const noexcept
{ return _M_what
.c_str(); }
725 std::string
_M_gen_what();
729 std::string _M_what
= _M_gen_what();
732 /// @cond undocumented
733 struct path::_Cmpt
: path
735 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
736 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
738 _Cmpt() : _M_pos(-1) { }
743 // specialize _Cvt for degenerate 'noconv' case
745 struct path::_Cvt
<path::value_type
>
747 template<typename _Iter
>
749 _S_convert(_Iter __first
, _Iter __last
)
750 { return string_type
{__first
, __last
}; }
753 template<typename _CharT
>
756 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
757 #ifdef _GLIBCXX_USE_CHAR8_T
759 _S_wconvert(const char8_t
* __f
, const char8_t
* __l
, const char8_t
*)
761 const char* __f2
= (const char*)__f
;
762 const char* __l2
= (const char*)__l
;
764 std::codecvt_utf8_utf16
<wchar_t> __wcvt
;
765 if (__str_codecvt_in_all(__f2
, __l2
, __wstr
, __wcvt
))
771 _S_wconvert(const char* __f
, const char* __l
, const char*)
773 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
774 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
776 if (__str_codecvt_in_all(__f
, __l
, __wstr
, __cvt
))
778 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
779 "Cannot convert character sequence",
780 std::make_error_code(errc::illegal_byte_sequence
)));
784 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, const void*)
786 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t>
789 if (__str_codecvt_out_all(__f
, __l
, __str
, __cvt
))
791 const char* __f2
= __str
.data();
792 const char* __l2
= __f2
+ __str
.size();
793 std::codecvt_utf8_utf16
<wchar_t> __wcvt
;
795 if (__str_codecvt_in_all(__f2
, __l2
, __wstr
, __wcvt
))
798 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
799 "Cannot convert character sequence",
800 std::make_error_code(errc::illegal_byte_sequence
)));
804 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
806 return _S_wconvert(__f
, __l
, (const _CharT
*)nullptr);
810 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
812 #ifdef _GLIBCXX_USE_CHAR8_T
813 if constexpr (is_same
<_CharT
, char8_t
>::value
)
814 return string_type(__f
, __l
);
818 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t>
821 if (__str_codecvt_out_all(__f
, __l
, __str
, __cvt
))
823 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
824 "Cannot convert character sequence",
825 std::make_error_code(errc::illegal_byte_sequence
)));
831 _S_convert(_CharT
* __f
, _CharT
* __l
)
833 return _S_convert(const_cast<const _CharT
*>(__f
),
834 const_cast<const _CharT
*>(__l
));
837 template<typename _Iter
>
839 _S_convert(_Iter __first
, _Iter __last
)
841 const std::basic_string
<_CharT
> __str(__first
, __last
);
842 return _S_convert(__str
.data(), __str
.data() + __str
.size());
845 template<typename _Iter
, typename _Cont
>
847 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
848 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
849 { return _S_convert(__first
.base(), __last
.base()); }
853 /// An iterator for the components of a path
857 using difference_type
= std::ptrdiff_t;
858 using value_type
= path
;
859 using reference
= const path
&;
860 using pointer
= const path
*;
861 using iterator_category
= std::bidirectional_iterator_tag
;
863 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
865 iterator(const iterator
&) = default;
866 iterator
& operator=(const iterator
&) = default;
868 reference
operator*() const;
869 pointer
operator->() const { return std::__addressof(**this); }
871 iterator
& operator++();
872 iterator
operator++(int) { auto __tmp
= *this; ++*this; return __tmp
; }
874 iterator
& operator--();
875 iterator
operator--(int) { auto __tmp
= *this; --*this; return __tmp
; }
877 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
878 { return __lhs
._M_equals(__rhs
); }
880 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
881 { return !__lhs
._M_equals(__rhs
); }
886 iterator(const path
* __path
, path::_List::const_iterator __iter
)
887 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
890 iterator(const path
* __path
, bool __at_end
)
891 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
894 bool _M_equals(iterator
) const;
897 path::_List::const_iterator _M_cur
;
898 bool _M_at_end
; // only used when type != _Multi
903 path::operator=(path
&& __p
) noexcept
905 _M_pathname
= std::move(__p
._M_pathname
);
906 _M_cmpts
= std::move(__p
._M_cmpts
);
907 _M_type
= __p
._M_type
;
913 path::operator=(string_type
&& __source
)
914 { return *this = path(std::move(__source
)); }
917 path::assign(string_type
&& __source
)
918 { return *this = path(std::move(__source
)); }
921 path::operator+=(const path
& __p
)
923 return operator+=(__p
.native());
927 path::operator+=(const string_type
& __x
)
935 path::operator+=(const value_type
* __x
)
943 path::operator+=(value_type __x
)
950 #if __cplusplus >= 201402L
952 path::operator+=(basic_string_view
<value_type
> __x
)
954 _M_pathname
.append(__x
.data(), __x
.size());
960 template<typename _CharT
>
961 inline __detail::_Path
<_CharT
*, _CharT
*>&
962 path::operator+=(_CharT __x
)
964 auto* __addr
= std::__addressof(__x
);
965 return concat(__addr
, __addr
+ 1);
969 path::make_preferred()
971 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
972 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
973 preferred_separator
);
978 inline void path::swap(path
& __rhs
) noexcept
980 _M_pathname
.swap(__rhs
._M_pathname
);
981 _M_cmpts
.swap(__rhs
._M_cmpts
);
982 std::swap(_M_type
, __rhs
._M_type
);
985 template<typename _CharT
, typename _Traits
, typename _Allocator
>
986 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
987 path::string(const _Allocator
& __a
) const
989 if (is_same
<_CharT
, value_type
>::value
)
990 return { _M_pathname
.begin(), _M_pathname
.end(), __a
};
992 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
994 const value_type
* __first
= _M_pathname
.data();
995 const value_type
* __last
= __first
+ _M_pathname
.size();
997 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
998 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
999 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
1001 // First convert native string from UTF-16 to to UTF-8.
1002 // XXX This assumes that the execution wide-character set is UTF-16.
1003 codecvt_utf8_utf16
<value_type
> __cvt
;
1004 _String __u8str
{_CharAlloc
{__a
}};
1005 if (__str_codecvt_out_all(__first
, __last
, __u8str
, __cvt
))
1010 operator()(const _String
& __from
, _String
&, true_type
)
1011 { return std::__addressof(__from
); }
1014 operator()(const _String
& __from
, _WString
& __to
, false_type
)
1016 #ifdef _GLIBCXX_USE_CHAR8_T
1017 if constexpr (is_same
<_CharT
, char8_t
>::value
)
1019 __to
.assign(__from
.begin(), __from
.end());
1020 return std::__addressof(__to
);
1025 // Convert UTF-8 to wide string.
1026 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t>
1028 const char* __f
= __from
.data();
1029 const char* __l
= __f
+ __from
.size();
1030 if (__str_codecvt_in_all(__f
, __l
, __to
, __cvt
))
1031 return std::__addressof(__to
);
1036 _WString
__wstr(__a
);
1037 if (auto* __p
= __dispatch(__u8str
, __wstr
, is_same
<_CharT
, char>{}))
1041 #ifdef _GLIBCXX_USE_CHAR8_T
1042 if constexpr (is_same
<_CharT
, char8_t
>::value
)
1043 return _WString(__first
, __last
, __a
);
1047 struct _UCvt
: std::codecvt
<_CharT
, char, std::mbstate_t> { } __cvt
;
1048 _WString
__wstr(__a
);
1049 if (__str_codecvt_in_all(__first
, __last
, __wstr
, __cvt
))
1053 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1054 "Cannot convert character sequence",
1055 std::make_error_code(errc::illegal_byte_sequence
)));
1059 path::string() const { return string
<char>(); }
1061 #if _GLIBCXX_USE_WCHAR_T
1063 path::wstring() const { return string
<wchar_t>(); }
1066 #ifdef _GLIBCXX_USE_CHAR8_T
1067 inline std::u8string
1068 path::u8string() const { return string
<char8_t
>(); }
1071 path::u8string() const
1073 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1075 // convert from native wide encoding (assumed to be UTF-16) to UTF-8
1076 std::codecvt_utf8_utf16
<value_type
> __cvt
;
1077 const value_type
* __first
= _M_pathname
.data();
1078 const value_type
* __last
= __first
+ _M_pathname
.size();
1079 if (__str_codecvt_out_all(__first
, __last
, __str
, __cvt
))
1081 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1082 "Cannot convert character sequence",
1083 std::make_error_code(errc::illegal_byte_sequence
)));
1088 #endif // _GLIBCXX_USE_CHAR8_T
1090 inline std::u16string
1091 path::u16string() const { return string
<char16_t
>(); }
1093 inline std::u32string
1094 path::u32string() const { return string
<char32_t
>(); }
1096 template<typename _CharT
, typename _Traits
, typename _Allocator
>
1097 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
1098 path::generic_string(const _Allocator
& __a
) const
1100 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1101 const _CharT __slash
= is_same
<_CharT
, wchar_t>::value
1103 : _CharT('/'); // Assume value is correct for the encoding.
1105 const _CharT __slash
= _CharT('/');
1107 basic_string
<_CharT
, _Traits
, _Allocator
> __str(__a
);
1108 __str
.reserve(_M_pathname
.size());
1109 bool __add_slash
= false;
1110 for (auto& __elem
: *this)
1112 if (__elem
._M_type
== _Type::_Root_dir
)
1119 __str
+= __elem
.string
<_CharT
, _Traits
, _Allocator
>(__a
);
1120 __add_slash
= __elem
._M_type
== _Type::_Filename
;
1126 path::generic_string() const { return generic_string
<char>(); }
1128 #if _GLIBCXX_USE_WCHAR_T
1130 path::generic_wstring() const { return generic_string
<wchar_t>(); }
1133 #ifdef _GLIBCXX_USE_CHAR8_T
1134 inline std::u8string
1135 path::generic_u8string() const { return generic_string
<char8_t
>(); }
1138 path::generic_u8string() const { return generic_string
<char>(); }
1141 inline std::u16string
1142 path::generic_u16string() const { return generic_string
<char16_t
>(); }
1144 inline std::u32string
1145 path::generic_u32string() const { return generic_string
<char32_t
>(); }
1148 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
1151 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
1153 #if __cplusplus >= 201402L
1155 path::compare(basic_string_view
<value_type
> __s
) const
1156 { return compare(path(__s
)); }
1160 path::filename() const { return empty() ? path() : *--end(); }
1165 auto ext
= _M_find_extension();
1166 if (ext
.first
&& ext
.second
!= 0)
1167 return path
{ext
.first
->substr(0, ext
.second
)};
1172 path::extension() const
1174 auto ext
= _M_find_extension();
1175 if (ext
.first
&& ext
.second
!= string_type::npos
)
1176 return path
{ext
.first
->substr(ext
.second
)};
1181 path::has_stem() const
1183 auto ext
= _M_find_extension();
1184 return ext
.first
&& ext
.second
!= 0;
1188 path::has_extension() const
1190 auto ext
= _M_find_extension();
1191 return ext
.first
&& ext
.second
!= string_type::npos
;
1195 path::is_absolute() const
1197 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1198 return has_root_name() && has_root_directory();
1200 return has_root_directory();
1204 inline path::iterator
1207 if (_M_type
== _Type::_Multi
)
1208 return iterator(this, _M_cmpts
.begin());
1209 return iterator(this, false);
1212 inline path::iterator
1215 if (_M_type
== _Type::_Multi
)
1216 return iterator(this, _M_cmpts
.end());
1217 return iterator(this, true);
1220 inline path::iterator
&
1221 path::iterator::operator++()
1223 __glibcxx_assert(_M_path
!= nullptr);
1224 if (_M_path
->_M_type
== _Type::_Multi
)
1226 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1231 __glibcxx_assert(!_M_at_end
);
1237 inline path::iterator
&
1238 path::iterator::operator--()
1240 __glibcxx_assert(_M_path
!= nullptr);
1241 if (_M_path
->_M_type
== _Type::_Multi
)
1243 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
1248 __glibcxx_assert(_M_at_end
);
1254 inline path::iterator::reference
1255 path::iterator::operator*() const
1257 __glibcxx_assert(_M_path
!= nullptr);
1258 if (_M_path
->_M_type
== _Type::_Multi
)
1260 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1267 path::iterator::_M_equals(iterator __rhs
) const
1269 if (_M_path
!= __rhs
._M_path
)
1271 if (_M_path
== nullptr)
1273 if (_M_path
->_M_type
== path::_Type::_Multi
)
1274 return _M_cur
== __rhs
._M_cur
;
1275 return _M_at_end
== __rhs
._M_at_end
;
1278 // @} group filesystem-ts
1279 _GLIBCXX_END_NAMESPACE_CXX11
1281 } // namespace filesystem
1282 } // namespace experimental
1284 _GLIBCXX_END_NAMESPACE_VERSION
1289 #endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H