]>
Commit | Line | Data |
---|---|---|
0ca7ba9a JW |
1 | // Class filesystem::path -*- C++ -*- |
2 | ||
7adcbafe | 3 | // Copyright (C) 2014-2022 Free Software Foundation, Inc. |
0ca7ba9a JW |
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 | ||
6b4f8906 | 25 | /** @file experimental/bits/fs_path.h |
0ca7ba9a JW |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{experimental/filesystem} | |
28 | */ | |
29 | ||
30 | #ifndef _GLIBCXX_EXPERIMENTAL_FS_PATH_H | |
31 | #define _GLIBCXX_EXPERIMENTAL_FS_PATH_H 1 | |
32 | ||
33 | #if __cplusplus < 201103L | |
34 | # include <bits/c++0x_warning.h> | |
35 | #else | |
36 | ||
37 | #include <utility> | |
38 | #include <type_traits> | |
a00d74c4 | 39 | #include <vector> |
0ca7ba9a JW |
40 | #include <locale> |
41 | #include <iosfwd> | |
42 | #include <codecvt> | |
43 | #include <system_error> | |
44 | #include <bits/stl_algobase.h> | |
45 | #include <bits/quoted_string.h> | |
46 | #include <bits/locale_conv.h> | |
f0414b97 JW |
47 | #if __cplusplus == 201402L |
48 | # include <experimental/string_view> | |
49 | #endif | |
0ca7ba9a JW |
50 | |
51 | #if defined(_WIN32) && !defined(__CYGWIN__) | |
52 | # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1 | |
53 | # include <algorithm> | |
54 | #endif | |
55 | ||
56 | namespace std _GLIBCXX_VISIBILITY(default) | |
57 | { | |
4a15d842 FD |
58 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
59 | ||
0ca7ba9a JW |
60 | namespace experimental |
61 | { | |
62 | namespace filesystem | |
63 | { | |
64 | inline namespace v1 | |
65 | { | |
0ca7ba9a JW |
66 | _GLIBCXX_BEGIN_NAMESPACE_CXX11 |
67 | ||
f0414b97 JW |
68 | #if __cplusplus == 201402L |
69 | using std::experimental::basic_string_view; | |
70 | #elif __cplusplus > 201402L | |
71 | using std::basic_string_view; | |
72 | #endif | |
73 | ||
f2ce64b5 JW |
74 | /// @cond undocumented |
75 | namespace __detail | |
76 | { | |
a1e7d33b TH |
77 | /** @addtogroup filesystem-ts |
78 | * @{ | |
0ca7ba9a JW |
79 | */ |
80 | ||
a1e7d33b TH |
81 | template<typename _CharT, |
82 | typename _Ch = typename remove_const<_CharT>::type> | |
83 | using __is_encoded_char | |
84 | = __or_<is_same<_Ch, char>, | |
85 | is_same<_Ch, wchar_t>, | |
c124af93 | 86 | #ifdef _GLIBCXX_USE_CHAR8_T |
a1e7d33b | 87 | is_same<_Ch, char8_t>, |
c124af93 | 88 | #endif |
a1e7d33b TH |
89 | is_same<_Ch, char16_t>, |
90 | is_same<_Ch, char32_t>>; | |
0ca7ba9a | 91 | |
a1e7d33b TH |
92 | template<typename _Iter, |
93 | typename _Iter_traits = std::iterator_traits<_Iter>> | |
94 | using __is_path_iter_src | |
95 | = __and_<__is_encoded_char<typename _Iter_traits::value_type>, | |
96 | std::is_base_of<std::input_iterator_tag, | |
97 | typename _Iter_traits::iterator_category>>; | |
0ca7ba9a | 98 | |
a1e7d33b TH |
99 | template<typename _Iter> |
100 | static __is_path_iter_src<_Iter> | |
101 | __is_path_src(_Iter, int); | |
0ca7ba9a | 102 | |
a1e7d33b TH |
103 | template<typename _CharT, typename _Traits, typename _Alloc> |
104 | static __is_encoded_char<_CharT> | |
105 | __is_path_src(const basic_string<_CharT, _Traits, _Alloc>&, int); | |
0ca7ba9a | 106 | |
f0414b97 | 107 | #if __cplusplus >= 201402L |
a1e7d33b TH |
108 | template<typename _CharT, typename _Traits> |
109 | static __is_encoded_char<_CharT> | |
110 | __is_path_src(const basic_string_view<_CharT, _Traits>&, int); | |
f0414b97 JW |
111 | #endif |
112 | ||
a1e7d33b TH |
113 | template<typename _Unknown> |
114 | static std::false_type | |
115 | __is_path_src(const _Unknown&, ...); | |
0ca7ba9a | 116 | |
a1e7d33b TH |
117 | template<typename _Tp1, typename _Tp2> |
118 | struct __constructible_from; | |
0ca7ba9a | 119 | |
a1e7d33b TH |
120 | template<typename _Iter> |
121 | struct __constructible_from<_Iter, _Iter> | |
122 | : __is_path_iter_src<_Iter> | |
123 | { }; | |
0ca7ba9a | 124 | |
a1e7d33b TH |
125 | template<typename _Source> |
126 | struct __constructible_from<_Source, void> | |
45aa7a44 | 127 | : decltype(__is_path_src(std::declval<const _Source&>(), 0)) |
a1e7d33b TH |
128 | { }; |
129 | ||
130 | template<typename _Tp1, typename _Tp2 = void, | |
131 | typename _Tp1_nocv = typename remove_cv<_Tp1>::type, | |
132 | typename _Tp1_noptr = typename remove_pointer<_Tp1>::type> | |
133 | using _Path = typename | |
134 | std::enable_if<__and_<__not_<is_same<_Tp1_nocv, path>>, | |
135 | __not_<is_void<_Tp1_noptr>>, | |
136 | __constructible_from<_Tp1, _Tp2>>::value, | |
137 | path>::type; | |
0ca7ba9a | 138 | |
a1e7d33b | 139 | template<typename _Source> |
00c8f2a5 | 140 | inline _Source |
a1e7d33b | 141 | _S_range_begin(_Source __begin) { return __begin; } |
0ca7ba9a | 142 | |
a1e7d33b | 143 | struct __null_terminated { }; |
0ca7ba9a | 144 | |
a1e7d33b | 145 | template<typename _Source> |
00c8f2a5 | 146 | inline __null_terminated |
a1e7d33b | 147 | _S_range_end(_Source) { return {}; } |
0ca7ba9a | 148 | |
a1e7d33b | 149 | template<typename _CharT, typename _Traits, typename _Alloc> |
00c8f2a5 | 150 | inline const _CharT* |
a1e7d33b TH |
151 | _S_range_begin(const basic_string<_CharT, _Traits, _Alloc>& __str) |
152 | { return __str.data(); } | |
0ca7ba9a | 153 | |
a1e7d33b | 154 | template<typename _CharT, typename _Traits, typename _Alloc> |
00c8f2a5 | 155 | inline const _CharT* |
a1e7d33b TH |
156 | _S_range_end(const basic_string<_CharT, _Traits, _Alloc>& __str) |
157 | { return __str.data() + __str.size(); } | |
0ca7ba9a | 158 | |
f0414b97 | 159 | #if __cplusplus >= 201402L |
a1e7d33b | 160 | template<typename _CharT, typename _Traits> |
00c8f2a5 | 161 | inline const _CharT* |
a1e7d33b TH |
162 | _S_range_begin(const basic_string_view<_CharT, _Traits>& __str) |
163 | { return __str.data(); } | |
164 | ||
165 | template<typename _CharT, typename _Traits> | |
00c8f2a5 | 166 | inline const _CharT* |
a1e7d33b TH |
167 | _S_range_end(const basic_string_view<_CharT, _Traits>& __str) |
168 | { return __str.data() + __str.size(); } | |
f0414b97 JW |
169 | #endif |
170 | ||
a1e7d33b TH |
171 | template<typename _Tp, |
172 | typename _Iter = decltype(_S_range_begin(std::declval<_Tp>())), | |
2b4e2c93 TH |
173 | typename _Val = typename std::iterator_traits<_Iter>::value_type, |
174 | typename _UnqualVal = typename std::remove_const<_Val>::type> | |
a1e7d33b | 175 | using __value_type_is_char = typename std::enable_if< |
2b4e2c93 TH |
176 | std::is_same<_UnqualVal, char>::value, |
177 | _UnqualVal>::type; | |
178 | ||
179 | template<typename _Tp, | |
180 | typename _Iter = decltype(_S_range_begin(std::declval<_Tp>())), | |
181 | typename _Val = typename std::iterator_traits<_Iter>::value_type, | |
182 | typename _UnqualVal = typename std::remove_const<_Val>::type> | |
183 | using __value_type_is_char_or_char8_t = typename std::enable_if< | |
184 | __or_< | |
185 | std::is_same<_UnqualVal, char> | |
186 | #ifdef _GLIBCXX_USE_CHAR8_T | |
187 | ,std::is_same<_UnqualVal, char8_t> | |
188 | #endif | |
189 | >::value, _UnqualVal>::type; | |
a1e7d33b | 190 | |
f2ce64b5 | 191 | /// @} group filesystem-ts |
a1e7d33b TH |
192 | } // namespace __detail |
193 | /// @endcond | |
0ca7ba9a | 194 | |
f2ce64b5 JW |
195 | /** @addtogroup filesystem-ts |
196 | * @{ | |
197 | */ | |
198 | ||
a1e7d33b | 199 | /// A filesystem path. |
f2ce64b5 | 200 | /// @ingroup filesystem-ts |
a1e7d33b TH |
201 | class path |
202 | { | |
0ca7ba9a JW |
203 | public: |
204 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
205 | typedef wchar_t value_type; | |
206 | static constexpr value_type preferred_separator = L'\\'; | |
207 | #else | |
208 | typedef char value_type; | |
209 | static constexpr value_type preferred_separator = '/'; | |
210 | #endif | |
211 | typedef std::basic_string<value_type> string_type; | |
212 | ||
213 | // constructors and destructor | |
214 | ||
215 | path() noexcept { } | |
216 | ||
217 | path(const path& __p) = default; | |
218 | ||
219 | path(path&& __p) noexcept | |
220 | : _M_pathname(std::move(__p._M_pathname)), _M_type(__p._M_type) | |
221 | { | |
441ed45c JW |
222 | if (_M_type == _Type::_Multi) |
223 | _M_split_cmpts(); | |
0ca7ba9a JW |
224 | __p.clear(); |
225 | } | |
226 | ||
e59e183f JW |
227 | path(string_type&& __source) |
228 | : _M_pathname(std::move(__source)) | |
229 | { _M_split_cmpts(); } | |
230 | ||
0ca7ba9a | 231 | template<typename _Source, |
a1e7d33b | 232 | typename _Require = __detail::_Path<_Source>> |
0ca7ba9a | 233 | path(_Source const& __source) |
a1e7d33b TH |
234 | : _M_pathname(_S_convert(__detail::_S_range_begin(__source), |
235 | __detail::_S_range_end(__source))) | |
0ca7ba9a JW |
236 | { _M_split_cmpts(); } |
237 | ||
238 | template<typename _InputIterator, | |
a1e7d33b | 239 | typename _Require = __detail::_Path<_InputIterator, _InputIterator>> |
0ca7ba9a JW |
240 | path(_InputIterator __first, _InputIterator __last) |
241 | : _M_pathname(_S_convert(__first, __last)) | |
242 | { _M_split_cmpts(); } | |
243 | ||
244 | template<typename _Source, | |
a1e7d33b TH |
245 | typename _Require = __detail::_Path<_Source>, |
246 | typename _Require2 = __detail::__value_type_is_char<_Source>> | |
0ca7ba9a | 247 | path(_Source const& __source, const locale& __loc) |
a1e7d33b TH |
248 | : _M_pathname(_S_convert_loc(__detail::_S_range_begin(__source), |
249 | __detail::_S_range_end(__source), __loc)) | |
0ca7ba9a JW |
250 | { _M_split_cmpts(); } |
251 | ||
252 | template<typename _InputIterator, | |
a1e7d33b TH |
253 | typename _Require = __detail::_Path<_InputIterator, _InputIterator>, |
254 | typename _Require2 = __detail::__value_type_is_char<_InputIterator>> | |
0ca7ba9a JW |
255 | path(_InputIterator __first, _InputIterator __last, const locale& __loc) |
256 | : _M_pathname(_S_convert_loc(__first, __last, __loc)) | |
257 | { _M_split_cmpts(); } | |
258 | ||
259 | ~path() = default; | |
260 | ||
261 | // assignments | |
262 | ||
263 | path& operator=(const path& __p) = default; | |
264 | path& operator=(path&& __p) noexcept; | |
e59e183f JW |
265 | path& operator=(string_type&& __source); |
266 | path& assign(string_type&& __source); | |
0ca7ba9a JW |
267 | |
268 | template<typename _Source> | |
a1e7d33b | 269 | __detail::_Path<_Source>& |
0ca7ba9a JW |
270 | operator=(_Source const& __source) |
271 | { return *this = path(__source); } | |
272 | ||
273 | template<typename _Source> | |
a1e7d33b | 274 | __detail::_Path<_Source>& |
0ca7ba9a JW |
275 | assign(_Source const& __source) |
276 | { return *this = path(__source); } | |
277 | ||
278 | template<typename _InputIterator> | |
a1e7d33b | 279 | __detail::_Path<_InputIterator, _InputIterator>& |
0ca7ba9a JW |
280 | assign(_InputIterator __first, _InputIterator __last) |
281 | { return *this = path(__first, __last); } | |
282 | ||
283 | // appends | |
284 | ||
285 | path& operator/=(const path& __p) { return _M_append(__p._M_pathname); } | |
286 | ||
24cc0de9 | 287 | template<typename _Source> |
a1e7d33b | 288 | __detail::_Path<_Source>& |
0ca7ba9a JW |
289 | operator/=(_Source const& __source) |
290 | { return append(__source); } | |
291 | ||
292 | template<typename _Source> | |
a1e7d33b | 293 | __detail::_Path<_Source>& |
0ca7ba9a JW |
294 | append(_Source const& __source) |
295 | { | |
a1e7d33b TH |
296 | return _M_append(_S_convert(__detail::_S_range_begin(__source), |
297 | __detail::_S_range_end(__source))); | |
0ca7ba9a JW |
298 | } |
299 | ||
300 | template<typename _InputIterator> | |
a1e7d33b | 301 | __detail::_Path<_InputIterator, _InputIterator>& |
0ca7ba9a JW |
302 | append(_InputIterator __first, _InputIterator __last) |
303 | { return _M_append(_S_convert(__first, __last)); } | |
304 | ||
305 | // concatenation | |
306 | ||
307 | path& operator+=(const path& __x); | |
308 | path& operator+=(const string_type& __x); | |
309 | path& operator+=(const value_type* __x); | |
310 | path& operator+=(value_type __x); | |
f0414b97 JW |
311 | #if __cplusplus >= 201402L |
312 | path& operator+=(basic_string_view<value_type> __x); | |
313 | #endif | |
0ca7ba9a JW |
314 | |
315 | template<typename _Source> | |
a1e7d33b | 316 | __detail::_Path<_Source>& |
0ca7ba9a JW |
317 | operator+=(_Source const& __x) { return concat(__x); } |
318 | ||
319 | template<typename _CharT> | |
a1e7d33b | 320 | __detail::_Path<_CharT*, _CharT*>& |
0ca7ba9a JW |
321 | operator+=(_CharT __x); |
322 | ||
323 | template<typename _Source> | |
a1e7d33b | 324 | __detail::_Path<_Source>& |
0ca7ba9a | 325 | concat(_Source const& __x) |
a1e7d33b TH |
326 | { |
327 | return *this += _S_convert(__detail::_S_range_begin(__x), | |
328 | __detail::_S_range_end(__x)); | |
329 | } | |
0ca7ba9a JW |
330 | |
331 | template<typename _InputIterator> | |
a1e7d33b | 332 | __detail::_Path<_InputIterator, _InputIterator>& |
0ca7ba9a JW |
333 | concat(_InputIterator __first, _InputIterator __last) |
334 | { return *this += _S_convert(__first, __last); } | |
335 | ||
336 | // modifiers | |
337 | ||
338 | void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); } | |
339 | ||
340 | path& make_preferred(); | |
341 | path& remove_filename(); | |
342 | path& replace_filename(const path& __replacement); | |
343 | path& replace_extension(const path& __replacement = path()); | |
344 | ||
345 | void swap(path& __rhs) noexcept; | |
346 | ||
347 | // native format observers | |
348 | ||
349 | const string_type& native() const noexcept { return _M_pathname; } | |
350 | const value_type* c_str() const noexcept { return _M_pathname.c_str(); } | |
351 | operator string_type() const { return _M_pathname; } | |
352 | ||
353 | template<typename _CharT, typename _Traits = std::char_traits<_CharT>, | |
354 | typename _Allocator = std::allocator<_CharT>> | |
355 | std::basic_string<_CharT, _Traits, _Allocator> | |
356 | string(const _Allocator& __a = _Allocator()) const; | |
357 | ||
358 | std::string string() const; | |
7fcdbdd2 | 359 | #if _GLIBCXX_USE_WCHAR_T |
0ca7ba9a | 360 | std::wstring wstring() const; |
7fcdbdd2 | 361 | #endif |
c124af93 TH |
362 | #ifdef _GLIBCXX_USE_CHAR8_T |
363 | __attribute__((__abi_tag__("__u8"))) | |
364 | std::u8string u8string() const; | |
365 | #else | |
0ca7ba9a | 366 | std::string u8string() const; |
c124af93 | 367 | #endif // _GLIBCXX_USE_CHAR8_T |
0ca7ba9a JW |
368 | std::u16string u16string() const; |
369 | std::u32string u32string() const; | |
370 | ||
371 | // generic format observers | |
372 | template<typename _CharT, typename _Traits = std::char_traits<_CharT>, | |
373 | typename _Allocator = std::allocator<_CharT>> | |
374 | std::basic_string<_CharT, _Traits, _Allocator> | |
375 | generic_string(const _Allocator& __a = _Allocator()) const; | |
376 | ||
377 | std::string generic_string() const; | |
7fcdbdd2 | 378 | #if _GLIBCXX_USE_WCHAR_T |
0ca7ba9a | 379 | std::wstring generic_wstring() const; |
7fcdbdd2 | 380 | #endif |
c124af93 TH |
381 | #ifdef _GLIBCXX_USE_CHAR8_T |
382 | __attribute__((__abi_tag__("__u8"))) | |
383 | std::u8string generic_u8string() const; | |
384 | #else | |
0ca7ba9a | 385 | std::string generic_u8string() const; |
c124af93 | 386 | #endif // _GLIBCXX_USE_CHAR8_T |
0ca7ba9a JW |
387 | std::u16string generic_u16string() const; |
388 | std::u32string generic_u32string() const; | |
389 | ||
390 | // compare | |
391 | ||
392 | int compare(const path& __p) const noexcept; | |
393 | int compare(const string_type& __s) const; | |
394 | int compare(const value_type* __s) const; | |
f0414b97 JW |
395 | #if __cplusplus >= 201402L |
396 | int compare(const basic_string_view<value_type> __s) const; | |
397 | #endif | |
0ca7ba9a JW |
398 | |
399 | // decomposition | |
400 | ||
401 | path root_name() const; | |
402 | path root_directory() const; | |
403 | path root_path() const; | |
404 | path relative_path() const; | |
405 | path parent_path() const; | |
406 | path filename() const; | |
407 | path stem() const; | |
408 | path extension() const; | |
409 | ||
410 | // query | |
411 | ||
d715f554 | 412 | _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_pathname.empty(); } |
0ca7ba9a JW |
413 | bool has_root_name() const; |
414 | bool has_root_directory() const; | |
415 | bool has_root_path() const; | |
416 | bool has_relative_path() const; | |
417 | bool has_parent_path() const; | |
418 | bool has_filename() const; | |
419 | bool has_stem() const; | |
420 | bool has_extension() const; | |
9534a5e6 | 421 | bool is_absolute() const; |
0ca7ba9a JW |
422 | bool is_relative() const { return !is_absolute(); } |
423 | ||
424 | // iterators | |
425 | class iterator; | |
426 | typedef iterator const_iterator; | |
427 | ||
428 | iterator begin() const; | |
429 | iterator end() const; | |
430 | ||
caace300 | 431 | /// @cond undocumented |
49d729ea JW |
432 | // Create a basic_string by reading until a null character. |
433 | template<typename _InputIterator, | |
434 | typename _Traits = std::iterator_traits<_InputIterator>, | |
435 | typename _CharT | |
436 | = typename std::remove_cv<typename _Traits::value_type>::type> | |
437 | static std::basic_string<_CharT> | |
438 | _S_string_from_iter(_InputIterator __source) | |
439 | { | |
440 | std::basic_string<_CharT> __str; | |
441 | for (_CharT __ch = *__source; __ch != _CharT(); __ch = *++__source) | |
442 | __str.push_back(__ch); | |
443 | return __str; | |
444 | } | |
caace300 | 445 | /// @endcond |
49d729ea | 446 | |
0ca7ba9a JW |
447 | private: |
448 | enum class _Type : unsigned char { | |
449 | _Multi, _Root_name, _Root_dir, _Filename | |
450 | }; | |
451 | ||
452 | path(string_type __str, _Type __type) : _M_pathname(__str), _M_type(__type) | |
453 | { | |
2f1e8e7c JW |
454 | __glibcxx_assert(!empty()); |
455 | __glibcxx_assert(_M_type != _Type::_Multi); | |
0ca7ba9a JW |
456 | } |
457 | ||
458 | enum class _Split { _Stem, _Extension }; | |
459 | ||
460 | path& _M_append(const string_type& __str) | |
461 | { | |
462 | if (!_M_pathname.empty() && !_S_is_dir_sep(_M_pathname.back()) | |
463 | && !__str.empty() && !_S_is_dir_sep(__str.front())) | |
464 | _M_pathname += preferred_separator; | |
465 | _M_pathname += __str; | |
466 | _M_split_cmpts(); | |
467 | return *this; | |
468 | } | |
469 | ||
470 | pair<const string_type*, size_t> _M_find_extension() const; | |
471 | ||
472 | template<typename _CharT> | |
473 | struct _Cvt; | |
474 | ||
475 | static string_type | |
a1e7d33b | 476 | _S_convert(value_type* __src, __detail::__null_terminated) |
0ca7ba9a JW |
477 | { return string_type(__src); } |
478 | ||
479 | static string_type | |
a1e7d33b | 480 | _S_convert(const value_type* __src, __detail::__null_terminated) |
0ca7ba9a JW |
481 | { return string_type(__src); } |
482 | ||
483 | template<typename _Iter> | |
484 | static string_type | |
485 | _S_convert(_Iter __first, _Iter __last) | |
486 | { | |
487 | using __value_type = typename std::iterator_traits<_Iter>::value_type; | |
435f4342 JW |
488 | return _Cvt<typename remove_cv<__value_type>::type>:: |
489 | _S_convert(__first, __last); | |
0ca7ba9a JW |
490 | } |
491 | ||
492 | template<typename _InputIterator> | |
493 | static string_type | |
a1e7d33b | 494 | _S_convert(_InputIterator __src, __detail::__null_terminated) |
0ca7ba9a | 495 | { |
49d729ea JW |
496 | auto __s = _S_string_from_iter(__src); |
497 | return _S_convert(__s.c_str(), __s.c_str() + __s.size()); | |
0ca7ba9a JW |
498 | } |
499 | ||
500 | static string_type | |
501 | _S_convert_loc(const char* __first, const char* __last, | |
502 | const std::locale& __loc); | |
503 | ||
584d52b0 JW |
504 | static string_type |
505 | _S_convert_loc(char* __first, char* __last, const std::locale& __loc) | |
506 | { | |
507 | return _S_convert_loc(const_cast<const char*>(__first), | |
508 | const_cast<const char*>(__last), __loc); | |
509 | } | |
510 | ||
0ca7ba9a JW |
511 | template<typename _Iter> |
512 | static string_type | |
513 | _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc) | |
514 | { | |
515 | const std::string __str(__first, __last); | |
516 | return _S_convert_loc(__str.data(), __str.data()+__str.size(), __loc); | |
517 | } | |
518 | ||
519 | template<typename _InputIterator> | |
520 | static string_type | |
a1e7d33b | 521 | _S_convert_loc(_InputIterator __src, __detail::__null_terminated, |
0ca7ba9a JW |
522 | const std::locale& __loc) |
523 | { | |
700e6332 | 524 | const std::string __s = _S_string_from_iter(__src); |
49d729ea | 525 | return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc); |
0ca7ba9a JW |
526 | } |
527 | ||
441ed45c | 528 | static bool _S_is_dir_sep(value_type __ch) |
0ca7ba9a JW |
529 | { |
530 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
531 | return __ch == L'/' || __ch == preferred_separator; | |
532 | #else | |
533 | return __ch == '/'; | |
534 | #endif | |
535 | } | |
536 | ||
537 | void _M_split_cmpts(); | |
538 | void _M_trim(); | |
d6e023f3 JW |
539 | void _M_add_root_name(size_t __n); |
540 | void _M_add_root_dir(size_t __pos); | |
541 | void _M_add_filename(size_t __pos, size_t __n); | |
0ca7ba9a JW |
542 | |
543 | string_type _M_pathname; | |
544 | ||
545 | struct _Cmpt; | |
a00d74c4 | 546 | using _List = _GLIBCXX_STD_C::vector<_Cmpt>; |
0ca7ba9a JW |
547 | _List _M_cmpts; // empty unless _M_type == _Type::_Multi |
548 | _Type _M_type = _Type::_Multi; | |
549 | }; | |
550 | ||
caace300 JW |
551 | /// @relates std::experimental::filesystem::path @{ |
552 | ||
041aa6ab | 553 | /// Swap overload for paths |
0ca7ba9a JW |
554 | inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); } |
555 | ||
041aa6ab | 556 | /// Compute a hash value for a path |
0ca7ba9a JW |
557 | size_t hash_value(const path& __p) noexcept; |
558 | ||
559 | /// Compare paths | |
1e690757 | 560 | inline bool operator<(const path& __lhs, const path& __rhs) noexcept; |
0ca7ba9a JW |
561 | |
562 | /// Compare paths | |
563 | inline bool operator<=(const path& __lhs, const path& __rhs) noexcept | |
564 | { return !(__rhs < __lhs); } | |
565 | ||
566 | /// Compare paths | |
567 | inline bool operator>(const path& __lhs, const path& __rhs) noexcept | |
568 | { return __rhs < __lhs; } | |
569 | ||
570 | /// Compare paths | |
571 | inline bool operator>=(const path& __lhs, const path& __rhs) noexcept | |
572 | { return !(__lhs < __rhs); } | |
573 | ||
574 | /// Compare paths | |
1e690757 | 575 | inline bool operator==(const path& __lhs, const path& __rhs) noexcept; |
0ca7ba9a JW |
576 | |
577 | /// Compare paths | |
578 | inline bool operator!=(const path& __lhs, const path& __rhs) noexcept | |
579 | { return !(__lhs == __rhs); } | |
580 | ||
581 | /// Append one path to another | |
582 | inline path operator/(const path& __lhs, const path& __rhs) | |
a989f637 JW |
583 | { |
584 | path __result(__lhs); | |
585 | __result /= __rhs; | |
586 | return __result; | |
587 | } | |
0ca7ba9a JW |
588 | |
589 | /// Write a path to a stream | |
590 | template<typename _CharT, typename _Traits> | |
591 | basic_ostream<_CharT, _Traits>& | |
592 | operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) | |
593 | { | |
594 | auto __tmp = __p.string<_CharT, _Traits>(); | |
595 | using __quoted_string | |
596 | = std::__detail::_Quoted_string<decltype(__tmp)&, _CharT>; | |
9534a5e6 | 597 | __os << __quoted_string{__tmp, _CharT('"'), _CharT('\\')}; |
0ca7ba9a JW |
598 | return __os; |
599 | } | |
600 | ||
601 | /// Read a path from a stream | |
602 | template<typename _CharT, typename _Traits> | |
603 | basic_istream<_CharT, _Traits>& | |
604 | operator>>(basic_istream<_CharT, _Traits>& __is, path& __p) | |
605 | { | |
606 | basic_string<_CharT, _Traits> __tmp; | |
607 | using __quoted_string | |
608 | = std::__detail::_Quoted_string<decltype(__tmp)&, _CharT>; | |
9534a5e6 | 609 | if (__is >> __quoted_string{ __tmp, _CharT('"'), _CharT('\\') }) |
0ca7ba9a JW |
610 | __p = std::move(__tmp); |
611 | return __is; | |
612 | } | |
613 | ||
caace300 | 614 | /// Create a path from a UTF-8-encoded sequence of char |
2b4e2c93 TH |
615 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS |
616 | template<typename _InputIterator> | |
0ca7ba9a | 617 | inline path |
2b4e2c93 | 618 | __u8path(_InputIterator __first, _InputIterator __last, char) |
0ca7ba9a | 619 | { |
26b1320e JW |
620 | // XXX This assumes native wide encoding is UTF-16. |
621 | std::codecvt_utf8_utf16<path::value_type> __cvt; | |
622 | path::string_type __tmp; | |
623 | const std::string __u8str{__first, __last}; | |
624 | const char* const __ptr = __u8str.data(); | |
625 | if (__str_codecvt_in_all(__ptr, __ptr + __u8str.size(), __tmp, __cvt)) | |
626 | return path{ __tmp }; | |
627 | _GLIBCXX_THROW_OR_ABORT(filesystem_error( | |
628 | "Cannot convert character sequence", | |
629 | std::make_error_code(errc::illegal_byte_sequence))); | |
2b4e2c93 TH |
630 | } |
631 | ||
632 | #ifdef _GLIBCXX_USE_CHAR8_T | |
633 | template<typename _InputIterator> | |
634 | inline path | |
635 | __u8path(_InputIterator __first, _InputIterator __last, char8_t) | |
636 | { | |
637 | return path{ __first, __last }; | |
638 | } | |
639 | #endif // _GLIBCXX_USE_CHAR8_T | |
640 | #endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
641 | ||
642 | template<typename _InputIterator, | |
643 | typename _Require = __detail::_Path<_InputIterator, _InputIterator>, | |
644 | typename _CharT = | |
645 | __detail::__value_type_is_char_or_char8_t<_InputIterator>> | |
646 | inline path | |
647 | u8path(_InputIterator __first, _InputIterator __last) | |
648 | { | |
649 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
650 | return __u8path(__first, __last, _CharT{}); | |
0ca7ba9a | 651 | #else |
26b1320e | 652 | return path{ __first, __last }; |
0ca7ba9a JW |
653 | #endif |
654 | } | |
655 | ||
caace300 | 656 | /// Create a path from a UTF-8-encoded sequence of char |
2b4e2c93 | 657 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS |
3eda32cb JW |
658 | inline path |
659 | __u8path(const string& __s, char) | |
660 | { | |
661 | return filesystem::u8path(__s.data(), __s.data() + __s.size()); | |
662 | } | |
663 | ||
2b4e2c93 | 664 | template<typename _Source> |
3eda32cb JW |
665 | inline __enable_if_t<is_convertible<const _Source&, string>::value, path> |
666 | __u8path(const _Source& __source, char) | |
667 | { | |
668 | std::string __s = __source; | |
669 | return filesystem::u8path(__s.data(), __s.data() + __s.size()); | |
670 | } | |
671 | ||
672 | template<typename _Source> | |
673 | inline __enable_if_t<!is_convertible<const _Source&, string>::value, path> | |
2b4e2c93 TH |
674 | __u8path(const _Source& __source, char) |
675 | { | |
676 | std::string __s = path::_S_string_from_iter(__source); | |
677 | return filesystem::u8path(__s.data(), __s.data() + __s.size()); | |
678 | } | |
679 | ||
680 | #ifdef _GLIBCXX_USE_CHAR8_T | |
681 | template<typename _Source> | |
682 | inline path | |
683 | __u8path(const _Source& __source, char8_t) | |
684 | { | |
685 | return path{ __source }; | |
686 | } | |
687 | #endif // _GLIBCXX_USE_CHAR8_T | |
688 | #endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
689 | ||
a1e7d33b TH |
690 | template<typename _Source, |
691 | typename _Require = __detail::_Path<_Source>, | |
2b4e2c93 TH |
692 | typename _CharT = |
693 | __detail::__value_type_is_char_or_char8_t<_Source>> | |
0ca7ba9a | 694 | inline path |
26b1320e | 695 | u8path(const _Source& __source) |
0ca7ba9a JW |
696 | { |
697 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
2b4e2c93 | 698 | return __u8path(__source, _CharT{}); |
0ca7ba9a | 699 | #else |
26b1320e | 700 | return path{ __source }; |
0ca7ba9a JW |
701 | #endif |
702 | } | |
703 | ||
caace300 JW |
704 | /// @} |
705 | ||
706 | /// Exception type thrown by the Filesystem TS library | |
0ca7ba9a JW |
707 | class filesystem_error : public std::system_error |
708 | { | |
709 | public: | |
710 | filesystem_error(const string& __what_arg, error_code __ec) | |
711 | : system_error(__ec, __what_arg) { } | |
712 | ||
713 | filesystem_error(const string& __what_arg, const path& __p1, | |
714 | error_code __ec) | |
715 | : system_error(__ec, __what_arg), _M_path1(__p1) { } | |
716 | ||
717 | filesystem_error(const string& __what_arg, const path& __p1, | |
718 | const path& __p2, error_code __ec) | |
719 | : system_error(__ec, __what_arg), _M_path1(__p1), _M_path2(__p2) | |
720 | { } | |
721 | ||
5b244a78 JW |
722 | ~filesystem_error(); |
723 | ||
0ca7ba9a JW |
724 | const path& path1() const noexcept { return _M_path1; } |
725 | const path& path2() const noexcept { return _M_path2; } | |
726 | const char* what() const noexcept { return _M_what.c_str(); } | |
727 | ||
728 | private: | |
729 | std::string _M_gen_what(); | |
730 | ||
731 | path _M_path1; | |
732 | path _M_path2; | |
733 | std::string _M_what = _M_gen_what(); | |
734 | }; | |
735 | ||
caace300 | 736 | /// @cond undocumented |
22f5095f JW |
737 | struct path::_Cmpt : path |
738 | { | |
739 | _Cmpt(string_type __s, _Type __t, size_t __pos) | |
740 | : path(std::move(__s), __t), _M_pos(__pos) { } | |
741 | ||
742 | _Cmpt() : _M_pos(-1) { } | |
743 | ||
744 | size_t _M_pos; | |
745 | }; | |
746 | ||
0ca7ba9a JW |
747 | // specialize _Cvt for degenerate 'noconv' case |
748 | template<> | |
749 | struct path::_Cvt<path::value_type> | |
750 | { | |
751 | template<typename _Iter> | |
752 | static string_type | |
753 | _S_convert(_Iter __first, _Iter __last) | |
754 | { return string_type{__first, __last}; } | |
755 | }; | |
756 | ||
757 | template<typename _CharT> | |
758 | struct path::_Cvt | |
759 | { | |
760 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
3eda32cb | 761 | #ifdef _GLIBCXX_USE_CHAR8_T |
0ca7ba9a | 762 | static string_type |
3eda32cb JW |
763 | _S_wconvert(const char8_t* __f, const char8_t* __l, const char8_t*) |
764 | { | |
765 | const char* __f2 = (const char*)__f; | |
766 | const char* __l2 = (const char*)__l; | |
767 | std::wstring __wstr; | |
768 | std::codecvt_utf8_utf16<wchar_t> __wcvt; | |
769 | if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt)) | |
770 | return __wstr; | |
771 | } | |
772 | #endif | |
773 | ||
774 | static string_type | |
775 | _S_wconvert(const char* __f, const char* __l, const char*) | |
0ca7ba9a JW |
776 | { |
777 | using _Cvt = std::codecvt<wchar_t, char, mbstate_t>; | |
778 | const auto& __cvt = std::use_facet<_Cvt>(std::locale{}); | |
779 | std::wstring __wstr; | |
26b1320e | 780 | if (__str_codecvt_in_all(__f, __l, __wstr, __cvt)) |
0ca7ba9a JW |
781 | return __wstr; |
782 | _GLIBCXX_THROW_OR_ABORT(filesystem_error( | |
783 | "Cannot convert character sequence", | |
784 | std::make_error_code(errc::illegal_byte_sequence))); | |
785 | } | |
786 | ||
787 | static string_type | |
3eda32cb | 788 | _S_wconvert(const _CharT* __f, const _CharT* __l, const void*) |
0ca7ba9a | 789 | { |
3eda32cb JW |
790 | struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> |
791 | { } __cvt; | |
792 | std::string __str; | |
793 | if (__str_codecvt_out_all(__f, __l, __str, __cvt)) | |
0ca7ba9a | 794 | { |
3eda32cb JW |
795 | const char* __f2 = __str.data(); |
796 | const char* __l2 = __f2 + __str.size(); | |
797 | std::codecvt_utf8_utf16<wchar_t> __wcvt; | |
798 | std::wstring __wstr; | |
799 | if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt)) | |
800 | return __wstr; | |
0ca7ba9a | 801 | } |
3eda32cb JW |
802 | _GLIBCXX_THROW_OR_ABORT(filesystem_error( |
803 | "Cannot convert character sequence", | |
804 | std::make_error_code(errc::illegal_byte_sequence))); | |
0ca7ba9a JW |
805 | } |
806 | ||
807 | static string_type | |
808 | _S_convert(const _CharT* __f, const _CharT* __l) | |
809 | { | |
3eda32cb | 810 | return _S_wconvert(__f, __l, (const _CharT*)nullptr); |
0ca7ba9a JW |
811 | } |
812 | #else | |
813 | static string_type | |
814 | _S_convert(const _CharT* __f, const _CharT* __l) | |
815 | { | |
c124af93 TH |
816 | #ifdef _GLIBCXX_USE_CHAR8_T |
817 | if constexpr (is_same<_CharT, char8_t>::value) | |
26b1320e | 818 | return string_type(__f, __l); |
c124af93 | 819 | else |
c124af93 | 820 | #endif |
3eda32cb | 821 | { |
26b1320e JW |
822 | struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> |
823 | { } __cvt; | |
c124af93 | 824 | std::string __str; |
26b1320e | 825 | if (__str_codecvt_out_all(__f, __l, __str, __cvt)) |
c124af93 | 826 | return __str; |
3eda32cb JW |
827 | _GLIBCXX_THROW_OR_ABORT(filesystem_error( |
828 | "Cannot convert character sequence", | |
829 | std::make_error_code(errc::illegal_byte_sequence))); | |
c124af93 | 830 | } |
0ca7ba9a JW |
831 | } |
832 | #endif | |
833 | ||
834 | static string_type | |
835 | _S_convert(_CharT* __f, _CharT* __l) | |
836 | { | |
837 | return _S_convert(const_cast<const _CharT*>(__f), | |
838 | const_cast<const _CharT*>(__l)); | |
839 | } | |
840 | ||
841 | template<typename _Iter> | |
842 | static string_type | |
843 | _S_convert(_Iter __first, _Iter __last) | |
844 | { | |
845 | const std::basic_string<_CharT> __str(__first, __last); | |
846 | return _S_convert(__str.data(), __str.data() + __str.size()); | |
847 | } | |
848 | ||
849 | template<typename _Iter, typename _Cont> | |
850 | static string_type | |
851 | _S_convert(__gnu_cxx::__normal_iterator<_Iter, _Cont> __first, | |
852 | __gnu_cxx::__normal_iterator<_Iter, _Cont> __last) | |
853 | { return _S_convert(__first.base(), __last.base()); } | |
854 | }; | |
caace300 | 855 | /// @endcond |
0ca7ba9a JW |
856 | |
857 | /// An iterator for the components of a path | |
858 | class path::iterator | |
859 | { | |
860 | public: | |
861 | using difference_type = std::ptrdiff_t; | |
862 | using value_type = path; | |
863 | using reference = const path&; | |
864 | using pointer = const path*; | |
865 | using iterator_category = std::bidirectional_iterator_tag; | |
866 | ||
42eff613 | 867 | iterator() noexcept : _M_path(nullptr), _M_cur(), _M_at_end() { } |
0ca7ba9a JW |
868 | |
869 | iterator(const iterator&) = default; | |
870 | iterator& operator=(const iterator&) = default; | |
871 | ||
42eff613 JW |
872 | reference operator*() const noexcept; |
873 | pointer operator->() const noexcept { return std::__addressof(**this); } | |
0ca7ba9a | 874 | |
42eff613 | 875 | iterator& operator++() noexcept; |
0ca7ba9a | 876 | |
42eff613 JW |
877 | iterator operator++(int) noexcept |
878 | { auto __tmp = *this; ++*this; return __tmp; } | |
0ca7ba9a | 879 | |
42eff613 JW |
880 | iterator& operator--() noexcept; |
881 | ||
882 | iterator operator--(int) noexcept | |
883 | { auto __tmp = *this; --*this; return __tmp; } | |
884 | ||
885 | friend bool | |
886 | operator==(const iterator& __lhs, const iterator& __rhs) noexcept | |
d6e023f3 | 887 | { return __lhs._M_equals(__rhs); } |
0ca7ba9a | 888 | |
42eff613 JW |
889 | friend bool |
890 | operator!=(const iterator& __lhs, const iterator& __rhs) noexcept | |
d6e023f3 | 891 | { return !__lhs._M_equals(__rhs); } |
0ca7ba9a JW |
892 | |
893 | private: | |
894 | friend class path; | |
895 | ||
42eff613 | 896 | iterator(const path* __path, path::_List::const_iterator __iter) noexcept |
0ca7ba9a JW |
897 | : _M_path(__path), _M_cur(__iter), _M_at_end() |
898 | { } | |
899 | ||
42eff613 | 900 | iterator(const path* __path, bool __at_end) noexcept |
0ca7ba9a JW |
901 | : _M_path(__path), _M_cur(), _M_at_end(__at_end) |
902 | { } | |
903 | ||
42eff613 | 904 | bool _M_equals(iterator) const noexcept; |
0ca7ba9a JW |
905 | |
906 | const path* _M_path; | |
907 | path::_List::const_iterator _M_cur; | |
908 | bool _M_at_end; // only used when type != _Multi | |
909 | }; | |
910 | ||
911 | ||
912 | inline path& | |
913 | path::operator=(path&& __p) noexcept | |
914 | { | |
915 | _M_pathname = std::move(__p._M_pathname); | |
916 | _M_cmpts = std::move(__p._M_cmpts); | |
917 | _M_type = __p._M_type; | |
918 | __p.clear(); | |
919 | return *this; | |
920 | } | |
921 | ||
e59e183f JW |
922 | inline path& |
923 | path::operator=(string_type&& __source) | |
924 | { return *this = path(std::move(__source)); } | |
925 | ||
926 | inline path& | |
927 | path::assign(string_type&& __source) | |
928 | { return *this = path(std::move(__source)); } | |
929 | ||
0ca7ba9a JW |
930 | inline path& |
931 | path::operator+=(const path& __p) | |
932 | { | |
933 | return operator+=(__p.native()); | |
934 | } | |
935 | ||
936 | inline path& | |
937 | path::operator+=(const string_type& __x) | |
938 | { | |
939 | _M_pathname += __x; | |
940 | _M_split_cmpts(); | |
941 | return *this; | |
942 | } | |
943 | ||
944 | inline path& | |
945 | path::operator+=(const value_type* __x) | |
946 | { | |
947 | _M_pathname += __x; | |
948 | _M_split_cmpts(); | |
949 | return *this; | |
950 | } | |
951 | ||
952 | inline path& | |
953 | path::operator+=(value_type __x) | |
954 | { | |
955 | _M_pathname += __x; | |
956 | _M_split_cmpts(); | |
957 | return *this; | |
958 | } | |
959 | ||
f0414b97 JW |
960 | #if __cplusplus >= 201402L |
961 | inline path& | |
962 | path::operator+=(basic_string_view<value_type> __x) | |
963 | { | |
964 | _M_pathname.append(__x.data(), __x.size()); | |
965 | _M_split_cmpts(); | |
966 | return *this; | |
967 | } | |
968 | #endif | |
969 | ||
0ca7ba9a | 970 | template<typename _CharT> |
a1e7d33b | 971 | inline __detail::_Path<_CharT*, _CharT*>& |
0ca7ba9a JW |
972 | path::operator+=(_CharT __x) |
973 | { | |
d6e023f3 | 974 | auto* __addr = std::__addressof(__x); |
0ca7ba9a JW |
975 | return concat(__addr, __addr + 1); |
976 | } | |
977 | ||
978 | inline path& | |
979 | path::make_preferred() | |
980 | { | |
981 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
982 | std::replace(_M_pathname.begin(), _M_pathname.end(), L'/', | |
983 | preferred_separator); | |
984 | #endif | |
985 | return *this; | |
986 | } | |
987 | ||
988 | inline void path::swap(path& __rhs) noexcept | |
989 | { | |
990 | _M_pathname.swap(__rhs._M_pathname); | |
991 | _M_cmpts.swap(__rhs._M_cmpts); | |
992 | std::swap(_M_type, __rhs._M_type); | |
993 | } | |
994 | ||
995 | template<typename _CharT, typename _Traits, typename _Allocator> | |
996 | inline std::basic_string<_CharT, _Traits, _Allocator> | |
997 | path::string(const _Allocator& __a) const | |
998 | { | |
999 | if (is_same<_CharT, value_type>::value) | |
1000 | return { _M_pathname.begin(), _M_pathname.end(), __a }; | |
1001 | ||
26b1320e JW |
1002 | using _WString = basic_string<_CharT, _Traits, _Allocator>; |
1003 | ||
0ca7ba9a JW |
1004 | const value_type* __first = _M_pathname.data(); |
1005 | const value_type* __last = __first + _M_pathname.size(); | |
1006 | ||
1007 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
bf53e6a9 | 1008 | using _CharAlloc = __alloc_rebind<_Allocator, char>; |
0ca7ba9a | 1009 | using _String = basic_string<char, char_traits<char>, _CharAlloc>; |
0ca7ba9a | 1010 | |
26b1320e JW |
1011 | // First convert native string from UTF-16 to to UTF-8. |
1012 | // XXX This assumes that the execution wide-character set is UTF-16. | |
1013 | codecvt_utf8_utf16<value_type> __cvt; | |
0ca7ba9a | 1014 | _String __u8str{_CharAlloc{__a}}; |
26b1320e | 1015 | if (__str_codecvt_out_all(__first, __last, __u8str, __cvt)) |
0ca7ba9a JW |
1016 | { |
1017 | struct | |
1018 | { | |
1019 | const _String* | |
1020 | operator()(const _String& __from, _String&, true_type) | |
1021 | { return std::__addressof(__from); } | |
1022 | ||
1023 | _WString* | |
1024 | operator()(const _String& __from, _WString& __to, false_type) | |
1025 | { | |
c124af93 TH |
1026 | #ifdef _GLIBCXX_USE_CHAR8_T |
1027 | if constexpr (is_same<_CharT, char8_t>::value) | |
1028 | { | |
1029 | __to.assign(__from.begin(), __from.end()); | |
1030 | return std::__addressof(__to); | |
1031 | } | |
1032 | else | |
c124af93 | 1033 | #endif |
26b1320e JW |
1034 | { |
1035 | // Convert UTF-8 to wide string. | |
1036 | struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> | |
1037 | { } __cvt; | |
c124af93 TH |
1038 | const char* __f = __from.data(); |
1039 | const char* __l = __f + __from.size(); | |
26b1320e | 1040 | if (__str_codecvt_in_all(__f, __l, __to, __cvt)) |
c124af93 | 1041 | return std::__addressof(__to); |
c124af93 | 1042 | } |
0ca7ba9a JW |
1043 | return nullptr; |
1044 | } | |
1045 | } __dispatch; | |
26b1320e | 1046 | _WString __wstr(__a); |
0ca7ba9a JW |
1047 | if (auto* __p = __dispatch(__u8str, __wstr, is_same<_CharT, char>{})) |
1048 | return *__p; | |
1049 | } | |
1050 | #else | |
c124af93 TH |
1051 | #ifdef _GLIBCXX_USE_CHAR8_T |
1052 | if constexpr (is_same<_CharT, char8_t>::value) | |
26b1320e | 1053 | return _WString(__first, __last, __a); |
c124af93 | 1054 | else |
c124af93 | 1055 | #endif |
26b1320e JW |
1056 | { |
1057 | struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> { } __cvt; | |
1058 | _WString __wstr(__a); | |
1059 | if (__str_codecvt_in_all(__first, __last, __wstr, __cvt)) | |
c124af93 | 1060 | return __wstr; |
c124af93 | 1061 | } |
0ca7ba9a JW |
1062 | #endif |
1063 | _GLIBCXX_THROW_OR_ABORT(filesystem_error( | |
1064 | "Cannot convert character sequence", | |
1065 | std::make_error_code(errc::illegal_byte_sequence))); | |
1066 | } | |
1067 | ||
1068 | inline std::string | |
1069 | path::string() const { return string<char>(); } | |
1070 | ||
7fcdbdd2 | 1071 | #if _GLIBCXX_USE_WCHAR_T |
0ca7ba9a JW |
1072 | inline std::wstring |
1073 | path::wstring() const { return string<wchar_t>(); } | |
7fcdbdd2 | 1074 | #endif |
0ca7ba9a | 1075 | |
c124af93 TH |
1076 | #ifdef _GLIBCXX_USE_CHAR8_T |
1077 | inline std::u8string | |
1078 | path::u8string() const { return string<char8_t>(); } | |
1079 | #else | |
0ca7ba9a JW |
1080 | inline std::string |
1081 | path::u8string() const | |
1082 | { | |
1083 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
1084 | std::string __str; | |
26b1320e JW |
1085 | // convert from native wide encoding (assumed to be UTF-16) to UTF-8 |
1086 | std::codecvt_utf8_utf16<value_type> __cvt; | |
0ca7ba9a JW |
1087 | const value_type* __first = _M_pathname.data(); |
1088 | const value_type* __last = __first + _M_pathname.size(); | |
26b1320e | 1089 | if (__str_codecvt_out_all(__first, __last, __str, __cvt)) |
0ca7ba9a JW |
1090 | return __str; |
1091 | _GLIBCXX_THROW_OR_ABORT(filesystem_error( | |
1092 | "Cannot convert character sequence", | |
1093 | std::make_error_code(errc::illegal_byte_sequence))); | |
1094 | #else | |
1095 | return _M_pathname; | |
1096 | #endif | |
1097 | } | |
c124af93 | 1098 | #endif // _GLIBCXX_USE_CHAR8_T |
0ca7ba9a JW |
1099 | |
1100 | inline std::u16string | |
1101 | path::u16string() const { return string<char16_t>(); } | |
1102 | ||
1103 | inline std::u32string | |
1104 | path::u32string() const { return string<char32_t>(); } | |
1105 | ||
0ca7ba9a JW |
1106 | template<typename _CharT, typename _Traits, typename _Allocator> |
1107 | inline std::basic_string<_CharT, _Traits, _Allocator> | |
1108 | path::generic_string(const _Allocator& __a) const | |
a577c0c2 JW |
1109 | { |
1110 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
1111 | const _CharT __slash = is_same<_CharT, wchar_t>::value | |
1112 | ? _CharT(L'/') | |
1113 | : _CharT('/'); // Assume value is correct for the encoding. | |
1114 | #else | |
1115 | const _CharT __slash = _CharT('/'); | |
1116 | #endif | |
1117 | basic_string<_CharT, _Traits, _Allocator> __str(__a); | |
1118 | __str.reserve(_M_pathname.size()); | |
1119 | bool __add_slash = false; | |
1120 | for (auto& __elem : *this) | |
1121 | { | |
1122 | if (__elem._M_type == _Type::_Root_dir) | |
1123 | { | |
1124 | __str += __slash; | |
1125 | continue; | |
1126 | } | |
1127 | if (__add_slash) | |
1128 | __str += __slash; | |
1129 | __str += __elem.string<_CharT, _Traits, _Allocator>(__a); | |
1130 | __add_slash = __elem._M_type == _Type::_Filename; | |
1131 | } | |
1132 | return __str; | |
1133 | } | |
0ca7ba9a JW |
1134 | |
1135 | inline std::string | |
a577c0c2 | 1136 | path::generic_string() const { return generic_string<char>(); } |
0ca7ba9a | 1137 | |
7fcdbdd2 | 1138 | #if _GLIBCXX_USE_WCHAR_T |
0ca7ba9a | 1139 | inline std::wstring |
a577c0c2 | 1140 | path::generic_wstring() const { return generic_string<wchar_t>(); } |
7fcdbdd2 | 1141 | #endif |
0ca7ba9a | 1142 | |
c124af93 TH |
1143 | #ifdef _GLIBCXX_USE_CHAR8_T |
1144 | inline std::u8string | |
a577c0c2 | 1145 | path::generic_u8string() const { return generic_string<char8_t>(); } |
c124af93 | 1146 | #else |
0ca7ba9a | 1147 | inline std::string |
a577c0c2 | 1148 | path::generic_u8string() const { return generic_string<char>(); } |
c124af93 | 1149 | #endif |
0ca7ba9a JW |
1150 | |
1151 | inline std::u16string | |
a577c0c2 | 1152 | path::generic_u16string() const { return generic_string<char16_t>(); } |
0ca7ba9a JW |
1153 | |
1154 | inline std::u32string | |
a577c0c2 | 1155 | path::generic_u32string() const { return generic_string<char32_t>(); } |
0ca7ba9a JW |
1156 | |
1157 | inline int | |
1158 | path::compare(const string_type& __s) const { return compare(path(__s)); } | |
1159 | ||
1160 | inline int | |
1161 | path::compare(const value_type* __s) const { return compare(path(__s)); } | |
1162 | ||
f0414b97 JW |
1163 | #if __cplusplus >= 201402L |
1164 | inline int | |
1165 | path::compare(basic_string_view<value_type> __s) const | |
1166 | { return compare(path(__s)); } | |
1167 | #endif | |
1168 | ||
0ca7ba9a JW |
1169 | inline path |
1170 | path::filename() const { return empty() ? path() : *--end(); } | |
1171 | ||
1172 | inline path | |
1173 | path::stem() const | |
1174 | { | |
1175 | auto ext = _M_find_extension(); | |
1176 | if (ext.first && ext.second != 0) | |
1177 | return path{ext.first->substr(0, ext.second)}; | |
1178 | return {}; | |
1179 | } | |
1180 | ||
1181 | inline path | |
1182 | path::extension() const | |
1183 | { | |
1184 | auto ext = _M_find_extension(); | |
1185 | if (ext.first && ext.second != string_type::npos) | |
1186 | return path{ext.first->substr(ext.second)}; | |
1187 | return {}; | |
1188 | } | |
1189 | ||
1190 | inline bool | |
1191 | path::has_stem() const | |
1192 | { | |
1193 | auto ext = _M_find_extension(); | |
1194 | return ext.first && ext.second != 0; | |
1195 | } | |
1196 | ||
1197 | inline bool | |
1198 | path::has_extension() const | |
1199 | { | |
1200 | auto ext = _M_find_extension(); | |
1201 | return ext.first && ext.second != string_type::npos; | |
1202 | } | |
1203 | ||
9534a5e6 JW |
1204 | inline bool |
1205 | path::is_absolute() const | |
1206 | { | |
1207 | #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS | |
1208 | return has_root_name() && has_root_directory(); | |
1209 | #else | |
1210 | return has_root_directory(); | |
1211 | #endif | |
1212 | } | |
1213 | ||
0ca7ba9a | 1214 | inline path::iterator |
42eff613 | 1215 | path::begin() const noexcept |
0ca7ba9a JW |
1216 | { |
1217 | if (_M_type == _Type::_Multi) | |
1218 | return iterator(this, _M_cmpts.begin()); | |
1219 | return iterator(this, false); | |
1220 | } | |
1221 | ||
1222 | inline path::iterator | |
42eff613 | 1223 | path::end() const noexcept |
0ca7ba9a JW |
1224 | { |
1225 | if (_M_type == _Type::_Multi) | |
1226 | return iterator(this, _M_cmpts.end()); | |
1227 | return iterator(this, true); | |
1228 | } | |
1229 | ||
1230 | inline path::iterator& | |
42eff613 | 1231 | path::iterator::operator++() noexcept |
0ca7ba9a | 1232 | { |
2f1e8e7c | 1233 | __glibcxx_assert(_M_path != nullptr); |
0ca7ba9a JW |
1234 | if (_M_path->_M_type == _Type::_Multi) |
1235 | { | |
2f1e8e7c | 1236 | __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end()); |
0ca7ba9a JW |
1237 | ++_M_cur; |
1238 | } | |
1239 | else | |
1240 | { | |
2f1e8e7c | 1241 | __glibcxx_assert(!_M_at_end); |
0ca7ba9a JW |
1242 | _M_at_end = true; |
1243 | } | |
1244 | return *this; | |
1245 | } | |
1246 | ||
1247 | inline path::iterator& | |
42eff613 | 1248 | path::iterator::operator--() noexcept |
0ca7ba9a | 1249 | { |
2f1e8e7c | 1250 | __glibcxx_assert(_M_path != nullptr); |
0ca7ba9a JW |
1251 | if (_M_path->_M_type == _Type::_Multi) |
1252 | { | |
2f1e8e7c | 1253 | __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin()); |
0ca7ba9a JW |
1254 | --_M_cur; |
1255 | } | |
1256 | else | |
1257 | { | |
2f1e8e7c | 1258 | __glibcxx_assert(_M_at_end); |
0ca7ba9a JW |
1259 | _M_at_end = false; |
1260 | } | |
1261 | return *this; | |
1262 | } | |
1263 | ||
1264 | inline path::iterator::reference | |
42eff613 | 1265 | path::iterator::operator*() const noexcept |
0ca7ba9a | 1266 | { |
2f1e8e7c | 1267 | __glibcxx_assert(_M_path != nullptr); |
0ca7ba9a JW |
1268 | if (_M_path->_M_type == _Type::_Multi) |
1269 | { | |
2f1e8e7c | 1270 | __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end()); |
0ca7ba9a JW |
1271 | return *_M_cur; |
1272 | } | |
1273 | return *_M_path; | |
1274 | } | |
1275 | ||
1276 | inline bool | |
42eff613 | 1277 | path::iterator::_M_equals(iterator __rhs) const noexcept |
0ca7ba9a JW |
1278 | { |
1279 | if (_M_path != __rhs._M_path) | |
1280 | return false; | |
1281 | if (_M_path == nullptr) | |
1282 | return true; | |
1283 | if (_M_path->_M_type == path::_Type::_Multi) | |
1284 | return _M_cur == __rhs._M_cur; | |
1285 | return _M_at_end == __rhs._M_at_end; | |
1286 | } | |
1287 | ||
1e690757 JW |
1288 | // Define these now that path and path::iterator are complete. |
1289 | // They needs to consider the string_view(Range&&) constructor during | |
1290 | // overload resolution, which depends on whether range<path> is satisfied, | |
1291 | // which depends on whether path::iterator is complete. | |
1292 | inline bool operator<(const path& __lhs, const path& __rhs) noexcept | |
1293 | { return __lhs.compare(__rhs) < 0; } | |
1294 | ||
1295 | inline bool operator==(const path& __lhs, const path& __rhs) noexcept | |
1296 | { return __lhs.compare(__rhs) == 0; } | |
1297 | ||
f0b88346 | 1298 | /// @} group filesystem-ts |
0ca7ba9a | 1299 | _GLIBCXX_END_NAMESPACE_CXX11 |
0ca7ba9a JW |
1300 | } // namespace v1 |
1301 | } // namespace filesystem | |
1302 | } // namespace experimental | |
4a15d842 FD |
1303 | |
1304 | _GLIBCXX_END_NAMESPACE_VERSION | |
0ca7ba9a JW |
1305 | } // namespace std |
1306 | ||
1307 | #endif // C++11 | |
1308 | ||
1309 | #endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H |