1
0

filesystem 31 KB

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