blob: 04180d1792e2b4a7ba68f9da3b6c7e59cae67e03 [file] [log] [blame]
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001// -*- C++ -*-
2//===--------------------------- filesystem -------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10#ifndef _LIBCPP_EXPERIMENTAL_FILESYSTEM
11#define _LIBCPP_EXPERIMENTAL_FILESYSTEM
12/*
13 filesystem synopsis
14
15 namespace std { namespace experimental { namespace filesystem { inline namespace v1 {
16
17 class path;
18
19 void swap(path& lhs, path& rhs) _NOEXCEPT;
20 size_t hash_value(const path& p) _NOEXCEPT;
21
22 bool operator==(const path& lhs, const path& rhs) _NOEXCEPT;
23 bool operator!=(const path& lhs, const path& rhs) _NOEXCEPT;
24 bool operator< (const path& lhs, const path& rhs) _NOEXCEPT;
25 bool operator<=(const path& lhs, const path& rhs) _NOEXCEPT;
26 bool operator> (const path& lhs, const path& rhs) _NOEXCEPT;
27 bool operator>=(const path& lhs, const path& rhs) _NOEXCEPT;
28
29 path operator/ (const path& lhs, const path& rhs);
30
31 template <class charT, class traits>
32 basic_ostream<charT, traits>&
33 operator<<(basic_ostream<charT, traits>& os, const path& p);
34
35 template <class charT, class traits>
36 basic_istream<charT, traits>&
37 operator>>(basic_istream<charT, traits>& is, path& p);
38
39 template <class Source>
40 path u8path(const Source& source);
41 template <class InputIterator>
42 path u8path(InputIterator first, InputIterator last);
43
44 class filesystem_error;
45 class directory_entry;
46
47 class directory_iterator;
48
49 // enable directory_iterator range-based for statements
50 directory_iterator begin(directory_iterator iter) noexcept;
51 directory_iterator end(const directory_iterator&) noexcept;
52
53 class recursive_directory_iterator;
54
55 // enable recursive_directory_iterator range-based for statements
56 recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
57 recursive_directory_iterator end(const recursive_directory_iterator&) noexcept;
58
59 class file_status;
60
61 struct space_info
62 {
63 uintmax_t capacity;
64 uintmax_t free;
65 uintmax_t available;
66 };
67
68 enum class file_type;
69 enum class perms;
70 enum class copy_options;
71 enum class directory_options;
72
73 typedef chrono::time_point<trivial-clock> file_time_type;
74
75 // operational functions
76
77 path absolute(const path& p, const path& base=current_path());
78
79 path canonical(const path& p, const path& base = current_path());
80 path canonical(const path& p, error_code& ec);
81 path canonical(const path& p, const path& base, error_code& ec);
82
83 void copy(const path& from, const path& to);
84 void copy(const path& from, const path& to, error_code& ec) _NOEXCEPT;
85 void copy(const path& from, const path& to, copy_options options);
86 void copy(const path& from, const path& to, copy_options options,
87 error_code& ec) _NOEXCEPT;
88
89 bool copy_file(const path& from, const path& to);
90 bool copy_file(const path& from, const path& to, error_code& ec) _NOEXCEPT;
91 bool copy_file(const path& from, const path& to, copy_options option);
92 bool copy_file(const path& from, const path& to, copy_options option,
93 error_code& ec) _NOEXCEPT;
94
95 void copy_symlink(const path& existing_symlink, const path& new_symlink);
96 void copy_symlink(const path& existing_symlink, const path& new_symlink,
97 error_code& ec) _NOEXCEPT;
98
99 bool create_directories(const path& p);
100 bool create_directories(const path& p, error_code& ec) _NOEXCEPT;
101
102 bool create_directory(const path& p);
103 bool create_directory(const path& p, error_code& ec) _NOEXCEPT;
104
105 bool create_directory(const path& p, const path& attributes);
106 bool create_directory(const path& p, const path& attributes,
107 error_code& ec) _NOEXCEPT;
108
109 void create_directory_symlink(const path& to, const path& new_symlink);
110 void create_directory_symlink(const path& to, const path& new_symlink,
111 error_code& ec) _NOEXCEPT;
112
113 void create_hard_link(const path& to, const path& new_hard_link);
114 void create_hard_link(const path& to, const path& new_hard_link,
115 error_code& ec) _NOEXCEPT;
116
117 void create_symlink(const path& to, const path& new_symlink);
118 void create_symlink(const path& to, const path& new_symlink,
119 error_code& ec) _NOEXCEPT;
120
121 path current_path();
122 path current_path(error_code& ec);
123 void current_path(const path& p);
124 void current_path(const path& p, error_code& ec) _NOEXCEPT;
125
126 bool exists(file_status s) _NOEXCEPT;
127 bool exists(const path& p);
128 bool exists(const path& p, error_code& ec) _NOEXCEPT;
129
130 bool equivalent(const path& p1, const path& p2);
131 bool equivalent(const path& p1, const path& p2, error_code& ec) _NOEXCEPT;
132
133 uintmax_t file_size(const path& p);
134 uintmax_t file_size(const path& p, error_code& ec) _NOEXCEPT;
135
136 uintmax_t hard_link_count(const path& p);
137 uintmax_t hard_link_count(const path& p, error_code& ec) _NOEXCEPT;
138
139 bool is_block_file(file_status s) _NOEXCEPT;
140 bool is_block_file(const path& p);
141 bool is_block_file(const path& p, error_code& ec) _NOEXCEPT;
142
143 bool is_character_file(file_status s) _NOEXCEPT;
144 bool is_character_file(const path& p);
145 bool is_character_file(const path& p, error_code& ec) _NOEXCEPT;
146
147 bool is_directory(file_status s) _NOEXCEPT;
148 bool is_directory(const path& p);
149 bool is_directory(const path& p, error_code& ec) _NOEXCEPT;
150
151 bool is_empty(const path& p);
152 bool is_empty(const path& p, error_code& ec) _NOEXCEPT;
153
154 bool is_fifo(file_status s) _NOEXCEPT;
155 bool is_fifo(const path& p);
156 bool is_fifo(const path& p, error_code& ec) _NOEXCEPT;
157
158 bool is_other(file_status s) _NOEXCEPT;
159 bool is_other(const path& p);
160 bool is_other(const path& p, error_code& ec) _NOEXCEPT;
161
162 bool is_regular_file(file_status s) _NOEXCEPT;
163 bool is_regular_file(const path& p);
164 bool is_regular_file(const path& p, error_code& ec) _NOEXCEPT;
165
166 bool is_socket(file_status s) _NOEXCEPT;
167 bool is_socket(const path& p);
168 bool is_socket(const path& p, error_code& ec) _NOEXCEPT;
169
170 bool is_symlink(file_status s) _NOEXCEPT;
171 bool is_symlink(const path& p);
172 bool is_symlink(const path& p, error_code& ec) _NOEXCEPT;
173
174 file_time_type last_write_time(const path& p);
175 file_time_type last_write_time(const path& p, error_code& ec) _NOEXCEPT;
176 void last_write_time(const path& p, file_time_type new_time);
177 void last_write_time(const path& p, file_time_type new_time,
178 error_code& ec) _NOEXCEPT;
179
180 void permissions(const path& p, perms prms);
181 void permissions(const path& p, perms prms, error_code& ec) _NOEXCEPT;
182
183 path read_symlink(const path& p);
184 path read_symlink(const path& p, error_code& ec);
185
186 bool remove(const path& p);
187 bool remove(const path& p, error_code& ec) _NOEXCEPT;
188
189 uintmax_t remove_all(const path& p);
190 uintmax_t remove_all(const path& p, error_code& ec) _NOEXCEPT;
191
192 void rename(const path& from, const path& to);
193 void rename(const path& from, const path& to, error_code& ec) _NOEXCEPT;
194
195 void resize_file(const path& p, uintmax_t size);
196 void resize_file(const path& p, uintmax_t size, error_code& ec) _NOEXCEPT;
197
198 space_info space(const path& p);
199 space_info space(const path& p, error_code& ec) _NOEXCEPT;
200
201 file_status status(const path& p);
202 file_status status(const path& p, error_code& ec) _NOEXCEPT;
203
204 bool status_known(file_status s) _NOEXCEPT;
205
206 file_status symlink_status(const path& p);
207 file_status symlink_status(const path& p, error_code& ec) _NOEXCEPT;
208
209 path system_complete(const path& p);
210 path system_complete(const path& p, error_code& ec);
211
212 path temp_directory_path();
213 path temp_directory_path(error_code& ec);
214
215} } } } // namespaces std::experimental::filesystem::v1
216
217*/
218
219#include <experimental/__config>
220#include <cstddef>
221#include <chrono>
222#include <iterator>
223#include <iosfwd>
224#include <locale>
225#include <memory>
226#include <stack>
227#include <string>
228#include <system_error>
229#include <utility>
230#include <iomanip> // for quoted
Eric Fiselier2645dbe2016-07-23 03:10:56 +0000231#include <string_view>
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000232
233#include <__debug>
234
235#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
236#pragma GCC system_header
237#endif
238
239#define __cpp_lib_experimental_filesystem 201406
240
241_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
242
243typedef chrono::time_point<std::chrono::system_clock> file_time_type;
244
245struct _LIBCPP_TYPE_VIS space_info
246{
247 uintmax_t capacity;
248 uintmax_t free;
249 uintmax_t available;
250};
251
Eric Fiselier833d6442016-09-15 22:27:07 +0000252enum class _LIBCPP_ENUM_VIS file_type : signed char
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000253{
254 none = 0,
255 not_found = -1,
256 regular = 1,
257 directory = 2,
258 symlink = 3,
259 block = 4,
260 character = 5,
261 fifo = 6,
262 socket = 7,
263 unknown = 8
264};
265
Eric Fiselier833d6442016-09-15 22:27:07 +0000266enum class _LIBCPP_ENUM_VIS perms : unsigned
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000267{
268 none = 0,
269
270 owner_read = 0400,
271 owner_write = 0200,
272 owner_exec = 0100,
273 owner_all = 0700,
274
275 group_read = 040,
276 group_write = 020,
277 group_exec = 010,
278 group_all = 070,
279
280 others_read = 04,
281 others_write = 02,
282 others_exec = 01,
283 others_all = 07,
284
285 all = 0777,
286
287 set_uid = 04000,
288 set_gid = 02000,
289 sticky_bit = 01000,
290 mask = 07777,
291 unknown = 0xFFFF,
292
293 add_perms = 0x10000,
294 remove_perms = 0x20000,
Eric Fiselier7c96ddb2016-06-21 22:42:42 +0000295 symlink_nofollow = 0x40000
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000296};
297
298_LIBCPP_INLINE_VISIBILITY
299inline _LIBCPP_CONSTEXPR perms operator&(perms _LHS, perms _RHS)
300{ return static_cast<perms>(static_cast<unsigned>(_LHS) & static_cast<unsigned>(_RHS)); }
301
302_LIBCPP_INLINE_VISIBILITY
303inline _LIBCPP_CONSTEXPR perms operator|(perms _LHS, perms _RHS)
304{ return static_cast<perms>(static_cast<unsigned>(_LHS) | static_cast<unsigned>(_RHS)); }
305
306_LIBCPP_INLINE_VISIBILITY
307inline _LIBCPP_CONSTEXPR perms operator^(perms _LHS, perms _RHS)
308{ return static_cast<perms>(static_cast<unsigned>(_LHS) ^ static_cast<unsigned>(_RHS)); }
309
310_LIBCPP_INLINE_VISIBILITY
311inline _LIBCPP_CONSTEXPR perms operator~(perms _LHS)
312{ return static_cast<perms>(~static_cast<unsigned>(_LHS)); }
313
314_LIBCPP_INLINE_VISIBILITY
315inline perms& operator&=(perms& _LHS, perms _RHS)
316{ return _LHS = _LHS & _RHS; }
317
318_LIBCPP_INLINE_VISIBILITY
319inline perms& operator|=(perms& _LHS, perms _RHS)
320{ return _LHS = _LHS | _RHS; }
321
322_LIBCPP_INLINE_VISIBILITY
323inline perms& operator^=(perms& _LHS, perms _RHS)
324{ return _LHS = _LHS ^ _RHS; }
325
Eric Fiselier833d6442016-09-15 22:27:07 +0000326enum class _LIBCPP_ENUM_VIS copy_options : unsigned short
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000327{
328 none = 0,
329 skip_existing = 1,
330 overwrite_existing = 2,
331 update_existing = 4,
332 recursive = 8,
333 copy_symlinks = 16,
334 skip_symlinks = 32,
335 directories_only = 64,
336 create_symlinks = 128,
337 create_hard_links = 256,
338 __in_recursive_copy = 512,
339};
340
341_LIBCPP_INLINE_VISIBILITY
342inline _LIBCPP_CONSTEXPR copy_options operator&(copy_options _LHS, copy_options _RHS)
343{ return static_cast<copy_options>(static_cast<unsigned short>(_LHS) & static_cast<unsigned short>(_RHS)); }
344
345_LIBCPP_INLINE_VISIBILITY
346inline _LIBCPP_CONSTEXPR copy_options operator|(copy_options _LHS, copy_options _RHS)
347{ return static_cast<copy_options>(static_cast<unsigned short>(_LHS) | static_cast<unsigned short>(_RHS)); }
348
349_LIBCPP_INLINE_VISIBILITY
350inline _LIBCPP_CONSTEXPR copy_options operator^(copy_options _LHS, copy_options _RHS)
351{ return static_cast<copy_options>(static_cast<unsigned short>(_LHS) ^ static_cast<unsigned short>(_RHS)); }
352
353_LIBCPP_INLINE_VISIBILITY
354inline _LIBCPP_CONSTEXPR copy_options operator~(copy_options _LHS)
355{ return static_cast<copy_options>(~static_cast<unsigned short>(_LHS)); }
356
357_LIBCPP_INLINE_VISIBILITY
358inline copy_options& operator&=(copy_options& _LHS, copy_options _RHS)
359{ return _LHS = _LHS & _RHS; }
360
361_LIBCPP_INLINE_VISIBILITY
362inline copy_options& operator|=(copy_options& _LHS, copy_options _RHS)
363{ return _LHS = _LHS | _RHS; }
364
365_LIBCPP_INLINE_VISIBILITY
366inline copy_options& operator^=(copy_options& _LHS, copy_options _RHS)
367{ return _LHS = _LHS ^ _RHS; }
368
369
Eric Fiselier833d6442016-09-15 22:27:07 +0000370enum class _LIBCPP_ENUM_VIS directory_options : unsigned char
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000371{
372 none = 0,
373 follow_directory_symlink = 1,
374 skip_permission_denied = 2
375};
376
377_LIBCPP_INLINE_VISIBILITY
378inline _LIBCPP_CONSTEXPR directory_options operator&(directory_options _LHS, directory_options _RHS)
379{ return static_cast<directory_options>(static_cast<unsigned char>(_LHS) & static_cast<unsigned char>(_RHS)); }
380
381_LIBCPP_INLINE_VISIBILITY
382inline _LIBCPP_CONSTEXPR directory_options operator|(directory_options _LHS, directory_options _RHS)
383{ return static_cast<directory_options>(static_cast<unsigned char>(_LHS) | static_cast<unsigned char>(_RHS)); }
384
385_LIBCPP_INLINE_VISIBILITY
386inline _LIBCPP_CONSTEXPR directory_options operator^(directory_options _LHS, directory_options _RHS)
387{ return static_cast<directory_options>(static_cast<unsigned char>(_LHS) ^ static_cast<unsigned char>(_RHS)); }
388
389_LIBCPP_INLINE_VISIBILITY
390inline _LIBCPP_CONSTEXPR directory_options operator~(directory_options _LHS)
391{ return static_cast<directory_options>(~static_cast<unsigned char>(_LHS)); }
392
393_LIBCPP_INLINE_VISIBILITY
394inline directory_options& operator&=(directory_options& _LHS, directory_options _RHS)
395{ return _LHS = _LHS & _RHS; }
396
397_LIBCPP_INLINE_VISIBILITY
398inline directory_options& operator|=(directory_options& _LHS, directory_options _RHS)
399{ return _LHS = _LHS | _RHS; }
400
401_LIBCPP_INLINE_VISIBILITY
402inline directory_options& operator^=(directory_options& _LHS, directory_options _RHS)
403{ return _LHS = _LHS ^ _RHS; }
404
405
406class _LIBCPP_TYPE_VIS file_status
407{
408public:
409 // constructors
410 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7c7df642017-03-06 21:02:06 +0000411 file_status() _NOEXCEPT : file_status(file_type::none) {}
412 _LIBCPP_INLINE_VISIBILITY
413 explicit file_status(file_type __ft,
414 perms __prms = perms::unknown) _NOEXCEPT
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000415 : __ft_(__ft), __prms_(__prms)
416 {}
417
418 file_status(const file_status&) _NOEXCEPT = default;
419 file_status(file_status&&) _NOEXCEPT = default;
420
421 _LIBCPP_INLINE_VISIBILITY
422 ~file_status() {}
423
424 file_status& operator=(const file_status&) _NOEXCEPT = default;
425 file_status& operator=(file_status&&) _NOEXCEPT = default;
426
427 // observers
428 _LIBCPP_ALWAYS_INLINE
429 file_type type() const _NOEXCEPT {
430 return __ft_;
431 }
432
433 _LIBCPP_ALWAYS_INLINE
434 perms permissions() const _NOEXCEPT {
435 return __prms_;
436 }
437
438 // modifiers
439 _LIBCPP_ALWAYS_INLINE
440 void type(file_type __ft) _NOEXCEPT {
441 __ft_ = __ft;
442 }
443
444 _LIBCPP_ALWAYS_INLINE
445 void permissions(perms __p) _NOEXCEPT {
446 __prms_ = __p;
447 }
448private:
449 file_type __ft_;
450 perms __prms_;
451};
452
453class _LIBCPP_TYPE_VIS directory_entry;
454
455template <class _Tp> struct __can_convert_char {
456 static const bool value = false;
457};
Eric Fiselier113315b2016-08-28 21:26:01 +0000458template <class _Tp> struct __can_convert_char<const _Tp>
459 : public __can_convert_char<_Tp> {
460};
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000461template <> struct __can_convert_char<char> {
462 static const bool value = true;
463 using __char_type = char;
464};
465template <> struct __can_convert_char<wchar_t> {
466 static const bool value = true;
467 using __char_type = wchar_t;
468};
469template <> struct __can_convert_char<char16_t> {
470 static const bool value = true;
471 using __char_type = char16_t;
472};
473template <> struct __can_convert_char<char32_t> {
474 static const bool value = true;
475 using __char_type = char32_t;
476};
477
478template <class _ECharT>
479typename enable_if<__can_convert_char<_ECharT>::value, bool>::type
480__is_separator(_ECharT __e) {
481 return __e == _ECharT('/');
482};
483
484struct _NullSentinal {};
485
486template <class _Tp>
487using _Void = void;
488
489template <class _Tp, class = void>
490struct __is_pathable_string : public false_type {};
491
492template <class _ECharT, class _Traits, class _Alloc>
493struct __is_pathable_string<basic_string<_ECharT, _Traits, _Alloc>,
494 _Void<typename __can_convert_char<_ECharT>::__char_type>>
495: public __can_convert_char<_ECharT>
496{
497 using _Str = basic_string<_ECharT, _Traits, _Alloc>;
498 using _Base = __can_convert_char<_ECharT>;
499 static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); }
500 static _ECharT const* __range_end(_Str const& __s) { return __s.data() + __s.length(); }
501 static _ECharT __first_or_null(_Str const& __s) {
502 return __s.empty() ? _ECharT{} : __s[0];
503 }
504};
505
Eric Fiselier2645dbe2016-07-23 03:10:56 +0000506
507template <class _ECharT, class _Traits>
508struct __is_pathable_string<basic_string_view<_ECharT, _Traits>,
509 _Void<typename __can_convert_char<_ECharT>::__char_type>>
510: public __can_convert_char<_ECharT>
511{
512 using _Str = basic_string_view<_ECharT, _Traits>;
513 using _Base = __can_convert_char<_ECharT>;
514 static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); }
515 static _ECharT const* __range_end(_Str const& __s) { return __s.data() + __s.length(); }
516 static _ECharT __first_or_null(_Str const& __s) {
517 return __s.empty() ? _ECharT{} : __s[0];
518 }
519};
520
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000521template <class _Source,
522 class _DS = typename decay<_Source>::type,
523 class _UnqualPtrType = typename remove_const<
524 typename remove_pointer<_DS>::type>::type,
525 bool _IsCharPtr = is_pointer<_DS>::value &&
526 __can_convert_char<_UnqualPtrType>::value
527 >
528struct __is_pathable_char_array : false_type {};
529
530template <class _Source, class _ECharT, class _UPtr>
531struct __is_pathable_char_array<_Source, _ECharT*, _UPtr, true>
532 : __can_convert_char<typename remove_const<_ECharT>::type>
533{
534 using _Base = __can_convert_char<typename remove_const<_ECharT>::type>;
535
536 static _ECharT const* __range_begin(const _ECharT* __b) { return __b; }
537 static _ECharT const* __range_end(const _ECharT* __b)
538 {
539 using _Iter = const _ECharT*;
540 const _ECharT __sentinal = _ECharT{};
541 _Iter __e = __b;
542 for (; *__e != __sentinal; ++__e)
543 ;
544 return __e;
545 }
546
547 static _ECharT __first_or_null(const _ECharT* __b) { return *__b; }
548};
549
550template <class _Iter, bool _IsIt = __is_input_iterator<_Iter>::value, class = void>
551struct __is_pathable_iter : false_type {};
552
553template <class _Iter>
554struct __is_pathable_iter<_Iter, true,
555 _Void<typename __can_convert_char<typename iterator_traits<_Iter>::value_type>::__char_type>>
556 : __can_convert_char<typename iterator_traits<_Iter>::value_type>
557{
558 using _ECharT = typename iterator_traits<_Iter>::value_type;
559 using _Base = __can_convert_char<_ECharT>;
560
561 static _Iter __range_begin(_Iter __b) { return __b; }
562 static _NullSentinal __range_end(_Iter) { return _NullSentinal{}; }
563
564 static _ECharT __first_or_null(_Iter __b) { return *__b; }
565};
566
567template <class _Tp, bool _IsStringT = __is_pathable_string<_Tp>::value,
568 bool _IsCharIterT = __is_pathable_char_array<_Tp>::value,
569 bool _IsIterT = !_IsCharIterT && __is_pathable_iter<_Tp>::value
570 >
571struct __is_pathable : false_type {
572 static_assert(!_IsStringT && !_IsCharIterT && !_IsIterT, "Must all be false");
573};
574
575template <class _Tp>
576struct __is_pathable<_Tp, true, false, false> : __is_pathable_string<_Tp> {};
577
578
579template <class _Tp>
580struct __is_pathable<_Tp, false, true, false> : __is_pathable_char_array<_Tp> {};
581
582
583template <class _Tp>
584struct __is_pathable<_Tp, false, false, true> : __is_pathable_iter<_Tp> {};
585
586
587template <class _ECharT>
588struct _PathCVT {
589 static_assert(__can_convert_char<_ECharT>::value, "Char type not convertible");
590
591 typedef __narrow_to_utf8<sizeof(_ECharT)*__CHAR_BIT__> _Narrower;
592
593 static void __append_range(string& __dest, _ECharT const* __b, _ECharT const* __e) {
594 _Narrower()(back_inserter(__dest), __b, __e);
595 }
596
597 template <class _Iter>
598 static void __append_range(string& __dest, _Iter __b, _Iter __e) {
599 static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload");
600 if (__b == __e) return;
601 basic_string<_ECharT> __tmp(__b, __e);
602 _Narrower()(back_inserter(__dest), __tmp.data(),
603 __tmp.data() + __tmp.length());
604 }
605
606 template <class _Iter>
607 static void __append_range(string& __dest, _Iter __b, _NullSentinal) {
608 static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload");
609 const _ECharT __sentinal = _ECharT{};
610 if (*__b == __sentinal) return;
611 basic_string<_ECharT> __tmp;
612 for (; *__b != __sentinal; ++__b)
613 __tmp.push_back(*__b);
614 _Narrower()(back_inserter(__dest), __tmp.data(),
615 __tmp.data() + __tmp.length());
616 }
617
618 template <class _Source>
619 static void __append_source(string& __dest, _Source const& __s)
620 {
621 using _Traits = __is_pathable<_Source>;
622 __append_range(__dest, _Traits::__range_begin(__s), _Traits::__range_end(__s));
623 }
624};
625
626template <>
627struct _PathCVT<char> {
Eric Fiselierad1a12c2016-10-30 23:53:50 +0000628
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000629 template <class _Iter>
Eric Fiselier026d38e2016-10-31 02:46:25 +0000630 static typename enable_if<
631 __is_exactly_input_iterator<_Iter>::value
632 >::type __append_range(string& __dest, _Iter __b, _Iter __e) {
633 for (; __b != __e; ++__b)
634 __dest.push_back(*__b);
635 }
636
637 template <class _Iter>
638 static typename enable_if<
639 __is_forward_iterator<_Iter>::value
640 >::type __append_range(string& __dest, _Iter __b, _Iter __e) {
641 __dest.__append_forward_unsafe(__b, __e);
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000642 }
643
644 template <class _Iter>
645 static void __append_range(string& __dest, _Iter __b, _NullSentinal) {
646 const char __sentinal = char{};
647 for (; *__b != __sentinal; ++__b)
648 __dest.push_back(*__b);
649 }
650
651 template <class _Source>
652 static void __append_source(string& __dest, _Source const& __s)
653 {
654 using _Traits = __is_pathable<_Source>;
Eric Fiselierad1a12c2016-10-30 23:53:50 +0000655 __append_range(__dest, _Traits::__range_begin(__s),
656 _Traits::__range_end(__s));
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000657 }
658};
659
660
661class _LIBCPP_TYPE_VIS path
662{
663 template <class _SourceOrIter, class _Tp = path&>
664 using _EnableIfPathable = typename
665 enable_if<__is_pathable<_SourceOrIter>::value, _Tp>::type;
666
667 template <class _Tp>
668 using _SourceChar = typename __is_pathable<_Tp>::__char_type;
669
670 template <class _Tp>
671 using _SourceCVT = _PathCVT<_SourceChar<_Tp>>;
672
673public:
674 typedef char value_type;
675 typedef basic_string<value_type> string_type;
Eric Fiselier2645dbe2016-07-23 03:10:56 +0000676 typedef _VSTD::string_view __string_view;
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000677 static _LIBCPP_CONSTEXPR value_type preferred_separator = '/';
678
679 // constructors and destructor
680 _LIBCPP_INLINE_VISIBILITY path() _NOEXCEPT {}
681 _LIBCPP_INLINE_VISIBILITY path(const path& __p) : __pn_(__p.__pn_) {}
682 _LIBCPP_INLINE_VISIBILITY path(path&& __p) _NOEXCEPT : __pn_(_VSTD::move(__p.__pn_)) {}
683
684 _LIBCPP_INLINE_VISIBILITY
685 path(string_type&& __s) _NOEXCEPT : __pn_(_VSTD::move(__s)) {}
686
687 template <
688 class _Source,
689 class = _EnableIfPathable<_Source, void>
690 >
691 path(const _Source& __src) {
692 _SourceCVT<_Source>::__append_source(__pn_, __src);
693 }
694
695 template <class _InputIt>
696 path(_InputIt __first, _InputIt __last) {
697 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
698 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
699 }
700
701 // TODO Implement locale conversions.
702 template <class _Source,
703 class = _EnableIfPathable<_Source, void>
704 >
705 path(const _Source& __src, const locale& __loc);
706 template <class _InputIt>
707 path(_InputIt __first, _InputIt _last, const locale& __loc);
708
709 _LIBCPP_INLINE_VISIBILITY
710 ~path() = default;
711
712 // assignments
713 _LIBCPP_INLINE_VISIBILITY
714 path& operator=(const path& __p) {
715 __pn_ = __p.__pn_;
716 return *this;
717 }
718
719 _LIBCPP_INLINE_VISIBILITY
720 path& operator=(path&& __p) _NOEXCEPT {
721 __pn_ = _VSTD::move(__p.__pn_);
722 return *this;
723 }
724
Eric Fiselierf2b48892017-01-18 05:48:55 +0000725 template <class = void>
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000726 _LIBCPP_INLINE_VISIBILITY
727 path& operator=(string_type&& __s) _NOEXCEPT {
728 __pn_ = _VSTD::move(__s);
729 return *this;
730 }
731
732 _LIBCPP_INLINE_VISIBILITY
733 path& assign(string_type&& __s) _NOEXCEPT {
734 __pn_ = _VSTD::move(__s);
735 return *this;
736 }
737
738 template <class _Source>
739 _LIBCPP_INLINE_VISIBILITY
740 _EnableIfPathable<_Source>
741 operator=(const _Source& __src)
742 { return this->assign(__src); }
743
744
745 template <class _Source>
746 _EnableIfPathable<_Source>
747 assign(const _Source& __src) {
748 __pn_.clear();
749 _SourceCVT<_Source>::__append_source(__pn_, __src);
750 return *this;
751 }
752
753 template <class _InputIt>
754 path& assign(_InputIt __first, _InputIt __last) {
755 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
756 __pn_.clear();
757 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
758 return *this;
759 }
760
761private:
762 template <class _ECharT>
763 void __append_sep_if_needed(_ECharT __first_or_null) {
764 const _ECharT __null_val = {};
765 bool __append_sep = !empty() &&
766 !__is_separator(__pn_.back()) &&
767 __first_or_null != __null_val && // non-empty
768 !__is_separator(__first_or_null);
769 if (__append_sep)
770 __pn_ += preferred_separator;
771 }
772
773public:
774 // appends
775 path& operator/=(const path& __p) {
Eric Fiselier4ca4e502016-10-15 21:29:44 +0000776 _LIBCPP_ASSERT(!__p.has_root_name(),
777 "cannot append to a path with a root name");
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000778 __append_sep_if_needed(__p.empty() ? char{} : __p.__pn_[0]);
779 __pn_ += __p.native();
780 return *this;
781 }
782
783 template <class _Source>
784 _LIBCPP_INLINE_VISIBILITY
785 _EnableIfPathable<_Source>
786 operator/=(const _Source& __src) {
787 return this->append(__src);
788 }
789
790 template <class _Source>
791 _EnableIfPathable<_Source>
792 append(const _Source& __src) {
793 using _Traits = __is_pathable<_Source>;
794 using _CVT = _PathCVT<_SourceChar<_Source>>;
795 __append_sep_if_needed(_Traits::__first_or_null(__src));
796 _CVT::__append_source(__pn_, __src);
797 return *this;
798 }
799
800 template <class _InputIt>
801 path& append(_InputIt __first, _InputIt __last) {
802 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
803 static_assert(__can_convert_char<_ItVal>::value, "Must convertible");
804 using _CVT = _PathCVT<_ItVal>;
805 if (__first != __last) {
806 __append_sep_if_needed(*__first);
807 _CVT::__append_range(__pn_, __first, __last);
808 }
809 return *this;
810 }
811
812 // concatenation
813 _LIBCPP_INLINE_VISIBILITY
814 path& operator+=(const path& __x) {
815 __pn_ += __x.__pn_;
816 return *this;
817 }
818
819 _LIBCPP_INLINE_VISIBILITY
820 path& operator+=(const string_type& __x) {
821 __pn_ += __x;
822 return *this;
823 }
824
825 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2645dbe2016-07-23 03:10:56 +0000826 path& operator+=(__string_view __x) {
827 __pn_ += __x;
828 return *this;
829 }
830
831 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000832 path& operator+=(const value_type* __x) {
833 __pn_ += __x;
834 return *this;
835 }
836
837 _LIBCPP_INLINE_VISIBILITY
838 path& operator+=(value_type __x) {
839 __pn_ += __x;
840 return *this;
841 }
842
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000843 template <class _ECharT>
844 typename enable_if<__can_convert_char<_ECharT>::value, path&>::type
845 operator+=(_ECharT __x)
846 {
847 basic_string<_ECharT> __tmp;
848 __tmp += __x;
849 _PathCVT<_ECharT>::__append_source(__pn_, __tmp);
850 return *this;
851 }
852
853 template <class _Source>
854 _EnableIfPathable<_Source>
855 operator+=(const _Source& __x) {
856 return this->concat(__x);
857 }
858
859 template <class _Source>
860 _EnableIfPathable<_Source>
861 concat(const _Source& __x) {
862 _SourceCVT<_Source>::__append_source(__pn_, __x);
863 return *this;
864 }
865
866 template <class _InputIt>
867 path& concat(_InputIt __first, _InputIt __last) {
868 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
869 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
870 return *this;
871 }
872
873 // modifiers
874 _LIBCPP_INLINE_VISIBILITY
875 void clear() _NOEXCEPT {
876 __pn_.clear();
877 }
878
879 path& make_preferred() { return *this; }
Eric Fiselier620a9a52016-10-15 22:37:42 +0000880
881 _LIBCPP_INLINE_VISIBILITY
882 path& remove_filename() {
883 if (__pn_.size() == __root_path_raw().size())
884 clear();
885 else
886 __pn_ = __parent_path();
887 return *this;
888 }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000889
890 path& replace_filename(const path& __replacement) {
891 remove_filename();
892 return (*this /= __replacement);
893 }
894
895 path& replace_extension(const path& __replacement = path());
896
897 _LIBCPP_INLINE_VISIBILITY
898 void swap(path& __rhs) _NOEXCEPT {
899 __pn_.swap(__rhs.__pn_);
900 }
901
902 // native format observers
903 _LIBCPP_INLINE_VISIBILITY
904 const string_type& native() const _NOEXCEPT {
905 return __pn_;
906 }
907
908 _LIBCPP_INLINE_VISIBILITY
909 const value_type* c_str() const _NOEXCEPT { return __pn_.c_str(); }
910
911 _LIBCPP_INLINE_VISIBILITY operator string_type() const { return __pn_; }
912
913 template <class _ECharT, class _Traits = char_traits<_ECharT>,
914 class _Allocator = allocator<_ECharT> >
915 basic_string<_ECharT, _Traits, _Allocator>
916 string(const _Allocator& __a = _Allocator()) const {
917 using _CVT = __widen_from_utf8<sizeof(_ECharT)*__CHAR_BIT__>;
918 using _Str = basic_string<_ECharT, _Traits, _Allocator>;
919 _Str __s(__a);
920 __s.reserve(__pn_.size());
921 _CVT()(back_inserter(__s), __pn_.data(), __pn_.data() + __pn_.size());
922 return __s;
923 }
924
925 _LIBCPP_INLINE_VISIBILITY std::string string() const { return __pn_; }
926 _LIBCPP_INLINE_VISIBILITY std::wstring wstring() const { return string<wchar_t>(); }
927 _LIBCPP_INLINE_VISIBILITY std::string u8string() const { return __pn_; }
928 _LIBCPP_INLINE_VISIBILITY std::u16string u16string() const { return string<char16_t>(); }
929 _LIBCPP_INLINE_VISIBILITY std::u32string u32string() const { return string<char32_t>(); }
930
931 // generic format observers
932 template <class _ECharT, class _Traits = char_traits<_ECharT>,
933 class _Allocator = allocator<_ECharT>
934 >
935 basic_string<_ECharT, _Traits, _Allocator>
936 generic_string(const _Allocator& __a = _Allocator()) const {
937 return string<_ECharT, _Traits, _Allocator>(__a);
938 }
939
940 std::string generic_string() const { return __pn_; }
941 std::wstring generic_wstring() const { return string<wchar_t>(); }
942 std::string generic_u8string() const { return __pn_; }
943 std::u16string generic_u16string() const { return string<char16_t>(); }
944 std::u32string generic_u32string() const { return string<char32_t>(); }
945
946private:
Saleem Abdulrasool52241cb2017-01-30 03:58:26 +0000947 int __compare(__string_view) const;
948 __string_view __root_name() const;
949 __string_view __root_directory() const;
950 __string_view __root_path_raw() const;
951 __string_view __relative_path() const;
952 __string_view __parent_path() const;
953 __string_view __filename() const;
954 __string_view __stem() const;
955 __string_view __extension() const;
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000956
957public:
958 // compare
Eric Fiselier2645dbe2016-07-23 03:10:56 +0000959 _LIBCPP_INLINE_VISIBILITY int compare(const path& __p) const _NOEXCEPT { return __compare(__p.__pn_);}
960 _LIBCPP_INLINE_VISIBILITY int compare(const string_type& __s) const { return __compare(__s); }
961 _LIBCPP_INLINE_VISIBILITY int compare(__string_view __s) const { return __compare(__s); }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000962 _LIBCPP_INLINE_VISIBILITY int compare(const value_type* __s) const { return __compare(__s); }
963
964 // decomposition
Eric Fiselier2645dbe2016-07-23 03:10:56 +0000965 _LIBCPP_INLINE_VISIBILITY path root_name() const { return string_type(__root_name()); }
966 _LIBCPP_INLINE_VISIBILITY path root_directory() const { return string_type(__root_directory()); }
967 _LIBCPP_INLINE_VISIBILITY path root_path() const { return root_name().append(string_type(__root_directory())); }
968 _LIBCPP_INLINE_VISIBILITY path relative_path() const { return string_type(__relative_path()); }
969 _LIBCPP_INLINE_VISIBILITY path parent_path() const { return string_type(__parent_path()); }
970 _LIBCPP_INLINE_VISIBILITY path filename() const { return string_type(__filename()); }
971 _LIBCPP_INLINE_VISIBILITY path stem() const { return string_type(__stem());}
972 _LIBCPP_INLINE_VISIBILITY path extension() const { return string_type(__extension()); }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000973
974 // query
975 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT { return __pn_.empty(); }
976
977 _LIBCPP_INLINE_VISIBILITY bool has_root_name() const { return !__root_name().empty(); }
978 _LIBCPP_INLINE_VISIBILITY bool has_root_directory() const { return !__root_directory().empty(); }
Eric Fiselier620a9a52016-10-15 22:37:42 +0000979 _LIBCPP_INLINE_VISIBILITY bool has_root_path() const { return !__root_path_raw().empty(); }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000980 _LIBCPP_INLINE_VISIBILITY bool has_relative_path() const { return !__relative_path().empty(); }
981 _LIBCPP_INLINE_VISIBILITY bool has_parent_path() const { return !__parent_path().empty(); }
982 _LIBCPP_INLINE_VISIBILITY bool has_filename() const { return !__filename().empty(); }
983 _LIBCPP_INLINE_VISIBILITY bool has_stem() const { return !__stem().empty(); }
984 _LIBCPP_INLINE_VISIBILITY bool has_extension() const { return !__extension().empty(); }
985
986 _LIBCPP_INLINE_VISIBILITY bool is_absolute() const { return has_root_directory(); }
987 _LIBCPP_INLINE_VISIBILITY bool is_relative() const { return !is_absolute(); }
988
989 // iterators
990 class _LIBCPP_TYPE_VIS iterator;
991 typedef iterator const_iterator;
992
Saleem Abdulrasool52241cb2017-01-30 03:58:26 +0000993 iterator begin() const;
994 iterator end() const;
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000995
996private:
997 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2645dbe2016-07-23 03:10:56 +0000998 path& __assign_view(__string_view const& __s) noexcept { __pn_ = string_type(__s); return *this; }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000999 string_type __pn_;
1000};
1001
1002inline _LIBCPP_ALWAYS_INLINE
1003void swap(path& __lhs, path& __rhs) _NOEXCEPT {
1004 __lhs.swap(__rhs);
1005}
1006
1007_LIBCPP_FUNC_VIS
1008size_t hash_value(const path& __p) _NOEXCEPT;
1009
1010inline _LIBCPP_INLINE_VISIBILITY
1011bool operator==(const path& __lhs, const path& __rhs) _NOEXCEPT
1012{ return __lhs.compare(__rhs) == 0; }
1013
1014inline _LIBCPP_INLINE_VISIBILITY
1015bool operator!=(const path& __lhs, const path& __rhs) _NOEXCEPT
1016{ return __lhs.compare(__rhs) != 0; }
1017
1018inline _LIBCPP_INLINE_VISIBILITY
1019bool operator<(const path& __lhs, const path& __rhs) _NOEXCEPT
1020{ return __lhs.compare(__rhs) < 0; }
1021
1022inline _LIBCPP_INLINE_VISIBILITY
1023bool operator<=(const path& __lhs, const path& __rhs) _NOEXCEPT
1024{ return __lhs.compare(__rhs) <= 0; }
1025
1026inline _LIBCPP_INLINE_VISIBILITY
1027bool operator>(const path& __lhs, const path& __rhs) _NOEXCEPT
1028{ return __lhs.compare(__rhs) > 0; }
1029
1030inline _LIBCPP_INLINE_VISIBILITY
1031bool operator>=(const path& __lhs, const path& __rhs) _NOEXCEPT
1032{ return __lhs.compare(__rhs) >= 0; }
1033
1034inline _LIBCPP_INLINE_VISIBILITY
1035path operator/(const path& __lhs, const path& __rhs) {
1036 return path(__lhs) /= __rhs;
1037}
1038
1039template <class _CharT, class _Traits>
1040_LIBCPP_INLINE_VISIBILITY
1041typename enable_if<is_same<_CharT, char>::value &&
1042 is_same<_Traits, char_traits<char>>::value,
1043 basic_ostream<_CharT, _Traits>&
1044>::type
1045operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) {
1046 __os << std::__quoted(__p.native());
1047 return __os;
1048}
1049
1050template <class _CharT, class _Traits>
1051_LIBCPP_INLINE_VISIBILITY
1052typename enable_if<!is_same<_CharT, char>::value ||
1053 !is_same<_Traits, char_traits<char>>::value,
1054 basic_ostream<_CharT, _Traits>&
1055>::type
1056operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) {
1057 __os << std::__quoted(__p.string<_CharT, _Traits>());
1058 return __os;
1059}
1060
1061template <class _CharT, class _Traits>
1062_LIBCPP_INLINE_VISIBILITY
1063basic_istream<_CharT, _Traits>&
1064operator>>(basic_istream<_CharT, _Traits>& __is, path& __p)
1065{
1066 basic_string<_CharT, _Traits> __tmp;
1067 __is >> __quoted(__tmp);
1068 __p = __tmp;
1069 return __is;
1070}
1071
1072template <class _Source>
1073_LIBCPP_INLINE_VISIBILITY
1074typename enable_if<__is_pathable<_Source>::value, path>::type
1075u8path(const _Source& __s){
1076 static_assert(is_same<typename __is_pathable<_Source>::__char_type, char>::value,
1077 "u8path(Source const&) requires Source have a character type of type 'char'");
1078 return path(__s);
1079}
1080
1081template <class _InputIt>
1082_LIBCPP_INLINE_VISIBILITY
1083typename enable_if<__is_pathable<_InputIt>::value, path>::type
1084u8path(_InputIt __f, _InputIt __l) {
1085 static_assert(is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
1086 "u8path(Iter, Iter) requires Iter have a value_type of type 'char'");
1087 return path(__f, __l);
1088}
1089
1090class _LIBCPP_TYPE_VIS path::iterator
1091{
1092public:
1093 typedef bidirectional_iterator_tag iterator_category;
Eric Fiselier08673732017-04-04 01:05:59 +00001094
1095 // FIXME: As of 3/April/2017 Clang warns on `value_type` shadowing the
1096 // definition in path. Clang should be fixed and this should be removed.
1097#if defined(__clang__)
1098#pragma clang diagnostic push
1099#pragma clang diagnostic ignored "-Wshadow"
1100#endif
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001101 typedef path value_type;
Eric Fiselier08673732017-04-04 01:05:59 +00001102#if defined(__clang__)
1103#pragma clang diagnostic pop
1104#endif
1105
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001106 typedef std::ptrdiff_t difference_type;
1107 typedef const path* pointer;
1108 typedef const path& reference;
1109public:
1110 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier271a19e2016-10-30 23:30:38 +00001111 iterator() : __stashed_elem_(), __path_ptr_(nullptr),
1112 __entry_(), __state_(__singular) {}
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001113
1114 iterator(const iterator&) = default;
1115 ~iterator() = default;
1116
1117 iterator& operator=(const iterator&) = default;
1118
1119 _LIBCPP_INLINE_VISIBILITY
1120 reference operator*() const {
Eric Fiselier271a19e2016-10-30 23:30:38 +00001121 return __stashed_elem_;
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001122 }
1123
1124 _LIBCPP_INLINE_VISIBILITY
1125 pointer operator->() const {
Eric Fiselier271a19e2016-10-30 23:30:38 +00001126 return &__stashed_elem_;
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001127 }
1128
1129 _LIBCPP_INLINE_VISIBILITY
1130 iterator& operator++() {
Eric Fiselier271a19e2016-10-30 23:30:38 +00001131 _LIBCPP_ASSERT(__state_ != __singular,
1132 "attempting to increment a singular iterator");
1133 _LIBCPP_ASSERT(__state_ != __at_end,
1134 "attempting to increment the end iterator");
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001135 return __increment();
1136 }
1137
1138 _LIBCPP_INLINE_VISIBILITY
1139 iterator operator++(int) {
1140 iterator __it(*this);
1141 this->operator++();
1142 return __it;
1143 }
1144
1145 _LIBCPP_INLINE_VISIBILITY
1146 iterator& operator--() {
Eric Fiselier271a19e2016-10-30 23:30:38 +00001147 _LIBCPP_ASSERT(__state_ != __singular,
1148 "attempting to decrement a singular iterator");
1149 _LIBCPP_ASSERT(__entry_.data() != __path_ptr_->native().data(),
1150 "attempting to decrement the begin iterator");
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001151 return __decrement();
1152 }
1153
1154 _LIBCPP_INLINE_VISIBILITY
1155 iterator operator--(int) {
1156 iterator __it(*this);
1157 this->operator--();
1158 return __it;
1159 }
1160
1161private:
1162 friend class path;
Eric Fiselier03f7d102016-09-16 00:07:16 +00001163
Eric Fiselier271a19e2016-10-30 23:30:38 +00001164 static constexpr unsigned char __singular = 0;
1165 static constexpr unsigned char __at_end = 6;
1166
Eric Fiselier03f7d102016-09-16 00:07:16 +00001167 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001168 friend bool operator==(const iterator&, const iterator&);
1169
Saleem Abdulrasool52241cb2017-01-30 03:58:26 +00001170 iterator& __increment();
1171 iterator& __decrement();
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001172
Eric Fiselier271a19e2016-10-30 23:30:38 +00001173 path __stashed_elem_;
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001174 const path* __path_ptr_;
Eric Fiselier271a19e2016-10-30 23:30:38 +00001175 path::__string_view __entry_;
1176 unsigned char __state_;
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001177};
1178
1179inline _LIBCPP_INLINE_VISIBILITY
1180bool operator==(const path::iterator& __lhs, const path::iterator& __rhs) {
1181 return __lhs.__path_ptr_ == __rhs.__path_ptr_ &&
Eric Fiselier271a19e2016-10-30 23:30:38 +00001182 __lhs.__entry_.data() == __rhs.__entry_.data();
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001183}
1184
1185inline _LIBCPP_INLINE_VISIBILITY
1186bool operator!=(const path::iterator& __lhs, const path::iterator& __rhs) {
1187 return !(__lhs == __rhs);
1188}
1189
1190class _LIBCPP_EXCEPTION_ABI filesystem_error : public system_error
1191{
1192public:
1193 _LIBCPP_INLINE_VISIBILITY
1194 filesystem_error(const string& __what, error_code __ec)
1195 : system_error(__ec, __what),
1196 __paths_(make_shared<_Storage>(path(), path()))
1197 {}
1198
1199 _LIBCPP_INLINE_VISIBILITY
1200 filesystem_error(const string& __what, const path& __p1, error_code __ec)
1201 : system_error(__ec, __what),
1202 __paths_(make_shared<_Storage>(__p1, path()))
1203 {}
1204
1205 _LIBCPP_INLINE_VISIBILITY
1206 filesystem_error(const string& __what, const path& __p1, const path& __p2,
1207 error_code __ec)
1208 : system_error(__ec, __what),
1209 __paths_(make_shared<_Storage>(__p1, __p2))
1210 {}
1211
1212 _LIBCPP_INLINE_VISIBILITY
1213 const path& path1() const _NOEXCEPT {
1214 return __paths_->first;
1215 }
1216
1217 _LIBCPP_INLINE_VISIBILITY
1218 const path& path2() const _NOEXCEPT {
1219 return __paths_->second;
1220 }
1221
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001222 ~filesystem_error() override; // key function
1223
1224 // TODO(ericwf): Create a custom error message.
1225 //const char* what() const _NOEXCEPT;
1226
1227private:
1228 typedef pair<path, path> _Storage;
1229 shared_ptr<_Storage> __paths_;
1230};
1231
Marshall Clowe7acb0e2016-08-25 17:47:09 +00001232template <class... _Args>
1233_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001234#ifndef _LIBCPP_NO_EXCEPTIONS
Marshall Clowe7acb0e2016-08-25 17:47:09 +00001235void __throw_filesystem_error(_Args && ...__args)
1236{
Marshall Clowe7acb0e2016-08-25 17:47:09 +00001237 throw filesystem_error(std::forward<_Args>(__args)...);
Marshall Clowe7acb0e2016-08-25 17:47:09 +00001238}
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001239#else
1240void __throw_filesystem_error(_Args&&...)
1241{
1242 _VSTD::abort();
1243}
1244#endif
1245
Marshall Clowe7acb0e2016-08-25 17:47:09 +00001246
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001247// operational functions
1248
1249_LIBCPP_FUNC_VIS
1250path __canonical(const path&, const path&, error_code *__ec=nullptr);
1251_LIBCPP_FUNC_VIS
1252void __copy(const path& __from, const path& __to, copy_options __opt,
1253 error_code *__ec=nullptr);
1254_LIBCPP_FUNC_VIS
1255bool __copy_file(const path& __from, const path& __to, copy_options __opt,
1256 error_code *__ec=nullptr);
1257_LIBCPP_FUNC_VIS
1258void __copy_symlink(const path& __existing_symlink, const path& __new_symlink,
1259 error_code *__ec=nullptr);
1260_LIBCPP_FUNC_VIS
1261bool __create_directories(const path& p, error_code *ec=nullptr);
1262_LIBCPP_FUNC_VIS
1263bool __create_directory(const path& p, error_code *ec=nullptr);
1264_LIBCPP_FUNC_VIS
1265bool __create_directory(const path& p, const path & attributes,
1266 error_code *ec=nullptr);
1267_LIBCPP_FUNC_VIS
1268void __create_directory_symlink(const path& __to, const path& __new_symlink,
1269 error_code *__ec=nullptr);
1270_LIBCPP_FUNC_VIS
1271void __create_hard_link(const path& __to, const path& __new_hard_link,
1272 error_code *__ec=nullptr);
1273_LIBCPP_FUNC_VIS
1274void __create_symlink(const path& __to, const path& __new_symlink,
1275 error_code *__ec=nullptr);
1276_LIBCPP_FUNC_VIS
1277path __current_path(error_code *__ec=nullptr);
1278_LIBCPP_FUNC_VIS
1279void __current_path(const path&, error_code *__ec=nullptr);
1280_LIBCPP_FUNC_VIS
1281bool __equivalent(const path&, const path&, error_code *__ec=nullptr);
1282_LIBCPP_FUNC_VIS
1283uintmax_t __file_size(const path&, error_code *__ec=nullptr);
1284_LIBCPP_FUNC_VIS
1285uintmax_t __hard_link_count(const path&, error_code *__ec=nullptr);
1286_LIBCPP_FUNC_VIS
1287bool __fs_is_empty(const path& p, error_code *ec=nullptr);
1288_LIBCPP_FUNC_VIS
1289file_time_type __last_write_time(const path& p, error_code *ec=nullptr);
1290_LIBCPP_FUNC_VIS
1291void __last_write_time(const path& p, file_time_type new_time,
1292 error_code *ec=nullptr);
1293_LIBCPP_FUNC_VIS
1294void __permissions(const path& p, perms prms, error_code *ec=nullptr);
1295_LIBCPP_FUNC_VIS
1296path __read_symlink(const path& p, error_code *ec=nullptr);
1297_LIBCPP_FUNC_VIS
1298bool __remove(const path& p, error_code *ec=nullptr);
1299_LIBCPP_FUNC_VIS
1300uintmax_t __remove_all(const path& p, error_code *ec=nullptr);
1301_LIBCPP_FUNC_VIS
1302void __rename(const path& from, const path& to, error_code *ec=nullptr);
1303_LIBCPP_FUNC_VIS
1304void __resize_file(const path& p, uintmax_t size, error_code *ec=nullptr);
1305_LIBCPP_FUNC_VIS
1306space_info __space(const path&, error_code *__ec=nullptr);
1307_LIBCPP_FUNC_VIS
1308file_status __status(const path&, error_code *__ec=nullptr);
1309_LIBCPP_FUNC_VIS
1310file_status __symlink_status(const path&, error_code *__ec=nullptr);
1311_LIBCPP_FUNC_VIS
1312path __system_complete(const path&, error_code *__ec=nullptr);
1313_LIBCPP_FUNC_VIS
1314path __temp_directory_path(error_code *__ec=nullptr);
1315
1316inline _LIBCPP_INLINE_VISIBILITY
1317path current_path() {
1318 return __current_path();
1319}
1320
1321inline _LIBCPP_INLINE_VISIBILITY
1322path current_path(error_code& __ec) {
1323 return __current_path(&__ec);
1324}
1325
1326inline _LIBCPP_INLINE_VISIBILITY
1327void current_path(const path& __p) {
1328 __current_path(__p);
1329}
1330
1331inline _LIBCPP_INLINE_VISIBILITY
1332void current_path(const path& __p, error_code& __ec) _NOEXCEPT {
1333 __current_path(__p, &__ec);
1334}
1335
1336_LIBCPP_FUNC_VIS
1337path absolute(const path&, const path& __p2 = current_path());
1338
1339inline _LIBCPP_INLINE_VISIBILITY
1340path canonical(const path& __p, const path& __base = current_path()) {
1341 return __canonical(__p, __base);
1342}
1343
1344inline _LIBCPP_INLINE_VISIBILITY
1345path canonical(const path& __p, error_code& __ec) {
1346 path __base = __current_path(&__ec);
1347 if (__ec) return {};
1348 return __canonical(__p, __base, &__ec);
1349}
1350
1351inline _LIBCPP_INLINE_VISIBILITY
1352path canonical(const path& __p, const path& __base, error_code& __ec) {
1353 return __canonical(__p, __base, &__ec);
1354}
1355
1356inline _LIBCPP_INLINE_VISIBILITY
1357void copy(const path& __from, const path& __to) {
1358 __copy(__from, __to, copy_options::none);
1359}
1360
1361inline _LIBCPP_INLINE_VISIBILITY
1362void copy(const path& __from, const path& __to, error_code& __ec) _NOEXCEPT {
1363 __copy(__from, __to, copy_options::none, &__ec);
1364}
1365
1366inline _LIBCPP_INLINE_VISIBILITY
1367void copy(const path& __from, const path& __to, copy_options __opt) {
1368 __copy(__from, __to, __opt);
1369}
1370
1371inline _LIBCPP_INLINE_VISIBILITY
1372void copy(const path& __from, const path& __to,
1373 copy_options __opt, error_code& __ec) _NOEXCEPT {
1374 __copy(__from, __to, __opt, &__ec);
1375}
1376
1377inline _LIBCPP_INLINE_VISIBILITY
1378bool copy_file(const path& __from, const path& __to) {
1379 return __copy_file(__from, __to, copy_options::none);
1380}
1381
1382inline _LIBCPP_INLINE_VISIBILITY
1383bool copy_file(const path& __from, const path& __to, error_code& __ec) _NOEXCEPT {
1384 return __copy_file(__from, __to, copy_options::none, &__ec);
1385}
1386
1387inline _LIBCPP_INLINE_VISIBILITY
1388bool copy_file(const path& __from, const path& __to, copy_options __opt) {
1389 return __copy_file(__from, __to, __opt);
1390}
1391
1392inline _LIBCPP_INLINE_VISIBILITY
1393bool copy_file(const path& __from, const path& __to,
1394 copy_options __opt, error_code& __ec) _NOEXCEPT {
1395 return __copy_file(__from, __to, __opt, &__ec);
1396}
1397
1398inline _LIBCPP_INLINE_VISIBILITY
1399void copy_symlink(const path& __existing, const path& __new) {
1400 __copy_symlink(__existing, __new);
1401}
1402
1403inline _LIBCPP_INLINE_VISIBILITY
1404void copy_symlink(const path& __ext, const path& __new, error_code& __ec) _NOEXCEPT {
1405 __copy_symlink(__ext, __new, &__ec);
1406}
1407
1408inline _LIBCPP_INLINE_VISIBILITY
1409bool create_directories(const path& __p) {
1410 return __create_directories(__p);
1411}
1412
1413inline _LIBCPP_INLINE_VISIBILITY
1414bool create_directories(const path& __p, error_code& __ec) _NOEXCEPT {
1415 return __create_directories(__p, &__ec);
1416}
1417
1418inline _LIBCPP_INLINE_VISIBILITY
1419bool create_directory(const path& __p) {
1420 return __create_directory(__p);
1421}
1422
1423inline _LIBCPP_INLINE_VISIBILITY
1424bool create_directory(const path& __p, error_code& __ec) _NOEXCEPT {
1425 return __create_directory(__p, &__ec);
1426}
1427
1428inline _LIBCPP_INLINE_VISIBILITY
1429bool create_directory(const path& __p, const path& __attrs) {
1430 return __create_directory(__p, __attrs);
1431}
1432
1433inline _LIBCPP_INLINE_VISIBILITY
1434bool create_directory(const path& __p, const path& __attrs, error_code& __ec) _NOEXCEPT {
1435 return __create_directory(__p, __attrs, &__ec);
1436}
1437
1438inline _LIBCPP_INLINE_VISIBILITY
1439void create_directory_symlink(const path& __to, const path& __new) {
1440 __create_directory_symlink(__to, __new);
1441}
1442
1443inline _LIBCPP_INLINE_VISIBILITY
1444void create_directory_symlink(const path& __to, const path& __new,
1445 error_code& __ec) _NOEXCEPT {
1446 __create_directory_symlink(__to, __new, &__ec);
1447}
1448
1449inline _LIBCPP_INLINE_VISIBILITY
1450void create_hard_link(const path& __to, const path& __new) {
1451 __create_hard_link(__to, __new);
1452}
1453
1454inline _LIBCPP_INLINE_VISIBILITY
1455void create_hard_link(const path& __to, const path& __new, error_code& __ec) _NOEXCEPT {
1456 __create_hard_link(__to, __new, &__ec);
1457}
1458
1459inline _LIBCPP_INLINE_VISIBILITY
1460void create_symlink(const path& __to, const path& __new) {
1461 __create_symlink(__to, __new);
1462}
1463
1464inline _LIBCPP_INLINE_VISIBILITY
1465void create_symlink(const path& __to, const path& __new, error_code& __ec) _NOEXCEPT {
1466 return __create_symlink(__to, __new, &__ec);
1467}
1468
1469inline _LIBCPP_INLINE_VISIBILITY
1470bool status_known(file_status __s) _NOEXCEPT {
1471 return __s.type() != file_type::none;
1472}
1473
1474inline _LIBCPP_INLINE_VISIBILITY
1475bool exists(file_status __s) _NOEXCEPT {
1476 return status_known(__s) && __s.type() != file_type::not_found;
1477}
1478
1479inline _LIBCPP_INLINE_VISIBILITY
1480bool exists(const path& __p) {
1481 return exists(__status(__p));
1482}
1483
1484inline _LIBCPP_INLINE_VISIBILITY
1485bool exists(const path& __p, error_code& __ec) _NOEXCEPT {
Eric Fiselier756a6bd2016-06-21 22:11:16 +00001486 auto __s = __status(__p, &__ec);
1487 if (status_known(__s)) __ec.clear();
1488 return exists(__s);
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001489}
1490
1491inline _LIBCPP_INLINE_VISIBILITY
1492bool equivalent(const path& __p1, const path& __p2) {
1493 return __equivalent(__p1, __p2);
1494}
1495
1496inline _LIBCPP_INLINE_VISIBILITY
1497bool equivalent(const path& __p1, const path& __p2, error_code& __ec) _NOEXCEPT {
1498 return __equivalent(__p1, __p2, &__ec);
1499}
1500
1501inline _LIBCPP_INLINE_VISIBILITY
1502uintmax_t file_size(const path& __p) {
1503 return __file_size(__p);
1504}
1505
1506inline _LIBCPP_INLINE_VISIBILITY
1507uintmax_t file_size(const path& __p, error_code& __ec) _NOEXCEPT {
1508 return __file_size(__p, &__ec);
1509}
1510
1511inline _LIBCPP_INLINE_VISIBILITY
1512uintmax_t hard_link_count(const path& __p) {
1513 return __hard_link_count(__p);
1514}
1515
1516inline _LIBCPP_INLINE_VISIBILITY
1517uintmax_t hard_link_count(const path& __p, error_code& __ec) _NOEXCEPT {
1518 return __hard_link_count(__p, &__ec);
1519}
1520
1521inline _LIBCPP_INLINE_VISIBILITY
1522bool is_block_file(file_status __s) _NOEXCEPT {
1523 return __s.type() == file_type::block;
1524}
1525
1526inline _LIBCPP_INLINE_VISIBILITY
1527bool is_block_file(const path& __p) {
1528 return is_block_file(__status(__p));
1529}
1530
1531inline _LIBCPP_INLINE_VISIBILITY
1532bool is_block_file(const path& __p, error_code& __ec) _NOEXCEPT {
1533 return is_block_file(__status(__p, &__ec));
1534}
1535
1536inline _LIBCPP_INLINE_VISIBILITY
1537bool is_character_file(file_status __s) _NOEXCEPT {
1538 return __s.type() == file_type::character;
1539}
1540
1541inline _LIBCPP_INLINE_VISIBILITY
1542bool is_character_file(const path& __p) {
1543 return is_character_file(__status(__p));
1544}
1545
1546inline _LIBCPP_INLINE_VISIBILITY
1547bool is_character_file(const path& __p, error_code& __ec) _NOEXCEPT {
1548 return is_character_file(__status(__p, &__ec));
1549}
1550
1551inline _LIBCPP_INLINE_VISIBILITY
1552bool is_directory(file_status __s) _NOEXCEPT {
1553 return __s.type() == file_type::directory;
1554}
1555
1556inline _LIBCPP_INLINE_VISIBILITY
1557bool is_directory(const path& __p) {
1558 return is_directory(__status(__p));
1559}
1560
1561inline _LIBCPP_INLINE_VISIBILITY
1562bool is_directory(const path& __p, error_code& __ec) _NOEXCEPT {
1563 return is_directory(__status(__p, &__ec));
1564}
1565
1566inline _LIBCPP_INLINE_VISIBILITY
1567bool is_empty(const path& __p) {
1568 return __fs_is_empty(__p);
1569}
1570
1571inline _LIBCPP_INLINE_VISIBILITY
1572bool is_empty(const path& __p, error_code& __ec) _NOEXCEPT {
1573 return __fs_is_empty(__p, &__ec);
1574}
1575
1576inline _LIBCPP_INLINE_VISIBILITY
1577bool is_fifo(file_status __s) _NOEXCEPT {
1578 return __s.type() == file_type::fifo;
1579}
1580inline _LIBCPP_INLINE_VISIBILITY
1581bool is_fifo(const path& __p) {
1582 return is_fifo(__status(__p));
1583}
1584
1585inline _LIBCPP_INLINE_VISIBILITY
1586bool is_fifo(const path& __p, error_code& __ec) _NOEXCEPT {
1587 return is_fifo(__status(__p, &__ec));
1588}
1589
1590inline _LIBCPP_INLINE_VISIBILITY
1591bool is_regular_file(file_status __s) _NOEXCEPT {
1592 return __s.type() == file_type::regular;
1593}
1594
1595inline _LIBCPP_INLINE_VISIBILITY
1596bool is_regular_file(const path& __p) {
1597 return is_regular_file(__status(__p));
1598}
1599
1600inline _LIBCPP_INLINE_VISIBILITY
1601bool is_regular_file(const path& __p, error_code& __ec) _NOEXCEPT {
1602 return is_regular_file(__status(__p, &__ec));
1603}
1604
1605inline _LIBCPP_INLINE_VISIBILITY
1606bool is_socket(file_status __s) _NOEXCEPT {
1607 return __s.type() == file_type::socket;
1608}
1609
1610inline _LIBCPP_INLINE_VISIBILITY
1611bool is_socket(const path& __p) {
1612 return is_socket(__status(__p));
1613}
1614
1615inline _LIBCPP_INLINE_VISIBILITY
1616bool is_socket(const path& __p, error_code& __ec) _NOEXCEPT {
1617 return is_socket(__status(__p, &__ec));
1618}
1619
1620inline _LIBCPP_INLINE_VISIBILITY
1621bool is_symlink(file_status __s) _NOEXCEPT {
1622 return __s.type() == file_type::symlink;
1623}
1624
1625inline _LIBCPP_INLINE_VISIBILITY
1626bool is_symlink(const path& __p) {
1627 return is_symlink(__symlink_status(__p));
1628}
1629
1630inline _LIBCPP_INLINE_VISIBILITY
1631bool is_symlink(const path& __p, error_code& __ec) _NOEXCEPT {
1632 return is_symlink(__symlink_status(__p, &__ec));
1633}
1634
1635inline _LIBCPP_INLINE_VISIBILITY
1636bool is_other(file_status __s) _NOEXCEPT {
1637 return exists(__s)
1638 && !is_regular_file(__s) && !is_directory(__s) && !is_symlink(__s);
1639}
1640
1641inline _LIBCPP_INLINE_VISIBILITY
1642bool is_other(const path& __p) {
1643 return is_other(__status(__p));
1644}
1645
1646inline _LIBCPP_INLINE_VISIBILITY
1647bool is_other(const path& __p, error_code& __ec) _NOEXCEPT {
1648 return is_other(__status(__p, &__ec));
1649}
1650
1651inline _LIBCPP_INLINE_VISIBILITY
1652file_time_type last_write_time(const path& __p) {
1653 return __last_write_time(__p);
1654}
1655
1656inline _LIBCPP_INLINE_VISIBILITY
1657file_time_type last_write_time(const path& __p, error_code& __ec) _NOEXCEPT {
1658 return __last_write_time(__p, &__ec);
1659}
1660
1661inline _LIBCPP_INLINE_VISIBILITY
1662void last_write_time(const path& __p, file_time_type __t) {
1663 __last_write_time(__p, __t);
1664}
1665
1666inline _LIBCPP_INLINE_VISIBILITY
1667void last_write_time(const path& __p, file_time_type __t, error_code& __ec) _NOEXCEPT {
1668 __last_write_time(__p, __t, &__ec);
1669}
1670
1671inline _LIBCPP_INLINE_VISIBILITY
1672void permissions(const path& __p, perms __prms) {
1673 __permissions(__p, __prms);
1674}
1675
1676inline _LIBCPP_INLINE_VISIBILITY
1677void permissions(const path& __p, perms __prms, error_code& __ec) {
1678 __permissions(__p, __prms, &__ec);
1679}
1680
1681inline _LIBCPP_INLINE_VISIBILITY
1682path read_symlink(const path& __p) {
1683 return __read_symlink(__p);
1684}
1685
1686inline _LIBCPP_INLINE_VISIBILITY
1687path read_symlink(const path& __p, error_code& __ec) {
1688 return __read_symlink(__p, &__ec);
1689}
1690
1691inline _LIBCPP_INLINE_VISIBILITY
1692bool remove(const path& __p) {
1693 return __remove(__p);
1694}
1695
1696inline _LIBCPP_INLINE_VISIBILITY
1697bool remove(const path& __p, error_code& __ec) _NOEXCEPT {
1698 return __remove(__p, &__ec);
1699}
1700
1701inline _LIBCPP_INLINE_VISIBILITY
1702uintmax_t remove_all(const path& __p) {
1703 return __remove_all(__p);
1704}
1705
1706inline _LIBCPP_INLINE_VISIBILITY
1707uintmax_t remove_all(const path& __p, error_code& __ec) _NOEXCEPT {
1708 return __remove_all(__p, &__ec);
1709}
1710
1711inline _LIBCPP_INLINE_VISIBILITY
1712void rename(const path& __from, const path& __to) {
1713 return __rename(__from, __to);
1714}
1715
1716inline _LIBCPP_INLINE_VISIBILITY
1717void rename(const path& __from, const path& __to, error_code& __ec) _NOEXCEPT {
1718 return __rename(__from, __to, &__ec);
1719}
1720
1721inline _LIBCPP_INLINE_VISIBILITY
1722void resize_file(const path& __p, uintmax_t __ns) {
1723 return __resize_file(__p, __ns);
1724}
1725
1726inline _LIBCPP_INLINE_VISIBILITY
1727void resize_file(const path& __p, uintmax_t __ns, error_code& __ec) _NOEXCEPT {
1728 return __resize_file(__p, __ns, &__ec);
1729}
1730
1731inline _LIBCPP_INLINE_VISIBILITY
1732space_info space(const path& __p) {
1733 return __space(__p);
1734}
1735
1736inline _LIBCPP_INLINE_VISIBILITY
1737space_info space(const path& __p, error_code& __ec) _NOEXCEPT {
1738 return __space(__p, &__ec);
1739}
1740
1741inline _LIBCPP_INLINE_VISIBILITY
1742file_status status(const path& __p) {
1743 return __status(__p);
1744}
1745
1746inline _LIBCPP_INLINE_VISIBILITY
1747file_status status(const path& __p, error_code& __ec) _NOEXCEPT {
1748 return __status(__p, &__ec);
1749}
1750
1751inline _LIBCPP_INLINE_VISIBILITY
1752file_status symlink_status(const path& __p) {
1753 return __symlink_status(__p);
1754}
1755
1756inline _LIBCPP_INLINE_VISIBILITY
1757file_status symlink_status(const path& __p, error_code& __ec) _NOEXCEPT {
1758 return __symlink_status(__p, &__ec);
1759}
1760
1761inline _LIBCPP_INLINE_VISIBILITY
1762path system_complete(const path& __p) {
1763 return __system_complete(__p);
1764}
1765
1766inline _LIBCPP_INLINE_VISIBILITY
1767path system_complete(const path& __p, error_code& __ec) {
1768 return __system_complete(__p, &__ec);
1769}
1770
1771inline _LIBCPP_INLINE_VISIBILITY
1772path temp_directory_path() {
1773 return __temp_directory_path();
1774}
1775
1776inline _LIBCPP_INLINE_VISIBILITY
1777path temp_directory_path(error_code& __ec) {
1778 return __temp_directory_path(&__ec);
1779}
1780
1781
1782class directory_entry
1783{
1784 typedef _VSTD_FS::path _Path;
1785
1786public:
1787 // constructors and destructors
1788 directory_entry() _NOEXCEPT = default;
1789 directory_entry(directory_entry const&) = default;
1790 directory_entry(directory_entry&&) _NOEXCEPT = default;
1791
1792 _LIBCPP_INLINE_VISIBILITY
1793 explicit directory_entry(_Path const& __p) : __p_(__p) {}
1794
1795 ~directory_entry() {}
1796
1797 directory_entry& operator=(directory_entry const&) = default;
1798 directory_entry& operator=(directory_entry&&) _NOEXCEPT = default;
1799
1800 _LIBCPP_INLINE_VISIBILITY
1801 void assign(_Path const& __p) {
1802 __p_ = __p;
1803 }
1804
1805 _LIBCPP_INLINE_VISIBILITY
1806 void replace_filename(_Path const& __p) {
1807 __p_ = __p_.parent_path() / __p;
1808 }
1809
1810 _LIBCPP_INLINE_VISIBILITY
1811 _Path const& path() const _NOEXCEPT {
1812 return __p_;
1813 }
1814
1815 _LIBCPP_INLINE_VISIBILITY
1816 operator const _Path&() const _NOEXCEPT {
1817 return __p_;
1818 }
1819
1820 _LIBCPP_INLINE_VISIBILITY
1821 file_status status() const {
1822 return _VSTD_FS::status(__p_);
1823 }
1824
1825 _LIBCPP_INLINE_VISIBILITY
1826 file_status status(error_code& __ec) const _NOEXCEPT {
1827 return _VSTD_FS::status(__p_, __ec);
1828 }
1829
1830 _LIBCPP_INLINE_VISIBILITY
1831 file_status symlink_status() const {
1832 return _VSTD_FS::symlink_status(__p_);
1833 }
1834
1835 _LIBCPP_INLINE_VISIBILITY
1836 file_status symlink_status(error_code& __ec) const _NOEXCEPT {
1837 return _VSTD_FS::symlink_status(__p_, __ec);
1838 }
1839
1840 _LIBCPP_INLINE_VISIBILITY
1841 bool operator< (directory_entry const& __rhs) const _NOEXCEPT {
1842 return __p_ < __rhs.__p_;
1843 }
1844
1845 _LIBCPP_INLINE_VISIBILITY
1846 bool operator==(directory_entry const& __rhs) const _NOEXCEPT {
1847 return __p_ == __rhs.__p_;
1848 }
1849
1850 _LIBCPP_INLINE_VISIBILITY
1851 bool operator!=(directory_entry const& __rhs) const _NOEXCEPT {
1852 return __p_ != __rhs.__p_;
1853 }
1854
1855 _LIBCPP_INLINE_VISIBILITY
1856 bool operator<=(directory_entry const& __rhs) const _NOEXCEPT {
1857 return __p_ <= __rhs.__p_;
1858 }
1859
1860 _LIBCPP_INLINE_VISIBILITY
1861 bool operator> (directory_entry const& __rhs) const _NOEXCEPT {
1862 return __p_ > __rhs.__p_;
1863 }
1864
1865 _LIBCPP_INLINE_VISIBILITY
1866 bool operator>=(directory_entry const& __rhs) const _NOEXCEPT {
1867 return __p_ >= __rhs.__p_;
1868 }
1869private:
1870 _Path __p_;
1871};
1872
1873
1874class directory_iterator;
1875class recursive_directory_iterator;
1876class __dir_stream;
1877
1878class __dir_element_proxy {
1879public:
1880
1881 inline _LIBCPP_INLINE_VISIBILITY
1882 directory_entry operator*() { return _VSTD::move(__elem_); }
1883
1884private:
1885 friend class directory_iterator;
1886 friend class recursive_directory_iterator;
1887 explicit __dir_element_proxy(directory_entry const& __e) : __elem_(__e) {}
1888 __dir_element_proxy(__dir_element_proxy&& __o) : __elem_(_VSTD::move(__o.__elem_)) {}
1889 directory_entry __elem_;
1890};
1891
1892class directory_iterator
1893{
1894public:
1895 typedef directory_entry value_type;
1896 typedef ptrdiff_t difference_type;
1897 typedef value_type const* pointer;
1898 typedef value_type const& reference;
1899 typedef input_iterator_tag iterator_category;
1900
1901public:
1902 //ctor & dtor
1903 directory_iterator() _NOEXCEPT
1904 { }
1905
1906 explicit directory_iterator(const path& __p)
1907 : directory_iterator(__p, nullptr)
1908 { }
1909
1910 directory_iterator(const path& __p, directory_options __opts)
1911 : directory_iterator(__p, nullptr, __opts)
1912 { }
1913
1914 directory_iterator(const path& __p, error_code& __ec) _NOEXCEPT
1915 : directory_iterator(__p, &__ec)
1916 { }
1917
1918 directory_iterator(const path& __p, directory_options __opts,
1919 error_code& __ec) _NOEXCEPT
1920 : directory_iterator(__p, &__ec, __opts)
1921 { }
1922
1923 directory_iterator(const directory_iterator&) = default;
1924 directory_iterator(directory_iterator&&) = default;
1925 directory_iterator& operator=(const directory_iterator&) = default;
1926
1927 directory_iterator& operator=(directory_iterator&& __o) _NOEXCEPT {
1928 // non-default implementation provided to support self-move assign.
1929 if (this != &__o) {
1930 __imp_ = _VSTD::move(__o.__imp_);
1931 }
1932 return *this;
1933 }
1934
1935 ~directory_iterator() = default;
1936
1937 const directory_entry& operator*() const {
1938 _LIBCPP_ASSERT(__imp_, "The end iterator cannot be dereferenced");
Saleem Abdulrasoolb35cd982017-01-30 00:15:47 +00001939 return __dereference();
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001940 }
1941
1942 const directory_entry* operator->() const
1943 { return &**this; }
1944
1945 directory_iterator& operator++()
1946 { return __increment(); }
1947
1948 __dir_element_proxy operator++(int) {
1949 __dir_element_proxy __p(**this);
1950 __increment();
1951 return __p;
1952 }
1953
1954 directory_iterator& increment(error_code& __ec) _NOEXCEPT
1955 { return __increment(&__ec); }
1956
1957private:
Eric Fiselier03f7d102016-09-16 00:07:16 +00001958 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001959 friend bool operator==(const directory_iterator& __lhs,
1960 const directory_iterator& __rhs) _NOEXCEPT;
1961
1962 // construct the dir_stream
1963 _LIBCPP_FUNC_VIS
Saleem Abdulrasoolb35cd982017-01-30 00:15:47 +00001964 directory_iterator(const path&, error_code *,
1965 directory_options = directory_options::none);
1966
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001967 _LIBCPP_FUNC_VIS
1968 directory_iterator& __increment(error_code * __ec = nullptr);
Saleem Abdulrasoolb35cd982017-01-30 00:15:47 +00001969
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001970 _LIBCPP_FUNC_VIS
Saleem Abdulrasoolb35cd982017-01-30 00:15:47 +00001971 const directory_entry& __dereference() const;
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001972
1973private:
1974 shared_ptr<__dir_stream> __imp_;
1975};
1976
1977
1978inline _LIBCPP_INLINE_VISIBILITY
1979bool operator==(const directory_iterator& __lhs,
1980 const directory_iterator& __rhs) _NOEXCEPT {
1981 return __lhs.__imp_ == __rhs.__imp_;
1982}
1983
1984inline _LIBCPP_INLINE_VISIBILITY
1985bool operator!=(const directory_iterator& __lhs,
1986 const directory_iterator& __rhs) _NOEXCEPT {
1987 return !(__lhs == __rhs);
1988}
1989
1990// enable directory_iterator range-based for statements
1991inline _LIBCPP_INLINE_VISIBILITY
1992directory_iterator begin(directory_iterator __iter) _NOEXCEPT {
1993 return __iter;
1994}
1995
1996inline _LIBCPP_INLINE_VISIBILITY
1997directory_iterator end(const directory_iterator&) _NOEXCEPT {
1998 return directory_iterator();
1999}
2000
2001class recursive_directory_iterator {
2002public:
2003 using value_type = directory_entry;
2004 using difference_type = std::ptrdiff_t;
2005 using pointer = directory_entry const *;
2006 using reference = directory_entry const &;
2007 using iterator_category = std::input_iterator_tag;
2008
2009public:
2010 // constructors and destructor
2011 _LIBCPP_INLINE_VISIBILITY
2012 recursive_directory_iterator() _NOEXCEPT
2013 : __rec_(false)
2014 {}
2015
2016 _LIBCPP_INLINE_VISIBILITY
2017 explicit recursive_directory_iterator(const path& __p,
2018 directory_options __xoptions = directory_options::none)
2019 : recursive_directory_iterator(__p, __xoptions, nullptr)
2020 { }
2021
2022 _LIBCPP_INLINE_VISIBILITY
2023 recursive_directory_iterator(const path& __p,
2024 directory_options __xoptions, error_code& __ec) _NOEXCEPT
2025 : recursive_directory_iterator(__p, __xoptions, &__ec)
2026 { }
2027
2028 _LIBCPP_INLINE_VISIBILITY
2029 recursive_directory_iterator(const path& __p, error_code& __ec) _NOEXCEPT
2030 : recursive_directory_iterator(__p, directory_options::none, &__ec)
2031 { }
2032
2033 recursive_directory_iterator(const recursive_directory_iterator&) = default;
2034 recursive_directory_iterator(recursive_directory_iterator&&) = default;
2035
2036 recursive_directory_iterator &
2037 operator=(const recursive_directory_iterator&) = default;
2038
2039 _LIBCPP_INLINE_VISIBILITY
2040 recursive_directory_iterator &
2041 operator=(recursive_directory_iterator&& __o) noexcept {
2042 // non-default implementation provided to support self-move assign.
2043 if (this != &__o) {
2044 __imp_ = _VSTD::move(__o.__imp_);
2045 __rec_ = __o.__rec_;
2046 }
2047 return *this;
2048 }
2049
2050 ~recursive_directory_iterator() = default;
2051
2052 _LIBCPP_INLINE_VISIBILITY
2053 const directory_entry& operator*() const
Saleem Abdulrasoolb35cd982017-01-30 00:15:47 +00002054 { return __dereference(); }
Eric Fiselier6e9a6942016-06-17 19:46:40 +00002055
2056 _LIBCPP_INLINE_VISIBILITY
2057 const directory_entry* operator->() const
Saleem Abdulrasoolb35cd982017-01-30 00:15:47 +00002058 { return &__dereference(); }
Eric Fiselier6e9a6942016-06-17 19:46:40 +00002059
2060 recursive_directory_iterator& operator++()
2061 { return __increment(); }
2062
2063 _LIBCPP_INLINE_VISIBILITY
2064 __dir_element_proxy operator++(int) {
2065 __dir_element_proxy __p(**this);
2066 __increment();
2067 return __p;
2068 }
2069
2070 _LIBCPP_INLINE_VISIBILITY
2071 recursive_directory_iterator& increment(error_code& __ec) _NOEXCEPT
2072 { return __increment(&__ec); }
2073
2074 _LIBCPP_FUNC_VIS directory_options options() const;
2075 _LIBCPP_FUNC_VIS int depth() const;
2076
2077 _LIBCPP_INLINE_VISIBILITY
2078 void pop() { __pop(); }
2079
2080 _LIBCPP_INLINE_VISIBILITY
2081 void pop(error_code& __ec)
2082 { __pop(&__ec); }
2083
2084 _LIBCPP_INLINE_VISIBILITY
2085 bool recursion_pending() const
2086 { return __rec_; }
2087
2088 _LIBCPP_INLINE_VISIBILITY
2089 void disable_recursion_pending()
2090 { __rec_ = false; }
2091
2092private:
2093 recursive_directory_iterator(const path& __p, directory_options __opt,
2094 error_code *__ec);
2095
2096 _LIBCPP_FUNC_VIS
Saleem Abdulrasoolb35cd982017-01-30 00:15:47 +00002097 const directory_entry& __dereference() const;
Eric Fiselier6e9a6942016-06-17 19:46:40 +00002098
2099 _LIBCPP_FUNC_VIS
2100 bool __try_recursion(error_code* __ec);
2101
2102 _LIBCPP_FUNC_VIS
2103 void __advance(error_code* __ec=nullptr);
2104
2105 _LIBCPP_FUNC_VIS
2106 recursive_directory_iterator& __increment(error_code *__ec=nullptr);
2107
2108 _LIBCPP_FUNC_VIS
2109 void __pop(error_code* __ec=nullptr);
2110
Eric Fiselier03f7d102016-09-16 00:07:16 +00002111 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6e9a6942016-06-17 19:46:40 +00002112 friend bool operator==(const recursive_directory_iterator&,
2113 const recursive_directory_iterator&) _NOEXCEPT;
2114
2115 struct __shared_imp;
2116 shared_ptr<__shared_imp> __imp_;
2117 bool __rec_;
2118}; // class recursive_directory_iterator
2119
2120
Eric Fiselier03f7d102016-09-16 00:07:16 +00002121inline _LIBCPP_INLINE_VISIBILITY
2122bool operator==(const recursive_directory_iterator& __lhs,
2123 const recursive_directory_iterator& __rhs) _NOEXCEPT
Eric Fiselier6e9a6942016-06-17 19:46:40 +00002124{
2125 return __lhs.__imp_ == __rhs.__imp_;
2126}
2127
2128_LIBCPP_INLINE_VISIBILITY
2129inline bool operator!=(const recursive_directory_iterator& __lhs,
2130 const recursive_directory_iterator& __rhs) _NOEXCEPT
2131{
2132 return !(__lhs == __rhs);
2133}
2134// enable recursive_directory_iterator range-based for statements
2135inline _LIBCPP_INLINE_VISIBILITY
2136recursive_directory_iterator begin(recursive_directory_iterator __iter) _NOEXCEPT {
2137 return __iter;
2138}
2139
2140inline _LIBCPP_INLINE_VISIBILITY
2141recursive_directory_iterator end(const recursive_directory_iterator&) _NOEXCEPT {
2142 return recursive_directory_iterator();
2143}
2144
2145_LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM
2146
2147#endif // _LIBCPP_EXPERIMENTAL_FILESYSTEM