1
0

filesystem 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. // -*-c++-*-
  2. // vim: set ft=cpp:
  3. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. file Copyright.txt 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 const utf8_state s_start = 0;
  44. static const utf8_state 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(const utf8_state state, const std::uint8_t 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(const std::wstring& 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(const std::string& 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(const std::wstring& 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(const std::string& 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()(const std::string& in)
  227. {
  228. return unicode<wchar_t>::from_utf8(in);
  229. }
  230. std::wstring operator()(const char* 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()(const std::wstring& in)
  241. {
  242. return unicode<wchar_t>::to_utf8(in);
  243. }
  244. std::string operator()(const wchar_t* 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()(const std::string& in) { return in; }
  255. std::string operator()(const char* 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()(const std::wstring& in) { return in; }
  263. std::wstring operator()(const wchar_t* 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(const std::string& in,
  279. const Alloc& 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(const char* in,
  286. const Alloc& 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, const Alloc& 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(const std::string& 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(const char* 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(const std::wstring& in,
  322. const Alloc& 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(const wchar_t* in,
  329. const Alloc& 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, const Alloc& 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(const std::wstring& 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(const wchar_t* 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, const cm::string_view 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. const std::basic_string<char, Traits, Alloc>& s)
  417. {
  418. append_range(p, s.begin(), s.end());
  419. }
  420. template <typename Source>
  421. static void append_source(std::string& p, const Source& 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. const std::basic_string<wchar_t, Traits, Alloc>& s)
  457. {
  458. append_range(p, s.begin(), s.end());
  459. }
  460. template <typename Source>
  461. static void append_source(std::string& p, const Source& 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(const path& 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(const Source& 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(const Iterator 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=(const path& 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=(const Source& 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(const Source& 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/=(const path& p);
  661. template <typename Source, typename = enable_if_pathable<Source>>
  662. path& append(const Source& source)
  663. {
  664. return this->operator/=(path(source));
  665. }
  666. template <typename Source>
  667. path& operator/=(const Source& 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+=(const path& p)
  677. {
  678. this->path_ += p.path_;
  679. return *this;
  680. }
  681. path& operator+=(const string_type& 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+=(const value_type* str)
  693. {
  694. this->path_ +=
  695. internals::string_converter<value_type>::to<path_type::value_type>(str);
  696. return *this;
  697. }
  698. path& operator+=(const value_type 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(const Source& 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+=(const Source& 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. # if defined(__CYGWIN__)
  740. // FIXME: Avoid crash due to CYGWIN/MSYS bug(?). See CMake Issue 22090.
  741. static_cast<void>(this->path_.data());
  742. # endif
  743. auto fname = this->get_filename();
  744. if (!fname.empty()) {
  745. this->path_.erase(fname.data() - this->path_.data());
  746. }
  747. return *this;
  748. }
  749. path& replace_filename(const path& replacement)
  750. {
  751. this->remove_filename();
  752. this->operator/=(replacement);
  753. return *this;
  754. }
  755. path& replace_extension(const path& replacement = path())
  756. {
  757. # if defined(__CYGWIN__)
  758. // FIXME: Avoid crash due to CYGWIN/MSYS bug(?). See CMake Issue 22090.
  759. static_cast<void>(this->path_.data());
  760. # endif
  761. auto ext = this->get_filename_fragment(filename_fragment::extension);
  762. if (!ext.empty()) {
  763. this->path_.erase(ext.data() - this->path_.data());
  764. }
  765. if (!replacement.path_.empty()) {
  766. if (replacement.path_[0] != '.') {
  767. this->path_ += '.';
  768. }
  769. this->path_.append(replacement.path_);
  770. }
  771. return *this;
  772. }
  773. void swap(path& other) noexcept { this->path_.swap(other.path_); }
  774. // Format observers
  775. // ================
  776. const string_type& native() const noexcept
  777. {
  778. # if defined(_WIN32) && !defined(__CYGWIN__)
  779. this->native_path_ = internals::string_converter<
  780. path_type::value_type>::to<string_type::value_type>(this->path_);
  781. return this->native_path_;
  782. # else
  783. return this->path_;
  784. # endif
  785. }
  786. const value_type* c_str() const noexcept { return this->native().c_str(); }
  787. operator string_type() const { return this->native(); }
  788. template <
  789. typename Char, typename Traits = std::char_traits<Char>,
  790. typename Alloc = std::allocator<Char>,
  791. cm::enable_if_t<(std::is_same<Char, char>::value &&
  792. std::is_same<Traits, std::char_traits<char>>::value) ||
  793. (std::is_same<Char, wchar_t>::value &&
  794. std::is_same<Traits, std::char_traits<wchar_t>>::value),
  795. int> = 1>
  796. std::basic_string<Char, Traits, Alloc> string(const Alloc& a = Alloc()) const
  797. {
  798. return internals::string_converter<path_type::value_type>::to<Char, Traits,
  799. Alloc>(
  800. this->path_, a);
  801. }
  802. const std::string string() const { return this->path_; }
  803. std::wstring wstring() const
  804. {
  805. std::string out = this->string();
  806. return internals::string_converter<path_type::value_type>::to<
  807. std::wstring::value_type>(out);
  808. }
  809. template <
  810. typename Char, typename Traits = std::char_traits<Char>,
  811. typename Alloc = std::allocator<Char>,
  812. cm::enable_if_t<(std::is_same<Char, char>::value &&
  813. std::is_same<Traits, std::char_traits<char>>::value) ||
  814. (std::is_same<Char, wchar_t>::value &&
  815. std::is_same<Traits, std::char_traits<wchar_t>>::value),
  816. int> = 1>
  817. std::basic_string<Char, Traits, Alloc> generic_string(
  818. const Alloc& a = Alloc()) const
  819. {
  820. return internals::string_converter<path_type::value_type>::to<Char, Traits,
  821. Alloc>(
  822. this->get_generic(), a);
  823. }
  824. std::string generic_string() const { return this->get_generic(); }
  825. std::wstring generic_wstring() const
  826. {
  827. auto dest = this->generic_string();
  828. return internals::string_converter<path_type::value_type>::to<
  829. std::wstring::value_type>(dest);
  830. }
  831. // Compare
  832. // =======
  833. int compare(const path& p) const noexcept
  834. {
  835. return this->compare_path(p.path_);
  836. }
  837. int compare(const string_type& str) const
  838. {
  839. return this->compare_path(
  840. internals::string_converter<value_type>::to<path_type::value_type>(str));
  841. }
  842. int compare(const value_type* str) const
  843. {
  844. return this->compare_path(
  845. internals::string_converter<value_type>::to<path_type::value_type>(str));
  846. }
  847. int compare(cm::string_view str) const { return this->compare_path(str); }
  848. // Generation
  849. // ==========
  850. path lexically_normal() const;
  851. path lexically_relative(const path& base) const;
  852. path lexically_proximate(const path& base) const
  853. {
  854. path result = this->lexically_relative(base);
  855. return result.empty() ? *this : result;
  856. }
  857. // Decomposition
  858. // =============
  859. path root_name() const { return get_root_name(); }
  860. path root_directory() const { return this->get_root_directory(); }
  861. path root_path() const
  862. {
  863. return this->root_name().append(this->get_root_directory());
  864. }
  865. path relative_path() const { return this->get_relative_path(); }
  866. path parent_path() const { return this->get_parent_path(); }
  867. path filename() const { return this->get_filename(); }
  868. path stem() const
  869. {
  870. return this->get_filename_fragment(filename_fragment::stem);
  871. }
  872. path extension() const
  873. {
  874. return this->get_filename_fragment(filename_fragment::extension);
  875. }
  876. // Queries
  877. // =======
  878. bool empty() const noexcept { return this->path_.empty(); }
  879. bool has_root_name() const { return !this->get_root_name().empty(); }
  880. bool has_root_directory() const
  881. {
  882. return !this->get_root_directory().empty();
  883. }
  884. bool has_root_path() const
  885. {
  886. return this->has_root_name() || this->has_root_directory();
  887. }
  888. bool has_relative_path() const { return !this->get_relative_path().empty(); }
  889. bool has_parent_path() const { return !this->get_parent_path().empty(); }
  890. bool has_filename() const { return !this->get_filename().empty(); }
  891. bool has_stem() const
  892. {
  893. return !this->get_filename_fragment(filename_fragment::stem).empty();
  894. }
  895. bool has_extension() const
  896. {
  897. return !this->get_filename_fragment(filename_fragment::extension).empty();
  898. }
  899. bool is_absolute() const
  900. {
  901. # if defined(_WIN32) && !defined(__CYGWIN__)
  902. return this->has_root_name() && this->has_root_directory();
  903. # else
  904. // For CYGWIN, root_name (i.e. //host or /cygdrive/x) is not considered.
  905. // Same as current GNU g++ implementation (9.3).
  906. return this->has_root_directory();
  907. # endif
  908. }
  909. bool is_relative() const { return !this->is_absolute(); }
  910. // Iterators
  911. // =========
  912. inline iterator begin() const;
  913. inline iterator end() const;
  914. // Non-members
  915. // ===========
  916. friend inline bool operator==(const path& lhs, const path& rhs) noexcept
  917. {
  918. return lhs.compare(rhs) == 0;
  919. }
  920. friend inline bool operator!=(const path& lhs, const path& rhs) noexcept
  921. {
  922. return lhs.compare(rhs) != 0;
  923. }
  924. friend inline bool operator<(const path& lhs, const path& rhs) noexcept
  925. {
  926. return lhs.compare(rhs) < 0;
  927. }
  928. friend inline bool operator<=(const path& lhs, const path& rhs) noexcept
  929. {
  930. return lhs.compare(rhs) <= 0;
  931. }
  932. friend inline bool operator>(const path& lhs, const path& rhs) noexcept
  933. {
  934. return lhs.compare(rhs) > 0;
  935. }
  936. friend inline bool operator>=(const path& lhs, const path& rhs) noexcept
  937. {
  938. return lhs.compare(rhs) >= 0;
  939. }
  940. friend inline path operator/(const path& lhs, const path& rhs)
  941. {
  942. path result(lhs);
  943. result /= rhs;
  944. return result;
  945. }
  946. template <typename Char, typename Traits>
  947. friend inline cm::enable_if_t<
  948. (std::is_same<Char, path::value_type>::value &&
  949. std::is_same<Traits, std::char_traits<path::value_type>>::value) ||
  950. (std::is_same<Char, path::path_type::value_type>::value &&
  951. std::is_same<Traits,
  952. std::char_traits<path::path_type::value_type>>::value),
  953. std::basic_ostream<Char, Traits>&>
  954. operator<<(std::basic_ostream<Char, Traits>& os, const path& p)
  955. {
  956. os << cm::quoted(p.string<Char, Traits>());
  957. return os;
  958. }
  959. template <typename Char, typename Traits>
  960. friend inline cm::enable_if_t<
  961. (std::is_same<Char, path::value_type>::value &&
  962. std::is_same<Traits, std::char_traits<path::value_type>>::value) ||
  963. (std::is_same<Char, path::path_type::value_type>::value &&
  964. std::is_same<Traits,
  965. std::char_traits<path::path_type::value_type>>::value),
  966. std::basic_istream<Char, Traits>&>
  967. operator>>(std::basic_istream<Char, Traits>& is, path& p)
  968. {
  969. std::basic_string<Char, Traits> tmp;
  970. is >> cm::quoted(tmp);
  971. p = tmp;
  972. return is;
  973. }
  974. private:
  975. friend class iterator;
  976. friend std::size_t hash_value(const path& p) noexcept;
  977. path_type get_generic() const;
  978. cm::string_view get_root_name() const;
  979. cm::string_view get_root_directory() const;
  980. cm::string_view get_relative_path() const;
  981. cm::string_view get_parent_path() const;
  982. cm::string_view get_filename() const;
  983. cm::string_view get_filename_fragment(filename_fragment fragment) const;
  984. int compare_path(cm::string_view str) const;
  985. path_type path_;
  986. # if defined(_WIN32) && !defined(__CYGWIN__)
  987. mutable string_type native_path_;
  988. # endif
  989. };
  990. class path::iterator
  991. {
  992. public:
  993. using iterator_category = std::bidirectional_iterator_tag;
  994. using value_type = path;
  995. using difference_type = std::ptrdiff_t;
  996. using pointer = const path*;
  997. using reference = const path&;
  998. iterator();
  999. iterator(const iterator& other);
  1000. ~iterator();
  1001. iterator& operator=(const iterator& other);
  1002. reference operator*() const { return this->path_element_; }
  1003. pointer operator->() const { return &this->path_element_; }
  1004. iterator& operator++();
  1005. iterator operator++(int)
  1006. {
  1007. iterator it(*this);
  1008. this->operator++();
  1009. return it;
  1010. }
  1011. iterator& operator--();
  1012. iterator operator--(int)
  1013. {
  1014. iterator it(*this);
  1015. this->operator--();
  1016. return it;
  1017. }
  1018. private:
  1019. friend class path;
  1020. friend bool operator==(const iterator&, const iterator&);
  1021. iterator(const path* p, bool at_end = false);
  1022. const path* path_;
  1023. std::unique_ptr<internals::path_parser> parser_;
  1024. path path_element_;
  1025. };
  1026. inline path::iterator path::begin() const
  1027. {
  1028. return iterator(this);
  1029. }
  1030. inline path::iterator path::end() const
  1031. {
  1032. return iterator(this, true);
  1033. }
  1034. // Non-member functions
  1035. // ====================
  1036. bool operator==(const path::iterator& lhs, const path::iterator& rhs);
  1037. inline bool operator!=(const path::iterator& lhs, const path::iterator& rhs)
  1038. {
  1039. return !(lhs == rhs);
  1040. }
  1041. inline void swap(path& lhs, path& rhs) noexcept
  1042. {
  1043. lhs.swap(rhs);
  1044. }
  1045. std::size_t hash_value(const path& p) noexcept;
  1046. #endif
  1047. } // namespace filesystem
  1048. } // namespace cm