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  { return path(__lhs) /= __rhs; }
553 
554  /// Write a path to a stream
555  template<typename _CharT, typename _Traits>
556  basic_ostream<_CharT, _Traits>&
557  operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p)
558  {
559  auto __tmp = __p.string<_CharT, _Traits>();
560  using __quoted_string
562  __os << __quoted_string{__tmp, '"', '\\'};
563  return __os;
564  }
565 
566  /// Read a path from a stream
567  template<typename _CharT, typename _Traits>
568  basic_istream<_CharT, _Traits>&
569  operator>>(basic_istream<_CharT, _Traits>& __is, path& __p)
570  {
571  basic_string<_CharT, _Traits> __tmp;
572  using __quoted_string
574  if (__is >> __quoted_string{ __tmp, '"', '\\' })
575  __p = std::move(__tmp);
576  return __is;
577  }
578 
579  template<typename _Source>
580  inline auto
581  u8path(const _Source& __source)
582  -> decltype(filesystem::path(__source, std::locale::classic()))
583  {
584 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
585  const std::string __u8str{__source};
586  return std::filesystem::u8path(__u8str.begin(), __u8str.end());
587 #else
588  return path{ __source };
589 #endif
590  }
591 
592  template<typename _InputIterator>
593  inline auto
594  u8path(_InputIterator __first, _InputIterator __last)
595  -> decltype(filesystem::path(__first, __last, std::locale::classic()))
596  {
597 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
598  codecvt_utf8<value_type> __cvt;
599  string_type __tmp;
600  if (__str_codecvt_in(__first, __last, __tmp, __cvt))
601  return path{ __tmp };
602  else
603  return {};
604 #else
605  return path{ __first, __last };
606 #endif
607  }
608 
609  class filesystem_error : public std::system_error
610  {
611  public:
612  filesystem_error(const string& __what_arg, error_code __ec)
613  : system_error(__ec, __what_arg) { }
614 
615  filesystem_error(const string& __what_arg, const path& __p1,
616  error_code __ec)
617  : system_error(__ec, __what_arg), _M_path1(__p1) { }
618 
619  filesystem_error(const string& __what_arg, const path& __p1,
620  const path& __p2, error_code __ec)
621  : system_error(__ec, __what_arg), _M_path1(__p1), _M_path2(__p2)
622  { }
623 
624  ~filesystem_error();
625 
626  const path& path1() const noexcept { return _M_path1; }
627  const path& path2() const noexcept { return _M_path2; }
628  const char* what() const noexcept { return _M_what.c_str(); }
629 
630  private:
631  std::string _M_gen_what();
632 
633  path _M_path1;
634  path _M_path2;
635  std::string _M_what = _M_gen_what();
636  };
637 
638  struct path::_Cmpt : path
639  {
640  _Cmpt(string_type __s, _Type __t, size_t __pos)
641  : path(std::move(__s), __t), _M_pos(__pos) { }
642 
643  _Cmpt() : _M_pos(-1) { }
644 
645  size_t _M_pos;
646  };
647 
648  // specialize _Cvt for degenerate 'noconv' case
649  template<>
650  struct path::_Cvt<path::value_type>
651  {
652  template<typename _Iter>
653  static string_type
654  _S_convert(_Iter __first, _Iter __last)
655  { return string_type{__first, __last}; }
656  };
657 
658  template<typename _CharT>
659  struct path::_Cvt
660  {
661 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
662  static string_type
663  _S_wconvert(const char* __f, const char* __l, true_type)
664  {
666  const auto& __cvt = std::use_facet<_Cvt>(std::locale{});
667  std::wstring __wstr;
668  if (__str_codecvt_in(__f, __l, __wstr, __cvt))
669  return __wstr;
670  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
671  "Cannot convert character sequence",
672  std::make_error_code(errc::illegal_byte_sequence)));
673  }
674 
675  static string_type
676  _S_wconvert(const _CharT* __f, const _CharT* __l, false_type)
677  {
678  std::codecvt_utf8<_CharT> __cvt;
679  std::string __str;
680  if (__str_codecvt_out(__f, __l, __str, __cvt))
681  {
682  const char* __f2 = __str.data();
683  const char* __l2 = __f2 + __str.size();
684  std::codecvt_utf8<wchar_t> __wcvt;
685  std::wstring __wstr;
686  if (__str_codecvt_in(__f2, __l2, __wstr, __wcvt))
687  return __wstr;
688  }
689  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
690  "Cannot convert character sequence",
691  std::make_error_code(errc::illegal_byte_sequence)));
692  }
693 
694  static string_type
695  _S_convert(const _CharT* __f, const _CharT* __l)
696  {
697  return _S_wconvert(__f, __l, is_same<_CharT, char>{});
698  }
699 #else
700  static string_type
701  _S_convert(const _CharT* __f, const _CharT* __l)
702  {
703  std::codecvt_utf8<_CharT> __cvt;
704  std::string __str;
705  if (__str_codecvt_out(__f, __l, __str, __cvt))
706  return __str;
707  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
708  "Cannot convert character sequence",
709  std::make_error_code(errc::illegal_byte_sequence)));
710  }
711 #endif
712 
713  static string_type
714  _S_convert(_CharT* __f, _CharT* __l)
715  {
716  return _S_convert(const_cast<const _CharT*>(__f),
717  const_cast<const _CharT*>(__l));
718  }
719 
720  template<typename _Iter>
721  static string_type
722  _S_convert(_Iter __first, _Iter __last)
723  {
724  const std::basic_string<_CharT> __str(__first, __last);
725  return _S_convert(__str.data(), __str.data() + __str.size());
726  }
727 
728  template<typename _Iter, typename _Cont>
729  static string_type
730  _S_convert(__gnu_cxx::__normal_iterator<_Iter, _Cont> __first,
731  __gnu_cxx::__normal_iterator<_Iter, _Cont> __last)
732  { return _S_convert(__first.base(), __last.base()); }
733  };
734 
735  /// An iterator for the components of a path
736  class path::iterator
737  {
738  public:
739  using difference_type = std::ptrdiff_t;
740  using value_type = path;
741  using reference = const path&;
742  using pointer = const path*;
743  using iterator_category = std::bidirectional_iterator_tag;
744 
745  iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
746 
747  iterator(const iterator&) = default;
748  iterator& operator=(const iterator&) = default;
749 
750  reference operator*() const;
751  pointer operator->() const { return std::__addressof(**this); }
752 
753  iterator& operator++();
754  iterator operator++(int) { auto __tmp = *this; ++*this; return __tmp; }
755 
756  iterator& operator--();
757  iterator operator--(int) { auto __tmp = *this; --*this; return __tmp; }
758 
759  friend bool operator==(const iterator& __lhs, const iterator& __rhs)
760  { return __lhs._M_equals(__rhs); }
761 
762  friend bool operator!=(const iterator& __lhs, const iterator& __rhs)
763  { return !__lhs._M_equals(__rhs); }
764 
765  private:
766  friend class path;
767 
768  iterator(const path* __path, path::_List::const_iterator __iter)
769  : _M_path(__path), _M_cur(__iter), _M_at_end()
770  { }
771 
772  iterator(const path* __path, bool __at_end)
773  : _M_path(__path), _M_cur(), _M_at_end(__at_end)
774  { }
775 
776  bool _M_equals(iterator) const;
777 
778  const path* _M_path;
779  path::_List::const_iterator _M_cur;
780  bool _M_at_end; // only used when type != _Multi
781  };
782 
783 
784  inline path&
785  path::operator=(path&& __p) noexcept
786  {
787  _M_pathname = std::move(__p._M_pathname);
788  _M_cmpts = std::move(__p._M_cmpts);
789  _M_type = __p._M_type;
790  __p.clear();
791  return *this;
792  }
793 
794  inline path&
795  path::operator=(string_type&& __source)
796  { return *this = path(std::move(__source)); }
797 
798  inline path&
799  path::assign(string_type&& __source)
800  { return *this = path(std::move(__source)); }
801 
802  inline path&
803  path::operator+=(const path& __p)
804  {
805  return operator+=(__p.native());
806  }
807 
808  inline path&
809  path::operator+=(const string_type& __x)
810  {
811  _M_pathname += __x;
812  _M_split_cmpts();
813  return *this;
814  }
815 
816  inline path&
817  path::operator+=(const value_type* __x)
818  {
819  _M_pathname += __x;
820  _M_split_cmpts();
821  return *this;
822  }
823 
824  inline path&
825  path::operator+=(value_type __x)
826  {
827  _M_pathname += __x;
828  _M_split_cmpts();
829  return *this;
830  }
831 
832  inline path&
833  path::operator+=(basic_string_view<value_type> __x)
834  {
835  _M_pathname.append(__x.data(), __x.size());
836  _M_split_cmpts();
837  return *this;
838  }
839 
840  template<typename _CharT>
841  inline path::_Path<_CharT*, _CharT*>&
842  path::operator+=(_CharT __x)
843  {
844  auto* __addr = std::__addressof(__x);
845  return concat(__addr, __addr + 1);
846  }
847 
848  inline path&
849  path::make_preferred()
850  {
851 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
852  std::replace(_M_pathname.begin(), _M_pathname.end(), L'/',
853  preferred_separator);
854 #endif
855  return *this;
856  }
857 
858  inline void path::swap(path& __rhs) noexcept
859  {
860  _M_pathname.swap(__rhs._M_pathname);
861  _M_cmpts.swap(__rhs._M_cmpts);
862  std::swap(_M_type, __rhs._M_type);
863  }
864 
865  template<typename _CharT, typename _Traits, typename _Allocator>
867  path::_S_str_convert(const string_type& __str, const _Allocator& __a)
868  {
869  if (__str.size() == 0)
871 
872  const value_type* __first = __str.data();
873  const value_type* __last = __first + __str.size();
874 
875 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
876  using _CharAlloc = __alloc_rebind<_Allocator, char>;
877  using _String = basic_string<char, char_traits<char>, _CharAlloc>;
878  using _WString = basic_string<_CharT, _Traits, _Allocator>;
879 
880  // use codecvt_utf8<wchar_t> to convert native string to UTF-8
881  codecvt_utf8<value_type> __cvt;
882  _String __u8str{_CharAlloc{__a}};
883  if (__str_codecvt_out(__first, __last, __u8str, __cvt))
884  {
885  if constexpr (is_same_v<_CharT, char>)
886  return __u8str;
887  else
888  {
889  _WString __wstr;
890  // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
891  codecvt_utf8<_CharT> __cvt;
892  const char* __f = __u8str.data();
893  const char* __l = __f + __u8str.size();
894  if (__str_codecvt_in(__f, __l, __wstr, __cvt))
895  return __wstr;
896  }
897  }
898 #else
899  codecvt_utf8<_CharT> __cvt;
900  basic_string<_CharT, _Traits, _Allocator> __wstr{__a};
901  if (__str_codecvt_in(__first, __last, __wstr, __cvt))
902  return __wstr;
903 #endif
904  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
905  "Cannot convert character sequence",
906  std::make_error_code(errc::illegal_byte_sequence)));
907  }
908 
909  template<typename _CharT, typename _Traits, typename _Allocator>
910  inline basic_string<_CharT, _Traits, _Allocator>
911  path::string(const _Allocator& __a) const
912  {
913  if constexpr (is_same_v<_CharT, value_type>)
914 #if _GLIBCXX_USE_CXX11_ABI
915  return { _M_pathname, __a };
916 #else
917  return { _M_pathname, string_type::size_type(0), __a };
918 #endif
919  else
920  return _S_str_convert<_CharT, _Traits>(_M_pathname, __a);
921  }
922 
923  inline std::string
924  path::string() const { return string<char>(); }
925 
926 #if _GLIBCXX_USE_WCHAR_T
927  inline std::wstring
928  path::wstring() const { return string<wchar_t>(); }
929 #endif
930 
931  inline std::string
932  path::u8string() const
933  {
934 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
935  std::string __str;
936  // convert from native encoding to UTF-8
937  codecvt_utf8<value_type> __cvt;
938  const value_type* __first = _M_pathname.data();
939  const value_type* __last = __first + _M_pathname.size();
940  if (__str_codecvt_out(__first, __last, __str, __cvt))
941  return __str;
942  _GLIBCXX_THROW_OR_ABORT(filesystem_error(
943  "Cannot convert character sequence",
944  std::make_error_code(errc::illegal_byte_sequence)));
945 #else
946  return _M_pathname;
947 #endif
948  }
949 
950  inline std::u16string
951  path::u16string() const { return string<char16_t>(); }
952 
953  inline std::u32string
954  path::u32string() const { return string<char32_t>(); }
955 
956  template<typename _CharT, typename _Traits, typename _Allocator>
958  path::generic_string(const _Allocator& __a) const
959  {
960 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
961  const value_type __slash = L'/';
962 #else
963  const value_type __slash = '/';
964 #endif
965  string_type __str(__a);
966 
967  if (_M_type == _Type::_Root_dir)
968  __str.assign(1, __slash);
969  else
970  {
971  __str.reserve(_M_pathname.size());
972  bool __add_slash = false;
973  for (auto& __elem : *this)
974  {
975  if (__add_slash)
976  __str += __slash;
977  __str += __elem._M_pathname;
978  __add_slash = __elem._M_type == _Type::_Filename;
979  }
980  }
981 
982  if constexpr (is_same_v<_CharT, value_type>)
983  return __str;
984  else
985  return _S_str_convert<_CharT, _Traits>(__str, __a);
986  }
987 
988  inline std::string
989  path::generic_string() const
990  { return generic_string<char>(); }
991 
992 #if _GLIBCXX_USE_WCHAR_T
993  inline std::wstring
994  path::generic_wstring() const
995  { return generic_string<wchar_t>(); }
996 #endif
997 
998  inline std::string
999  path::generic_u8string() const
1000  { return generic_string(); }
1001 
1002  inline std::u16string
1003  path::generic_u16string() const
1004  { return generic_string<char16_t>(); }
1005 
1006  inline std::u32string
1007  path::generic_u32string() const
1008  { return generic_string<char32_t>(); }
1009 
1010  inline int
1011  path::compare(const string_type& __s) const { return compare(path(__s)); }
1012 
1013  inline int
1014  path::compare(const value_type* __s) const { return compare(path(__s)); }
1015 
1016  inline int
1017  path::compare(basic_string_view<value_type> __s) const
1018  { return compare(path(__s)); }
1019 
1020  inline path
1021  path::filename() const
1022  {
1023  if (empty())
1024  return {};
1025  else if (_M_type == _Type::_Filename)
1026  return *this;
1027  else if (_M_type == _Type::_Multi)
1028  {
1029  if (_M_pathname.back() == preferred_separator)
1030  return {};
1031  auto& __last = *--end();
1032  if (__last._M_type == _Type::_Filename)
1033  return __last;
1034  }
1035  return {};
1036  }
1037 
1038  inline path
1039  path::stem() const
1040  {
1041  auto ext = _M_find_extension();
1042  if (ext.first && ext.second != 0)
1043  return path{ext.first->substr(0, ext.second)};
1044  return {};
1045  }
1046 
1047  inline path
1048  path::extension() const
1049  {
1050  auto ext = _M_find_extension();
1051  if (ext.first && ext.second != string_type::npos)
1052  return path{ext.first->substr(ext.second)};
1053  return {};
1054  }
1055 
1056  inline bool
1057  path::has_stem() const
1058  {
1059  auto ext = _M_find_extension();
1060  return ext.first && ext.second != 0;
1061  }
1062 
1063  inline bool
1064  path::has_extension() const
1065  {
1066  auto ext = _M_find_extension();
1067  return ext.first && ext.second != string_type::npos;
1068  }
1069 
1070  inline path::iterator
1071  path::begin() const
1072  {
1073  if (_M_type == _Type::_Multi)
1074  return iterator(this, _M_cmpts.begin());
1075  return iterator(this, false);
1076  }
1077 
1078  inline path::iterator
1079  path::end() const
1080  {
1081  if (_M_type == _Type::_Multi)
1082  return iterator(this, _M_cmpts.end());
1083  return iterator(this, true);
1084  }
1085 
1086  inline path::iterator&
1087  path::iterator::operator++()
1088  {
1089  __glibcxx_assert(_M_path != nullptr);
1090  if (_M_path->_M_type == _Type::_Multi)
1091  {
1092  __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1093  ++_M_cur;
1094  }
1095  else
1096  {
1097  __glibcxx_assert(!_M_at_end);
1098  _M_at_end = true;
1099  }
1100  return *this;
1101  }
1102 
1103  inline path::iterator&
1104  path::iterator::operator--()
1105  {
1106  __glibcxx_assert(_M_path != nullptr);
1107  if (_M_path->_M_type == _Type::_Multi)
1108  {
1109  __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
1110  --_M_cur;
1111  }
1112  else
1113  {
1114  __glibcxx_assert(_M_at_end);
1115  _M_at_end = false;
1116  }
1117  return *this;
1118  }
1119 
1120  inline path::iterator::reference
1121  path::iterator::operator*() const
1122  {
1123  __glibcxx_assert(_M_path != nullptr);
1124  if (_M_path->_M_type == _Type::_Multi)
1125  {
1126  __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1127  return *_M_cur;
1128  }
1129  return *_M_path;
1130  }
1131 
1132  inline bool
1133  path::iterator::_M_equals(iterator __rhs) const
1134  {
1135  if (_M_path != __rhs._M_path)
1136  return false;
1137  if (_M_path == nullptr)
1138  return true;
1139  if (_M_path->_M_type == path::_Type::_Multi)
1140  return _M_cur == __rhs._M_cur;
1141  return _M_at_end == __rhs._M_at_end;
1142  }
1143 
1144  // @} group filesystem
1145 _GLIBCXX_END_NAMESPACE_CXX11
1146 } // namespace filesystem
1147 
1148 _GLIBCXX_END_NAMESPACE_VERSION
1149 } // namespace std
1150 
1151 #endif // C++17
1152 
1153 #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.
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
size_t hash_value(const path &__p) noexcept
Compare paths.
Bidirectional iterators support a superset of forward iterator operations.
ISO C++ entities toplevel namespace is std.
Marking input iterators.
path u8path(const _Source &__source)
Compare paths.
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:1336
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:1954
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
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