libstdc++
experimental/bits/fs_path.h
Go to the documentation of this file.
1 // Class filesystem::path -*- C++ -*-
2 
3 // Copyright (C) 2014-2019 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 
56 namespace std _GLIBCXX_VISIBILITY(default)
57 {
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 
60 namespace experimental
61 {
62 namespace filesystem
63 {
64 inline 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  /**
75  * @ingroup filesystem-ts
76  * @{
77  */
78 
79  /// A filesystem path.
80  class path
81  {
82  template<typename _CharT,
83  typename _Ch = typename remove_const<_CharT>::type>
84  using __is_encoded_char
85  = __or_<is_same<_Ch, char>,
87 #ifdef _GLIBCXX_USE_CHAR8_T
89 #endif
92 
93  template<typename _Iter,
94  typename _Iter_traits = std::iterator_traits<_Iter>>
95  using __is_path_iter_src
96  = __and_<__is_encoded_char<typename _Iter_traits::value_type>,
98  typename _Iter_traits::iterator_category>>;
99 
100  template<typename _Iter>
101  static __is_path_iter_src<_Iter>
102  __is_path_src(_Iter, int);
103 
104  template<typename _CharT, typename _Traits, typename _Alloc>
105  static __is_encoded_char<_CharT>
106  __is_path_src(const basic_string<_CharT, _Traits, _Alloc>&, int);
107 
108 #if __cplusplus >= 201402L
109  template<typename _CharT, typename _Traits>
110  static __is_encoded_char<_CharT>
111  __is_path_src(const basic_string_view<_CharT, _Traits>&, int);
112 #endif
113 
114  template<typename _Unknown>
115  static std::false_type
116  __is_path_src(const _Unknown&, ...);
117 
118  template<typename _Tp1, typename _Tp2>
119  struct __constructible_from;
120 
121  template<typename _Iter>
122  struct __constructible_from<_Iter, _Iter>
123  : __is_path_iter_src<_Iter>
124  { };
125 
126  template<typename _Source>
127  struct __constructible_from<_Source, void>
128  : decltype(__is_path_src(std::declval<_Source>(), 0))
129  { };
130 
131  template<typename _Tp1, typename _Tp2 = void,
132  typename _Tp1_nocv = typename remove_cv<_Tp1>::type,
133  typename _Tp1_noptr = typename remove_pointer<_Tp1>::type>
134  using _Path = typename
136  __not_<is_void<_Tp1_noptr>>,
137  __constructible_from<_Tp1, _Tp2>>::value,
138  path>::type;
139 
140  template<typename _Source>
141  static _Source
142  _S_range_begin(_Source __begin) { return __begin; }
143 
144  struct __null_terminated { };
145 
146  template<typename _Source>
147  static __null_terminated
148  _S_range_end(_Source) { return {}; }
149 
150  template<typename _CharT, typename _Traits, typename _Alloc>
151  static const _CharT*
152  _S_range_begin(const basic_string<_CharT, _Traits, _Alloc>& __str)
153  { return __str.data(); }
154 
155  template<typename _CharT, typename _Traits, typename _Alloc>
156  static const _CharT*
157  _S_range_end(const basic_string<_CharT, _Traits, _Alloc>& __str)
158  { return __str.data() + __str.size(); }
159 
160 #if __cplusplus >= 201402L
161  template<typename _CharT, typename _Traits>
162  static const _CharT*
163  _S_range_begin(const basic_string_view<_CharT, _Traits>& __str)
164  { return __str.data(); }
165 
166  template<typename _CharT, typename _Traits>
167  static const _CharT*
168  _S_range_end(const basic_string_view<_CharT, _Traits>& __str)
169  { return __str.data() + __str.size(); }
170 #endif
171 
172  template<typename _Tp,
173  typename _Iter = decltype(_S_range_begin(std::declval<_Tp>())),
174  typename _Val = typename std::iterator_traits<_Iter>::value_type>
175  using __value_type_is_char = typename std::enable_if<
177  >::type;
178 
179  public:
180 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
181  typedef wchar_t value_type;
182  static constexpr value_type preferred_separator = L'\\';
183 #else
184  typedef char value_type;
185  static constexpr value_type preferred_separator = '/';
186 #endif
188 
189  // constructors and destructor
190 
191  path() noexcept { }
192 
193  path(const path& __p) = default;
194 
195  path(path&& __p) noexcept
196  : _M_pathname(std::move(__p._M_pathname)), _M_type(__p._M_type)
197  {
198  _M_split_cmpts();
199  __p.clear();
200  }
201 
202  path(string_type&& __source)
203  : _M_pathname(std::move(__source))
204  { _M_split_cmpts(); }
205 
206  template<typename _Source,
207  typename _Require = _Path<_Source>>
208  path(_Source const& __source)
209  : _M_pathname(_S_convert(_S_range_begin(__source),
210  _S_range_end(__source)))
211  { _M_split_cmpts(); }
212 
213  template<typename _InputIterator,
214  typename _Require = _Path<_InputIterator, _InputIterator>>
215  path(_InputIterator __first, _InputIterator __last)
216  : _M_pathname(_S_convert(__first, __last))
217  { _M_split_cmpts(); }
218 
219  template<typename _Source,
220  typename _Require = _Path<_Source>,
221  typename _Require2 = __value_type_is_char<_Source>>
222  path(_Source const& __source, const locale& __loc)
223  : _M_pathname(_S_convert_loc(_S_range_begin(__source),
224  _S_range_end(__source), __loc))
225  { _M_split_cmpts(); }
226 
227  template<typename _InputIterator,
228  typename _Require = _Path<_InputIterator, _InputIterator>,
229  typename _Require2 = __value_type_is_char<_InputIterator>>
230  path(_InputIterator __first, _InputIterator __last, const locale& __loc)
231  : _M_pathname(_S_convert_loc(__first, __last, __loc))
232  { _M_split_cmpts(); }
233 
234  ~path() = default;
235 
236  // assignments
237 
238  path& operator=(const path& __p) = default;
239  path& operator=(path&& __p) noexcept;
240  path& operator=(string_type&& __source);
241  path& assign(string_type&& __source);
242 
243  template<typename _Source>
244  _Path<_Source>&
245  operator=(_Source const& __source)
246  { return *this = path(__source); }
247 
248  template<typename _Source>
249  _Path<_Source>&
250  assign(_Source const& __source)
251  { return *this = path(__source); }
252 
253  template<typename _InputIterator>
254  _Path<_InputIterator, _InputIterator>&
255  assign(_InputIterator __first, _InputIterator __last)
256  { return *this = path(__first, __last); }
257 
258  // appends
259 
260  path& operator/=(const path& __p) { return _M_append(__p._M_pathname); }
261 
262  template <class _Source>
263  _Path<_Source>&
264  operator/=(_Source const& __source)
265  { return append(__source); }
266 
267  template<typename _Source>
268  _Path<_Source>&
269  append(_Source const& __source)
270  {
271  return _M_append(_S_convert(_S_range_begin(__source),
272  _S_range_end(__source)));
273  }
274 
275  template<typename _InputIterator>
276  _Path<_InputIterator, _InputIterator>&
277  append(_InputIterator __first, _InputIterator __last)
278  { return _M_append(_S_convert(__first, __last)); }
279 
280  // concatenation
281 
282  path& operator+=(const path& __x);
283  path& operator+=(const string_type& __x);
284  path& operator+=(const value_type* __x);
285  path& operator+=(value_type __x);
286 #if __cplusplus >= 201402L
287  path& operator+=(basic_string_view<value_type> __x);
288 #endif
289 
290  template<typename _Source>
291  _Path<_Source>&
292  operator+=(_Source const& __x) { return concat(__x); }
293 
294  template<typename _CharT>
295  _Path<_CharT*, _CharT*>&
296  operator+=(_CharT __x);
297 
298  template<typename _Source>
299  _Path<_Source>&
300  concat(_Source const& __x)
301  { return *this += _S_convert(_S_range_begin(__x), _S_range_end(__x)); }
302 
303  template<typename _InputIterator>
304  _Path<_InputIterator, _InputIterator>&
305  concat(_InputIterator __first, _InputIterator __last)
306  { return *this += _S_convert(__first, __last); }
307 
308  // modifiers
309 
310  void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); }
311 
312  path& make_preferred();
313  path& remove_filename();
314  path& replace_filename(const path& __replacement);
315  path& replace_extension(const path& __replacement = path());
316 
317  void swap(path& __rhs) noexcept;
318 
319  // native format observers
320 
321  const string_type& native() const noexcept { return _M_pathname; }
322  const value_type* c_str() const noexcept { return _M_pathname.c_str(); }
323  operator string_type() const { return _M_pathname; }
324 
325  template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
326  typename _Allocator = std::allocator<_CharT>>
328  string(const _Allocator& __a = _Allocator()) const;
329 
330  std::string string() const;
331 #if _GLIBCXX_USE_WCHAR_T
332  std::wstring wstring() const;
333 #endif
334 #ifdef _GLIBCXX_USE_CHAR8_T
335  __attribute__((__abi_tag__("__u8")))
336  std::u8string u8string() const;
337 #else
338  std::string u8string() const;
339 #endif // _GLIBCXX_USE_CHAR8_T
340  std::u16string u16string() const;
341  std::u32string u32string() const;
342 
343  // generic format observers
344  template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
345  typename _Allocator = std::allocator<_CharT>>
347  generic_string(const _Allocator& __a = _Allocator()) const;
348 
349  std::string generic_string() const;
350 #if _GLIBCXX_USE_WCHAR_T
351  std::wstring generic_wstring() const;
352 #endif
353 #ifdef _GLIBCXX_USE_CHAR8_T
354  __attribute__((__abi_tag__("__u8")))
355  std::u8string generic_u8string() const;
356 #else
357  std::string generic_u8string() const;
358 #endif // _GLIBCXX_USE_CHAR8_T
359  std::u16string generic_u16string() const;
360  std::u32string generic_u32string() const;
361 
362  // compare
363 
364  int compare(const path& __p) const noexcept;
365  int compare(const string_type& __s) const;
366  int compare(const value_type* __s) const;
367 #if __cplusplus >= 201402L
368  int compare(const basic_string_view<value_type> __s) const;
369 #endif
370 
371  // decomposition
372 
373  path root_name() const;
374  path root_directory() const;
375  path root_path() const;
376  path relative_path() const;
377  path parent_path() const;
378  path filename() const;
379  path stem() const;
380  path extension() const;
381 
382  // query
383 
384  _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_pathname.empty(); }
385  bool has_root_name() const;
386  bool has_root_directory() const;
387  bool has_root_path() const;
388  bool has_relative_path() const;
389  bool has_parent_path() const;
390  bool has_filename() const;
391  bool has_stem() const;
392  bool has_extension() const;
393  bool is_absolute() const;
394  bool is_relative() const { return !is_absolute(); }
395 
396  // iterators
397  class iterator;
398  typedef iterator const_iterator;
399 
400  iterator begin() const;
401  iterator end() const;
402 
403  // Create a basic_string by reading until a null character.
404  template<typename _InputIterator,
405  typename _Traits = std::iterator_traits<_InputIterator>,
406  typename _CharT
407  = typename std::remove_cv<typename _Traits::value_type>::type>
409  _S_string_from_iter(_InputIterator __source)
410  {
412  for (_CharT __ch = *__source; __ch != _CharT(); __ch = *++__source)
413  __str.push_back(__ch);
414  return __str;
415  }
416 
417  private:
418  enum class _Type : unsigned char {
419  _Multi, _Root_name, _Root_dir, _Filename
420  };
421 
422  path(string_type __str, _Type __type) : _M_pathname(__str), _M_type(__type)
423  {
424  __glibcxx_assert(!empty());
425  __glibcxx_assert(_M_type != _Type::_Multi);
426  }
427 
428  enum class _Split { _Stem, _Extension };
429 
430  path& _M_append(const string_type& __str)
431  {
432  if (!_M_pathname.empty() && !_S_is_dir_sep(_M_pathname.back())
433  && !__str.empty() && !_S_is_dir_sep(__str.front()))
434  _M_pathname += preferred_separator;
435  _M_pathname += __str;
436  _M_split_cmpts();
437  return *this;
438  }
439 
440  pair<const string_type*, size_t> _M_find_extension() const;
441 
442  template<typename _CharT>
443  struct _Cvt;
444 
445  static string_type
446  _S_convert(value_type* __src, __null_terminated)
447  { return string_type(__src); }
448 
449  static string_type
450  _S_convert(const value_type* __src, __null_terminated)
451  { return string_type(__src); }
452 
453  template<typename _Iter>
454  static string_type
455  _S_convert(_Iter __first, _Iter __last)
456  {
457  using __value_type = typename std::iterator_traits<_Iter>::value_type;
458  return _Cvt<typename remove_cv<__value_type>::type>::
459  _S_convert(__first, __last);
460  }
461 
462  template<typename _InputIterator>
463  static string_type
464  _S_convert(_InputIterator __src, __null_terminated)
465  {
466  auto __s = _S_string_from_iter(__src);
467  return _S_convert(__s.c_str(), __s.c_str() + __s.size());
468  }
469 
470  static string_type
471  _S_convert_loc(const char* __first, const char* __last,
472  const std::locale& __loc);
473 
474  template<typename _Iter>
475  static string_type
476  _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc)
477  {
478  const std::string __str(__first, __last);
479  return _S_convert_loc(__str.data(), __str.data()+__str.size(), __loc);
480  }
481 
482  template<typename _InputIterator>
483  static string_type
484  _S_convert_loc(_InputIterator __src, __null_terminated,
485  const std::locale& __loc)
486  {
487  std::string __s = _S_string_from_iter(__src);
488  return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
489  }
490 
491  bool _S_is_dir_sep(value_type __ch)
492  {
493 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
494  return __ch == L'/' || __ch == preferred_separator;
495 #else
496  return __ch == '/';
497 #endif
498  }
499 
500  void _M_split_cmpts();
501  void _M_trim();
502  void _M_add_root_name(size_t __n);
503  void _M_add_root_dir(size_t __pos);
504  void _M_add_filename(size_t __pos, size_t __n);
505 
506  string_type _M_pathname;
507 
508  struct _Cmpt;
509  using _List = _GLIBCXX_STD_C::vector<_Cmpt>;
510  _List _M_cmpts; // empty unless _M_type == _Type::_Multi
511  _Type _M_type = _Type::_Multi;
512  };
513 
514  inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); }
515 
516  size_t hash_value(const path& __p) noexcept;
517 
518  /// Compare paths
519  inline bool operator<(const path& __lhs, const path& __rhs) noexcept
520  { return __lhs.compare(__rhs) < 0; }
521 
522  /// Compare paths
523  inline bool operator<=(const path& __lhs, const path& __rhs) noexcept
524  { return !(__rhs < __lhs); }
525 
526  /// Compare paths
527  inline bool operator>(const path& __lhs, const path& __rhs) noexcept
528  { return __rhs < __lhs; }
529 
530  /// Compare paths
531  inline bool operator>=(const path& __lhs, const path& __rhs) noexcept
532  { return !(__lhs < __rhs); }
533 
534  /// Compare paths
535  inline bool operator==(const path& __lhs, const path& __rhs) noexcept
536  { return __lhs.compare(__rhs) == 0; }
537 
538  /// Compare paths
539  inline bool operator!=(const path& __lhs, const path& __rhs) noexcept
540  { return !(__lhs == __rhs); }
541 
542  /// Append one path to another
543  inline path operator/(const path& __lhs, const path& __rhs)
544  {
545  path __result(__lhs);
546  __result /= __rhs;
547  return __result;
548  }
549 
550  /// Write a path to a stream
551  template<typename _CharT, typename _Traits>
553  operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p)
554  {
555  auto __tmp = __p.string<_CharT, _Traits>();
556  using __quoted_string
558  __os << __quoted_string{__tmp, _CharT('"'), _CharT('\\')};
559  return __os;
560  }
561 
562  /// Read a path from a stream
563  template<typename _CharT, typename _Traits>
566  {
568  using __quoted_string
570  if (__is >> __quoted_string{ __tmp, _CharT('"'), _CharT('\\') })
571  __p = std::move(__tmp);
572  return __is;
573  }
574 
575  // TODO constrain with _Path<Source> and __value_type_is_char
576  template<typename _Source>
577  inline path
578  u8path(const _Source& __source)
579  {
580 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
581  return path{ path::string_type{__source} };
582 #else
583  return path{ __source };
584 #endif
585  }
586 
587  // TODO constrain with _Path<InputIterator, InputIterator> and __value_type_is_char
588  template<typename _InputIterator>
589  inline path
590  u8path(_InputIterator __first, _InputIterator __last)
591  {
592 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
593  return path{ path::string_type{__first, __last} };
594 #else
595  return path{ __first, __last };
596 #endif
597  }
598 
599  class filesystem_error : public std::system_error
600  {
601  public:
602  filesystem_error(const string& __what_arg, error_code __ec)
603  : system_error(__ec, __what_arg) { }
604 
605  filesystem_error(const string& __what_arg, const path& __p1,
606  error_code __ec)
607  : system_error(__ec, __what_arg), _M_path1(__p1) { }
608 
609  filesystem_error(const string& __what_arg, const path& __p1,
610  const path& __p2, error_code __ec)
611  : system_error(__ec, __what_arg), _M_path1(__p1), _M_path2(__p2)
612  { }
613 
614  ~filesystem_error();
615 
616  const path& path1() const noexcept { return _M_path1; }
617  const path& path2() const noexcept { return _M_path2; }
618  const char* what() const noexcept { return _M_what.c_str(); }
619 
620  private:
621  std::string _M_gen_what();
622 
623  path _M_path1;
624  path _M_path2;
625  std::string _M_what = _M_gen_what();
626  };
627 
628  struct path::_Cmpt : path
629  {
630  _Cmpt(string_type __s, _Type __t, size_t __pos)
631  : path(std::move(__s), __t), _M_pos(__pos) { }
632 
633  _Cmpt() : _M_pos(-1) { }
634 
635  size_t _M_pos;
636  };
637 
638  // specialize _Cvt for degenerate 'noconv' case
639  template<>
640  struct path::_Cvt<path::value_type>
641  {
642  template<typename _Iter>
643  static string_type
644  _S_convert(_Iter __first, _Iter __last)
645  { return string_type{__first, __last}; }
646  };
647 
648  template<typename _CharT>
649  struct path::_Cvt
650  {
651 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
652  static string_type
653  _S_wconvert(const char* __f, const char* __l, true_type)
654  {
656  const auto& __cvt = std::use_facet<_Cvt>(std::locale{});
657  std::wstring __wstr;
658  if (__str_codecvt_in(__f, __l, __wstr, __cvt))
659  return __wstr;
660  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
661  "Cannot convert character sequence",
662  std::make_error_code(errc::illegal_byte_sequence)));
663  }
664 
665  static string_type
666  _S_wconvert(const _CharT* __f, const _CharT* __l, false_type)
667  {
668  std::codecvt_utf8<_CharT> __cvt;
669  std::string __str;
670  if (__str_codecvt_out(__f, __l, __str, __cvt))
671  {
672  const char* __f2 = __str.data();
673  const char* __l2 = __f2 + __str.size();
674  std::codecvt_utf8<wchar_t> __wcvt;
675  std::wstring __wstr;
676  if (__str_codecvt_in(__f2, __l2, __wstr, __wcvt))
677  return __wstr;
678  }
679  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
680  "Cannot convert character sequence",
681  std::make_error_code(errc::illegal_byte_sequence)));
682  }
683 
684  static string_type
685  _S_convert(const _CharT* __f, const _CharT* __l)
686  {
687  return _S_wconvert(__f, __l, is_same<_CharT, char>{});
688  }
689 #else
690  static string_type
691  _S_convert(const _CharT* __f, const _CharT* __l)
692  {
693 #ifdef _GLIBCXX_USE_CHAR8_T
694  if constexpr (is_same<_CharT, char8_t>::value)
695  {
696  string_type __str(__f, __l);
697  return __str;
698  }
699  else
700  {
701 #endif
702  std::codecvt_utf8<_CharT> __cvt;
703  std::string __str;
704  if (__str_codecvt_out(__f, __l, __str, __cvt))
705  return __str;
706 #ifdef _GLIBCXX_USE_CHAR8_T
707  }
708 #endif
709  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
710  "Cannot convert character sequence",
711  std::make_error_code(errc::illegal_byte_sequence)));
712  }
713 #endif
714 
715  static string_type
716  _S_convert(_CharT* __f, _CharT* __l)
717  {
718  return _S_convert(const_cast<const _CharT*>(__f),
719  const_cast<const _CharT*>(__l));
720  }
721 
722  template<typename _Iter>
723  static string_type
724  _S_convert(_Iter __first, _Iter __last)
725  {
726  const std::basic_string<_CharT> __str(__first, __last);
727  return _S_convert(__str.data(), __str.data() + __str.size());
728  }
729 
730  template<typename _Iter, typename _Cont>
731  static string_type
732  _S_convert(__gnu_cxx::__normal_iterator<_Iter, _Cont> __first,
733  __gnu_cxx::__normal_iterator<_Iter, _Cont> __last)
734  { return _S_convert(__first.base(), __last.base()); }
735  };
736 
737  /// An iterator for the components of a path
739  {
740  public:
741  using difference_type = std::ptrdiff_t;
742  using value_type = path;
743  using reference = const path&;
744  using pointer = const path*;
746 
747  iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
748 
749  iterator(const iterator&) = default;
750  iterator& operator=(const iterator&) = default;
751 
752  reference operator*() const;
753  pointer operator->() const { return std::__addressof(**this); }
754 
755  iterator& operator++();
756  iterator operator++(int) { auto __tmp = *this; ++*this; return __tmp; }
757 
758  iterator& operator--();
759  iterator operator--(int) { auto __tmp = *this; --*this; return __tmp; }
760 
761  friend bool operator==(const iterator& __lhs, const iterator& __rhs)
762  { return __lhs._M_equals(__rhs); }
763 
764  friend bool operator!=(const iterator& __lhs, const iterator& __rhs)
765  { return !__lhs._M_equals(__rhs); }
766 
767  private:
768  friend class path;
769 
770  iterator(const path* __path, path::_List::const_iterator __iter)
771  : _M_path(__path), _M_cur(__iter), _M_at_end()
772  { }
773 
774  iterator(const path* __path, bool __at_end)
775  : _M_path(__path), _M_cur(), _M_at_end(__at_end)
776  { }
777 
778  bool _M_equals(iterator) const;
779 
780  const path* _M_path;
781  path::_List::const_iterator _M_cur;
782  bool _M_at_end; // only used when type != _Multi
783  };
784 
785 
786  inline path&
787  path::operator=(path&& __p) noexcept
788  {
789  _M_pathname = std::move(__p._M_pathname);
790  _M_cmpts = std::move(__p._M_cmpts);
791  _M_type = __p._M_type;
792  __p.clear();
793  return *this;
794  }
795 
796  inline path&
797  path::operator=(string_type&& __source)
798  { return *this = path(std::move(__source)); }
799 
800  inline path&
801  path::assign(string_type&& __source)
802  { return *this = path(std::move(__source)); }
803 
804  inline path&
805  path::operator+=(const path& __p)
806  {
807  return operator+=(__p.native());
808  }
809 
810  inline path&
811  path::operator+=(const string_type& __x)
812  {
813  _M_pathname += __x;
814  _M_split_cmpts();
815  return *this;
816  }
817 
818  inline path&
819  path::operator+=(const value_type* __x)
820  {
821  _M_pathname += __x;
822  _M_split_cmpts();
823  return *this;
824  }
825 
826  inline path&
827  path::operator+=(value_type __x)
828  {
829  _M_pathname += __x;
830  _M_split_cmpts();
831  return *this;
832  }
833 
834 #if __cplusplus >= 201402L
835  inline path&
836  path::operator+=(basic_string_view<value_type> __x)
837  {
838  _M_pathname.append(__x.data(), __x.size());
839  _M_split_cmpts();
840  return *this;
841  }
842 #endif
843 
844  template<typename _CharT>
845  inline path::_Path<_CharT*, _CharT*>&
846  path::operator+=(_CharT __x)
847  {
848  auto* __addr = std::__addressof(__x);
849  return concat(__addr, __addr + 1);
850  }
851 
852  inline path&
853  path::make_preferred()
854  {
855 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
856  std::replace(_M_pathname.begin(), _M_pathname.end(), L'/',
857  preferred_separator);
858 #endif
859  return *this;
860  }
861 
862  inline void path::swap(path& __rhs) noexcept
863  {
864  _M_pathname.swap(__rhs._M_pathname);
865  _M_cmpts.swap(__rhs._M_cmpts);
866  std::swap(_M_type, __rhs._M_type);
867  }
868 
869  template<typename _CharT, typename _Traits, typename _Allocator>
871  path::string(const _Allocator& __a) const
872  {
874  return { _M_pathname.begin(), _M_pathname.end(), __a };
875 
876  const value_type* __first = _M_pathname.data();
877  const value_type* __last = __first + _M_pathname.size();
878 
879 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
880  using _CharAlloc = __alloc_rebind<_Allocator, char>;
881  using _String = basic_string<char, char_traits<char>, _CharAlloc>;
883 
884  // use codecvt_utf8<wchar_t> to convert native string to UTF-8
885  codecvt_utf8<value_type> __cvt;
886  _String __u8str{_CharAlloc{__a}};
887  if (__str_codecvt_out(__first, __last, __u8str, __cvt))
888  {
889  struct
890  {
891  const _String*
892  operator()(const _String& __from, _String&, true_type)
893  { return std::__addressof(__from); }
894 
895  _WString*
896  operator()(const _String& __from, _WString& __to, false_type)
897  {
898 #ifdef _GLIBCXX_USE_CHAR8_T
899  if constexpr (is_same<_CharT, char8_t>::value)
900  {
901  __to.assign(__from.begin(), __from.end());
902  return std::__addressof(__to);
903  }
904  else
905  {
906 #endif
907  // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
908  codecvt_utf8<_CharT> __cvt;
909  const char* __f = __from.data();
910  const char* __l = __f + __from.size();
911  if (__str_codecvt_in(__f, __l, __to, __cvt))
912  return std::__addressof(__to);
913 #ifdef _GLIBCXX_USE_CHAR8_T
914  }
915 #endif
916  return nullptr;
917  }
918  } __dispatch;
919  _WString __wstr;
920  if (auto* __p = __dispatch(__u8str, __wstr, is_same<_CharT, char>{}))
921  return *__p;
922  }
923 #else
924 #ifdef _GLIBCXX_USE_CHAR8_T
925  if constexpr (is_same<_CharT, char8_t>::value)
926  {
927  basic_string<_CharT, _Traits, _Allocator> __wstr{__first, __last, __a};
928  return __wstr;
929  }
930  else
931  {
932 #endif
933  codecvt_utf8<_CharT> __cvt;
935  if (__str_codecvt_in(__first, __last, __wstr, __cvt))
936  return __wstr;
937 #ifdef _GLIBCXX_USE_CHAR8_T
938  }
939 #endif
940 #endif
941  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
942  "Cannot convert character sequence",
943  std::make_error_code(errc::illegal_byte_sequence)));
944  }
945 
946  inline std::string
947  path::string() const { return string<char>(); }
948 
949 #if _GLIBCXX_USE_WCHAR_T
950  inline std::wstring
951  path::wstring() const { return string<wchar_t>(); }
952 #endif
953 
954 #ifdef _GLIBCXX_USE_CHAR8_T
955  inline std::u8string
956  path::u8string() const { return string<char8_t>(); }
957 #else
958  inline std::string
959  path::u8string() const
960  {
961 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
962  std::string __str;
963  // convert from native encoding to UTF-8
964  codecvt_utf8<value_type> __cvt;
965  const value_type* __first = _M_pathname.data();
966  const value_type* __last = __first + _M_pathname.size();
967  if (__str_codecvt_out(__first, __last, __str, __cvt))
968  return __str;
969  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
970  "Cannot convert character sequence",
971  std::make_error_code(errc::illegal_byte_sequence)));
972 #else
973  return _M_pathname;
974 #endif
975  }
976 #endif // _GLIBCXX_USE_CHAR8_T
977 
978  inline std::u16string
979  path::u16string() const { return string<char16_t>(); }
980 
981  inline std::u32string
982  path::u32string() const { return string<char32_t>(); }
983 
984 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
985  template<typename _CharT, typename _Traits, typename _Allocator>
987  path::generic_string(const _Allocator& __a) const
988  { return string<_CharT, _Traits, _Allocator>(__a); }
989 
990  inline std::string
991  path::generic_string() const { return string(); }
992 
993 #if _GLIBCXX_USE_WCHAR_T
994  inline std::wstring
995  path::generic_wstring() const { return wstring(); }
996 #endif
997 
998 #ifdef _GLIBCXX_USE_CHAR8_T
999  inline std::u8string
1000  path::generic_u8string() const { return u8string(); }
1001 #else
1002  inline std::string
1003  path::generic_u8string() const { return u8string(); }
1004 #endif
1005 
1006  inline std::u16string
1007  path::generic_u16string() const { return u16string(); }
1008 
1009  inline std::u32string
1010  path::generic_u32string() const { return u32string(); }
1011 #endif
1012 
1013  inline int
1014  path::compare(const string_type& __s) const { return compare(path(__s)); }
1015 
1016  inline int
1017  path::compare(const value_type* __s) const { return compare(path(__s)); }
1018 
1019 #if __cplusplus >= 201402L
1020  inline int
1021  path::compare(basic_string_view<value_type> __s) const
1022  { return compare(path(__s)); }
1023 #endif
1024 
1025  inline path
1026  path::filename() const { return empty() ? path() : *--end(); }
1027 
1028  inline path
1029  path::stem() const
1030  {
1031  auto ext = _M_find_extension();
1032  if (ext.first && ext.second != 0)
1033  return path{ext.first->substr(0, ext.second)};
1034  return {};
1035  }
1036 
1037  inline path
1038  path::extension() const
1039  {
1040  auto ext = _M_find_extension();
1041  if (ext.first && ext.second != string_type::npos)
1042  return path{ext.first->substr(ext.second)};
1043  return {};
1044  }
1045 
1046  inline bool
1047  path::has_stem() const
1048  {
1049  auto ext = _M_find_extension();
1050  return ext.first && ext.second != 0;
1051  }
1052 
1053  inline bool
1054  path::has_extension() const
1055  {
1056  auto ext = _M_find_extension();
1057  return ext.first && ext.second != string_type::npos;
1058  }
1059 
1060  inline bool
1061  path::is_absolute() const
1062  {
1063 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1064  return has_root_name() && has_root_directory();
1065 #else
1066  return has_root_directory();
1067 #endif
1068  }
1069 
1070  inline path::iterator
1071  path::begin() const
1072  {
1073  if (_M_type == _Type::_Multi)
1074  return iterator(this, _M_cmpts.begin());
1075  return iterator(this, false);
1076  }
1077 
1078  inline path::iterator
1079  path::end() const
1080  {
1081  if (_M_type == _Type::_Multi)
1082  return iterator(this, _M_cmpts.end());
1083  return iterator(this, true);
1084  }
1085 
1086  inline path::iterator&
1087  path::iterator::operator++()
1088  {
1089  __glibcxx_assert(_M_path != nullptr);
1090  if (_M_path->_M_type == _Type::_Multi)
1091  {
1092  __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1093  ++_M_cur;
1094  }
1095  else
1096  {
1097  __glibcxx_assert(!_M_at_end);
1098  _M_at_end = true;
1099  }
1100  return *this;
1101  }
1102 
1103  inline path::iterator&
1104  path::iterator::operator--()
1105  {
1106  __glibcxx_assert(_M_path != nullptr);
1107  if (_M_path->_M_type == _Type::_Multi)
1108  {
1109  __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
1110  --_M_cur;
1111  }
1112  else
1113  {
1114  __glibcxx_assert(_M_at_end);
1115  _M_at_end = false;
1116  }
1117  return *this;
1118  }
1119 
1121  path::iterator::operator*() const
1122  {
1123  __glibcxx_assert(_M_path != nullptr);
1124  if (_M_path->_M_type == _Type::_Multi)
1125  {
1126  __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1127  return *_M_cur;
1128  }
1129  return *_M_path;
1130  }
1131 
1132  inline bool
1133  path::iterator::_M_equals(iterator __rhs) const
1134  {
1135  if (_M_path != __rhs._M_path)
1136  return false;
1137  if (_M_path == nullptr)
1138  return true;
1139  if (_M_path->_M_type == path::_Type::_Multi)
1140  return _M_cur == __rhs._M_cur;
1141  return _M_at_end == __rhs._M_at_end;
1142  }
1143 
1144  // @} group filesystem-ts
1145 _GLIBCXX_END_NAMESPACE_CXX11
1146 } // namespace v1
1147 } // namespace filesystem
1148 } // namespace experimental
1149 
1150 _GLIBCXX_END_NAMESPACE_VERSION
1151 } // namespace std
1152 
1153 #endif // C++11
1154 
1155 #endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H
path u8path(const _Source &__source)
Compare paths.
Template class basic_istream.
Definition: iosfwd:83
Thrown to indicate error code of underlying system.
Definition: system_error:341
reference front()
_GLIBCXX20_CONSTEXPR complex< _Tp > operator/(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x divided by y.
Definition: complex:417
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
is_same
Definition: type_traits:1292
Class codecvt<wchar_t, char, mbstate_t> specialization.
Definition: codecvt.h:401
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
ISO C++ entities toplevel namespace is std.
basic_string< char16_t > u16string
A string of char16_t.
Definition: stringfwd.h:93
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:208
Bidirectional iterators support a superset of forward iterator operations.
basic_string< wchar_t > wstring
A string of wchar_t.
Definition: stringfwd.h:83
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:2045
Marking input iterators.
An iterator for the components of a path.
_GLIBCXX_END_NAMESPACE_CXX11 typedef basic_string< char > string
A string of char.
Definition: stringfwd.h:74
Managing sequences of characters and character-like objects.
static const size_type npos
Value returned by various member functions when they fail.
is_base_of
Definition: type_traits:1301
Template class basic_ostream.
Definition: iosfwd:86
error_code
Definition: system_error:146
_GLIBCXX20_CONSTEXPR complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition: complex:387
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1470
basic_string< char32_t > u32string
A string of char32_t.
Definition: stringfwd.h:96
_GLIBCXX_NODISCARD bool empty() const noexcept
size_t hash_value(const path &__p) noexcept
Compare paths.
Container class for localization functionality.The locale class is first a class wrapper for C librar...
const _CharT * data() const noexcept
Return const pointer to contents.
Struct for delimited strings.
Definition: quoted_string.h:49
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
integral_constant
Definition: type_traits:57
void replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__old_value, const _Tp &__new_value)
Replace each occurrence of one value in a sequence with another value.
Definition: stl_algo.h:4346
void push_back(_CharT __c)
Append a single character.