source: Daodan/MSYS2/mingw32/include/c++/11.2.0/experimental/bits/fs_path.h@ 1177

Last change on this file since 1177 was 1166, checked in by rossy, 3 years ago

Daodan: Replace MinGW build env with an up-to-date MSYS2 env

File size: 36.5 KB
Line 
1// Class filesystem::path -*- C++ -*-
2
3// Copyright (C) 2014-2021 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
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}
28 */
29
30#ifndef _GLIBCXX_EXPERIMENTAL_FS_PATH_H
31#define _GLIBCXX_EXPERIMENTAL_FS_PATH_H 1
32
33#if __cplusplus < 201103L
34# include <bits/c++0x_warning.h>
35#else
36
37#include <utility>
38#include <type_traits>
39#include <vector>
40#include <locale>
41#include <iosfwd>
42#include <codecvt>
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>
49#endif
50
51#if defined(_WIN32) && !defined(__CYGWIN__)
52# define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
53# include <algorithm>
54#endif
55
56namespace std _GLIBCXX_VISIBILITY(default)
57{
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
59
60namespace experimental
61{
62namespace filesystem
63{
64inline namespace v1
65{
66_GLIBCXX_BEGIN_NAMESPACE_CXX11
67
68#if __cplusplus == 201402L
69 using std::experimental::basic_string_view;
70#elif __cplusplus > 201402L
71 using std::basic_string_view;
72#endif
73
74 /** @addtogroup filesystem-ts
75 * @{
76 */
77
78 /// @cond undocumented
79namespace __detail
80{
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>,
88#endif
89 is_same<_Ch, char16_t>,
90 is_same<_Ch, char32_t>>;
91
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>>;
98
99 template<typename _Iter>
100 static __is_path_iter_src<_Iter>
101 __is_path_src(_Iter, int);
102
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);
106
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);
111#endif
112
113 template<typename _Unknown>
114 static std::false_type
115 __is_path_src(const _Unknown&, ...);
116
117 template<typename _Tp1, typename _Tp2>
118 struct __constructible_from;
119
120 template<typename _Iter>
121 struct __constructible_from<_Iter, _Iter>
122 : __is_path_iter_src<_Iter>
123 { };
124
125 template<typename _Source>
126 struct __constructible_from<_Source, void>
127 : decltype(__is_path_src(std::declval<const _Source&>(), 0))
128 { };
129
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,
137 path>::type;
138
139 template<typename _Source>
140 inline _Source
141 _S_range_begin(_Source __begin) { return __begin; }
142
143 struct __null_terminated { };
144
145 template<typename _Source>
146 inline __null_terminated
147 _S_range_end(_Source) { return {}; }
148
149 template<typename _CharT, typename _Traits, typename _Alloc>
150 inline const _CharT*
151 _S_range_begin(const basic_string<_CharT, _Traits, _Alloc>& __str)
152 { return __str.data(); }
153
154 template<typename _CharT, typename _Traits, typename _Alloc>
155 inline const _CharT*
156 _S_range_end(const basic_string<_CharT, _Traits, _Alloc>& __str)
157 { return __str.data() + __str.size(); }
158
159#if __cplusplus >= 201402L
160 template<typename _CharT, typename _Traits>
161 inline const _CharT*
162 _S_range_begin(const basic_string_view<_CharT, _Traits>& __str)
163 { return __str.data(); }
164
165 template<typename _CharT, typename _Traits>
166 inline const _CharT*
167 _S_range_end(const basic_string_view<_CharT, _Traits>& __str)
168 { return __str.data() + __str.size(); }
169#endif
170
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,
177 _UnqualVal>::type;
178
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<
184 __or_<
185 std::is_same<_UnqualVal, char>
186#ifdef _GLIBCXX_USE_CHAR8_T
187 ,std::is_same<_UnqualVal, char8_t>
188#endif
189 >::value, _UnqualVal>::type;
190
191} // namespace __detail
192 /// @endcond
193
194 /// A filesystem path.
195 class path
196 {
197 public:
198#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
199 typedef wchar_t value_type;
200 static constexpr value_type preferred_separator = L'\\';
201#else
202 typedef char value_type;
203 static constexpr value_type preferred_separator = '/';
204#endif
205 typedef std::basic_string<value_type> string_type;
206
207 // constructors and destructor
208
209 path() noexcept { }
210
211 path(const path& __p) = default;
212
213 path(path&& __p) noexcept
214 : _M_pathname(std::move(__p._M_pathname)), _M_type(__p._M_type)
215 {
216 if (_M_type == _Type::_Multi)
217 _M_split_cmpts();
218 __p.clear();
219 }
220
221 path(string_type&& __source)
222 : _M_pathname(std::move(__source))
223 { _M_split_cmpts(); }
224
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(); }
231
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(); }
237
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(); }
245
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(); }
252
253 ~path() = default;
254
255 // assignments
256
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);
261
262 template<typename _Source>
263 __detail::_Path<_Source>&
264 operator=(_Source const& __source)
265 { return *this = path(__source); }
266
267 template<typename _Source>
268 __detail::_Path<_Source>&
269 assign(_Source const& __source)
270 { return *this = path(__source); }
271
272 template<typename _InputIterator>
273 __detail::_Path<_InputIterator, _InputIterator>&
274 assign(_InputIterator __first, _InputIterator __last)
275 { return *this = path(__first, __last); }
276
277 // appends
278
279 path& operator/=(const path& __p) { return _M_append(__p._M_pathname); }
280
281 template<typename _Source>
282 __detail::_Path<_Source>&
283 operator/=(_Source const& __source)
284 { return append(__source); }
285
286 template<typename _Source>
287 __detail::_Path<_Source>&
288 append(_Source const& __source)
289 {
290 return _M_append(_S_convert(__detail::_S_range_begin(__source),
291 __detail::_S_range_end(__source)));
292 }
293
294 template<typename _InputIterator>
295 __detail::_Path<_InputIterator, _InputIterator>&
296 append(_InputIterator __first, _InputIterator __last)
297 { return _M_append(_S_convert(__first, __last)); }
298
299 // concatenation
300
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);
307#endif
308
309 template<typename _Source>
310 __detail::_Path<_Source>&
311 operator+=(_Source const& __x) { return concat(__x); }
312
313 template<typename _CharT>
314 __detail::_Path<_CharT*, _CharT*>&
315 operator+=(_CharT __x);
316
317 template<typename _Source>
318 __detail::_Path<_Source>&
319 concat(_Source const& __x)
320 {
321 return *this += _S_convert(__detail::_S_range_begin(__x),
322 __detail::_S_range_end(__x));
323 }
324
325 template<typename _InputIterator>
326 __detail::_Path<_InputIterator, _InputIterator>&
327 concat(_InputIterator __first, _InputIterator __last)
328 { return *this += _S_convert(__first, __last); }
329
330 // modifiers
331
332 void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); }
333
334 path& make_preferred();
335 path& remove_filename();
336 path& replace_filename(const path& __replacement);
337 path& replace_extension(const path& __replacement = path());
338
339 void swap(path& __rhs) noexcept;
340
341 // native format observers
342
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; }
346
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;
351
352 std::string string() const;
353#if _GLIBCXX_USE_WCHAR_T
354 std::wstring wstring() const;
355#endif
356#ifdef _GLIBCXX_USE_CHAR8_T
357 __attribute__((__abi_tag__("__u8")))
358 std::u8string u8string() const;
359#else
360 std::string u8string() const;
361#endif // _GLIBCXX_USE_CHAR8_T
362 std::u16string u16string() const;
363 std::u32string u32string() const;
364
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;
370
371 std::string generic_string() const;
372#if _GLIBCXX_USE_WCHAR_T
373 std::wstring generic_wstring() const;
374#endif
375#ifdef _GLIBCXX_USE_CHAR8_T
376 __attribute__((__abi_tag__("__u8")))
377 std::u8string generic_u8string() const;
378#else
379 std::string generic_u8string() const;
380#endif // _GLIBCXX_USE_CHAR8_T
381 std::u16string generic_u16string() const;
382 std::u32string generic_u32string() const;
383
384 // compare
385
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;
391#endif
392
393 // decomposition
394
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;
401 path stem() const;
402 path extension() const;
403
404 // query
405
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(); }
417
418 // iterators
419 class iterator;
420 typedef iterator const_iterator;
421
422 iterator begin() const;
423 iterator end() const;
424
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>,
429 typename _CharT
430 = typename std::remove_cv<typename _Traits::value_type>::type>
431 static std::basic_string<_CharT>
432 _S_string_from_iter(_InputIterator __source)
433 {
434 std::basic_string<_CharT> __str;
435 for (_CharT __ch = *__source; __ch != _CharT(); __ch = *++__source)
436 __str.push_back(__ch);
437 return __str;
438 }
439 /// @endcond
440
441 private:
442 enum class _Type : unsigned char {
443 _Multi, _Root_name, _Root_dir, _Filename
444 };
445
446 path(string_type __str, _Type __type) : _M_pathname(__str), _M_type(__type)
447 {
448 __glibcxx_assert(!empty());
449 __glibcxx_assert(_M_type != _Type::_Multi);
450 }
451
452 enum class _Split { _Stem, _Extension };
453
454 path& _M_append(const string_type& __str)
455 {
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;
460 _M_split_cmpts();
461 return *this;
462 }
463
464 pair<const string_type*, size_t> _M_find_extension() const;
465
466 template<typename _CharT>
467 struct _Cvt;
468
469 static string_type
470 _S_convert(value_type* __src, __detail::__null_terminated)
471 { return string_type(__src); }
472
473 static string_type
474 _S_convert(const value_type* __src, __detail::__null_terminated)
475 { return string_type(__src); }
476
477 template<typename _Iter>
478 static string_type
479 _S_convert(_Iter __first, _Iter __last)
480 {
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);
484 }
485
486 template<typename _InputIterator>
487 static string_type
488 _S_convert(_InputIterator __src, __detail::__null_terminated)
489 {
490 auto __s = _S_string_from_iter(__src);
491 return _S_convert(__s.c_str(), __s.c_str() + __s.size());
492 }
493
494 static string_type
495 _S_convert_loc(const char* __first, const char* __last,
496 const std::locale& __loc);
497
498 static string_type
499 _S_convert_loc(char* __first, char* __last, const std::locale& __loc)
500 {
501 return _S_convert_loc(const_cast<const char*>(__first),
502 const_cast<const char*>(__last), __loc);
503 }
504
505 template<typename _Iter>
506 static string_type
507 _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc)
508 {
509 const std::string __str(__first, __last);
510 return _S_convert_loc(__str.data(), __str.data()+__str.size(), __loc);
511 }
512
513 template<typename _InputIterator>
514 static string_type
515 _S_convert_loc(_InputIterator __src, __detail::__null_terminated,
516 const std::locale& __loc)
517 {
518 const std::string __s = _S_string_from_iter(__src);
519 return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
520 }
521
522 static bool _S_is_dir_sep(value_type __ch)
523 {
524#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
525 return __ch == L'/' || __ch == preferred_separator;
526#else
527 return __ch == '/';
528#endif
529 }
530
531 void _M_split_cmpts();
532 void _M_trim();
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);
536
537 string_type _M_pathname;
538
539 struct _Cmpt;
540 using _List = _GLIBCXX_STD_C::vector<_Cmpt>;
541 _List _M_cmpts; // empty unless _M_type == _Type::_Multi
542 _Type _M_type = _Type::_Multi;
543 };
544
545 /// @relates std::experimental::filesystem::path @{
546
547 /// Swap overload for paths
548 inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); }
549
550 /// Compute a hash value for a path
551 size_t hash_value(const path& __p) noexcept;
552
553 /// Compare paths
554 inline bool operator<(const path& __lhs, const path& __rhs) noexcept;
555
556 /// Compare paths
557 inline bool operator<=(const path& __lhs, const path& __rhs) noexcept
558 { return !(__rhs < __lhs); }
559
560 /// Compare paths
561 inline bool operator>(const path& __lhs, const path& __rhs) noexcept
562 { return __rhs < __lhs; }
563
564 /// Compare paths
565 inline bool operator>=(const path& __lhs, const path& __rhs) noexcept
566 { return !(__lhs < __rhs); }
567
568 /// Compare paths
569 inline bool operator==(const path& __lhs, const path& __rhs) noexcept;
570
571 /// Compare paths
572 inline bool operator!=(const path& __lhs, const path& __rhs) noexcept
573 { return !(__lhs == __rhs); }
574
575 /// Append one path to another
576 inline path operator/(const path& __lhs, const path& __rhs)
577 {
578 path __result(__lhs);
579 __result /= __rhs;
580 return __result;
581 }
582
583 /// Write a path to a stream
584 template<typename _CharT, typename _Traits>
585 basic_ostream<_CharT, _Traits>&
586 operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p)
587 {
588 auto __tmp = __p.string<_CharT, _Traits>();
589 using __quoted_string
590 = std::__detail::_Quoted_string<decltype(__tmp)&, _CharT>;
591 __os << __quoted_string{__tmp, _CharT('"'), _CharT('\\')};
592 return __os;
593 }
594
595 /// Read a path from a stream
596 template<typename _CharT, typename _Traits>
597 basic_istream<_CharT, _Traits>&
598 operator>>(basic_istream<_CharT, _Traits>& __is, path& __p)
599 {
600 basic_string<_CharT, _Traits> __tmp;
601 using __quoted_string
602 = std::__detail::_Quoted_string<decltype(__tmp)&, _CharT>;
603 if (__is >> __quoted_string{ __tmp, _CharT('"'), _CharT('\\') })
604 __p = std::move(__tmp);
605 return __is;
606 }
607
608 /// Create a path from a UTF-8-encoded sequence of char
609#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
610 template<typename _InputIterator>
611 inline path
612 __u8path(_InputIterator __first, _InputIterator __last, char)
613 {
614 // XXX This assumes native wide encoding is UTF-16.
615 std::codecvt_utf8_utf16<path::value_type> __cvt;
616 path::string_type __tmp;
617 const std::string __u8str{__first, __last};
618 const char* const __ptr = __u8str.data();
619 if (__str_codecvt_in_all(__ptr, __ptr + __u8str.size(), __tmp, __cvt))
620 return path{ __tmp };
621 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
622 "Cannot convert character sequence",
623 std::make_error_code(errc::illegal_byte_sequence)));
624 }
625
626#ifdef _GLIBCXX_USE_CHAR8_T
627 template<typename _InputIterator>
628 inline path
629 __u8path(_InputIterator __first, _InputIterator __last, char8_t)
630 {
631 return path{ __first, __last };
632 }
633#endif // _GLIBCXX_USE_CHAR8_T
634#endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS
635
636 template<typename _InputIterator,
637 typename _Require = __detail::_Path<_InputIterator, _InputIterator>,
638 typename _CharT =
639 __detail::__value_type_is_char_or_char8_t<_InputIterator>>
640 inline path
641 u8path(_InputIterator __first, _InputIterator __last)
642 {
643#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
644 return __u8path(__first, __last, _CharT{});
645#else
646 return path{ __first, __last };
647#endif
648 }
649
650 /// Create a path from a UTF-8-encoded sequence of char
651#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
652 inline path
653 __u8path(const string& __s, char)
654 {
655 return filesystem::u8path(__s.data(), __s.data() + __s.size());
656 }
657
658 template<typename _Source>
659 inline __enable_if_t<is_convertible<const _Source&, string>::value, path>
660 __u8path(const _Source& __source, char)
661 {
662 std::string __s = __source;
663 return filesystem::u8path(__s.data(), __s.data() + __s.size());
664 }
665
666 template<typename _Source>
667 inline __enable_if_t<!is_convertible<const _Source&, string>::value, path>
668 __u8path(const _Source& __source, char)
669 {
670 std::string __s = path::_S_string_from_iter(__source);
671 return filesystem::u8path(__s.data(), __s.data() + __s.size());
672 }
673
674#ifdef _GLIBCXX_USE_CHAR8_T
675 template<typename _Source>
676 inline path
677 __u8path(const _Source& __source, char8_t)
678 {
679 return path{ __source };
680 }
681#endif // _GLIBCXX_USE_CHAR8_T
682#endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS
683
684 template<typename _Source,
685 typename _Require = __detail::_Path<_Source>,
686 typename _CharT =
687 __detail::__value_type_is_char_or_char8_t<_Source>>
688 inline path
689 u8path(const _Source& __source)
690 {
691#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
692 return __u8path(__source, _CharT{});
693#else
694 return path{ __source };
695#endif
696 }
697
698 /// @}
699
700 /// Exception type thrown by the Filesystem TS library
701 class filesystem_error : public std::system_error
702 {
703 public:
704 filesystem_error(const string& __what_arg, error_code __ec)
705 : system_error(__ec, __what_arg) { }
706
707 filesystem_error(const string& __what_arg, const path& __p1,
708 error_code __ec)
709 : system_error(__ec, __what_arg), _M_path1(__p1) { }
710
711 filesystem_error(const string& __what_arg, const path& __p1,
712 const path& __p2, error_code __ec)
713 : system_error(__ec, __what_arg), _M_path1(__p1), _M_path2(__p2)
714 { }
715
716 ~filesystem_error();
717
718 const path& path1() const noexcept { return _M_path1; }
719 const path& path2() const noexcept { return _M_path2; }
720 const char* what() const noexcept { return _M_what.c_str(); }
721
722 private:
723 std::string _M_gen_what();
724
725 path _M_path1;
726 path _M_path2;
727 std::string _M_what = _M_gen_what();
728 };
729
730 /// @cond undocumented
731 struct path::_Cmpt : path
732 {
733 _Cmpt(string_type __s, _Type __t, size_t __pos)
734 : path(std::move(__s), __t), _M_pos(__pos) { }
735
736 _Cmpt() : _M_pos(-1) { }
737
738 size_t _M_pos;
739 };
740
741 // specialize _Cvt for degenerate 'noconv' case
742 template<>
743 struct path::_Cvt<path::value_type>
744 {
745 template<typename _Iter>
746 static string_type
747 _S_convert(_Iter __first, _Iter __last)
748 { return string_type{__first, __last}; }
749 };
750
751 template<typename _CharT>
752 struct path::_Cvt
753 {
754#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
755#ifdef _GLIBCXX_USE_CHAR8_T
756 static string_type
757 _S_wconvert(const char8_t* __f, const char8_t* __l, const char8_t*)
758 {
759 const char* __f2 = (const char*)__f;
760 const char* __l2 = (const char*)__l;
761 std::wstring __wstr;
762 std::codecvt_utf8_utf16<wchar_t> __wcvt;
763 if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt))
764 return __wstr;
765 }
766#endif
767
768 static string_type
769 _S_wconvert(const char* __f, const char* __l, const char*)
770 {
771 using _Cvt = std::codecvt<wchar_t, char, mbstate_t>;
772 const auto& __cvt = std::use_facet<_Cvt>(std::locale{});
773 std::wstring __wstr;
774 if (__str_codecvt_in_all(__f, __l, __wstr, __cvt))
775 return __wstr;
776 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
777 "Cannot convert character sequence",
778 std::make_error_code(errc::illegal_byte_sequence)));
779 }
780
781 static string_type
782 _S_wconvert(const _CharT* __f, const _CharT* __l, const void*)
783 {
784 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t>
785 { } __cvt;
786 std::string __str;
787 if (__str_codecvt_out_all(__f, __l, __str, __cvt))
788 {
789 const char* __f2 = __str.data();
790 const char* __l2 = __f2 + __str.size();
791 std::codecvt_utf8_utf16<wchar_t> __wcvt;
792 std::wstring __wstr;
793 if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt))
794 return __wstr;
795 }
796 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
797 "Cannot convert character sequence",
798 std::make_error_code(errc::illegal_byte_sequence)));
799 }
800
801 static string_type
802 _S_convert(const _CharT* __f, const _CharT* __l)
803 {
804 return _S_wconvert(__f, __l, (const _CharT*)nullptr);
805 }
806#else
807 static string_type
808 _S_convert(const _CharT* __f, const _CharT* __l)
809 {
810#ifdef _GLIBCXX_USE_CHAR8_T
811 if constexpr (is_same<_CharT, char8_t>::value)
812 return string_type(__f, __l);
813 else
814#endif
815 {
816 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t>
817 { } __cvt;
818 std::string __str;
819 if (__str_codecvt_out_all(__f, __l, __str, __cvt))
820 return __str;
821 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
822 "Cannot convert character sequence",
823 std::make_error_code(errc::illegal_byte_sequence)));
824 }
825 }
826#endif
827
828 static string_type
829 _S_convert(_CharT* __f, _CharT* __l)
830 {
831 return _S_convert(const_cast<const _CharT*>(__f),
832 const_cast<const _CharT*>(__l));
833 }
834
835 template<typename _Iter>
836 static string_type
837 _S_convert(_Iter __first, _Iter __last)
838 {
839 const std::basic_string<_CharT> __str(__first, __last);
840 return _S_convert(__str.data(), __str.data() + __str.size());
841 }
842
843 template<typename _Iter, typename _Cont>
844 static string_type
845 _S_convert(__gnu_cxx::__normal_iterator<_Iter, _Cont> __first,
846 __gnu_cxx::__normal_iterator<_Iter, _Cont> __last)
847 { return _S_convert(__first.base(), __last.base()); }
848 };
849 /// @endcond
850
851 /// An iterator for the components of a path
852 class path::iterator
853 {
854 public:
855 using difference_type = std::ptrdiff_t;
856 using value_type = path;
857 using reference = const path&;
858 using pointer = const path*;
859 using iterator_category = std::bidirectional_iterator_tag;
860
861 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
862
863 iterator(const iterator&) = default;
864 iterator& operator=(const iterator&) = default;
865
866 reference operator*() const;
867 pointer operator->() const { return std::__addressof(**this); }
868
869 iterator& operator++();
870 iterator operator++(int) { auto __tmp = *this; ++*this; return __tmp; }
871
872 iterator& operator--();
873 iterator operator--(int) { auto __tmp = *this; --*this; return __tmp; }
874
875 friend bool operator==(const iterator& __lhs, const iterator& __rhs)
876 { return __lhs._M_equals(__rhs); }
877
878 friend bool operator!=(const iterator& __lhs, const iterator& __rhs)
879 { return !__lhs._M_equals(__rhs); }
880
881 private:
882 friend class path;
883
884 iterator(const path* __path, path::_List::const_iterator __iter)
885 : _M_path(__path), _M_cur(__iter), _M_at_end()
886 { }
887
888 iterator(const path* __path, bool __at_end)
889 : _M_path(__path), _M_cur(), _M_at_end(__at_end)
890 { }
891
892 bool _M_equals(iterator) const;
893
894 const path* _M_path;
895 path::_List::const_iterator _M_cur;
896 bool _M_at_end; // only used when type != _Multi
897 };
898
899
900 inline path&
901 path::operator=(path&& __p) noexcept
902 {
903 _M_pathname = std::move(__p._M_pathname);
904 _M_cmpts = std::move(__p._M_cmpts);
905 _M_type = __p._M_type;
906 __p.clear();
907 return *this;
908 }
909
910 inline path&
911 path::operator=(string_type&& __source)
912 { return *this = path(std::move(__source)); }
913
914 inline path&
915 path::assign(string_type&& __source)
916 { return *this = path(std::move(__source)); }
917
918 inline path&
919 path::operator+=(const path& __p)
920 {
921 return operator+=(__p.native());
922 }
923
924 inline path&
925 path::operator+=(const string_type& __x)
926 {
927 _M_pathname += __x;
928 _M_split_cmpts();
929 return *this;
930 }
931
932 inline path&
933 path::operator+=(const value_type* __x)
934 {
935 _M_pathname += __x;
936 _M_split_cmpts();
937 return *this;
938 }
939
940 inline path&
941 path::operator+=(value_type __x)
942 {
943 _M_pathname += __x;
944 _M_split_cmpts();
945 return *this;
946 }
947
948#if __cplusplus >= 201402L
949 inline path&
950 path::operator+=(basic_string_view<value_type> __x)
951 {
952 _M_pathname.append(__x.data(), __x.size());
953 _M_split_cmpts();
954 return *this;
955 }
956#endif
957
958 template<typename _CharT>
959 inline __detail::_Path<_CharT*, _CharT*>&
960 path::operator+=(_CharT __x)
961 {
962 auto* __addr = std::__addressof(__x);
963 return concat(__addr, __addr + 1);
964 }
965
966 inline path&
967 path::make_preferred()
968 {
969#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
970 std::replace(_M_pathname.begin(), _M_pathname.end(), L'/',
971 preferred_separator);
972#endif
973 return *this;
974 }
975
976 inline void path::swap(path& __rhs) noexcept
977 {
978 _M_pathname.swap(__rhs._M_pathname);
979 _M_cmpts.swap(__rhs._M_cmpts);
980 std::swap(_M_type, __rhs._M_type);
981 }
982
983 template<typename _CharT, typename _Traits, typename _Allocator>
984 inline std::basic_string<_CharT, _Traits, _Allocator>
985 path::string(const _Allocator& __a) const
986 {
987 if (is_same<_CharT, value_type>::value)
988 return { _M_pathname.begin(), _M_pathname.end(), __a };
989
990 using _WString = basic_string<_CharT, _Traits, _Allocator>;
991
992 const value_type* __first = _M_pathname.data();
993 const value_type* __last = __first + _M_pathname.size();
994
995#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
996 using _CharAlloc = __alloc_rebind<_Allocator, char>;
997 using _String = basic_string<char, char_traits<char>, _CharAlloc>;
998
999 // First convert native string from UTF-16 to to UTF-8.
1000 // XXX This assumes that the execution wide-character set is UTF-16.
1001 codecvt_utf8_utf16<value_type> __cvt;
1002 _String __u8str{_CharAlloc{__a}};
1003 if (__str_codecvt_out_all(__first, __last, __u8str, __cvt))
1004 {
1005 struct
1006 {
1007 const _String*
1008 operator()(const _String& __from, _String&, true_type)
1009 { return std::__addressof(__from); }
1010
1011 _WString*
1012 operator()(const _String& __from, _WString& __to, false_type)
1013 {
1014#ifdef _GLIBCXX_USE_CHAR8_T
1015 if constexpr (is_same<_CharT, char8_t>::value)
1016 {
1017 __to.assign(__from.begin(), __from.end());
1018 return std::__addressof(__to);
1019 }
1020 else
1021#endif
1022 {
1023 // Convert UTF-8 to wide string.
1024 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t>
1025 { } __cvt;
1026 const char* __f = __from.data();
1027 const char* __l = __f + __from.size();
1028 if (__str_codecvt_in_all(__f, __l, __to, __cvt))
1029 return std::__addressof(__to);
1030 }
1031 return nullptr;
1032 }
1033 } __dispatch;
1034 _WString __wstr(__a);
1035 if (auto* __p = __dispatch(__u8str, __wstr, is_same<_CharT, char>{}))
1036 return *__p;
1037 }
1038#else
1039#ifdef _GLIBCXX_USE_CHAR8_T
1040 if constexpr (is_same<_CharT, char8_t>::value)
1041 return _WString(__first, __last, __a);
1042 else
1043#endif
1044 {
1045 struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> { } __cvt;
1046 _WString __wstr(__a);
1047 if (__str_codecvt_in_all(__first, __last, __wstr, __cvt))
1048 return __wstr;
1049 }
1050#endif
1051 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1052 "Cannot convert character sequence",
1053 std::make_error_code(errc::illegal_byte_sequence)));
1054 }
1055
1056 inline std::string
1057 path::string() const { return string<char>(); }
1058
1059#if _GLIBCXX_USE_WCHAR_T
1060 inline std::wstring
1061 path::wstring() const { return string<wchar_t>(); }
1062#endif
1063
1064#ifdef _GLIBCXX_USE_CHAR8_T
1065 inline std::u8string
1066 path::u8string() const { return string<char8_t>(); }
1067#else
1068 inline std::string
1069 path::u8string() const
1070 {
1071#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1072 std::string __str;
1073 // convert from native wide encoding (assumed to be UTF-16) to UTF-8
1074 std::codecvt_utf8_utf16<value_type> __cvt;
1075 const value_type* __first = _M_pathname.data();
1076 const value_type* __last = __first + _M_pathname.size();
1077 if (__str_codecvt_out_all(__first, __last, __str, __cvt))
1078 return __str;
1079 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
1080 "Cannot convert character sequence",
1081 std::make_error_code(errc::illegal_byte_sequence)));
1082#else
1083 return _M_pathname;
1084#endif
1085 }
1086#endif // _GLIBCXX_USE_CHAR8_T
1087
1088 inline std::u16string
1089 path::u16string() const { return string<char16_t>(); }
1090
1091 inline std::u32string
1092 path::u32string() const { return string<char32_t>(); }
1093
1094 template<typename _CharT, typename _Traits, typename _Allocator>
1095 inline std::basic_string<_CharT, _Traits, _Allocator>
1096 path::generic_string(const _Allocator& __a) const
1097 {
1098#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1099 const _CharT __slash = is_same<_CharT, wchar_t>::value
1100 ? _CharT(L'/')
1101 : _CharT('/'); // Assume value is correct for the encoding.
1102#else
1103 const _CharT __slash = _CharT('/');
1104#endif
1105 basic_string<_CharT, _Traits, _Allocator> __str(__a);
1106 __str.reserve(_M_pathname.size());
1107 bool __add_slash = false;
1108 for (auto& __elem : *this)
1109 {
1110 if (__elem._M_type == _Type::_Root_dir)
1111 {
1112 __str += __slash;
1113 continue;
1114 }
1115 if (__add_slash)
1116 __str += __slash;
1117 __str += __elem.string<_CharT, _Traits, _Allocator>(__a);
1118 __add_slash = __elem._M_type == _Type::_Filename;
1119 }
1120 return __str;
1121 }
1122
1123 inline std::string
1124 path::generic_string() const { return generic_string<char>(); }
1125
1126#if _GLIBCXX_USE_WCHAR_T
1127 inline std::wstring
1128 path::generic_wstring() const { return generic_string<wchar_t>(); }
1129#endif
1130
1131#ifdef _GLIBCXX_USE_CHAR8_T
1132 inline std::u8string
1133 path::generic_u8string() const { return generic_string<char8_t>(); }
1134#else
1135 inline std::string
1136 path::generic_u8string() const { return generic_string<char>(); }
1137#endif
1138
1139 inline std::u16string
1140 path::generic_u16string() const { return generic_string<char16_t>(); }
1141
1142 inline std::u32string
1143 path::generic_u32string() const { return generic_string<char32_t>(); }
1144
1145 inline int
1146 path::compare(const string_type& __s) const { return compare(path(__s)); }
1147
1148 inline int
1149 path::compare(const value_type* __s) const { return compare(path(__s)); }
1150
1151#if __cplusplus >= 201402L
1152 inline int
1153 path::compare(basic_string_view<value_type> __s) const
1154 { return compare(path(__s)); }
1155#endif
1156
1157 inline path
1158 path::filename() const { return empty() ? path() : *--end(); }
1159
1160 inline path
1161 path::stem() const
1162 {
1163 auto ext = _M_find_extension();
1164 if (ext.first && ext.second != 0)
1165 return path{ext.first->substr(0, ext.second)};
1166 return {};
1167 }
1168
1169 inline path
1170 path::extension() const
1171 {
1172 auto ext = _M_find_extension();
1173 if (ext.first && ext.second != string_type::npos)
1174 return path{ext.first->substr(ext.second)};
1175 return {};
1176 }
1177
1178 inline bool
1179 path::has_stem() const
1180 {
1181 auto ext = _M_find_extension();
1182 return ext.first && ext.second != 0;
1183 }
1184
1185 inline bool
1186 path::has_extension() const
1187 {
1188 auto ext = _M_find_extension();
1189 return ext.first && ext.second != string_type::npos;
1190 }
1191
1192 inline bool
1193 path::is_absolute() const
1194 {
1195#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1196 return has_root_name() && has_root_directory();
1197#else
1198 return has_root_directory();
1199#endif
1200 }
1201
1202 inline path::iterator
1203 path::begin() const
1204 {
1205 if (_M_type == _Type::_Multi)
1206 return iterator(this, _M_cmpts.begin());
1207 return iterator(this, false);
1208 }
1209
1210 inline path::iterator
1211 path::end() const
1212 {
1213 if (_M_type == _Type::_Multi)
1214 return iterator(this, _M_cmpts.end());
1215 return iterator(this, true);
1216 }
1217
1218 inline path::iterator&
1219 path::iterator::operator++()
1220 {
1221 __glibcxx_assert(_M_path != nullptr);
1222 if (_M_path->_M_type == _Type::_Multi)
1223 {
1224 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1225 ++_M_cur;
1226 }
1227 else
1228 {
1229 __glibcxx_assert(!_M_at_end);
1230 _M_at_end = true;
1231 }
1232 return *this;
1233 }
1234
1235 inline path::iterator&
1236 path::iterator::operator--()
1237 {
1238 __glibcxx_assert(_M_path != nullptr);
1239 if (_M_path->_M_type == _Type::_Multi)
1240 {
1241 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
1242 --_M_cur;
1243 }
1244 else
1245 {
1246 __glibcxx_assert(_M_at_end);
1247 _M_at_end = false;
1248 }
1249 return *this;
1250 }
1251
1252 inline path::iterator::reference
1253 path::iterator::operator*() const
1254 {
1255 __glibcxx_assert(_M_path != nullptr);
1256 if (_M_path->_M_type == _Type::_Multi)
1257 {
1258 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1259 return *_M_cur;
1260 }
1261 return *_M_path;
1262 }
1263
1264 inline bool
1265 path::iterator::_M_equals(iterator __rhs) const
1266 {
1267 if (_M_path != __rhs._M_path)
1268 return false;
1269 if (_M_path == nullptr)
1270 return true;
1271 if (_M_path->_M_type == path::_Type::_Multi)
1272 return _M_cur == __rhs._M_cur;
1273 return _M_at_end == __rhs._M_at_end;
1274 }
1275
1276 // Define these now that path and path::iterator are complete.
1277 // They needs to consider the string_view(Range&&) constructor during
1278 // overload resolution, which depends on whether range<path> is satisfied,
1279 // which depends on whether path::iterator is complete.
1280 inline bool operator<(const path& __lhs, const path& __rhs) noexcept
1281 { return __lhs.compare(__rhs) < 0; }
1282
1283 inline bool operator==(const path& __lhs, const path& __rhs) noexcept
1284 { return __lhs.compare(__rhs) == 0; }
1285
1286 /// @} group filesystem-ts
1287_GLIBCXX_END_NAMESPACE_CXX11
1288} // namespace v1
1289} // namespace filesystem
1290} // namespace experimental
1291
1292_GLIBCXX_END_NAMESPACE_VERSION
1293} // namespace std
1294
1295#endif // C++11
1296
1297#endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H
Note: See TracBrowser for help on using the repository browser.