fs_path.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. // Class filesystem::path -*- C++ -*-
  2. // Copyright (C) 2014-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/bits/fs_path.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{filesystem}
  23. */
  24. #ifndef _GLIBCXX_FS_PATH_H
  25. #define _GLIBCXX_FS_PATH_H 1
  26. #if __cplusplus >= 201703L
  27. #include <utility>
  28. #include <type_traits>
  29. #include <locale>
  30. #include <iosfwd>
  31. #include <iomanip>
  32. #include <codecvt>
  33. #include <string_view>
  34. #include <system_error>
  35. #include <bits/stl_algobase.h>
  36. #include <bits/locale_conv.h>
  37. #include <ext/concurrence.h>
  38. #include <bits/shared_ptr.h>
  39. #include <bits/unique_ptr.h>
  40. #if defined(_WIN32) && !defined(__CYGWIN__)
  41. # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
  42. # include <algorithm>
  43. #endif
  44. namespace std _GLIBCXX_VISIBILITY(default)
  45. {
  46. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  47. namespace filesystem
  48. {
  49. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  50. /**
  51. * @ingroup filesystem
  52. * @{
  53. */
  54. /// A filesystem path.
  55. class path
  56. {
  57. template<typename _CharT, typename _Ch = remove_const_t<_CharT>>
  58. using __is_encoded_char
  59. = __or_<is_same<_Ch, char>,
  60. #ifdef _GLIBCXX_USE_CHAR8_T
  61. is_same<_Ch, char8_t>,
  62. #endif
  63. is_same<_Ch, wchar_t>,
  64. is_same<_Ch, char16_t>,
  65. is_same<_Ch, char32_t>>;
  66. template<typename _Iter,
  67. typename _Iter_traits = std::iterator_traits<_Iter>>
  68. using __is_path_iter_src
  69. = __and_<__is_encoded_char<typename _Iter_traits::value_type>,
  70. std::is_base_of<std::input_iterator_tag,
  71. typename _Iter_traits::iterator_category>>;
  72. template<typename _Iter>
  73. static __is_path_iter_src<_Iter>
  74. __is_path_src(_Iter, int);
  75. template<typename _CharT, typename _Traits, typename _Alloc>
  76. static __is_encoded_char<_CharT>
  77. __is_path_src(const basic_string<_CharT, _Traits, _Alloc>&, int);
  78. template<typename _CharT, typename _Traits>
  79. static __is_encoded_char<_CharT>
  80. __is_path_src(const basic_string_view<_CharT, _Traits>&, int);
  81. template<typename _Unknown>
  82. static std::false_type
  83. __is_path_src(const _Unknown&, ...);
  84. template<typename _Tp1, typename _Tp2>
  85. struct __constructible_from;
  86. template<typename _Iter>
  87. struct __constructible_from<_Iter, _Iter>
  88. : __is_path_iter_src<_Iter>
  89. { };
  90. template<typename _Source>
  91. struct __constructible_from<_Source, void>
  92. : decltype(__is_path_src(std::declval<_Source>(), 0))
  93. { };
  94. template<typename _Tp1, typename _Tp2 = void>
  95. using _Path = typename
  96. std::enable_if<__and_<__not_<is_same<remove_cv_t<_Tp1>, path>>,
  97. __not_<is_void<remove_pointer_t<_Tp1>>>,
  98. __constructible_from<_Tp1, _Tp2>>::value,
  99. path>::type;
  100. template<typename _Source>
  101. static _Source
  102. _S_range_begin(_Source __begin) { return __begin; }
  103. struct __null_terminated { };
  104. template<typename _Source>
  105. static __null_terminated
  106. _S_range_end(_Source) { return {}; }
  107. template<typename _CharT, typename _Traits, typename _Alloc>
  108. static const _CharT*
  109. _S_range_begin(const basic_string<_CharT, _Traits, _Alloc>& __str)
  110. { return __str.data(); }
  111. template<typename _CharT, typename _Traits, typename _Alloc>
  112. static const _CharT*
  113. _S_range_end(const basic_string<_CharT, _Traits, _Alloc>& __str)
  114. { return __str.data() + __str.size(); }
  115. template<typename _CharT, typename _Traits>
  116. static const _CharT*
  117. _S_range_begin(const basic_string_view<_CharT, _Traits>& __str)
  118. { return __str.data(); }
  119. template<typename _CharT, typename _Traits>
  120. static const _CharT*
  121. _S_range_end(const basic_string_view<_CharT, _Traits>& __str)
  122. { return __str.data() + __str.size(); }
  123. template<typename _Tp,
  124. typename _Iter = decltype(_S_range_begin(std::declval<_Tp>())),
  125. typename _Val = typename std::iterator_traits<_Iter>::value_type>
  126. using __value_type_is_char
  127. = std::enable_if_t<std::is_same_v<std::remove_const_t<_Val>, char>>;
  128. public:
  129. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  130. typedef wchar_t value_type;
  131. static constexpr value_type preferred_separator = L'\\';
  132. #else
  133. typedef char value_type;
  134. static constexpr value_type preferred_separator = '/';
  135. #endif
  136. typedef std::basic_string<value_type> string_type;
  137. enum format : unsigned char { native_format, generic_format, auto_format };
  138. // constructors and destructor
  139. path() noexcept { }
  140. path(const path& __p) = default;
  141. path(path&& __p)
  142. #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  143. noexcept
  144. #endif
  145. : _M_pathname(std::move(__p._M_pathname)),
  146. _M_cmpts(std::move(__p._M_cmpts))
  147. { __p.clear(); }
  148. path(string_type&& __source, format = auto_format)
  149. : _M_pathname(std::move(__source))
  150. { _M_split_cmpts(); }
  151. template<typename _Source,
  152. typename _Require = _Path<_Source>>
  153. path(_Source const& __source, format = auto_format)
  154. : _M_pathname(_S_convert(_S_range_begin(__source),
  155. _S_range_end(__source)))
  156. { _M_split_cmpts(); }
  157. template<typename _InputIterator,
  158. typename _Require = _Path<_InputIterator, _InputIterator>>
  159. path(_InputIterator __first, _InputIterator __last, format = auto_format)
  160. : _M_pathname(_S_convert(__first, __last))
  161. { _M_split_cmpts(); }
  162. template<typename _Source,
  163. typename _Require = _Path<_Source>,
  164. typename _Require2 = __value_type_is_char<_Source>>
  165. path(_Source const& __source, const locale& __loc, format = auto_format)
  166. : _M_pathname(_S_convert_loc(_S_range_begin(__source),
  167. _S_range_end(__source), __loc))
  168. { _M_split_cmpts(); }
  169. template<typename _InputIterator,
  170. typename _Require = _Path<_InputIterator, _InputIterator>,
  171. typename _Require2 = __value_type_is_char<_InputIterator>>
  172. path(_InputIterator __first, _InputIterator __last, const locale& __loc,
  173. format = auto_format)
  174. : _M_pathname(_S_convert_loc(__first, __last, __loc))
  175. { _M_split_cmpts(); }
  176. ~path() = default;
  177. // assignments
  178. path& operator=(const path&);
  179. path& operator=(path&&) noexcept;
  180. path& operator=(string_type&& __source);
  181. path& assign(string_type&& __source);
  182. template<typename _Source>
  183. _Path<_Source>&
  184. operator=(_Source const& __source)
  185. { return *this = path(__source); }
  186. template<typename _Source>
  187. _Path<_Source>&
  188. assign(_Source const& __source)
  189. { return *this = path(__source); }
  190. template<typename _InputIterator>
  191. _Path<_InputIterator, _InputIterator>&
  192. assign(_InputIterator __first, _InputIterator __last)
  193. { return *this = path(__first, __last); }
  194. // appends
  195. path& operator/=(const path& __p);
  196. template <class _Source>
  197. _Path<_Source>&
  198. operator/=(_Source const& __source)
  199. {
  200. _M_append(_S_convert(_S_range_begin(__source), _S_range_end(__source)));
  201. return *this;
  202. }
  203. template<typename _Source>
  204. _Path<_Source>&
  205. append(_Source const& __source)
  206. {
  207. _M_append(_S_convert(_S_range_begin(__source), _S_range_end(__source)));
  208. return *this;
  209. }
  210. template<typename _InputIterator>
  211. _Path<_InputIterator, _InputIterator>&
  212. append(_InputIterator __first, _InputIterator __last)
  213. {
  214. _M_append(_S_convert(__first, __last));
  215. return *this;
  216. }
  217. // concatenation
  218. path& operator+=(const path& __x);
  219. path& operator+=(const string_type& __x);
  220. path& operator+=(const value_type* __x);
  221. path& operator+=(value_type __x);
  222. path& operator+=(basic_string_view<value_type> __x);
  223. template<typename _Source>
  224. _Path<_Source>&
  225. operator+=(_Source const& __x) { return concat(__x); }
  226. template<typename _CharT>
  227. _Path<_CharT*, _CharT*>&
  228. operator+=(_CharT __x);
  229. template<typename _Source>
  230. _Path<_Source>&
  231. concat(_Source const& __x)
  232. {
  233. _M_concat(_S_convert(_S_range_begin(__x), _S_range_end(__x)));
  234. return *this;
  235. }
  236. template<typename _InputIterator>
  237. _Path<_InputIterator, _InputIterator>&
  238. concat(_InputIterator __first, _InputIterator __last)
  239. {
  240. _M_concat(_S_convert(__first, __last));
  241. return *this;
  242. }
  243. // modifiers
  244. void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); }
  245. path& make_preferred();
  246. path& remove_filename();
  247. path& replace_filename(const path& __replacement);
  248. path& replace_extension(const path& __replacement = path());
  249. void swap(path& __rhs) noexcept;
  250. // native format observers
  251. const string_type& native() const noexcept { return _M_pathname; }
  252. const value_type* c_str() const noexcept { return _M_pathname.c_str(); }
  253. operator string_type() const { return _M_pathname; }
  254. template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
  255. typename _Allocator = std::allocator<_CharT>>
  256. std::basic_string<_CharT, _Traits, _Allocator>
  257. string(const _Allocator& __a = _Allocator()) const;
  258. std::string string() const;
  259. #if _GLIBCXX_USE_WCHAR_T
  260. std::wstring wstring() const;
  261. #endif
  262. #ifdef _GLIBCXX_USE_CHAR8_T
  263. __attribute__((__abi_tag__("__u8")))
  264. std::u8string u8string() const;
  265. #else
  266. std::string u8string() const;
  267. #endif // _GLIBCXX_USE_CHAR8_T
  268. std::u16string u16string() const;
  269. std::u32string u32string() const;
  270. // generic format observers
  271. template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
  272. typename _Allocator = std::allocator<_CharT>>
  273. std::basic_string<_CharT, _Traits, _Allocator>
  274. generic_string(const _Allocator& __a = _Allocator()) const;
  275. std::string generic_string() const;
  276. #if _GLIBCXX_USE_WCHAR_T
  277. std::wstring generic_wstring() const;
  278. #endif
  279. #ifdef _GLIBCXX_USE_CHAR8_T
  280. __attribute__((__abi_tag__("__u8")))
  281. std::u8string generic_u8string() const;
  282. #else
  283. std::string generic_u8string() const;
  284. #endif // _GLIBCXX_USE_CHAR8_T
  285. std::u16string generic_u16string() const;
  286. std::u32string generic_u32string() const;
  287. // compare
  288. int compare(const path& __p) const noexcept;
  289. int compare(const string_type& __s) const noexcept;
  290. int compare(const value_type* __s) const noexcept;
  291. int compare(basic_string_view<value_type> __s) const noexcept;
  292. // decomposition
  293. path root_name() const;
  294. path root_directory() const;
  295. path root_path() const;
  296. path relative_path() const;
  297. path parent_path() const;
  298. path filename() const;
  299. path stem() const;
  300. path extension() const;
  301. // query
  302. [[nodiscard]] bool empty() const noexcept { return _M_pathname.empty(); }
  303. bool has_root_name() const noexcept;
  304. bool has_root_directory() const noexcept;
  305. bool has_root_path() const noexcept;
  306. bool has_relative_path() const noexcept;
  307. bool has_parent_path() const noexcept;
  308. bool has_filename() const noexcept;
  309. bool has_stem() const noexcept;
  310. bool has_extension() const noexcept;
  311. bool is_absolute() const noexcept;
  312. bool is_relative() const noexcept { return !is_absolute(); }
  313. // generation
  314. path lexically_normal() const;
  315. path lexically_relative(const path& base) const;
  316. path lexically_proximate(const path& base) const;
  317. // iterators
  318. class iterator;
  319. typedef iterator const_iterator;
  320. iterator begin() const;
  321. iterator end() const;
  322. /// Write a path to a stream
  323. template<typename _CharT, typename _Traits>
  324. friend std::basic_ostream<_CharT, _Traits>&
  325. operator<<(std::basic_ostream<_CharT, _Traits>& __os, const path& __p)
  326. {
  327. __os << std::quoted(__p.string<_CharT, _Traits>());
  328. return __os;
  329. }
  330. /// Read a path from a stream
  331. template<typename _CharT, typename _Traits>
  332. friend std::basic_istream<_CharT, _Traits>&
  333. operator>>(std::basic_istream<_CharT, _Traits>& __is, path& __p)
  334. {
  335. std::basic_string<_CharT, _Traits> __tmp;
  336. if (__is >> std::quoted(__tmp))
  337. __p = std::move(__tmp);
  338. return __is;
  339. }
  340. // non-member operators
  341. /// Compare paths
  342. friend bool operator<(const path& __lhs, const path& __rhs) noexcept
  343. { return __lhs.compare(__rhs) < 0; }
  344. /// Compare paths
  345. friend bool operator<=(const path& __lhs, const path& __rhs) noexcept
  346. { return !(__rhs < __lhs); }
  347. /// Compare paths
  348. friend bool operator>(const path& __lhs, const path& __rhs) noexcept
  349. { return __rhs < __lhs; }
  350. /// Compare paths
  351. friend bool operator>=(const path& __lhs, const path& __rhs) noexcept
  352. { return !(__lhs < __rhs); }
  353. /// Compare paths
  354. friend bool operator==(const path& __lhs, const path& __rhs) noexcept
  355. { return __lhs.compare(__rhs) == 0; }
  356. /// Compare paths
  357. friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
  358. { return !(__lhs == __rhs); }
  359. /// Append one path to another
  360. friend path operator/(const path& __lhs, const path& __rhs)
  361. {
  362. path __result(__lhs);
  363. __result /= __rhs;
  364. return __result;
  365. }
  366. // Create a basic_string by reading until a null character.
  367. template<typename _InputIterator,
  368. typename _Traits = std::iterator_traits<_InputIterator>,
  369. typename _CharT
  370. = typename std::remove_cv_t<typename _Traits::value_type>>
  371. static std::basic_string<_CharT>
  372. _S_string_from_iter(_InputIterator __source)
  373. {
  374. std::basic_string<_CharT> __str;
  375. for (_CharT __ch = *__source; __ch != _CharT(); __ch = *++__source)
  376. __str.push_back(__ch);
  377. return __str;
  378. }
  379. private:
  380. enum class _Type : unsigned char {
  381. _Multi = 0, _Root_name, _Root_dir, _Filename
  382. };
  383. path(basic_string_view<value_type> __str, _Type __type)
  384. : _M_pathname(__str)
  385. {
  386. __glibcxx_assert(__type != _Type::_Multi);
  387. _M_cmpts.type(__type);
  388. }
  389. enum class _Split { _Stem, _Extension };
  390. void _M_append(basic_string_view<value_type>);
  391. void _M_concat(basic_string_view<value_type>);
  392. pair<const string_type*, size_t> _M_find_extension() const noexcept;
  393. template<typename _CharT>
  394. struct _Cvt;
  395. static basic_string_view<value_type>
  396. _S_convert(value_type* __src, __null_terminated)
  397. { return __src; }
  398. static basic_string_view<value_type>
  399. _S_convert(const value_type* __src, __null_terminated)
  400. { return __src; }
  401. static basic_string_view<value_type>
  402. _S_convert(value_type* __first, value_type* __last)
  403. { return {__first, __last - __first}; }
  404. static basic_string_view<value_type>
  405. _S_convert(const value_type* __first, const value_type* __last)
  406. { return {__first, __last - __first}; }
  407. template<typename _Iter>
  408. static string_type
  409. _S_convert(_Iter __first, _Iter __last)
  410. {
  411. using __value_type = typename std::iterator_traits<_Iter>::value_type;
  412. return _Cvt<typename remove_cv<__value_type>::type>::
  413. _S_convert(__first, __last);
  414. }
  415. template<typename _InputIterator>
  416. static string_type
  417. _S_convert(_InputIterator __src, __null_terminated)
  418. {
  419. // Read from iterator into basic_string until a null value is seen:
  420. auto __s = _S_string_from_iter(__src);
  421. // Convert (if needed) from iterator's value type to path::value_type:
  422. return string_type(_S_convert(__s.data(), __s.data() + __s.size()));
  423. }
  424. static string_type
  425. _S_convert_loc(const char* __first, const char* __last,
  426. const std::locale& __loc);
  427. template<typename _Iter>
  428. static string_type
  429. _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc)
  430. {
  431. const std::string __str(__first, __last);
  432. return _S_convert_loc(__str.data(), __str.data()+__str.size(), __loc);
  433. }
  434. template<typename _InputIterator>
  435. static string_type
  436. _S_convert_loc(_InputIterator __src, __null_terminated,
  437. const std::locale& __loc)
  438. {
  439. const std::string __s = _S_string_from_iter(__src);
  440. return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
  441. }
  442. template<typename _CharT, typename _Traits, typename _Allocator>
  443. static basic_string<_CharT, _Traits, _Allocator>
  444. _S_str_convert(const string_type&, const _Allocator& __a);
  445. void _M_split_cmpts();
  446. _Type _M_type() const noexcept { return _M_cmpts.type(); }
  447. string_type _M_pathname;
  448. struct _Cmpt;
  449. struct _List
  450. {
  451. using value_type = _Cmpt;
  452. using iterator = value_type*;
  453. using const_iterator = const value_type*;
  454. _List();
  455. _List(const _List&);
  456. _List(_List&&) = default;
  457. _List& operator=(const _List&);
  458. _List& operator=(_List&&) = default;
  459. ~_List() = default;
  460. _Type type() const noexcept
  461. { return _Type{reinterpret_cast<uintptr_t>(_M_impl.get()) & 0x3}; }
  462. void type(_Type) noexcept;
  463. int size() const noexcept; // zero unless type() == _Type::_Multi
  464. bool empty() const noexcept; // true unless type() == _Type::_Multi
  465. void clear();
  466. void swap(_List& __l) noexcept { _M_impl.swap(__l._M_impl); }
  467. int capacity() const noexcept;
  468. void reserve(int, bool); ///< @pre type() == _Type::_Multi
  469. // All the member functions below here have a precondition !empty()
  470. // (and they should only be called from within the library).
  471. iterator begin();
  472. iterator end();
  473. const_iterator begin() const;
  474. const_iterator end() const;
  475. value_type& front() noexcept;
  476. value_type& back() noexcept;
  477. const value_type& front() const noexcept;
  478. const value_type& back() const noexcept;
  479. void pop_back();
  480. void _M_erase_from(const_iterator __pos); // erases [__pos,end())
  481. struct _Impl;
  482. struct _Impl_deleter
  483. {
  484. void operator()(_Impl*) const noexcept;
  485. };
  486. unique_ptr<_Impl, _Impl_deleter> _M_impl;
  487. };
  488. _List _M_cmpts;
  489. struct _Parser;
  490. };
  491. inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); }
  492. size_t hash_value(const path& __p) noexcept;
  493. template<typename _InputIterator>
  494. inline auto
  495. u8path(_InputIterator __first, _InputIterator __last)
  496. -> decltype(filesystem::path(__first, __last, std::locale::classic()))
  497. {
  498. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  499. // XXX This assumes native wide encoding is UTF-16.
  500. std::codecvt_utf8_utf16<path::value_type> __cvt;
  501. path::string_type __tmp;
  502. if constexpr (is_pointer_v<_InputIterator>)
  503. {
  504. if (__str_codecvt_in_all(__first, __last, __tmp, __cvt))
  505. return path{ __tmp };
  506. }
  507. else
  508. {
  509. const std::string __u8str{__first, __last};
  510. const char* const __ptr = __u8str.data();
  511. if (__str_codecvt_in_all(__ptr, __ptr + __u8str.size(), __tmp, __cvt))
  512. return path{ __tmp };
  513. }
  514. _GLIBCXX_THROW_OR_ABORT(filesystem_error(
  515. "Cannot convert character sequence",
  516. std::make_error_code(errc::illegal_byte_sequence)));
  517. #else
  518. // This assumes native normal encoding is UTF-8.
  519. return path{ __first, __last };
  520. #endif
  521. }
  522. template<typename _Source>
  523. inline auto
  524. u8path(const _Source& __source)
  525. -> decltype(filesystem::path(__source, std::locale::classic()))
  526. {
  527. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  528. if constexpr (is_convertible_v<const _Source&, std::string_view>)
  529. {
  530. const std::string_view __s = __source;
  531. return filesystem::u8path(__s.data(), __s.data() + __s.size());
  532. }
  533. else
  534. {
  535. std::string __s = path::_S_string_from_iter(__source);
  536. return filesystem::u8path(__s.data(), __s.data() + __s.size());
  537. }
  538. #else
  539. return path{ __source };
  540. #endif
  541. }
  542. class filesystem_error : public std::system_error
  543. {
  544. public:
  545. filesystem_error(const string& __what_arg, error_code __ec);
  546. filesystem_error(const string& __what_arg, const path& __p1,
  547. error_code __ec);
  548. filesystem_error(const string& __what_arg, const path& __p1,
  549. const path& __p2, error_code __ec);
  550. filesystem_error(const filesystem_error&) = default;
  551. filesystem_error& operator=(const filesystem_error&) = default;
  552. // No move constructor or assignment operator.
  553. // Copy rvalues instead, so that _M_impl is not left empty.
  554. ~filesystem_error();
  555. const path& path1() const noexcept;
  556. const path& path2() const noexcept;
  557. const char* what() const noexcept;
  558. private:
  559. struct _Impl;
  560. std::__shared_ptr<const _Impl> _M_impl;
  561. };
  562. struct path::_Cmpt : path
  563. {
  564. _Cmpt(basic_string_view<value_type> __s, _Type __t, size_t __pos)
  565. : path(__s, __t), _M_pos(__pos) { }
  566. _Cmpt() : _M_pos(-1) { }
  567. size_t _M_pos;
  568. };
  569. // specialize _Cvt for degenerate 'noconv' case
  570. template<>
  571. struct path::_Cvt<path::value_type>
  572. {
  573. template<typename _Iter>
  574. static string_type
  575. _S_convert(_Iter __first, _Iter __last)
  576. { return string_type{__first, __last}; }
  577. };
  578. #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
  579. // For POSIX converting from char8_t to char is also 'noconv'
  580. template<>
  581. struct path::_Cvt<char8_t>
  582. {
  583. template<typename _Iter>
  584. static string_type
  585. _S_convert(_Iter __first, _Iter __last)
  586. { return string_type(__first, __last); }
  587. };
  588. #endif
  589. template<typename _CharT>
  590. struct path::_Cvt
  591. {
  592. static string_type
  593. _S_convert(const _CharT* __f, const _CharT* __l)
  594. {
  595. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  596. std::wstring __wstr;
  597. if constexpr (is_same_v<_CharT, char>)
  598. {
  599. struct _UCvt : std::codecvt<wchar_t, char, std::mbstate_t>
  600. { } __cvt;
  601. if (__str_codecvt_in_all(__f, __l, __wstr, __cvt))
  602. return __wstr;
  603. }
  604. #ifdef _GLIBCXX_USE_CHAR8_T
  605. else if constexpr (is_same_v<_CharT, char8_t>)
  606. {
  607. const char* __f2 = (const char*)__f;
  608. const char* __l2 = (const char*)__l;
  609. std::codecvt_utf8_utf16<wchar_t> __wcvt;
  610. if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt))
  611. return __wstr;
  612. }
  613. #endif
  614. else // char16_t or char32_t
  615. {
  616. struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t>
  617. { } __cvt;
  618. std::string __str;
  619. if (__str_codecvt_out_all(__f, __l, __str, __cvt))
  620. {
  621. const char* __f2 = __str.data();
  622. const char* __l2 = __f2 + __str.size();
  623. std::codecvt_utf8_utf16<wchar_t> __wcvt;
  624. if (__str_codecvt_in_all(__f2, __l2, __wstr, __wcvt))
  625. return __wstr;
  626. }
  627. }
  628. #else // ! windows
  629. struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t>
  630. { } __cvt;
  631. std::string __str;
  632. if (__str_codecvt_out_all(__f, __l, __str, __cvt))
  633. return __str;
  634. #endif
  635. _GLIBCXX_THROW_OR_ABORT(filesystem_error(
  636. "Cannot convert character sequence",
  637. std::make_error_code(errc::illegal_byte_sequence)));
  638. }
  639. static string_type
  640. _S_convert(_CharT* __f, _CharT* __l)
  641. {
  642. return _S_convert(const_cast<const _CharT*>(__f),
  643. const_cast<const _CharT*>(__l));
  644. }
  645. template<typename _Iter>
  646. static string_type
  647. _S_convert(_Iter __first, _Iter __last)
  648. {
  649. const std::basic_string<_CharT> __str(__first, __last);
  650. return _S_convert(__str.data(), __str.data() + __str.size());
  651. }
  652. template<typename _Iter, typename _Cont>
  653. static string_type
  654. _S_convert(__gnu_cxx::__normal_iterator<_Iter, _Cont> __first,
  655. __gnu_cxx::__normal_iterator<_Iter, _Cont> __last)
  656. { return _S_convert(__first.base(), __last.base()); }
  657. };
  658. /// An iterator for the components of a path
  659. class path::iterator
  660. {
  661. public:
  662. using difference_type = std::ptrdiff_t;
  663. using value_type = path;
  664. using reference = const path&;
  665. using pointer = const path*;
  666. using iterator_category = std::bidirectional_iterator_tag;
  667. iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
  668. iterator(const iterator&) = default;
  669. iterator& operator=(const iterator&) = default;
  670. reference operator*() const;
  671. pointer operator->() const { return std::__addressof(**this); }
  672. iterator& operator++();
  673. iterator operator++(int) { auto __tmp = *this; ++*this; return __tmp; }
  674. iterator& operator--();
  675. iterator operator--(int) { auto __tmp = *this; --*this; return __tmp; }
  676. friend bool operator==(const iterator& __lhs, const iterator& __rhs)
  677. { return __lhs._M_equals(__rhs); }
  678. friend bool operator!=(const iterator& __lhs, const iterator& __rhs)
  679. { return !__lhs._M_equals(__rhs); }
  680. private:
  681. friend class path;
  682. bool _M_is_multi() const { return _M_path->_M_type() == _Type::_Multi; }
  683. friend difference_type
  684. __path_iter_distance(const iterator& __first, const iterator& __last)
  685. {
  686. __glibcxx_assert(__first._M_path != nullptr);
  687. __glibcxx_assert(__first._M_path == __last._M_path);
  688. if (__first._M_is_multi())
  689. return std::distance(__first._M_cur, __last._M_cur);
  690. else if (__first._M_at_end == __last._M_at_end)
  691. return 0;
  692. else
  693. return __first._M_at_end ? -1 : 1;
  694. }
  695. friend void
  696. __path_iter_advance(iterator& __i, difference_type __n)
  697. {
  698. if (__n == 1)
  699. ++__i;
  700. else if (__n == -1)
  701. --__i;
  702. else if (__n != 0)
  703. {
  704. __glibcxx_assert(__i._M_path != nullptr);
  705. __glibcxx_assert(__i._M_is_multi());
  706. // __glibcxx_assert(__i._M_path->_M_cmpts.end() - __i._M_cur >= __n);
  707. __i._M_cur += __n;
  708. }
  709. }
  710. iterator(const path* __path, path::_List::const_iterator __iter)
  711. : _M_path(__path), _M_cur(__iter), _M_at_end()
  712. { }
  713. iterator(const path* __path, bool __at_end)
  714. : _M_path(__path), _M_cur(), _M_at_end(__at_end)
  715. { }
  716. bool _M_equals(iterator) const;
  717. const path* _M_path;
  718. path::_List::const_iterator _M_cur;
  719. bool _M_at_end; // only used when type != _Multi
  720. };
  721. inline path&
  722. path::operator=(path&& __p) noexcept
  723. {
  724. if (&__p == this) [[__unlikely__]]
  725. return *this;
  726. _M_pathname = std::move(__p._M_pathname);
  727. _M_cmpts = std::move(__p._M_cmpts);
  728. __p.clear();
  729. return *this;
  730. }
  731. inline path&
  732. path::operator=(string_type&& __source)
  733. { return *this = path(std::move(__source)); }
  734. inline path&
  735. path::assign(string_type&& __source)
  736. { return *this = path(std::move(__source)); }
  737. inline path&
  738. path::operator+=(const string_type& __x)
  739. {
  740. _M_concat(__x);
  741. return *this;
  742. }
  743. inline path&
  744. path::operator+=(const value_type* __x)
  745. {
  746. _M_concat(__x);
  747. return *this;
  748. }
  749. inline path&
  750. path::operator+=(value_type __x)
  751. {
  752. _M_concat(basic_string_view<value_type>(&__x, 1));
  753. return *this;
  754. }
  755. inline path&
  756. path::operator+=(basic_string_view<value_type> __x)
  757. {
  758. _M_concat(__x);
  759. return *this;
  760. }
  761. template<typename _CharT>
  762. inline path::_Path<_CharT*, _CharT*>&
  763. path::operator+=(_CharT __x)
  764. {
  765. auto* __addr = std::__addressof(__x);
  766. return concat(__addr, __addr + 1);
  767. }
  768. inline path&
  769. path::make_preferred()
  770. {
  771. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  772. std::replace(_M_pathname.begin(), _M_pathname.end(), L'/',
  773. preferred_separator);
  774. #endif
  775. return *this;
  776. }
  777. inline void path::swap(path& __rhs) noexcept
  778. {
  779. _M_pathname.swap(__rhs._M_pathname);
  780. _M_cmpts.swap(__rhs._M_cmpts);
  781. }
  782. template<typename _CharT, typename _Traits, typename _Allocator>
  783. std::basic_string<_CharT, _Traits, _Allocator>
  784. path::_S_str_convert(const string_type& __str, const _Allocator& __a)
  785. {
  786. static_assert(!is_same_v<_CharT, value_type>);
  787. using _WString = basic_string<_CharT, _Traits, _Allocator>;
  788. if (__str.size() == 0)
  789. return _WString(__a);
  790. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  791. // First convert native string from UTF-16 to to UTF-8.
  792. // XXX This assumes that the execution wide-character set is UTF-16.
  793. std::codecvt_utf8_utf16<value_type> __cvt;
  794. using _CharAlloc = __alloc_rebind<_Allocator, char>;
  795. using _String = basic_string<char, char_traits<char>, _CharAlloc>;
  796. _String __u8str{_CharAlloc{__a}};
  797. const value_type* __wfirst = __str.data();
  798. const value_type* __wlast = __wfirst + __str.size();
  799. if (__str_codecvt_out_all(__wfirst, __wlast, __u8str, __cvt)) {
  800. if constexpr (is_same_v<_CharT, char>)
  801. return __u8str; // XXX assumes native ordinary encoding is UTF-8.
  802. else {
  803. const char* __first = __u8str.data();
  804. const char* __last = __first + __u8str.size();
  805. #else
  806. const value_type* __first = __str.data();
  807. const value_type* __last = __first + __str.size();
  808. #endif
  809. // Convert UTF-8 string to requested format.
  810. #ifdef _GLIBCXX_USE_CHAR8_T
  811. if constexpr (is_same_v<_CharT, char8_t>)
  812. return _WString(__first, __last, __a);
  813. else
  814. #endif
  815. {
  816. // Convert UTF-8 to wide string.
  817. _WString __wstr(__a);
  818. struct _UCvt : std::codecvt<_CharT, char, std::mbstate_t> { } __cvt;
  819. if (__str_codecvt_in_all(__first, __last, __wstr, __cvt))
  820. return __wstr;
  821. }
  822. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  823. } }
  824. #endif
  825. _GLIBCXX_THROW_OR_ABORT(filesystem_error(
  826. "Cannot convert character sequence",
  827. std::make_error_code(errc::illegal_byte_sequence)));
  828. }
  829. template<typename _CharT, typename _Traits, typename _Allocator>
  830. inline basic_string<_CharT, _Traits, _Allocator>
  831. path::string(const _Allocator& __a) const
  832. {
  833. if constexpr (is_same_v<_CharT, value_type>)
  834. return { _M_pathname.c_str(), _M_pathname.length(), __a };
  835. else
  836. return _S_str_convert<_CharT, _Traits>(_M_pathname, __a);
  837. }
  838. inline std::string
  839. path::string() const { return string<char>(); }
  840. #if _GLIBCXX_USE_WCHAR_T
  841. inline std::wstring
  842. path::wstring() const { return string<wchar_t>(); }
  843. #endif
  844. #ifdef _GLIBCXX_USE_CHAR8_T
  845. inline std::u8string
  846. path::u8string() const { return string<char8_t>(); }
  847. #else
  848. inline std::string
  849. path::u8string() const
  850. {
  851. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  852. std::string __str;
  853. // convert from native wide encoding (assumed to be UTF-16) to UTF-8
  854. std::codecvt_utf8_utf16<value_type> __cvt;
  855. const value_type* __first = _M_pathname.data();
  856. const value_type* __last = __first + _M_pathname.size();
  857. if (__str_codecvt_out_all(__first, __last, __str, __cvt))
  858. return __str;
  859. _GLIBCXX_THROW_OR_ABORT(filesystem_error(
  860. "Cannot convert character sequence",
  861. std::make_error_code(errc::illegal_byte_sequence)));
  862. #else
  863. return _M_pathname;
  864. #endif
  865. }
  866. #endif // _GLIBCXX_USE_CHAR8_T
  867. inline std::u16string
  868. path::u16string() const { return string<char16_t>(); }
  869. inline std::u32string
  870. path::u32string() const { return string<char32_t>(); }
  871. template<typename _CharT, typename _Traits, typename _Allocator>
  872. inline std::basic_string<_CharT, _Traits, _Allocator>
  873. path::generic_string(const _Allocator& __a) const
  874. {
  875. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  876. const value_type __slash = L'/';
  877. #else
  878. const value_type __slash = '/';
  879. #endif
  880. string_type __str(__a);
  881. if (_M_type() == _Type::_Root_dir)
  882. __str.assign(1, __slash);
  883. else
  884. {
  885. __str.reserve(_M_pathname.size());
  886. bool __add_slash = false;
  887. for (auto& __elem : *this)
  888. {
  889. if (__add_slash)
  890. __str += __slash;
  891. __str += __elem._M_pathname;
  892. __add_slash = __elem._M_type() == _Type::_Filename;
  893. }
  894. }
  895. if constexpr (is_same_v<_CharT, value_type>)
  896. return __str;
  897. else
  898. return _S_str_convert<_CharT, _Traits>(__str, __a);
  899. }
  900. inline std::string
  901. path::generic_string() const
  902. { return generic_string<char>(); }
  903. #if _GLIBCXX_USE_WCHAR_T
  904. inline std::wstring
  905. path::generic_wstring() const
  906. { return generic_string<wchar_t>(); }
  907. #endif
  908. #ifdef _GLIBCXX_USE_CHAR8_T
  909. inline std::u8string
  910. path::generic_u8string() const
  911. { return generic_string<char8_t>(); }
  912. #else
  913. inline std::string
  914. path::generic_u8string() const
  915. { return generic_string(); }
  916. #endif
  917. inline std::u16string
  918. path::generic_u16string() const
  919. { return generic_string<char16_t>(); }
  920. inline std::u32string
  921. path::generic_u32string() const
  922. { return generic_string<char32_t>(); }
  923. inline int
  924. path::compare(const string_type& __s) const noexcept
  925. { return compare(basic_string_view<value_type>(__s)); }
  926. inline int
  927. path::compare(const value_type* __s) const noexcept
  928. { return compare(basic_string_view<value_type>(__s)); }
  929. inline path
  930. path::filename() const
  931. {
  932. if (empty())
  933. return {};
  934. else if (_M_type() == _Type::_Filename)
  935. return *this;
  936. else if (_M_type() == _Type::_Multi)
  937. {
  938. if (_M_pathname.back() == preferred_separator)
  939. return {};
  940. auto& __last = *--end();
  941. if (__last._M_type() == _Type::_Filename)
  942. return __last;
  943. }
  944. return {};
  945. }
  946. inline path
  947. path::stem() const
  948. {
  949. auto ext = _M_find_extension();
  950. if (ext.first && ext.second != 0)
  951. return path{ext.first->substr(0, ext.second)};
  952. return {};
  953. }
  954. inline path
  955. path::extension() const
  956. {
  957. auto ext = _M_find_extension();
  958. if (ext.first && ext.second != string_type::npos)
  959. return path{ext.first->substr(ext.second)};
  960. return {};
  961. }
  962. inline bool
  963. path::has_stem() const noexcept
  964. {
  965. auto ext = _M_find_extension();
  966. return ext.first && ext.second != 0;
  967. }
  968. inline bool
  969. path::has_extension() const noexcept
  970. {
  971. auto ext = _M_find_extension();
  972. return ext.first && ext.second != string_type::npos;
  973. }
  974. inline bool
  975. path::is_absolute() const noexcept
  976. {
  977. #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  978. return has_root_name() && has_root_directory();
  979. #else
  980. return has_root_directory();
  981. #endif
  982. }
  983. inline path::iterator
  984. path::begin() const
  985. {
  986. if (_M_type() == _Type::_Multi)
  987. return iterator(this, _M_cmpts.begin());
  988. return iterator(this, empty());
  989. }
  990. inline path::iterator
  991. path::end() const
  992. {
  993. if (_M_type() == _Type::_Multi)
  994. return iterator(this, _M_cmpts.end());
  995. return iterator(this, true);
  996. }
  997. inline path::iterator&
  998. path::iterator::operator++()
  999. {
  1000. __glibcxx_assert(_M_path != nullptr);
  1001. if (_M_path->_M_type() == _Type::_Multi)
  1002. {
  1003. __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
  1004. ++_M_cur;
  1005. }
  1006. else
  1007. {
  1008. __glibcxx_assert(!_M_at_end);
  1009. _M_at_end = true;
  1010. }
  1011. return *this;
  1012. }
  1013. inline path::iterator&
  1014. path::iterator::operator--()
  1015. {
  1016. __glibcxx_assert(_M_path != nullptr);
  1017. if (_M_path->_M_type() == _Type::_Multi)
  1018. {
  1019. __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
  1020. --_M_cur;
  1021. }
  1022. else
  1023. {
  1024. __glibcxx_assert(_M_at_end);
  1025. _M_at_end = false;
  1026. }
  1027. return *this;
  1028. }
  1029. inline path::iterator::reference
  1030. path::iterator::operator*() const
  1031. {
  1032. __glibcxx_assert(_M_path != nullptr);
  1033. if (_M_path->_M_type() == _Type::_Multi)
  1034. {
  1035. __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
  1036. return *_M_cur;
  1037. }
  1038. return *_M_path;
  1039. }
  1040. inline bool
  1041. path::iterator::_M_equals(iterator __rhs) const
  1042. {
  1043. if (_M_path != __rhs._M_path)
  1044. return false;
  1045. if (_M_path == nullptr)
  1046. return true;
  1047. if (_M_path->_M_type() == path::_Type::_Multi)
  1048. return _M_cur == __rhs._M_cur;
  1049. return _M_at_end == __rhs._M_at_end;
  1050. }
  1051. // @} group filesystem
  1052. _GLIBCXX_END_NAMESPACE_CXX11
  1053. } // namespace filesystem
  1054. inline ptrdiff_t
  1055. distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
  1056. { return __path_iter_distance(__first, __last); }
  1057. template<typename _InputIterator, typename _Distance>
  1058. void
  1059. advance(filesystem::path::iterator& __i, _Distance __n)
  1060. { __path_iter_advance(__i, static_cast<ptrdiff_t>(__n)); }
  1061. extern template class __shared_ptr<const filesystem::filesystem_error::_Impl>;
  1062. _GLIBCXX_END_NAMESPACE_VERSION
  1063. } // namespace std
  1064. #endif // C++17
  1065. #endif // _GLIBCXX_FS_PATH_H