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