1 // Class filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2016 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>
48 #if defined(_WIN32) && !defined(__CYGWIN__)
49 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
53 namespace std
_GLIBCXX_VISIBILITY(default)
55 namespace experimental
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 _GLIBCXX_BEGIN_NAMESPACE_CXX11
69 /// A filesystem path.
72 template<typename _CharT
>
73 struct __is_encoded_char
: std::false_type
{ };
75 template<typename _Iter
,
76 typename _Iter_traits
= std::iterator_traits
<_Iter
>>
77 using __is_path_iter_src
78 = __and_
<__is_encoded_char
<typename
_Iter_traits::value_type
>,
79 std::is_base_of
<std::input_iterator_tag
,
80 typename
_Iter_traits::iterator_category
>>;
82 template<typename _Iter
>
83 static __is_path_iter_src
<_Iter
>
84 __is_path_src(_Iter
, int);
86 template<typename _CharT
, typename _Traits
, typename _Alloc
>
87 static __is_encoded_char
<_CharT
>
88 __is_path_src(const basic_string
<_CharT
, _Traits
, _Alloc
>&, int);
90 template<typename _Unknown
>
91 static std::false_type
92 __is_path_src(const _Unknown
&, ...);
94 template<typename _Tp1
, typename _Tp2
>
95 struct __constructible_from
;
97 template<typename _Iter
>
98 struct __constructible_from
<_Iter
, _Iter
>
99 : __is_path_iter_src
<_Iter
>
102 template<typename _Source
>
103 struct __constructible_from
<_Source
, void>
104 : decltype(__is_path_src(std::declval
<_Source
>(), 0))
107 template<typename _Tp1
, typename _Tp2
= void>
108 using _Path
= typename
109 std::enable_if
<__and_
<__not_
<is_same
<_Tp1
, path
>>,
110 __constructible_from
<_Tp1
, _Tp2
>>::value
,
113 template<typename _Source
>
115 _S_range_begin(_Source __begin
) { return __begin
; }
117 struct __null_terminated
{ };
119 template<typename _Source
>
120 static __null_terminated
121 _S_range_end(_Source
) { return {}; }
123 template<typename _CharT
, typename _Traits
, typename _Alloc
>
125 _S_range_begin(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
126 { return __str
.data(); }
128 template<typename _CharT
, typename _Traits
, typename _Alloc
>
130 _S_range_end(const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
131 { return __str
.data() + __str
.size(); }
133 template<typename _Tp
,
134 typename _Iter
= decltype(_S_range_begin(std::declval
<_Tp
>())),
135 typename _Val
= typename
std::iterator_traits
<_Iter
>::value_type
>
136 using __value_type_is_char
137 = typename
std::enable_if
<std::is_same
<_Val
, char>::value
>::type
;
140 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
141 typedef wchar_t value_type
;
142 static constexpr value_type preferred_separator
= L
'\\';
144 typedef char value_type
;
145 static constexpr value_type preferred_separator
= '/';
147 typedef std::basic_string
<value_type
> string_type
;
149 // constructors and destructor
153 path(const path
& __p
) = default;
155 path(path
&& __p
) noexcept
156 : _M_pathname(std::move(__p
._M_pathname
)), _M_type(__p
._M_type
)
162 template<typename _Source
,
163 typename _Require
= _Path
<_Source
>>
164 path(_Source
const& __source
)
165 : _M_pathname(_S_convert(_S_range_begin(__source
),
166 _S_range_end(__source
)))
167 { _M_split_cmpts(); }
169 template<typename _InputIterator
,
170 typename _Require
= _Path
<_InputIterator
, _InputIterator
>>
171 path(_InputIterator __first
, _InputIterator __last
)
172 : _M_pathname(_S_convert(__first
, __last
))
173 { _M_split_cmpts(); }
175 template<typename _Source
,
176 typename _Require
= _Path
<_Source
>,
177 typename _Require2
= __value_type_is_char
<_Source
>>
178 path(_Source
const& __source
, const locale
& __loc
)
179 : _M_pathname(_S_convert_loc(_S_range_begin(__source
),
180 _S_range_end(__source
), __loc
))
181 { _M_split_cmpts(); }
183 template<typename _InputIterator
,
184 typename _Require
= _Path
<_InputIterator
, _InputIterator
>,
185 typename _Require2
= __value_type_is_char
<_InputIterator
>>
186 path(_InputIterator __first
, _InputIterator __last
, const locale
& __loc
)
187 : _M_pathname(_S_convert_loc(__first
, __last
, __loc
))
188 { _M_split_cmpts(); }
194 path
& operator=(const path
& __p
) = default;
195 path
& operator=(path
&& __p
) noexcept
;
197 template<typename _Source
>
199 operator=(_Source
const& __source
)
200 { return *this = path(__source
); }
202 template<typename _Source
>
204 assign(_Source
const& __source
)
205 { return *this = path(__source
); }
207 template<typename _InputIterator
>
208 _Path
<_InputIterator
, _InputIterator
>&
209 assign(_InputIterator __first
, _InputIterator __last
)
210 { return *this = path(__first
, __last
); }
214 path
& operator/=(const path
& __p
) { return _M_append(__p
._M_pathname
); }
216 template <class _Source
>
218 operator/=(_Source
const& __source
)
219 { return append(__source
); }
221 template<typename _Source
>
223 append(_Source
const& __source
)
225 return _M_append(_S_convert(_S_range_begin(__source
),
226 _S_range_end(__source
)));
229 template<typename _InputIterator
>
230 _Path
<_InputIterator
, _InputIterator
>&
231 append(_InputIterator __first
, _InputIterator __last
)
232 { return _M_append(_S_convert(__first
, __last
)); }
236 path
& operator+=(const path
& __x
);
237 path
& operator+=(const string_type
& __x
);
238 path
& operator+=(const value_type
* __x
);
239 path
& operator+=(value_type __x
);
241 template<typename _Source
>
243 operator+=(_Source
const& __x
) { return concat(__x
); }
245 template<typename _CharT
>
246 _Path
<_CharT
*, _CharT
*>&
247 operator+=(_CharT __x
);
249 template<typename _Source
>
251 concat(_Source
const& __x
)
252 { return *this += _S_convert(_S_range_begin(__x
), _S_range_end(__x
)); }
254 template<typename _InputIterator
>
255 _Path
<_InputIterator
, _InputIterator
>&
256 concat(_InputIterator __first
, _InputIterator __last
)
257 { return *this += _S_convert(__first
, __last
); }
261 void clear() noexcept
{ _M_pathname
.clear(); _M_split_cmpts(); }
263 path
& make_preferred();
264 path
& remove_filename();
265 path
& replace_filename(const path
& __replacement
);
266 path
& replace_extension(const path
& __replacement
= path());
268 void swap(path
& __rhs
) noexcept
;
270 // native format observers
272 const string_type
& native() const noexcept
{ return _M_pathname
; }
273 const value_type
* c_str() const noexcept
{ return _M_pathname
.c_str(); }
274 operator string_type() const { return _M_pathname
; }
276 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
277 typename _Allocator
= std::allocator
<_CharT
>>
278 std::basic_string
<_CharT
, _Traits
, _Allocator
>
279 string(const _Allocator
& __a
= _Allocator()) const;
281 std::string
string() const;
282 #if _GLIBCXX_USE_WCHAR_T
283 std::wstring
wstring() const;
285 std::string
u8string() const;
286 std::u16string
u16string() const;
287 std::u32string
u32string() const;
289 // generic format observers
290 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>,
291 typename _Allocator
= std::allocator
<_CharT
>>
292 std::basic_string
<_CharT
, _Traits
, _Allocator
>
293 generic_string(const _Allocator
& __a
= _Allocator()) const;
295 std::string
generic_string() const;
296 #if _GLIBCXX_USE_WCHAR_T
297 std::wstring
generic_wstring() const;
299 std::string
generic_u8string() const;
300 std::u16string
generic_u16string() const;
301 std::u32string
generic_u32string() const;
305 int compare(const path
& __p
) const noexcept
;
306 int compare(const string_type
& __s
) const;
307 int compare(const value_type
* __s
) const;
311 path
root_name() const;
312 path
root_directory() const;
313 path
root_path() const;
314 path
relative_path() const;
315 path
parent_path() const;
316 path
filename() const;
318 path
extension() const;
322 bool empty() const noexcept
{ return _M_pathname
.empty(); }
323 bool has_root_name() const;
324 bool has_root_directory() const;
325 bool has_root_path() const;
326 bool has_relative_path() const;
327 bool has_parent_path() const;
328 bool has_filename() const;
329 bool has_stem() const;
330 bool has_extension() const;
331 bool is_absolute() const;
332 bool is_relative() const { return !is_absolute(); }
336 typedef iterator const_iterator
;
338 iterator
begin() const;
339 iterator
end() const;
342 enum class _Type
: unsigned char {
343 _Multi
, _Root_name
, _Root_dir
, _Filename
346 path(string_type __str
, _Type __type
) : _M_pathname(__str
), _M_type(__type
)
348 __glibcxx_assert(!empty());
349 __glibcxx_assert(_M_type
!= _Type::_Multi
);
352 enum class _Split
{ _Stem
, _Extension
};
354 path
& _M_append(const string_type
& __str
)
356 if (!_M_pathname
.empty() && !_S_is_dir_sep(_M_pathname
.back())
357 && !__str
.empty() && !_S_is_dir_sep(__str
.front()))
358 _M_pathname
+= preferred_separator
;
359 _M_pathname
+= __str
;
364 pair
<const string_type
*, size_t> _M_find_extension() const;
366 template<typename _CharT
>
370 _S_convert(value_type
* __src
, __null_terminated
)
371 { return string_type(__src
); }
374 _S_convert(const value_type
* __src
, __null_terminated
)
375 { return string_type(__src
); }
377 template<typename _Iter
>
379 _S_convert(_Iter __first
, _Iter __last
)
381 using __value_type
= typename
std::iterator_traits
<_Iter
>::value_type
;
382 return _Cvt
<__value_type
>::_S_convert(__first
, __last
);
385 template<typename _InputIterator
>
387 _S_convert(_InputIterator __src
, __null_terminated
)
389 using _Tp
= typename
std::iterator_traits
<_InputIterator
>::value_type
;
390 std::basic_string
<_Tp
> __tmp
;
391 while (*__src
!= _Tp
{})
392 __tmp
.push_back(*__src
++);
393 return _S_convert(__tmp
.data(), __tmp
.data() + __tmp
.size());
397 _S_convert_loc(const char* __first
, const char* __last
,
398 const std::locale
& __loc
);
400 template<typename _Iter
>
402 _S_convert_loc(_Iter __first
, _Iter __last
, const std::locale
& __loc
)
404 const std::string
__str(__first
, __last
);
405 return _S_convert_loc(__str
.data(), __str
.data()+__str
.size(), __loc
);
408 template<typename _InputIterator
>
410 _S_convert_loc(_InputIterator __src
, __null_terminated
,
411 const std::locale
& __loc
)
414 while (*__src
!= '\0')
415 __tmp
.push_back(*__src
++);
416 return _S_convert_loc(__tmp
.data(), __tmp
.data()+__tmp
.size(), __loc
);
419 bool _S_is_dir_sep(value_type __ch
)
421 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
422 return __ch
== L
'/' || __ch
== preferred_separator
;
428 void _M_split_cmpts();
430 void _M_add_root_name(size_t __n
);
431 void _M_add_root_dir(size_t __pos
);
432 void _M_add_filename(size_t __pos
, size_t __n
);
434 string_type _M_pathname
;
437 using _List
= _GLIBCXX_STD_C::vector
<_Cmpt
>;
438 _List _M_cmpts
; // empty unless _M_type == _Type::_Multi
439 _Type _M_type
= _Type::_Multi
;
442 inline void swap(path
& __lhs
, path
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
444 size_t hash_value(const path
& __p
) noexcept
;
447 inline bool operator<(const path
& __lhs
, const path
& __rhs
) noexcept
448 { return __lhs
.compare(__rhs
) < 0; }
451 inline bool operator<=(const path
& __lhs
, const path
& __rhs
) noexcept
452 { return !(__rhs
< __lhs
); }
455 inline bool operator>(const path
& __lhs
, const path
& __rhs
) noexcept
456 { return __rhs
< __lhs
; }
459 inline bool operator>=(const path
& __lhs
, const path
& __rhs
) noexcept
460 { return !(__lhs
< __rhs
); }
463 inline bool operator==(const path
& __lhs
, const path
& __rhs
) noexcept
464 { return __lhs
.compare(__rhs
) == 0; }
467 inline bool operator!=(const path
& __lhs
, const path
& __rhs
) noexcept
468 { return !(__lhs
== __rhs
); }
470 /// Append one path to another
471 inline path
operator/(const path
& __lhs
, const path
& __rhs
)
472 { return path(__lhs
) /= __rhs
; }
474 /// Write a path to a stream
475 template<typename _CharT
, typename _Traits
>
476 basic_ostream
<_CharT
, _Traits
>&
477 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const path
& __p
)
479 auto __tmp
= __p
.string
<_CharT
, _Traits
>();
480 using __quoted_string
481 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
482 __os
<< __quoted_string
{__tmp
, '"', '\\'};
486 /// Read a path from a stream
487 template<typename _CharT
, typename _Traits
>
488 basic_istream
<_CharT
, _Traits
>&
489 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, path
& __p
)
491 basic_string
<_CharT
, _Traits
> __tmp
;
492 using __quoted_string
493 = std::__detail::_Quoted_string
<decltype(__tmp
)&, _CharT
>;
494 if (__is
>> __quoted_string
{ __tmp
, '"', '\\' })
495 __p
= std::move(__tmp
);
499 // TODO constrain with _Path<Source> and __value_type_is_char
500 template<typename _Source
>
502 u8path(const _Source
& __source
)
504 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
505 return path
{ path::string_type
{__source
} };
507 return path
{ __source
};
511 // TODO constrain with _Path<InputIterator, InputIterator> and __value_type_is_char
512 template<typename _InputIterator
>
514 u8path(_InputIterator __first
, _InputIterator __last
)
516 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
517 return path
{ path::string_type
{__first
, __last
} };
519 return path
{ __first
, __last
};
523 class filesystem_error
: public std::system_error
526 filesystem_error(const string
& __what_arg
, error_code __ec
)
527 : system_error(__ec
, __what_arg
) { }
529 filesystem_error(const string
& __what_arg
, const path
& __p1
,
531 : system_error(__ec
, __what_arg
), _M_path1(__p1
) { }
533 filesystem_error(const string
& __what_arg
, const path
& __p1
,
534 const path
& __p2
, error_code __ec
)
535 : system_error(__ec
, __what_arg
), _M_path1(__p1
), _M_path2(__p2
)
540 const path
& path1() const noexcept
{ return _M_path1
; }
541 const path
& path2() const noexcept
{ return _M_path2
; }
542 const char* what() const noexcept
{ return _M_what
.c_str(); }
545 std::string
_M_gen_what();
549 std::string _M_what
= _M_gen_what();
553 struct path::__is_encoded_char
<char> : std::true_type
554 { using value_type
= char; };
557 struct path::__is_encoded_char
<wchar_t> : std::true_type
558 { using value_type
= wchar_t; };
561 struct path::__is_encoded_char
<char16_t
> : std::true_type
562 { using value_type
= char16_t
; };
565 struct path::__is_encoded_char
<char32_t
> : std::true_type
566 { using value_type
= char32_t
; };
568 struct path::_Cmpt
: path
570 _Cmpt(string_type __s
, _Type __t
, size_t __pos
)
571 : path(std::move(__s
), __t
), _M_pos(__pos
) { }
573 _Cmpt() : _M_pos(-1) { }
578 // specialize _Cvt for degenerate 'noconv' case
580 struct path::_Cvt
<path::value_type
>
582 template<typename _Iter
>
584 _S_convert(_Iter __first
, _Iter __last
)
585 { return string_type
{__first
, __last
}; }
588 template<typename _CharT
>
591 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
593 _S_wconvert(const char* __f
, const char* __l
, true_type
)
595 using _Cvt
= std::codecvt
<wchar_t, char, mbstate_t>;
596 const auto& __cvt
= std::use_facet
<_Cvt
>(std::locale
{});
598 if (__str_codecvt_in(__f
, __l
, __wstr
, __cvt
))
600 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
601 "Cannot convert character sequence",
602 std::make_error_code(errc::illegal_byte_sequence
)));
606 _S_wconvert(const _CharT
* __f
, const _CharT
* __l
, false_type
)
608 std::codecvt_utf8
<_CharT
> __cvt
;
610 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
612 const char* __f2
= __str
.data();
613 const char* __l2
= __f2
+ __str
.size();
614 std::codecvt_utf8
<wchar_t> __wcvt
;
616 if (__str_codecvt_in(__f2
, __l2
, __wstr
, __wcvt
))
619 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
620 "Cannot convert character sequence",
621 std::make_error_code(errc::illegal_byte_sequence
)));
625 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
627 return _S_wconvert(__f
, __l
, is_same
<_CharT
, char>{});
631 _S_convert(const _CharT
* __f
, const _CharT
* __l
)
633 std::codecvt_utf8
<_CharT
> __cvt
;
635 if (__str_codecvt_out(__f
, __l
, __str
, __cvt
))
637 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
638 "Cannot convert character sequence",
639 std::make_error_code(errc::illegal_byte_sequence
)));
644 _S_convert(_CharT
* __f
, _CharT
* __l
)
646 return _S_convert(const_cast<const _CharT
*>(__f
),
647 const_cast<const _CharT
*>(__l
));
650 template<typename _Iter
>
652 _S_convert(_Iter __first
, _Iter __last
)
654 const std::basic_string
<_CharT
> __str(__first
, __last
);
655 return _S_convert(__str
.data(), __str
.data() + __str
.size());
658 template<typename _Iter
, typename _Cont
>
660 _S_convert(__gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __first
,
661 __gnu_cxx::__normal_iterator
<_Iter
, _Cont
> __last
)
662 { return _S_convert(__first
.base(), __last
.base()); }
665 /// An iterator for the components of a path
669 using difference_type
= std::ptrdiff_t;
670 using value_type
= path
;
671 using reference
= const path
&;
672 using pointer
= const path
*;
673 using iterator_category
= std::bidirectional_iterator_tag
;
675 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
677 iterator(const iterator
&) = default;
678 iterator
& operator=(const iterator
&) = default;
680 reference
operator*() const;
681 pointer
operator->() const { return std::__addressof(**this); }
683 iterator
& operator++();
684 iterator
operator++(int) { auto __tmp
= *this; ++_M_cur
; return __tmp
; }
686 iterator
& operator--();
687 iterator
operator--(int) { auto __tmp
= *this; --_M_cur
; return __tmp
; }
689 friend bool operator==(const iterator
& __lhs
, const iterator
& __rhs
)
690 { return __lhs
._M_equals(__rhs
); }
692 friend bool operator!=(const iterator
& __lhs
, const iterator
& __rhs
)
693 { return !__lhs
._M_equals(__rhs
); }
698 iterator(const path
* __path
, path::_List::const_iterator __iter
)
699 : _M_path(__path
), _M_cur(__iter
), _M_at_end()
702 iterator(const path
* __path
, bool __at_end
)
703 : _M_path(__path
), _M_cur(), _M_at_end(__at_end
)
706 bool _M_equals(iterator
) const;
709 path::_List::const_iterator _M_cur
;
710 bool _M_at_end
; // only used when type != _Multi
715 path::operator=(path
&& __p
) noexcept
717 _M_pathname
= std::move(__p
._M_pathname
);
718 _M_cmpts
= std::move(__p
._M_cmpts
);
719 _M_type
= __p
._M_type
;
725 path::operator+=(const path
& __p
)
727 return operator+=(__p
.native());
731 path::operator+=(const string_type
& __x
)
739 path::operator+=(const value_type
* __x
)
747 path::operator+=(value_type __x
)
754 template<typename _CharT
>
755 inline path::_Path
<_CharT
*, _CharT
*>&
756 path::operator+=(_CharT __x
)
758 auto* __addr
= std::__addressof(__x
);
759 return concat(__addr
, __addr
+ 1);
763 path::make_preferred()
765 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
766 std::replace(_M_pathname
.begin(), _M_pathname
.end(), L
'/',
767 preferred_separator
);
772 inline void path::swap(path
& __rhs
) noexcept
774 _M_pathname
.swap(__rhs
._M_pathname
);
775 _M_cmpts
.swap(__rhs
._M_cmpts
);
776 std::swap(_M_type
, __rhs
._M_type
);
779 template<typename _CharT
, typename _Traits
, typename _Allocator
>
780 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
781 path::string(const _Allocator
& __a
) const
783 if (is_same
<_CharT
, value_type
>::value
)
784 return { _M_pathname
.begin(), _M_pathname
.end(), __a
};
786 const value_type
* __first
= _M_pathname
.data();
787 const value_type
* __last
= __first
+ _M_pathname
.size();
789 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
790 using _CharAlloc
= __alloc_rebind
<_Allocator
, char>;
791 using _String
= basic_string
<char, char_traits
<char>, _CharAlloc
>;
792 using _WString
= basic_string
<_CharT
, _Traits
, _Allocator
>;
794 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
795 codecvt_utf8
<value_type
> __cvt
;
796 _String __u8str
{_CharAlloc
{__a
}};
797 if (__str_codecvt_out(__first
, __last
, __u8str
, __cvt
))
802 operator()(const _String
& __from
, _String
&, true_type
)
803 { return std::__addressof(__from
); }
806 operator()(const _String
& __from
, _WString
& __to
, false_type
)
808 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
809 codecvt_utf8
<_CharT
> __cvt
;
810 const char* __f
= __from
.data();
811 const char* __l
= __f
+ __from
.size();
812 if (__str_codecvt_in(__f
, __l
, __to
, __cvt
))
813 return std::__addressof(__to
);
818 if (auto* __p
= __dispatch(__u8str
, __wstr
, is_same
<_CharT
, char>{}))
822 codecvt_utf8
<_CharT
> __cvt
;
823 basic_string
<_CharT
, _Traits
, _Allocator
> __wstr
{__a
};
824 if (__str_codecvt_in(__first
, __last
, __wstr
, __cvt
))
827 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
828 "Cannot convert character sequence",
829 std::make_error_code(errc::illegal_byte_sequence
)));
833 path::string() const { return string
<char>(); }
835 #if _GLIBCXX_USE_WCHAR_T
837 path::wstring() const { return string
<wchar_t>(); }
841 path::u8string() const
843 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
845 // convert from native encoding to UTF-8
846 codecvt_utf8
<value_type
> __cvt
;
847 const value_type
* __first
= _M_pathname
.data();
848 const value_type
* __last
= __first
+ _M_pathname
.size();
849 if (__str_codecvt_out(__first
, __last
, __str
, __cvt
))
851 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
852 "Cannot convert character sequence",
853 std::make_error_code(errc::illegal_byte_sequence
)));
859 inline std::u16string
860 path::u16string() const { return string
<char16_t
>(); }
862 inline std::u32string
863 path::u32string() const { return string
<char32_t
>(); }
865 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
866 template<typename _CharT
, typename _Traits
, typename _Allocator
>
867 inline std::basic_string
<_CharT
, _Traits
, _Allocator
>
868 path::generic_string(const _Allocator
& __a
) const
869 { return string
<_CharT
, _Traits
, _Allocator
>(__a
); }
872 path::generic_string() const { return string(); }
874 #if _GLIBCXX_USE_WCHAR_T
876 path::generic_wstring() const { return wstring(); }
880 path::generic_u8string() const { return u8string(); }
882 inline std::u16string
883 path::generic_u16string() const { return u16string(); }
885 inline std::u32string
886 path::generic_u32string() const { return u32string(); }
890 path::compare(const string_type
& __s
) const { return compare(path(__s
)); }
893 path::compare(const value_type
* __s
) const { return compare(path(__s
)); }
896 path::filename() const { return empty() ? path() : *--end(); }
901 auto ext
= _M_find_extension();
902 if (ext
.first
&& ext
.second
!= 0)
903 return path
{ext
.first
->substr(0, ext
.second
)};
908 path::extension() const
910 auto ext
= _M_find_extension();
911 if (ext
.first
&& ext
.second
!= string_type::npos
)
912 return path
{ext
.first
->substr(ext
.second
)};
917 path::has_stem() const
919 auto ext
= _M_find_extension();
920 return ext
.first
&& ext
.second
!= 0;
924 path::has_extension() const
926 auto ext
= _M_find_extension();
927 return ext
.first
&& ext
.second
!= string_type::npos
;
931 path::is_absolute() const
933 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
934 return has_root_name();
936 return has_root_directory();
940 inline path::iterator
943 if (_M_type
== _Type::_Multi
)
944 return iterator(this, _M_cmpts
.begin());
945 return iterator(this, false);
948 inline path::iterator
951 if (_M_type
== _Type::_Multi
)
952 return iterator(this, _M_cmpts
.end());
953 return iterator(this, true);
956 inline path::iterator
&
957 path::iterator::operator++()
959 __glibcxx_assert(_M_path
!= nullptr);
960 if (_M_path
->_M_type
== _Type::_Multi
)
962 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
967 __glibcxx_assert(!_M_at_end
);
973 inline path::iterator
&
974 path::iterator::operator--()
976 __glibcxx_assert(_M_path
!= nullptr);
977 if (_M_path
->_M_type
== _Type::_Multi
)
979 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.begin());
984 __glibcxx_assert(_M_at_end
);
990 inline path::iterator::reference
991 path::iterator::operator*() const
993 __glibcxx_assert(_M_path
!= nullptr);
994 if (_M_path
->_M_type
== _Type::_Multi
)
996 __glibcxx_assert(_M_cur
!= _M_path
->_M_cmpts
.end());
1003 path::iterator::_M_equals(iterator __rhs
) const
1005 if (_M_path
!= __rhs
._M_path
)
1007 if (_M_path
== nullptr)
1009 if (_M_path
->_M_type
== path::_Type::_Multi
)
1010 return _M_cur
== __rhs
._M_cur
;
1011 return _M_at_end
== __rhs
._M_at_end
;
1014 // @} group filesystem
1015 _GLIBCXX_END_NAMESPACE_CXX11
1016 _GLIBCXX_END_NAMESPACE_VERSION
1018 } // namespace filesystem
1019 } // namespace experimental
1024 #endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H