filesystem 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. // -*-c++-*-
  2. // vim: set ft=cpp:
  3. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. file LICENSE.rst or https://cmake.org/licensing for details. */
  5. #pragma once
  6. #include "cmSTL.hxx" // IWYU pragma: keep
  7. #if defined(CMake_HAVE_CXX_FILESYSTEM)
  8. # include <filesystem> // IWYU pragma: export
  9. #else
  10. # include <cstddef>
  11. # include <cstdint>
  12. # include <iostream>
  13. # include <iterator>
  14. # include <memory>
  15. # include <string>
  16. # include <utility>
  17. # include <cm/iomanip>
  18. # include <cm/string_view>
  19. # include <cm/type_traits>
  20. # include <cmext/iterator>
  21. # if defined(_WIN32) && !defined(__CYGWIN__)
  22. # include <algorithm>
  23. # endif
  24. #endif
  25. namespace cm {
  26. namespace filesystem {
  27. #if defined(CMake_HAVE_CXX_FILESYSTEM)
  28. using std::filesystem::path;
  29. using std::filesystem::swap;
  30. using std::filesystem::hash_value;
  31. #else
  32. # if !defined(CM_FILESYSTEM_SOURCE_TRAITS_ITERATOR)
  33. // Oracle DeveloperStudio C++ compiler on Solaris/Sparc fails to compile
  34. // the source_traits for iterator check. So disable it for now.
  35. # define CM_FILESYSTEM_SOURCE_TRAITS_ITERATOR 0
  36. # endif
  37. namespace internals {
  38. class path_parser;
  39. class unicode_helper
  40. {
  41. protected:
  42. using utf8_state = unsigned char;
  43. static utf8_state const s_start = 0;
  44. static utf8_state const s_reject = 8;
  45. static inline bool in_range(std::uint32_t c, std::uint32_t lo,
  46. std::uint32_t hi)
  47. {
  48. return (static_cast<std::uint32_t>(c - lo) < (hi - lo + 1));
  49. }
  50. static inline bool is_surrogate(std::uint32_t c)
  51. {
  52. return in_range(c, 0xd800, 0xdfff);
  53. }
  54. static inline bool is_high_surrogate(std::uint32_t c)
  55. {
  56. return (c & 0xfffffc00) == 0xd800;
  57. }
  58. static inline bool is_low_surrogate(std::uint32_t c)
  59. {
  60. return (c & 0xfffffc00) == 0xdc00;
  61. }
  62. static void append(std::string& str, std::uint32_t codepoint);
  63. static utf8_state decode(utf8_state const state, std::uint8_t const fragment,
  64. std::uint32_t& codepoint);
  65. };
  66. template <typename Char, typename = void>
  67. class unicode
  68. {
  69. };
  70. template <typename Char>
  71. class unicode<Char, typename std::enable_if<(sizeof(Char) == 4)>::type>
  72. : public unicode_helper
  73. {
  74. public:
  75. // UTF32 -> UTF8
  76. static std::string to_utf8(std::wstring const& str)
  77. {
  78. std::string result;
  79. for (auto c : str) {
  80. append(result, c);
  81. }
  82. return result;
  83. }
  84. static std::string to_utf8(Char c)
  85. {
  86. std::string result;
  87. append(result, c);
  88. return result;
  89. }
  90. // UTF8 -> UTF32
  91. static std::wstring from_utf8(std::string const& str)
  92. {
  93. std::wstring result;
  94. result.reserve(str.length());
  95. auto iter = str.begin();
  96. utf8_state state = s_start;
  97. std::uint32_t codepoint = 0;
  98. while (iter < str.end()) {
  99. if ((state = decode(state, static_cast<std::uint8_t>(*iter++),
  100. codepoint)) == s_start) {
  101. result += static_cast<std::wstring::value_type>(codepoint);
  102. codepoint = 0;
  103. } else if (state == s_reject) {
  104. result += static_cast<std::wstring::value_type>(0xfffd);
  105. state = s_start;
  106. codepoint = 0;
  107. }
  108. }
  109. if (state) {
  110. result += static_cast<std::wstring::value_type>(0xfffd);
  111. }
  112. return result;
  113. }
  114. static std::wstring from_utf8(char c)
  115. {
  116. std::wstring result;
  117. utf8_state state = s_start;
  118. std::uint32_t codepoint = 0;
  119. if ((state = decode(state, static_cast<std::uint8_t>(c), codepoint)) ==
  120. s_start) {
  121. result += static_cast<std::wstring::value_type>(codepoint);
  122. } else {
  123. result += static_cast<std::wstring::value_type>(0xfffd);
  124. }
  125. return result;
  126. }
  127. };
  128. template <typename Char>
  129. class unicode<Char, typename std::enable_if<(sizeof(Char) == 2)>::type>
  130. : public unicode_helper
  131. {
  132. public:
  133. // UTF16 -> UTF8
  134. static std::string to_utf8(std::wstring const& str)
  135. {
  136. std::string result;
  137. for (auto iter = str.begin(); iter != str.end(); ++iter) {
  138. std::uint32_t c = *iter;
  139. if (is_surrogate(c)) {
  140. ++iter;
  141. if (iter != str.end() && is_high_surrogate(c) &&
  142. is_low_surrogate(*iter)) {
  143. append(result, (std::uint32_t(c) << 10) + *iter - 0x35fdc00);
  144. } else {
  145. append(result, 0xfffd);
  146. if (iter == str.end()) {
  147. break;
  148. }
  149. }
  150. } else {
  151. append(result, c);
  152. }
  153. }
  154. return result;
  155. }
  156. static std::string to_utf8(Char c)
  157. {
  158. std::string result;
  159. if (is_surrogate(c)) {
  160. append(result, 0xfffd);
  161. } else {
  162. append(result, c);
  163. }
  164. return result;
  165. }
  166. // UTF8 -> UTF16
  167. static std::wstring from_utf8(std::string const& str)
  168. {
  169. std::wstring result;
  170. result.reserve(str.length());
  171. auto iter = str.begin();
  172. utf8_state state = s_start;
  173. std::uint32_t codepoint = 0;
  174. while (iter < str.end()) {
  175. if ((state = decode(state, static_cast<std::uint8_t>(*iter++),
  176. codepoint)) == s_start) {
  177. if (codepoint <= 0xffff) {
  178. result += static_cast<std::wstring::value_type>(codepoint);
  179. } else {
  180. codepoint -= 0x10000;
  181. result +=
  182. static_cast<std::wstring::value_type>((codepoint >> 10) + 0xd800);
  183. result += static_cast<std::wstring::value_type>((codepoint & 0x3ff) +
  184. 0xdc00);
  185. }
  186. codepoint = 0;
  187. } else if (state == s_reject) {
  188. result += static_cast<std::wstring::value_type>(0xfffd);
  189. state = s_start;
  190. codepoint = 0;
  191. }
  192. }
  193. if (state) {
  194. result += static_cast<std::wstring::value_type>(0xfffd);
  195. }
  196. return result;
  197. }
  198. static std::wstring from_utf8(char c)
  199. {
  200. std::wstring result;
  201. utf8_state state = s_start;
  202. std::uint32_t codepoint = 0;
  203. if ((state = decode(state, static_cast<std::uint8_t>(c), codepoint)) ==
  204. s_start) {
  205. if (codepoint <= 0xffff) {
  206. result += static_cast<std::wstring::value_type>(codepoint);
  207. } else {
  208. codepoint -= 0x10000;
  209. result +=
  210. static_cast<std::wstring::value_type>((codepoint >> 10) + 0xd800);
  211. result +=
  212. static_cast<std::wstring::value_type>((codepoint & 0x3ff) + 0xdc00);
  213. }
  214. } else {
  215. result += static_cast<std::wstring::value_type>(0xfffd);
  216. }
  217. return result;
  218. }
  219. };
  220. template <typename In, typename Out>
  221. class unicode_converter;
  222. template <>
  223. class unicode_converter<char, wchar_t>
  224. {
  225. public:
  226. std::wstring operator()(std::string const& in)
  227. {
  228. return unicode<wchar_t>::from_utf8(in);
  229. }
  230. std::wstring operator()(char const* in)
  231. {
  232. return unicode<wchar_t>::from_utf8(in);
  233. }
  234. std::wstring operator()(char in) { return unicode<wchar_t>::from_utf8(in); }
  235. };
  236. template <>
  237. class unicode_converter<wchar_t, char>
  238. {
  239. public:
  240. std::string operator()(std::wstring const& in)
  241. {
  242. return unicode<wchar_t>::to_utf8(in);
  243. }
  244. std::string operator()(wchar_t const* in)
  245. {
  246. return unicode<wchar_t>::to_utf8(in);
  247. }
  248. std::string operator()(wchar_t in) { return unicode<wchar_t>::to_utf8(in); }
  249. };
  250. template <>
  251. class unicode_converter<char, char>
  252. {
  253. public:
  254. std::string operator()(std::string const& in) { return in; }
  255. std::string operator()(char const* in) { return std::string(in); }
  256. std::string operator()(char in) { return std::string(1, in); }
  257. };
  258. template <>
  259. class unicode_converter<wchar_t, wchar_t>
  260. {
  261. public:
  262. std::wstring operator()(std::wstring const& in) { return in; }
  263. std::wstring operator()(wchar_t const* in) { return std::wstring(in); }
  264. std::wstring operator()(wchar_t in) { return std::wstring(1, in); }
  265. };
  266. template <typename In>
  267. struct string_converter
  268. {
  269. };
  270. template <>
  271. struct string_converter<char>
  272. {
  273. // some compilers, like gcc 4.8 does not implement the following C++11
  274. // signature:
  275. // std::string::string(const string&, const Allocator&)
  276. // As workaround, use char* pointer.
  277. template <typename Char, typename Traits, typename Alloc>
  278. static std::basic_string<Char, Traits, Alloc> to(std::string const& in,
  279. Alloc const& a)
  280. {
  281. return std::basic_string<Char, Traits, Alloc>(
  282. unicode_converter<char, Char>()(in).c_str(), a);
  283. }
  284. template <typename Char, typename Traits, typename Alloc>
  285. static std::basic_string<Char, Traits, Alloc> to(char const* in,
  286. Alloc const& a)
  287. {
  288. return std::basic_string<Char, Traits, Alloc>(
  289. unicode_converter<char, Char>()(in).c_str(), a);
  290. }
  291. template <typename Char, typename Traits, typename Alloc>
  292. static std::basic_string<Char, Traits, Alloc> to(char in, Alloc const& a)
  293. {
  294. return std::basic_string<Char, Traits, Alloc>(
  295. unicode_converter<char, Char>()(in).c_str(), a);
  296. }
  297. template <typename Char>
  298. static std::basic_string<Char> to(std::string const& in)
  299. {
  300. return std::basic_string<Char>(unicode_converter<char, Char>()(in));
  301. }
  302. template <typename Char>
  303. static std::basic_string<Char> to(char const* in)
  304. {
  305. return std::basic_string<Char>(unicode_converter<char, Char>()(in));
  306. }
  307. template <typename Char>
  308. static std::basic_string<Char> to(char in)
  309. {
  310. return std::basic_string<Char>(unicode_converter<char, Char>()(in));
  311. }
  312. };
  313. template <>
  314. struct string_converter<wchar_t>
  315. {
  316. // some compilers, like gcc 4.8 does not implement the following C++11
  317. // signature:
  318. // std::string::string(const string&, const Allocator&)
  319. // As workaround, use char* pointer.
  320. template <typename Char, typename Traits, typename Alloc>
  321. static std::basic_string<Char, Traits, Alloc> to(std::wstring const& in,
  322. Alloc const& a)
  323. {
  324. return std::basic_string<Char, Traits, Alloc>(
  325. unicode_converter<wchar_t, Char>()(in).c_str(), a);
  326. }
  327. template <typename Char, typename Traits, typename Alloc>
  328. static std::basic_string<Char, Traits, Alloc> to(wchar_t const* in,
  329. Alloc const& a)
  330. {
  331. return std::basic_string<Char, Traits, Alloc>(
  332. unicode_converter<wchar_t, Char>()(in).c_str(), a);
  333. }
  334. template <typename Char, typename Traits, typename Alloc>
  335. static std::basic_string<Char, Traits, Alloc> to(wchar_t in, Alloc const& a)
  336. {
  337. return std::basic_string<Char, Traits, Alloc>(
  338. unicode_converter<wchar_t, Char>()(in).c_str(), a);
  339. }
  340. template <typename Char>
  341. static std::basic_string<Char> to(std::wstring const& in)
  342. {
  343. return std::basic_string<Char>(unicode_converter<wchar_t, Char>()(in));
  344. }
  345. template <typename Char>
  346. static std::basic_string<Char> to(wchar_t const* in)
  347. {
  348. return std::basic_string<Char>(unicode_converter<wchar_t, Char>()(in));
  349. }
  350. template <typename Char>
  351. static std::basic_string<Char> to(wchar_t in)
  352. {
  353. return std::basic_string<Char>(unicode_converter<wchar_t, Char>()(in));
  354. }
  355. };
  356. template <typename T, typename = void>
  357. struct source_traits
  358. {
  359. };
  360. template <typename T, std::size_t N>
  361. struct source_traits<T[N]>
  362. {
  363. using value_type = T;
  364. };
  365. template <typename Char, typename Traits, typename Alloc>
  366. struct source_traits<std::basic_string<Char, Traits, Alloc>>
  367. {
  368. using value_type =
  369. typename std::basic_string<Char, Traits, Alloc>::value_type;
  370. };
  371. template <>
  372. struct source_traits<cm::string_view>
  373. {
  374. using value_type = cm::string_view::value_type;
  375. };
  376. # if CM_FILESYSTEM_SOURCE_TRAITS_ITERATOR
  377. template <typename T>
  378. struct source_traits<T, cm::enable_if_t<cm::is_iterator<T>::value, void>>
  379. {
  380. using value_type =
  381. typename std::iterator_traits<typename std::decay<T>::type>::value_type;
  382. };
  383. # endif
  384. template <typename In, typename Out>
  385. struct source_converter
  386. {
  387. };
  388. template <>
  389. struct source_converter<char, char>
  390. {
  391. template <typename Iterator>
  392. static void append_range(std::string& p, Iterator b, Iterator e)
  393. {
  394. if (b == e) {
  395. return;
  396. }
  397. p.append(b, e);
  398. }
  399. template <typename Iterator>
  400. static void append_range(std::string& p, Iterator b)
  401. {
  402. char e = '\0';
  403. if (*b == e) {
  404. return;
  405. }
  406. for (; *b != e; ++b) {
  407. p.push_back(*b);
  408. }
  409. }
  410. static void append_source(std::string& p, cm::string_view const s)
  411. {
  412. append_range(p, s.begin(), s.end());
  413. }
  414. template <typename Traits, typename Alloc>
  415. static void append_source(std::string& p,
  416. std::basic_string<char, Traits, Alloc> const& s)
  417. {
  418. append_range(p, s.begin(), s.end());
  419. }
  420. template <typename Source>
  421. static void append_source(std::string& p, Source const& s)
  422. {
  423. append_range(p, s);
  424. }
  425. static void set_source(std::string& p, std::string&& s) { p = std::move(s); }
  426. };
  427. template <>
  428. struct source_converter<wchar_t, char>
  429. {
  430. template <typename Iterator>
  431. static void append_range(std::string& p, Iterator b, Iterator e)
  432. {
  433. if (b == e) {
  434. return;
  435. }
  436. std::wstring tmp(b, e);
  437. std::string dest = string_converter<wchar_t>::to<char>(tmp);
  438. p.append(dest.begin(), dest.end());
  439. }
  440. template <typename Iterator>
  441. static void append_range(std::string& p, Iterator b)
  442. {
  443. wchar_t e = '\0';
  444. if (*b == e) {
  445. return;
  446. }
  447. std::wstring tmp;
  448. for (; *b != e; ++b) {
  449. tmp.push_back(*b);
  450. }
  451. std::string dest = string_converter<wchar_t>::to<char>(tmp);
  452. p.append(dest.begin(), dest.end());
  453. }
  454. template <typename Traits, typename Alloc>
  455. static void append_source(std::string& p,
  456. std::basic_string<wchar_t, Traits, Alloc> const& s)
  457. {
  458. append_range(p, s.begin(), s.end());
  459. }
  460. template <typename Source>
  461. static void append_source(std::string& p, Source const& s)
  462. {
  463. append_range(p, s);
  464. }
  465. static void set_source(std::string& p, std::wstring&& s)
  466. {
  467. p = string_converter<wchar_t>::to<char>(s);
  468. }
  469. };
  470. template <typename T>
  471. struct is_pathable_string : std::false_type
  472. {
  473. };
  474. template <typename Traits, typename Alloc>
  475. struct is_pathable_string<std::basic_string<char, Traits, Alloc>>
  476. : std::true_type
  477. {
  478. };
  479. template <typename Traits, typename Alloc>
  480. struct is_pathable_string<std::basic_string<wchar_t, Traits, Alloc>>
  481. : std::true_type
  482. {
  483. };
  484. template <>
  485. struct is_pathable_string<cm::string_view> : std::true_type
  486. {
  487. };
  488. template <typename T, typename = void>
  489. struct is_pathable_char_array : std::false_type
  490. {
  491. };
  492. template <typename T>
  493. struct is_pathable_char_array<
  494. T,
  495. cm::enable_if_t<
  496. std::is_same<char*, typename std::decay<T>::type>::value ||
  497. std::is_same<wchar_t*, typename std::decay<T>::type>::value,
  498. void>>
  499. : bool_constant<std::is_same<char*, typename std::decay<T>::type>::value ||
  500. std::is_same<wchar_t*, typename std::decay<T>::type>::value>
  501. {
  502. };
  503. template <typename T, typename = void>
  504. struct is_pathable_iterator : std::false_type
  505. {
  506. };
  507. template <typename T>
  508. struct is_pathable_iterator<
  509. T,
  510. cm::enable_if_t<
  511. is_input_iterator<T>::value &&
  512. (std::is_same<char,
  513. typename std::iterator_traits<
  514. typename std::decay<T>::type>::value_type>::value ||
  515. std::is_same<wchar_t,
  516. typename std::iterator_traits<
  517. typename std::decay<T>::type>::value_type>::value),
  518. void>>
  519. : bool_constant<
  520. std::is_same<char,
  521. typename std::iterator_traits<
  522. typename std::decay<T>::type>::value_type>::value ||
  523. std::is_same<wchar_t,
  524. typename std::iterator_traits<
  525. typename std::decay<T>::type>::value_type>::value>
  526. {
  527. };
  528. # if defined(__SUNPRO_CC) && defined(__sparc)
  529. // Oracle DeveloperStudio C++ compiler on Solaris/Sparc fails to compile
  530. // the full 'is_pathable' check. We use it only to improve error messages
  531. // via 'enable_if' when calling methods with incorrect types. Just
  532. // pretend all types are allowed so we can at least compile valid code.
  533. template <typename T>
  534. struct is_pathable : std::true_type
  535. {
  536. };
  537. # else
  538. template <typename T>
  539. struct is_pathable
  540. : bool_constant<is_pathable_string<T>::value ||
  541. is_pathable_char_array<T>::value ||
  542. is_pathable_iterator<T>::value>
  543. {
  544. };
  545. # endif
  546. }
  547. class path
  548. {
  549. using path_type = std::string;
  550. template <typename Source>
  551. using enable_if_pathable =
  552. enable_if_t<internals::is_pathable<Source>::value, path&>;
  553. enum class filename_fragment : unsigned char
  554. {
  555. stem,
  556. extension
  557. };
  558. public:
  559. # if defined(_WIN32) && !defined(__CYGWIN__)
  560. using value_type = wchar_t;
  561. # else
  562. using value_type = char;
  563. # endif
  564. using string_type = std::basic_string<value_type>;
  565. class iterator;
  566. using const_iterator = iterator;
  567. enum format : unsigned char
  568. {
  569. auto_format,
  570. native_format,
  571. generic_format
  572. };
  573. # if defined(_WIN32) && !defined(__CYGWIN__)
  574. static constexpr value_type preferred_separator = L'\\';
  575. # else
  576. static constexpr value_type preferred_separator = '/';
  577. # endif
  578. // Constructors
  579. // ============
  580. path() noexcept {}
  581. path(path const& p)
  582. : path_(p.path_)
  583. {
  584. }
  585. path(path&& p) noexcept
  586. : path_(std::move(p.path_))
  587. {
  588. }
  589. path(string_type&& source, format fmt = auto_format)
  590. {
  591. (void)fmt;
  592. internals::source_converter<value_type, path_type::value_type>::set_source(
  593. this->path_, std::move(source));
  594. }
  595. template <typename Source, typename = enable_if_pathable<Source>>
  596. path(Source const& source, format fmt = auto_format)
  597. {
  598. (void)fmt;
  599. internals::source_converter<
  600. typename internals::source_traits<Source>::value_type,
  601. path_type::value_type>::append_source(this->path_, source);
  602. }
  603. template <typename Iterator, typename = enable_if_pathable<Iterator>>
  604. path(Iterator const first, Iterator last, format fmt = auto_format)
  605. {
  606. (void)fmt;
  607. internals::source_converter<
  608. typename std::iterator_traits<Iterator>::value_type,
  609. path_type::value_type>::append_range(this->path_, first, last);
  610. }
  611. ~path() = default;
  612. // Assignments
  613. // ===========
  614. path& operator=(path const& p)
  615. {
  616. if (this != &p) {
  617. this->path_ = p.path_;
  618. }
  619. return *this;
  620. }
  621. path& operator=(path&& p) noexcept
  622. {
  623. if (this != &p) {
  624. this->path_ = std::move(p.path_);
  625. }
  626. return *this;
  627. }
  628. path& operator=(string_type&& source) { return this->assign(source); }
  629. template <typename Source, typename = enable_if_pathable<Source>>
  630. path& operator=(Source const& source)
  631. {
  632. return this->assign(source);
  633. }
  634. path& assign(string_type&& source)
  635. {
  636. internals::source_converter<value_type, path_type::value_type>::set_source(
  637. this->path_, std::move(source));
  638. return *this;
  639. }
  640. template <typename Source, typename = enable_if_pathable<Source>>
  641. path& assign(Source const& source)
  642. {
  643. this->path_.clear();
  644. internals::source_converter<
  645. typename internals::source_traits<Source>::value_type,
  646. path_type::value_type>::append_source(this->path_, source);
  647. return *this;
  648. }
  649. template <typename Iterator, typename = enable_if_pathable<Iterator>>
  650. path& assign(Iterator first, Iterator last)
  651. {
  652. this->path_.clear();
  653. internals::source_converter<
  654. typename std::iterator_traits<Iterator>::value_type,
  655. path_type::value_type>::append_range(this->path_, first, last);
  656. return *this;
  657. }
  658. // Concatenation
  659. // =============
  660. path& operator/=(path const& p);
  661. template <typename Source, typename = enable_if_pathable<Source>>
  662. path& append(Source const& source)
  663. {
  664. return this->operator/=(path(source));
  665. }
  666. template <typename Source>
  667. path& operator/=(Source const& source)
  668. {
  669. return this->append(source);
  670. }
  671. template <typename Iterator, typename = enable_if_pathable<Iterator>>
  672. path& append(Iterator first, Iterator last)
  673. {
  674. return this->operator/=(path(first, last));
  675. }
  676. path& operator+=(path const& p)
  677. {
  678. this->path_ += p.path_;
  679. return *this;
  680. }
  681. path& operator+=(string_type const& str)
  682. {
  683. this->path_ +=
  684. internals::string_converter<value_type>::to<path_type::value_type>(str);
  685. return *this;
  686. }
  687. path& operator+=(cm::string_view str)
  688. {
  689. this->path_.append(str.begin(), str.end());
  690. return *this;
  691. }
  692. path& operator+=(value_type const* str)
  693. {
  694. this->path_ +=
  695. internals::string_converter<value_type>::to<path_type::value_type>(str);
  696. return *this;
  697. }
  698. path& operator+=(value_type const c)
  699. {
  700. this->path_ +=
  701. internals::string_converter<value_type>::to<path_type::value_type>(c);
  702. return *this;
  703. }
  704. template <typename Source, typename = enable_if_pathable<Source>>
  705. path& concat(Source const& source)
  706. {
  707. internals::source_converter<
  708. typename internals::source_traits<Source>::value_type,
  709. path_type::value_type>::append_source(this->path_, source);
  710. return *this;
  711. }
  712. template <typename Source>
  713. path& operator+=(Source const& source)
  714. {
  715. return this->concat(source);
  716. }
  717. template <typename Iterator, typename = enable_if_pathable<Iterator>>
  718. path& concat(Iterator first, Iterator last)
  719. {
  720. internals::source_converter<
  721. typename std::iterator_traits<Iterator>::value_type,
  722. path_type::value_type>::append_range(this->path_, first, last);
  723. return *this;
  724. }
  725. // Modifiers
  726. // =========
  727. void clear() noexcept { this->path_.clear(); }
  728. path& make_preferred()
  729. {
  730. # if defined(_WIN32) && !defined(__CYGWIN__)
  731. std::replace(
  732. this->path_.begin(), this->path_.end(), '/',
  733. static_cast<path_type::value_type>(this->preferred_separator));
  734. # endif
  735. return *this;
  736. }
  737. path& remove_filename()
  738. {
  739. auto fname = this->get_filename();
  740. if (!fname.empty()) {
  741. this->path_.erase(fname.data() -
  742. // Avoid C++17 non-const .data() that may reallocate.
  743. static_cast<path_type const&>(this->path_).data());
  744. }
  745. return *this;
  746. }
  747. path& replace_filename(path const& replacement)
  748. {
  749. this->remove_filename();
  750. this->operator/=(replacement);
  751. return *this;
  752. }
  753. path& replace_extension(path const& replacement = path())
  754. {
  755. auto ext = this->get_filename_fragment(filename_fragment::extension);
  756. if (!ext.empty()) {
  757. this->path_.erase(ext.data() -
  758. // Avoid C++17 non-const .data() that may reallocate.
  759. static_cast<path_type const&>(this->path_).data());
  760. }
  761. if (!replacement.path_.empty()) {
  762. if (replacement.path_[0] != '.') {
  763. this->path_ += '.';
  764. }
  765. this->path_.append(replacement.path_);
  766. }
  767. return *this;
  768. }
  769. void swap(path& other) noexcept { this->path_.swap(other.path_); }
  770. // Format observers
  771. // ================
  772. string_type const& native() const noexcept
  773. {
  774. # if defined(_WIN32) && !defined(__CYGWIN__)
  775. this->native_path_ = internals::string_converter<
  776. path_type::value_type>::to<string_type::value_type>(this->path_);
  777. return this->native_path_;
  778. # else
  779. return this->path_;
  780. # endif
  781. }
  782. value_type const* c_str() const noexcept { return this->native().c_str(); }
  783. operator string_type() const { return this->native(); }
  784. template <
  785. typename Char, typename Traits = std::char_traits<Char>,
  786. typename Alloc = std::allocator<Char>,
  787. cm::enable_if_t<(std::is_same<Char, char>::value &&
  788. std::is_same<Traits, std::char_traits<char>>::value) ||
  789. (std::is_same<Char, wchar_t>::value &&
  790. std::is_same<Traits, std::char_traits<wchar_t>>::value),
  791. int> = 1>
  792. std::basic_string<Char, Traits, Alloc> string(Alloc const& a = Alloc()) const
  793. {
  794. return internals::string_converter<path_type::value_type>::to<Char, Traits,
  795. Alloc>(
  796. this->path_, a);
  797. }
  798. std::string const string() const { return this->path_; }
  799. std::wstring wstring() const
  800. {
  801. std::string out = this->string();
  802. return internals::string_converter<path_type::value_type>::to<
  803. std::wstring::value_type>(out);
  804. }
  805. template <
  806. typename Char, typename Traits = std::char_traits<Char>,
  807. typename Alloc = std::allocator<Char>,
  808. cm::enable_if_t<(std::is_same<Char, char>::value &&
  809. std::is_same<Traits, std::char_traits<char>>::value) ||
  810. (std::is_same<Char, wchar_t>::value &&
  811. std::is_same<Traits, std::char_traits<wchar_t>>::value),
  812. int> = 1>
  813. std::basic_string<Char, Traits, Alloc> generic_string(
  814. Alloc const& a = Alloc()) const
  815. {
  816. return internals::string_converter<path_type::value_type>::to<Char, Traits,
  817. Alloc>(
  818. this->get_generic(), a);
  819. }
  820. std::string generic_string() const { return this->get_generic(); }
  821. std::wstring generic_wstring() const
  822. {
  823. auto dest = this->generic_string();
  824. return internals::string_converter<path_type::value_type>::to<
  825. std::wstring::value_type>(dest);
  826. }
  827. // Compare
  828. // =======
  829. int compare(path const& p) const noexcept
  830. {
  831. return this->compare_path(p.path_);
  832. }
  833. int compare(string_type const& str) const
  834. {
  835. return this->compare_path(
  836. internals::string_converter<value_type>::to<path_type::value_type>(str));
  837. }
  838. int compare(value_type const* str) const
  839. {
  840. return this->compare_path(
  841. internals::string_converter<value_type>::to<path_type::value_type>(str));
  842. }
  843. int compare(cm::string_view str) const { return this->compare_path(str); }
  844. // Generation
  845. // ==========
  846. path lexically_normal() const;
  847. path lexically_relative(path const& base) const;
  848. path lexically_proximate(path const& base) const
  849. {
  850. path result = this->lexically_relative(base);
  851. return result.empty() ? *this : result;
  852. }
  853. // Decomposition
  854. // =============
  855. path root_name() const { return get_root_name(); }
  856. path root_directory() const { return this->get_root_directory(); }
  857. path root_path() const
  858. {
  859. return this->root_name().append(this->get_root_directory());
  860. }
  861. path relative_path() const { return this->get_relative_path(); }
  862. path parent_path() const { return this->get_parent_path(); }
  863. path filename() const { return this->get_filename(); }
  864. path stem() const
  865. {
  866. return this->get_filename_fragment(filename_fragment::stem);
  867. }
  868. path extension() const
  869. {
  870. return this->get_filename_fragment(filename_fragment::extension);
  871. }
  872. // Queries
  873. // =======
  874. bool empty() const noexcept { return this->path_.empty(); }
  875. bool has_root_name() const { return !this->get_root_name().empty(); }
  876. bool has_root_directory() const
  877. {
  878. return !this->get_root_directory().empty();
  879. }
  880. bool has_root_path() const
  881. {
  882. return this->has_root_name() || this->has_root_directory();
  883. }
  884. bool has_relative_path() const { return !this->get_relative_path().empty(); }
  885. bool has_parent_path() const { return !this->get_parent_path().empty(); }
  886. bool has_filename() const { return !this->get_filename().empty(); }
  887. bool has_stem() const
  888. {
  889. return !this->get_filename_fragment(filename_fragment::stem).empty();
  890. }
  891. bool has_extension() const
  892. {
  893. return !this->get_filename_fragment(filename_fragment::extension).empty();
  894. }
  895. bool is_absolute() const
  896. {
  897. # if defined(_WIN32) && !defined(__CYGWIN__)
  898. return this->has_root_name() && this->has_root_directory();
  899. # else
  900. // For CYGWIN, root_name (i.e. //host or /cygdrive/x) is not considered.
  901. // Same as current GNU g++ implementation (9.3).
  902. return this->has_root_directory();
  903. # endif
  904. }
  905. bool is_relative() const { return !this->is_absolute(); }
  906. // Iterators
  907. // =========
  908. inline iterator begin() const;
  909. inline iterator end() const;
  910. // Non-members
  911. // ===========
  912. friend inline bool operator==(path const& lhs, path const& rhs) noexcept
  913. {
  914. return lhs.compare(rhs) == 0;
  915. }
  916. friend inline bool operator!=(path const& lhs, path const& rhs) noexcept
  917. {
  918. return lhs.compare(rhs) != 0;
  919. }
  920. friend inline bool operator<(path const& lhs, path const& rhs) noexcept
  921. {
  922. return lhs.compare(rhs) < 0;
  923. }
  924. friend inline bool operator<=(path const& lhs, path const& rhs) noexcept
  925. {
  926. return lhs.compare(rhs) <= 0;
  927. }
  928. friend inline bool operator>(path const& lhs, path const& rhs) noexcept
  929. {
  930. return lhs.compare(rhs) > 0;
  931. }
  932. friend inline bool operator>=(path const& lhs, path const& rhs) noexcept
  933. {
  934. return lhs.compare(rhs) >= 0;
  935. }
  936. friend inline path operator/(path const& lhs, path const& rhs)
  937. {
  938. path result(lhs);
  939. result /= rhs;
  940. return result;
  941. }
  942. template <typename Char, typename Traits>
  943. friend inline cm::enable_if_t<
  944. (std::is_same<Char, path::value_type>::value &&
  945. std::is_same<Traits, std::char_traits<path::value_type>>::value) ||
  946. (std::is_same<Char, path::path_type::value_type>::value &&
  947. std::is_same<Traits,
  948. std::char_traits<path::path_type::value_type>>::value),
  949. std::basic_ostream<Char, Traits>&>
  950. operator<<(std::basic_ostream<Char, Traits>& os, path const& p)
  951. {
  952. os << cm::quoted(p.string<Char, Traits>());
  953. return os;
  954. }
  955. template <typename Char, typename Traits>
  956. friend inline cm::enable_if_t<
  957. (std::is_same<Char, path::value_type>::value &&
  958. std::is_same<Traits, std::char_traits<path::value_type>>::value) ||
  959. (std::is_same<Char, path::path_type::value_type>::value &&
  960. std::is_same<Traits,
  961. std::char_traits<path::path_type::value_type>>::value),
  962. std::basic_istream<Char, Traits>&>
  963. operator>>(std::basic_istream<Char, Traits>& is, path& p)
  964. {
  965. std::basic_string<Char, Traits> tmp;
  966. is >> cm::quoted(tmp);
  967. p = tmp;
  968. return is;
  969. }
  970. private:
  971. friend class iterator;
  972. friend std::size_t hash_value(path const& p) noexcept;
  973. path_type get_generic() const;
  974. cm::string_view get_root_name() const;
  975. cm::string_view get_root_directory() const;
  976. cm::string_view get_relative_path() const;
  977. cm::string_view get_parent_path() const;
  978. cm::string_view get_filename() const;
  979. cm::string_view get_filename_fragment(filename_fragment fragment) const;
  980. int compare_path(cm::string_view str) const;
  981. path_type path_;
  982. # if defined(_WIN32) && !defined(__CYGWIN__)
  983. mutable string_type native_path_;
  984. # endif
  985. };
  986. class path::iterator
  987. {
  988. public:
  989. using iterator_category = std::bidirectional_iterator_tag;
  990. using value_type = path;
  991. using difference_type = std::ptrdiff_t;
  992. using pointer = path const*;
  993. using reference = path const&;
  994. iterator();
  995. iterator(iterator const& other);
  996. ~iterator();
  997. iterator& operator=(iterator const& other);
  998. reference operator*() const { return this->path_element_; }
  999. pointer operator->() const { return &this->path_element_; }
  1000. iterator& operator++();
  1001. iterator operator++(int)
  1002. {
  1003. iterator it(*this);
  1004. this->operator++();
  1005. return it;
  1006. }
  1007. iterator& operator--();
  1008. iterator operator--(int)
  1009. {
  1010. iterator it(*this);
  1011. this->operator--();
  1012. return it;
  1013. }
  1014. private:
  1015. friend class path;
  1016. friend bool operator==(iterator const&, iterator const&);
  1017. iterator(path const* p, bool at_end = false);
  1018. path const* path_;
  1019. std::unique_ptr<internals::path_parser> parser_;
  1020. path path_element_;
  1021. };
  1022. inline path::iterator path::begin() const
  1023. {
  1024. return iterator(this);
  1025. }
  1026. inline path::iterator path::end() const
  1027. {
  1028. return iterator(this, true);
  1029. }
  1030. // Non-member functions
  1031. // ====================
  1032. bool operator==(path::iterator const& lhs, path::iterator const& rhs);
  1033. inline bool operator!=(path::iterator const& lhs, path::iterator const& rhs)
  1034. {
  1035. return !(lhs == rhs);
  1036. }
  1037. inline void swap(path& lhs, path& rhs) noexcept
  1038. {
  1039. lhs.swap(rhs);
  1040. }
  1041. std::size_t hash_value(path const& p) noexcept;
  1042. #endif
  1043. } // namespace filesystem
  1044. } // namespace cm