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