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