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