path.hpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. // filesystem path.hpp ---------------------------------------------------------------//
  2. // Copyright Vladimir Prus 2002
  3. // Copyright Beman Dawes 2002-2005, 2009
  4. // Copyright Andrey Semashev 2021
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. // Library home page: http://www.boost.org/libs/filesystem
  8. // path::stem(), extension(), and replace_extension() are based on
  9. // basename(), extension(), and change_extension() from the original
  10. // filesystem/convenience.hpp header by Vladimir Prus.
  11. #ifndef BOOST_FILESYSTEM_PATH_HPP
  12. #define BOOST_FILESYSTEM_PATH_HPP
  13. #include <boost/assert.hpp>
  14. #include <boost/filesystem/config.hpp>
  15. #include <boost/filesystem/path_traits.hpp> // includes <cwchar>
  16. #include <boost/iterator/iterator_facade.hpp>
  17. #include <boost/iterator/iterator_categories.hpp>
  18. #include <boost/core/enable_if.hpp>
  19. #include <boost/io/quoted.hpp>
  20. #include <boost/functional/hash_fwd.hpp>
  21. #include <boost/type_traits/integral_constant.hpp>
  22. #include <boost/type_traits/is_same.hpp>
  23. #include <boost/type_traits/is_integral.hpp>
  24. #include <boost/type_traits/remove_reference.hpp>
  25. #include <boost/type_traits/remove_cv.hpp>
  26. #include <boost/type_traits/decay.hpp>
  27. #include <cstddef>
  28. #include <cwchar> // for mbstate_t
  29. #include <string>
  30. #include <iosfwd>
  31. #include <iterator>
  32. #include <locale>
  33. #include <utility>
  34. #include <boost/filesystem/detail/header.hpp> // must be the last #include
  35. namespace boost {
  36. namespace filesystem {
  37. class path;
  38. namespace path_detail { // intentionally don't use filesystem::detail to not bring internal Boost.Filesystem functions into ADL via path_constants
  39. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  40. struct path_constants
  41. {
  42. typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
  43. typedef Char value_type;
  44. static BOOST_CONSTEXPR_OR_CONST value_type separator = Separator;
  45. static BOOST_CONSTEXPR_OR_CONST value_type preferred_separator = PreferredSeparator;
  46. static BOOST_CONSTEXPR_OR_CONST value_type dot = Dot;
  47. };
  48. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  49. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  50. BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
  51. path_constants< Char, Separator, PreferredSeparator, Dot >::separator;
  52. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  53. BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
  54. path_constants< Char, Separator, PreferredSeparator, Dot >::preferred_separator;
  55. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  56. BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
  57. path_constants< Char, Separator, PreferredSeparator, Dot >::dot;
  58. #endif
  59. // A struct that denotes a contiguous range of characters in a string. A lightweight alternative to string_view.
  60. struct substring
  61. {
  62. std::size_t pos;
  63. std::size_t size;
  64. };
  65. template< typename T >
  66. struct is_native_char_ptr_impl : public boost::false_type {};
  67. #if defined(BOOST_WINDOWS_API)
  68. template< >
  69. struct is_native_char_ptr_impl< wchar_t* > : public boost::true_type {};
  70. template< >
  71. struct is_native_char_ptr_impl< const wchar_t* > : public boost::true_type {};
  72. #else // defined(BOOST_WINDOWS_API)
  73. template< >
  74. struct is_native_char_ptr_impl< char* > : public boost::true_type {};
  75. template< >
  76. struct is_native_char_ptr_impl< const char* > : public boost::true_type {};
  77. #endif // defined(BOOST_WINDOWS_API)
  78. template< typename T >
  79. struct is_native_char_ptr : public is_native_char_ptr_impl< typename boost::remove_cv< typename boost::remove_reference< T >::type >::type > {};
  80. template< typename T >
  81. struct is_native_pathable_impl : public boost::false_type {};
  82. template< typename T >
  83. struct is_native_pathable_impl< T* > : public is_native_char_ptr_impl< T* > {};
  84. #if defined(BOOST_WINDOWS_API)
  85. template< >
  86. struct is_native_pathable_impl< const wchar_t[] > : public boost::true_type {};
  87. template< std::size_t N >
  88. struct is_native_pathable_impl< const wchar_t[N] > : public boost::true_type {};
  89. template< >
  90. struct is_native_pathable_impl< std::basic_string< wchar_t > > : public boost::true_type {};
  91. #else // defined(BOOST_WINDOWS_API)
  92. template< >
  93. struct is_native_pathable_impl< const char[] > : public boost::true_type {};
  94. template< std::size_t N >
  95. struct is_native_pathable_impl< const char[N] > : public boost::true_type {};
  96. template< >
  97. struct is_native_pathable_impl< std::basic_string< char > > : public boost::true_type {};
  98. #endif // defined(BOOST_WINDOWS_API)
  99. template< >
  100. struct is_native_pathable_impl< filesystem::path > : public boost::true_type {};
  101. template< typename T >
  102. struct is_native_pathable : public is_native_pathable_impl< typename boost::remove_cv< typename boost::remove_reference< T >::type >::type > {};
  103. } // namespace path_detail
  104. //------------------------------------------------------------------------------------//
  105. // //
  106. // class path //
  107. // //
  108. //------------------------------------------------------------------------------------//
  109. class path :
  110. public filesystem::path_detail::path_constants<
  111. #ifdef BOOST_WINDOWS_API
  112. wchar_t, L'/', L'\\', L'.'
  113. #else
  114. char, '/', '/', '.'
  115. #endif
  116. >
  117. {
  118. public:
  119. // value_type is the character type used by the operating system API to
  120. // represent paths.
  121. typedef path_constants_base::value_type value_type;
  122. typedef std::basic_string< value_type > string_type;
  123. typedef std::codecvt< wchar_t, char, std::mbstate_t > codecvt_type;
  124. // ----- character encoding conversions -----
  125. // Following the principle of least astonishment, path input arguments
  126. // passed to or obtained from the operating system via objects of
  127. // class path behave as if they were directly passed to or
  128. // obtained from the O/S API, unless conversion is explicitly requested.
  129. //
  130. // POSIX specfies that path strings are passed unchanged to and from the
  131. // API. Note that this is different from the POSIX command line utilities,
  132. // which convert according to a locale.
  133. //
  134. // Thus for POSIX, char strings do not undergo conversion. wchar_t strings
  135. // are converted to/from char using the path locale or, if a conversion
  136. // argument is given, using a conversion object modeled on
  137. // std::wstring_convert.
  138. //
  139. // The path locale, which is global to the thread, can be changed by the
  140. // imbue() function. It is initialized to an implementation defined locale.
  141. //
  142. // For Windows, wchar_t strings do not undergo conversion. char strings
  143. // are converted using the "ANSI" or "OEM" code pages, as determined by
  144. // the AreFileApisANSI() function, or, if a conversion argument is given,
  145. // using a conversion object modeled on std::wstring_convert.
  146. //
  147. // See m_pathname comments for further important rationale.
  148. // TODO: rules needed for operating systems that use / or .
  149. // differently, or format directory paths differently from file paths.
  150. //
  151. // **********************************************************************************
  152. //
  153. // More work needed: How to handle an operating system that may have
  154. // slash characters or dot characters in valid filenames, either because
  155. // it doesn't follow the POSIX standard, or because it allows MBCS
  156. // filename encodings that may contain slash or dot characters. For
  157. // example, ISO/IEC 2022 (JIS) encoding which allows switching to
  158. // JIS x0208-1983 encoding. A valid filename in this set of encodings is
  159. // 0x1B 0x24 0x42 [switch to X0208-1983] 0x24 0x2F [U+304F Kiragana letter KU]
  160. // ^^^^
  161. // Note that 0x2F is the ASCII slash character
  162. //
  163. // **********************************************************************************
  164. // Supported source arguments: half-open iterator range, container, c-array,
  165. // and single pointer to null terminated string.
  166. // All source arguments except pointers to null terminated byte strings support
  167. // multi-byte character strings which may have embedded nulls. Embedded null
  168. // support is required for some Asian languages on Windows.
  169. // "const codecvt_type& cvt=codecvt()" default arguments are not used because this
  170. // limits the impact of locale("") initialization failures on POSIX systems to programs
  171. // that actually depend on locale(""). It further ensures that exceptions thrown
  172. // as a result of such failues occur after main() has started, so can be caught.
  173. // ----- constructors -----
  174. path() BOOST_NOEXCEPT {}
  175. path(path const& p) : m_pathname(p.m_pathname) {}
  176. template< class Source >
  177. path(Source const& source, typename boost::enable_if_c<
  178. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value
  179. >::type* = 0)
  180. {
  181. path_traits::dispatch(source, m_pathname);
  182. }
  183. path(const value_type* s) : m_pathname(s) {}
  184. path(string_type const& s) : m_pathname(s) {}
  185. // As of October 2015 the interaction between noexcept and =default is so troublesome
  186. // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
  187. // functions. GCC is not even consistent for the same release on different platforms.
  188. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  189. path(path&& p) BOOST_NOEXCEPT : m_pathname(std::move(p.m_pathname))
  190. {
  191. }
  192. path(path&& p, codecvt_type const&) BOOST_NOEXCEPT : m_pathname(std::move(p.m_pathname))
  193. {
  194. }
  195. path& operator=(path&& p) BOOST_NOEXCEPT
  196. {
  197. m_pathname = std::move(p.m_pathname);
  198. return *this;
  199. }
  200. path& assign(path&& p) BOOST_NOEXCEPT
  201. {
  202. m_pathname = std::move(p.m_pathname);
  203. return *this;
  204. }
  205. path& assign(path&& p, codecvt_type const&) BOOST_NOEXCEPT
  206. {
  207. m_pathname = std::move(p.m_pathname);
  208. return *this;
  209. }
  210. path(string_type&& s) BOOST_NOEXCEPT : m_pathname(std::move(s))
  211. {
  212. }
  213. path(string_type&& s, codecvt_type const&) BOOST_NOEXCEPT : m_pathname(std::move(s))
  214. {
  215. }
  216. path& operator=(string_type&& p) BOOST_NOEXCEPT
  217. {
  218. m_pathname = std::move(p);
  219. return *this;
  220. }
  221. path& assign(string_type&& p) BOOST_NOEXCEPT
  222. {
  223. m_pathname = std::move(p);
  224. return *this;
  225. }
  226. path& assign(string_type&& p, codecvt_type const&) BOOST_NOEXCEPT
  227. {
  228. m_pathname = std::move(p);
  229. return *this;
  230. }
  231. #endif
  232. path(path const& p, codecvt_type const&) : m_pathname(p.m_pathname) {}
  233. path(const value_type* s, codecvt_type const&) : m_pathname(s) {}
  234. path(string_type const& s, codecvt_type const&) : m_pathname(s) {}
  235. template< class Source >
  236. path(Source const& source, codecvt_type const& cvt, typename boost::enable_if_c<
  237. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value
  238. >::type* = 0)
  239. {
  240. path_traits::dispatch(source, m_pathname, cvt);
  241. }
  242. path(const value_type* begin, const value_type* end) : m_pathname(begin, end) {}
  243. template< class InputIterator >
  244. path(InputIterator begin, InputIterator end, typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator > >::type* = 0)
  245. {
  246. if (begin != end)
  247. {
  248. // convert requires contiguous string, so copy
  249. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  250. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), m_pathname);
  251. }
  252. }
  253. path(const value_type* begin, const value_type* end, codecvt_type const&) : m_pathname(begin, end) {}
  254. template< class InputIterator >
  255. path(InputIterator begin, InputIterator end, codecvt_type const& cvt, typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator > >::type* = 0)
  256. {
  257. if (begin != end)
  258. {
  259. // convert requires contiguous string, so copy
  260. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  261. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), m_pathname, cvt);
  262. }
  263. }
  264. // ----- assignments -----
  265. // We need to explicitly define copy assignment as otherwise it will be implicitly defined as deleted because there is move assignment
  266. path& operator=(path const& p)
  267. {
  268. return assign(p);
  269. }
  270. path& operator=(string_type const& s)
  271. {
  272. return assign(s);
  273. }
  274. path& operator=(const value_type* ptr)
  275. {
  276. return assign(ptr);
  277. }
  278. template< class Source >
  279. typename boost::enable_if_c<
  280. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  281. path&
  282. >::type operator=(Source const& source)
  283. {
  284. return assign(source);
  285. }
  286. path& assign(path const& p)
  287. {
  288. m_pathname = p.m_pathname;
  289. return *this;
  290. }
  291. path& assign(string_type const& s)
  292. {
  293. m_pathname = s;
  294. return *this;
  295. }
  296. path& assign(const value_type* ptr)
  297. {
  298. m_pathname = ptr;
  299. return *this;
  300. }
  301. template< class Source >
  302. typename boost::enable_if_c<
  303. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  304. path&
  305. >::type assign(Source const& source)
  306. {
  307. m_pathname.clear();
  308. path_traits::dispatch(source, m_pathname);
  309. return *this;
  310. }
  311. path& assign(path const& p, codecvt_type const&)
  312. {
  313. m_pathname = p.m_pathname;
  314. return *this;
  315. }
  316. path& assign(string_type const& s, codecvt_type const&)
  317. {
  318. m_pathname = s;
  319. return *this;
  320. }
  321. path& assign(const value_type* ptr, codecvt_type const&)
  322. {
  323. m_pathname = ptr;
  324. return *this;
  325. }
  326. template< class Source >
  327. typename boost::enable_if_c<
  328. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  329. path&
  330. >::type assign(Source const& source, codecvt_type const& cvt)
  331. {
  332. m_pathname.clear();
  333. path_traits::dispatch(source, m_pathname, cvt);
  334. return *this;
  335. }
  336. path& assign(const value_type* begin, const value_type* end)
  337. {
  338. m_pathname.assign(begin, end);
  339. return *this;
  340. }
  341. template< class InputIterator >
  342. typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator >, path& >::type
  343. assign(InputIterator begin, InputIterator end)
  344. {
  345. m_pathname.clear();
  346. if (begin != end)
  347. {
  348. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  349. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), m_pathname);
  350. }
  351. return *this;
  352. }
  353. path& assign(const value_type* begin, const value_type* end, codecvt_type const&)
  354. {
  355. m_pathname.assign(begin, end);
  356. return *this;
  357. }
  358. template< class InputIterator >
  359. typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator >, path& >::type
  360. assign(InputIterator begin, InputIterator end, codecvt_type const& cvt)
  361. {
  362. m_pathname.clear();
  363. if (begin != end)
  364. {
  365. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  366. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), m_pathname, cvt);
  367. }
  368. return *this;
  369. }
  370. // ----- concatenation -----
  371. path& operator+=(path const& p)
  372. {
  373. return concat(p);
  374. }
  375. path& operator+=(const value_type* ptr)
  376. {
  377. return concat(ptr);
  378. }
  379. path& operator+=(string_type const& s)
  380. {
  381. return concat(s);
  382. }
  383. template< class Source >
  384. typename boost::enable_if_c<
  385. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  386. path&
  387. >::type operator+=(Source const& source)
  388. {
  389. return concat(source);
  390. }
  391. path& operator+=(value_type c)
  392. {
  393. m_pathname.push_back(c);
  394. return *this;
  395. }
  396. template< class CharT >
  397. typename boost::enable_if< boost::is_integral< CharT >, path& >::type
  398. operator+=(CharT c)
  399. {
  400. CharT tmp[2];
  401. tmp[0] = c;
  402. tmp[1] = static_cast< CharT >(0);
  403. return concat(tmp);
  404. }
  405. path& concat(path const& p)
  406. {
  407. m_pathname += p.m_pathname;
  408. return *this;
  409. }
  410. path& concat(const value_type* ptr)
  411. {
  412. m_pathname += ptr;
  413. return *this;
  414. }
  415. path& concat(string_type const& s)
  416. {
  417. m_pathname += s;
  418. return *this;
  419. }
  420. template< class Source >
  421. typename boost::enable_if_c<
  422. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  423. path&
  424. >::type concat(Source const& source)
  425. {
  426. path_traits::dispatch(source, m_pathname);
  427. return *this;
  428. }
  429. path& concat(path const& p, codecvt_type const&)
  430. {
  431. m_pathname += p.m_pathname;
  432. return *this;
  433. }
  434. path& concat(const value_type* ptr, codecvt_type const&)
  435. {
  436. m_pathname += ptr;
  437. return *this;
  438. }
  439. path& concat(string_type const& s, codecvt_type const&)
  440. {
  441. m_pathname += s;
  442. return *this;
  443. }
  444. template< class Source >
  445. typename boost::enable_if_c<
  446. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  447. path&
  448. >::type concat(Source const& source, codecvt_type const& cvt)
  449. {
  450. path_traits::dispatch(source, m_pathname, cvt);
  451. return *this;
  452. }
  453. path& concat(const value_type* begin, const value_type* end)
  454. {
  455. m_pathname.append(begin, end);
  456. return *this;
  457. }
  458. template< class InputIterator >
  459. typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator >, path& >::type
  460. concat(InputIterator begin, InputIterator end)
  461. {
  462. if (begin != end)
  463. {
  464. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  465. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), m_pathname);
  466. }
  467. return *this;
  468. }
  469. path& concat(const value_type* begin, const value_type* end, codecvt_type const&)
  470. {
  471. m_pathname.append(begin, end);
  472. return *this;
  473. }
  474. template< class InputIterator >
  475. typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator >, path& >::type
  476. concat(InputIterator begin, InputIterator end, codecvt_type const& cvt)
  477. {
  478. if (begin != end)
  479. {
  480. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  481. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), m_pathname, cvt);
  482. }
  483. return *this;
  484. }
  485. // ----- appends -----
  486. // if a separator is added, it is the preferred separator for the platform;
  487. // slash for POSIX, backslash for Windows
  488. path& operator/=(path const& p)
  489. {
  490. return append(p);
  491. }
  492. path& operator/=(const value_type* ptr)
  493. {
  494. return append(ptr);
  495. }
  496. path& operator/=(string_type const& s)
  497. {
  498. return append(s);
  499. }
  500. template< class Source >
  501. BOOST_FORCEINLINE typename boost::enable_if_c<
  502. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  503. path&
  504. >::type operator/=(Source const& source)
  505. {
  506. return append(source);
  507. }
  508. BOOST_FORCEINLINE path& append(path const& p)
  509. {
  510. BOOST_FILESYSTEM_VERSIONED_SYM(append)(p);
  511. return *this;
  512. }
  513. BOOST_FORCEINLINE path& append(string_type const& p)
  514. {
  515. BOOST_FILESYSTEM_VERSIONED_SYM(append)(p.c_str(), p.c_str() + p.size());
  516. return *this;
  517. }
  518. BOOST_FORCEINLINE path& append(const value_type* ptr)
  519. {
  520. BOOST_FILESYSTEM_VERSIONED_SYM(append)(ptr, ptr + string_type::traits_type::length(ptr));
  521. return *this;
  522. }
  523. template< class Source >
  524. typename boost::enable_if_c<
  525. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  526. path&
  527. >::type append(Source const& source)
  528. {
  529. path p;
  530. path_traits::dispatch(source, p.m_pathname);
  531. return append(p);
  532. }
  533. BOOST_FORCEINLINE path& append(path const& p, codecvt_type const&)
  534. {
  535. BOOST_FILESYSTEM_VERSIONED_SYM(append)(p);
  536. return *this;
  537. }
  538. BOOST_FORCEINLINE path& append(string_type const& p, codecvt_type const&)
  539. {
  540. BOOST_FILESYSTEM_VERSIONED_SYM(append)(p.c_str(), p.c_str() + p.size());
  541. return *this;
  542. }
  543. BOOST_FORCEINLINE path& append(const value_type* ptr, codecvt_type const&)
  544. {
  545. BOOST_FILESYSTEM_VERSIONED_SYM(append)(ptr, ptr + string_type::traits_type::length(ptr));
  546. return *this;
  547. }
  548. template< class Source >
  549. typename boost::enable_if_c<
  550. path_traits::is_pathable< typename boost::decay< Source >::type >::value && !path_detail::is_native_pathable< Source >::value,
  551. path&
  552. >::type append(Source const& source, codecvt_type const& cvt)
  553. {
  554. path p;
  555. path_traits::dispatch(source, p.m_pathname, cvt);
  556. return append(p);
  557. }
  558. BOOST_FORCEINLINE path& append(const value_type* begin, const value_type* end)
  559. {
  560. BOOST_FILESYSTEM_VERSIONED_SYM(append)(begin, end);
  561. return *this;
  562. }
  563. template< class InputIterator >
  564. typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator >, path& >::type
  565. append(InputIterator begin, InputIterator end)
  566. {
  567. path p;
  568. if (begin != end)
  569. {
  570. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  571. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), p.m_pathname);
  572. }
  573. return append(p);
  574. }
  575. BOOST_FORCEINLINE path& append(const value_type* begin, const value_type* end, codecvt_type const&)
  576. {
  577. BOOST_FILESYSTEM_VERSIONED_SYM(append)(begin, end);
  578. return *this;
  579. }
  580. template< class InputIterator >
  581. typename boost::disable_if< path_detail::is_native_char_ptr< InputIterator >, path& >::type
  582. append(InputIterator begin, InputIterator end, const codecvt_type& cvt)
  583. {
  584. path p;
  585. if (begin != end)
  586. {
  587. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > seq(begin, end);
  588. path_traits::convert(seq.c_str(), seq.c_str() + seq.size(), p.m_pathname, cvt);
  589. }
  590. return append(p);
  591. }
  592. // ----- modifiers -----
  593. void clear() BOOST_NOEXCEPT { m_pathname.clear(); }
  594. #ifdef BOOST_POSIX_API
  595. path& make_preferred()
  596. {
  597. // No effect on POSIX
  598. return *this;
  599. }
  600. #else // BOOST_WINDOWS_API
  601. BOOST_FILESYSTEM_DECL path& make_preferred(); // change slashes to backslashes
  602. #endif
  603. BOOST_FILESYSTEM_DECL path& remove_filename();
  604. BOOST_FILESYSTEM_DECL path& remove_trailing_separator();
  605. BOOST_FORCEINLINE path& replace_extension(path const& new_extension = path())
  606. {
  607. BOOST_FILESYSTEM_VERSIONED_SYM(replace_extension)(new_extension);
  608. return *this;
  609. }
  610. void swap(path& rhs) BOOST_NOEXCEPT { m_pathname.swap(rhs.m_pathname); }
  611. // ----- observers -----
  612. // For operating systems that format file paths differently than directory
  613. // paths, return values from observers are formatted as file names unless there
  614. // is a trailing separator, in which case returns are formatted as directory
  615. // paths. POSIX and Windows make no such distinction.
  616. // Implementations are permitted to return const values or const references.
  617. // The string or path returned by an observer are specified as being formatted
  618. // as "native" or "generic".
  619. //
  620. // For POSIX, these are all the same format; slashes and backslashes are as input and
  621. // are not modified.
  622. //
  623. // For Windows, native: as input; slashes and backslashes are not modified;
  624. // this is the format of the internally stored string.
  625. // generic: backslashes are converted to slashes
  626. // ----- native format observers -----
  627. string_type const& native() const BOOST_NOEXCEPT { return m_pathname; }
  628. const value_type* c_str() const BOOST_NOEXCEPT { return m_pathname.c_str(); }
  629. string_type::size_type size() const BOOST_NOEXCEPT { return m_pathname.size(); }
  630. template< class String >
  631. String string() const;
  632. template< class String >
  633. String string(codecvt_type const& cvt) const;
  634. #ifdef BOOST_WINDOWS_API
  635. std::string string() const
  636. {
  637. std::string tmp;
  638. if (!m_pathname.empty())
  639. path_traits::convert(m_pathname.c_str(), m_pathname.c_str() + m_pathname.size(), tmp);
  640. return tmp;
  641. }
  642. std::string string(codecvt_type const& cvt) const
  643. {
  644. std::string tmp;
  645. if (!m_pathname.empty())
  646. path_traits::convert(m_pathname.c_str(), m_pathname.c_str() + m_pathname.size(), tmp, cvt);
  647. return tmp;
  648. }
  649. // string_type is std::wstring, so there is no conversion
  650. std::wstring const& wstring() const { return m_pathname; }
  651. std::wstring const& wstring(codecvt_type const&) const { return m_pathname; }
  652. #else // BOOST_POSIX_API
  653. // string_type is std::string, so there is no conversion
  654. std::string const& string() const { return m_pathname; }
  655. std::string const& string(codecvt_type const&) const { return m_pathname; }
  656. std::wstring wstring() const
  657. {
  658. std::wstring tmp;
  659. if (!m_pathname.empty())
  660. path_traits::convert(m_pathname.c_str(), m_pathname.c_str() + m_pathname.size(), tmp);
  661. return tmp;
  662. }
  663. std::wstring wstring(codecvt_type const& cvt) const
  664. {
  665. std::wstring tmp;
  666. if (!m_pathname.empty())
  667. path_traits::convert(m_pathname.c_str(), m_pathname.c_str() + m_pathname.size(), tmp, cvt);
  668. return tmp;
  669. }
  670. #endif
  671. // ----- generic format observers -----
  672. // Experimental generic function returning generic formatted path (i.e. separators
  673. // are forward slashes). Motivation: simpler than a family of generic_*string
  674. // functions.
  675. #ifdef BOOST_WINDOWS_API
  676. BOOST_FILESYSTEM_DECL path generic_path() const;
  677. #else
  678. path generic_path() const { return path(*this); }
  679. #endif
  680. template< class String >
  681. String generic_string() const;
  682. template< class String >
  683. String generic_string(codecvt_type const& cvt) const;
  684. #ifdef BOOST_WINDOWS_API
  685. std::string generic_string() const { return generic_path().string(); }
  686. std::string generic_string(codecvt_type const& cvt) const { return generic_path().string(cvt); }
  687. std::wstring generic_wstring() const { return generic_path().wstring(); }
  688. std::wstring generic_wstring(codecvt_type const&) const { return generic_wstring(); }
  689. #else // BOOST_POSIX_API
  690. // On POSIX-like systems, the generic format is the same as the native format
  691. std::string const& generic_string() const { return m_pathname; }
  692. std::string const& generic_string(codecvt_type const&) const { return m_pathname; }
  693. std::wstring generic_wstring() const { return this->wstring(); }
  694. std::wstring generic_wstring(codecvt_type const& cvt) const { return this->wstring(cvt); }
  695. #endif
  696. // ----- compare -----
  697. BOOST_FORCEINLINE int compare(path const& p) const BOOST_NOEXCEPT // generic, lexicographical
  698. {
  699. return BOOST_FILESYSTEM_VERSIONED_SYM(compare)(p);
  700. }
  701. int compare(std::string const& s) const { return compare(path(s)); }
  702. int compare(const value_type* s) const { return compare(path(s)); }
  703. // ----- decomposition -----
  704. path root_path() const { return path(m_pathname.c_str(), m_pathname.c_str() + find_root_path_size()); }
  705. // returns 0 or 1 element path even on POSIX, root_name() is non-empty() for network paths
  706. path root_name() const { return path(m_pathname.c_str(), m_pathname.c_str() + find_root_name_size()); }
  707. // returns 0 or 1 element path
  708. path root_directory() const
  709. {
  710. path_detail::substring root_dir = find_root_directory();
  711. const value_type* p = m_pathname.c_str() + root_dir.pos;
  712. return path(p, p + root_dir.size);
  713. }
  714. path relative_path() const
  715. {
  716. path_detail::substring root_dir = find_relative_path();
  717. const value_type* p = m_pathname.c_str() + root_dir.pos;
  718. return path(p, p + root_dir.size);
  719. }
  720. path parent_path() const { return path(m_pathname.c_str(), m_pathname.c_str() + find_parent_path_size()); }
  721. BOOST_FORCEINLINE path filename() const { return BOOST_FILESYSTEM_VERSIONED_SYM(filename)(); } // returns 0 or 1 element path
  722. BOOST_FORCEINLINE path stem() const { return BOOST_FILESYSTEM_VERSIONED_SYM(stem)(); } // returns 0 or 1 element path
  723. BOOST_FORCEINLINE path extension() const { return BOOST_FILESYSTEM_VERSIONED_SYM(extension)(); } // returns 0 or 1 element path
  724. // ----- query -----
  725. bool empty() const BOOST_NOEXCEPT { return m_pathname.empty(); }
  726. bool filename_is_dot() const;
  727. bool filename_is_dot_dot() const;
  728. bool has_root_path() const { return find_root_path_size() > 0; }
  729. bool has_root_name() const { return find_root_name_size() > 0; }
  730. bool has_root_directory() const { return find_root_directory().size > 0; }
  731. bool has_relative_path() const { return find_relative_path().size > 0; }
  732. bool has_parent_path() const { return find_parent_path_size() > 0; }
  733. BOOST_FORCEINLINE bool has_filename() const { return BOOST_FILESYSTEM_VERSIONED_SYM(has_filename)(); }
  734. bool has_stem() const { return !stem().empty(); }
  735. bool has_extension() const { return !extension().empty(); }
  736. bool is_relative() const { return !is_absolute(); }
  737. bool is_absolute() const
  738. {
  739. // Windows CE has no root name (aka drive letters)
  740. #if defined(BOOST_WINDOWS_API) && !defined(UNDER_CE)
  741. return has_root_name() && has_root_directory();
  742. #else
  743. return has_root_directory();
  744. #endif
  745. }
  746. // ----- lexical operations -----
  747. BOOST_FORCEINLINE path lexically_normal() const { return BOOST_FILESYSTEM_VERSIONED_SYM(lexically_normal)(); }
  748. BOOST_FILESYSTEM_DECL path lexically_relative(path const& base) const;
  749. path lexically_proximate(path const& base) const
  750. {
  751. path tmp(lexically_relative(base));
  752. return tmp.empty() ? *this : tmp;
  753. }
  754. // ----- iterators -----
  755. class iterator;
  756. friend class iterator;
  757. typedef iterator const_iterator;
  758. class reverse_iterator;
  759. typedef reverse_iterator const_reverse_iterator;
  760. BOOST_FILESYSTEM_DECL iterator begin() const;
  761. BOOST_FILESYSTEM_DECL iterator end() const;
  762. reverse_iterator rbegin() const;
  763. reverse_iterator rend() const;
  764. // ----- static member functions -----
  765. static BOOST_FILESYSTEM_DECL std::locale imbue(std::locale const& loc);
  766. static BOOST_FILESYSTEM_DECL codecvt_type const& codecvt();
  767. // ----- deprecated functions -----
  768. #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  769. // recently deprecated functions supplied by default
  770. path& normalize()
  771. {
  772. path tmp(lexically_normal());
  773. m_pathname.swap(tmp.m_pathname);
  774. return *this;
  775. }
  776. path& remove_leaf() { return remove_filename(); }
  777. path leaf() const { return filename(); }
  778. path branch_path() const { return parent_path(); }
  779. path generic() const { return generic_path(); }
  780. bool has_leaf() const { return !m_pathname.empty(); }
  781. bool has_branch_path() const { return !parent_path().empty(); }
  782. bool is_complete() const { return is_absolute(); }
  783. #endif
  784. #if defined(BOOST_FILESYSTEM_DEPRECATED)
  785. // deprecated functions with enough signature or semantic changes that they are
  786. // not supplied by default
  787. std::string file_string() const { return string(); }
  788. std::string directory_string() const { return string(); }
  789. std::string native_file_string() const { return string(); }
  790. std::string native_directory_string() const { return string(); }
  791. string_type external_file_string() const { return native(); }
  792. string_type external_directory_string() const { return native(); }
  793. #endif
  794. //--------------------------------------------------------------------------------------//
  795. // class path private members //
  796. //--------------------------------------------------------------------------------------//
  797. private:
  798. bool has_filename_v3() const { return !m_pathname.empty(); }
  799. bool has_filename_v4() const { return find_filename_v4_size() > 0; }
  800. BOOST_FILESYSTEM_DECL path filename_v3() const;
  801. path filename_v4() const
  802. {
  803. string_type::size_type filename_size = find_filename_v4_size();
  804. string_type::size_type pos = m_pathname.size() - filename_size;
  805. const value_type* p = m_pathname.c_str() + pos;
  806. return path(p, p + filename_size);
  807. }
  808. BOOST_FILESYSTEM_DECL path stem_v3() const;
  809. BOOST_FILESYSTEM_DECL path stem_v4() const;
  810. BOOST_FILESYSTEM_DECL path extension_v3() const;
  811. path extension_v4() const
  812. {
  813. string_type::size_type extension_size = find_extension_v4_size();
  814. string_type::size_type pos = m_pathname.size() - extension_size;
  815. const value_type* p = m_pathname.c_str() + pos;
  816. return path(p, p + extension_size);
  817. }
  818. BOOST_FILESYSTEM_DECL void replace_extension_v3(path const& new_extension);
  819. BOOST_FILESYSTEM_DECL void replace_extension_v4(path const& new_extension);
  820. BOOST_FILESYSTEM_DECL path lexically_normal_v3() const;
  821. BOOST_FILESYSTEM_DECL path lexically_normal_v4() const;
  822. BOOST_FILESYSTEM_DECL int compare_v3(path const& p) const BOOST_NOEXCEPT;
  823. BOOST_FILESYSTEM_DECL int compare_v4(path const& p) const BOOST_NOEXCEPT;
  824. BOOST_FILESYSTEM_DECL void append_v3(const value_type* b, const value_type* e);
  825. BOOST_FILESYSTEM_DECL void append_v4(const value_type* b, const value_type* e);
  826. BOOST_FILESYSTEM_DECL void append_v3(path const& p);
  827. BOOST_FILESYSTEM_DECL void append_v4(path const& p);
  828. // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0.
  829. // Note: An append is never performed if size()==0, so a returned 0 is unambiguous.
  830. BOOST_FILESYSTEM_DECL string_type::size_type append_separator_if_needed();
  831. BOOST_FILESYSTEM_DECL void erase_redundant_separator(string_type::size_type sep_pos);
  832. BOOST_FILESYSTEM_DECL string_type::size_type find_root_name_size() const;
  833. BOOST_FILESYSTEM_DECL string_type::size_type find_root_path_size() const;
  834. BOOST_FILESYSTEM_DECL path_detail::substring find_root_directory() const;
  835. BOOST_FILESYSTEM_DECL path_detail::substring find_relative_path() const;
  836. BOOST_FILESYSTEM_DECL string_type::size_type find_parent_path_size() const;
  837. BOOST_FILESYSTEM_DECL string_type::size_type find_filename_v4_size() const;
  838. BOOST_FILESYSTEM_DECL string_type::size_type find_extension_v4_size() const;
  839. private:
  840. /*
  841. * m_pathname has the type, encoding, and format required by the native
  842. * operating system. Thus for POSIX and Windows there is no conversion for
  843. * passing m_pathname.c_str() to the O/S API or when obtaining a path from the
  844. * O/S API. POSIX encoding is unspecified other than for dot and slash
  845. * characters; POSIX just treats paths as a sequence of bytes. Windows
  846. * encoding is UCS-2 or UTF-16 depending on the version.
  847. */
  848. string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes,
  849. // slashes NOT converted to backslashes
  850. };
  851. namespace detail {
  852. BOOST_FILESYSTEM_DECL int lex_compare_v3(path::iterator first1, path::iterator last1, path::iterator first2, path::iterator last2);
  853. BOOST_FILESYSTEM_DECL int lex_compare_v4(path::iterator first1, path::iterator last1, path::iterator first2, path::iterator last2);
  854. BOOST_FILESYSTEM_DECL path const& dot_path();
  855. BOOST_FILESYSTEM_DECL path const& dot_dot_path();
  856. } // namespace detail
  857. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  858. typedef path wpath;
  859. #endif
  860. //------------------------------------------------------------------------------------//
  861. // class path::iterator //
  862. //------------------------------------------------------------------------------------//
  863. class path::iterator :
  864. public boost::iterator_facade<
  865. path::iterator,
  866. const path,
  867. boost::bidirectional_traversal_tag
  868. >
  869. {
  870. private:
  871. friend class boost::iterator_core_access;
  872. friend class boost::filesystem::path;
  873. friend class boost::filesystem::path::reverse_iterator;
  874. friend BOOST_FILESYSTEM_DECL int detail::lex_compare_v3(path::iterator first1, path::iterator last1, path::iterator first2, path::iterator last2);
  875. path const& dereference() const { return m_element; }
  876. bool equal(iterator const& rhs) const
  877. {
  878. return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos;
  879. }
  880. BOOST_FORCEINLINE void increment() { BOOST_FILESYSTEM_VERSIONED_SYM(increment)(); }
  881. BOOST_FORCEINLINE void decrement() { BOOST_FILESYSTEM_VERSIONED_SYM(decrement)(); }
  882. private:
  883. BOOST_FILESYSTEM_DECL void increment_v3();
  884. BOOST_FILESYSTEM_DECL void increment_v4();
  885. BOOST_FILESYSTEM_DECL void decrement_v3();
  886. BOOST_FILESYSTEM_DECL void decrement_v4();
  887. private:
  888. // current element
  889. path m_element;
  890. // path being iterated over
  891. const path* m_path_ptr;
  892. // position of m_element in m_path_ptr->m_pathname.
  893. // if m_element is implicit dot, m_pos is the
  894. // position of the last separator in the path.
  895. // end() iterator is indicated by
  896. // m_pos == m_path_ptr->m_pathname.size()
  897. string_type::size_type m_pos;
  898. };
  899. //------------------------------------------------------------------------------------//
  900. // class path::reverse_iterator //
  901. //------------------------------------------------------------------------------------//
  902. class path::reverse_iterator :
  903. public boost::iterator_facade<
  904. path::reverse_iterator,
  905. const path,
  906. boost::bidirectional_traversal_tag
  907. >
  908. {
  909. public:
  910. explicit reverse_iterator(iterator itr) :
  911. m_itr(itr)
  912. {
  913. if (itr != itr.m_path_ptr->begin())
  914. m_element = *--itr;
  915. }
  916. private:
  917. friend class boost::iterator_core_access;
  918. friend class boost::filesystem::path;
  919. path const& dereference() const { return m_element; }
  920. bool equal(reverse_iterator const& rhs) const { return m_itr == rhs.m_itr; }
  921. void increment()
  922. {
  923. --m_itr;
  924. if (m_itr != m_itr.m_path_ptr->begin())
  925. {
  926. iterator tmp = m_itr;
  927. m_element = *--tmp;
  928. }
  929. }
  930. void decrement()
  931. {
  932. m_element = *m_itr;
  933. ++m_itr;
  934. }
  935. private:
  936. iterator m_itr;
  937. path m_element;
  938. };
  939. //------------------------------------------------------------------------------------//
  940. // //
  941. // non-member functions //
  942. // //
  943. //------------------------------------------------------------------------------------//
  944. // std::lexicographical_compare would infinitely recurse because path iterators
  945. // yield paths, so provide a path aware version
  946. inline bool lexicographical_compare(path::iterator first1, path::iterator last1, path::iterator first2, path::iterator last2)
  947. {
  948. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::lex_compare)(first1, last1, first2, last2) < 0;
  949. }
  950. inline bool operator==(path const& lhs, path const& rhs)
  951. {
  952. return lhs.compare(rhs) == 0;
  953. }
  954. inline bool operator==(path const& lhs, path::string_type const& rhs)
  955. {
  956. return lhs.compare(rhs) == 0;
  957. }
  958. inline bool operator==(path::string_type const& lhs, path const& rhs)
  959. {
  960. return rhs.compare(lhs) == 0;
  961. }
  962. inline bool operator==(path const& lhs, const path::value_type* rhs)
  963. {
  964. return lhs.compare(rhs) == 0;
  965. }
  966. inline bool operator==(const path::value_type* lhs, path const& rhs)
  967. {
  968. return rhs.compare(lhs) == 0;
  969. }
  970. inline bool operator!=(path const& lhs, path const& rhs)
  971. {
  972. return lhs.compare(rhs) != 0;
  973. }
  974. inline bool operator!=(path const& lhs, path::string_type const& rhs)
  975. {
  976. return lhs.compare(rhs) != 0;
  977. }
  978. inline bool operator!=(path::string_type const& lhs, path const& rhs)
  979. {
  980. return rhs.compare(lhs) != 0;
  981. }
  982. inline bool operator!=(path const& lhs, const path::value_type* rhs)
  983. {
  984. return lhs.compare(rhs) != 0;
  985. }
  986. inline bool operator!=(const path::value_type* lhs, path const& rhs)
  987. {
  988. return rhs.compare(lhs) != 0;
  989. }
  990. // TODO: why do == and != have additional overloads, but the others don't?
  991. inline bool operator<(path const& lhs, path const& rhs)
  992. {
  993. return lhs.compare(rhs) < 0;
  994. }
  995. inline bool operator<=(path const& lhs, path const& rhs)
  996. {
  997. return !(rhs < lhs);
  998. }
  999. inline bool operator>(path const& lhs, path const& rhs)
  1000. {
  1001. return rhs < lhs;
  1002. }
  1003. inline bool operator>=(path const& lhs, path const& rhs)
  1004. {
  1005. return !(lhs < rhs);
  1006. }
  1007. // Note: Declared as a template to delay binding to Boost.ContainerHash functions and make the dependency optional
  1008. template< typename T >
  1009. inline typename boost::enable_if< boost::is_same< T, path >, std::size_t >::type hash_value(T const& p) BOOST_NOEXCEPT
  1010. {
  1011. #ifdef BOOST_WINDOWS_API
  1012. std::size_t seed = 0;
  1013. for (typename T::value_type const* it = p.c_str(); *it; ++it)
  1014. hash_combine(seed, *it == L'/' ? L'\\' : *it);
  1015. return seed;
  1016. #else // BOOST_POSIX_API
  1017. return hash_range(p.native().begin(), p.native().end());
  1018. #endif
  1019. }
  1020. inline void swap(path& lhs, path& rhs) BOOST_NOEXCEPT
  1021. {
  1022. lhs.swap(rhs);
  1023. }
  1024. inline path operator/(path const& lhs, path const& rhs)
  1025. {
  1026. path p = lhs;
  1027. p /= rhs;
  1028. return p;
  1029. }
  1030. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  1031. inline path operator/(path&& lhs, path const& rhs)
  1032. {
  1033. lhs /= rhs;
  1034. return std::move(lhs);
  1035. }
  1036. #endif
  1037. // inserters and extractors
  1038. // use boost::io::quoted() to handle spaces in paths
  1039. // use '&' as escape character to ease use for Windows paths
  1040. template< class Char, class Traits >
  1041. inline std::basic_ostream< Char, Traits >&
  1042. operator<<(std::basic_ostream< Char, Traits >& os, path const& p)
  1043. {
  1044. return os << boost::io::quoted(p.template string< std::basic_string< Char > >(), static_cast< Char >('&'));
  1045. }
  1046. template< class Char, class Traits >
  1047. inline std::basic_istream< Char, Traits >&
  1048. operator>>(std::basic_istream< Char, Traits >& is, path& p)
  1049. {
  1050. std::basic_string< Char > str;
  1051. is >> boost::io::quoted(str, static_cast< Char >('&'));
  1052. p = str;
  1053. return is;
  1054. }
  1055. // name_checks
  1056. // These functions are holdovers from version 1. It isn't clear they have much
  1057. // usefulness, or how to generalize them for later versions.
  1058. BOOST_FILESYSTEM_DECL bool portable_posix_name(std::string const& name);
  1059. BOOST_FILESYSTEM_DECL bool windows_name(std::string const& name);
  1060. BOOST_FILESYSTEM_DECL bool portable_name(std::string const& name);
  1061. BOOST_FILESYSTEM_DECL bool portable_directory_name(std::string const& name);
  1062. BOOST_FILESYSTEM_DECL bool portable_file_name(std::string const& name);
  1063. BOOST_FILESYSTEM_DECL bool native(std::string const& name);
  1064. namespace detail {
  1065. // For POSIX, is_directory_separator() and is_element_separator() are identical since
  1066. // a forward slash is the only valid directory separator and also the only valid
  1067. // element separator. For Windows, forward slash and back slash are the possible
  1068. // directory separators, but colon (example: "c:foo") is also an element separator.
  1069. inline bool is_directory_separator(path::value_type c) BOOST_NOEXCEPT
  1070. {
  1071. return c == path::separator
  1072. #ifdef BOOST_WINDOWS_API
  1073. || c == path::preferred_separator
  1074. #endif
  1075. ;
  1076. }
  1077. inline bool is_element_separator(path::value_type c) BOOST_NOEXCEPT
  1078. {
  1079. return c == path::separator
  1080. #ifdef BOOST_WINDOWS_API
  1081. || c == path::preferred_separator || c == L':'
  1082. #endif
  1083. ;
  1084. }
  1085. } // namespace detail
  1086. //------------------------------------------------------------------------------------//
  1087. // class path miscellaneous function implementations //
  1088. //------------------------------------------------------------------------------------//
  1089. inline path::reverse_iterator path::rbegin() const
  1090. {
  1091. return reverse_iterator(end());
  1092. }
  1093. inline path::reverse_iterator path::rend() const
  1094. {
  1095. return reverse_iterator(begin());
  1096. }
  1097. inline bool path::filename_is_dot() const
  1098. {
  1099. // implicit dot is tricky, so actually call filename(); see path::filename() example
  1100. // in reference.html
  1101. path p(filename());
  1102. return p.size() == 1 && *p.c_str() == dot;
  1103. }
  1104. inline bool path::filename_is_dot_dot() const
  1105. {
  1106. return size() >= 2 && m_pathname[size() - 1] == dot && m_pathname[size() - 2] == dot && (m_pathname.size() == 2 || detail::is_element_separator(m_pathname[size() - 3]));
  1107. // use detail::is_element_separator() rather than detail::is_directory_separator
  1108. // to deal with "c:.." edge case on Windows when ':' acts as a separator
  1109. }
  1110. //--------------------------------------------------------------------------------------//
  1111. // class path member template specializations //
  1112. //--------------------------------------------------------------------------------------//
  1113. template<>
  1114. inline std::string path::string< std::string >() const
  1115. {
  1116. return string();
  1117. }
  1118. template<>
  1119. inline std::wstring path::string< std::wstring >() const
  1120. {
  1121. return wstring();
  1122. }
  1123. template<>
  1124. inline std::string path::string< std::string >(const codecvt_type& cvt) const
  1125. {
  1126. return string(cvt);
  1127. }
  1128. template<>
  1129. inline std::wstring path::string< std::wstring >(const codecvt_type& cvt) const
  1130. {
  1131. return wstring(cvt);
  1132. }
  1133. template<>
  1134. inline std::string path::generic_string< std::string >() const
  1135. {
  1136. return generic_string();
  1137. }
  1138. template<>
  1139. inline std::wstring path::generic_string< std::wstring >() const
  1140. {
  1141. return generic_wstring();
  1142. }
  1143. template<>
  1144. inline std::string path::generic_string< std::string >(codecvt_type const& cvt) const
  1145. {
  1146. return generic_string(cvt);
  1147. }
  1148. template<>
  1149. inline std::wstring path::generic_string< std::wstring >(codecvt_type const& cvt) const
  1150. {
  1151. return generic_wstring(cvt);
  1152. }
  1153. //--------------------------------------------------------------------------------------//
  1154. // path_traits convert function implementations //
  1155. // requiring path::codecvt() be visable //
  1156. //--------------------------------------------------------------------------------------//
  1157. namespace path_traits { // without codecvt
  1158. inline void convert(const char* from,
  1159. const char* from_end, // 0 for null terminated MBCS
  1160. std::wstring& to)
  1161. {
  1162. convert(from, from_end, to, path::codecvt());
  1163. }
  1164. inline void convert(const wchar_t* from,
  1165. const wchar_t* from_end, // 0 for null terminated MBCS
  1166. std::string& to)
  1167. {
  1168. convert(from, from_end, to, path::codecvt());
  1169. }
  1170. inline void convert(const char* from, std::wstring& to)
  1171. {
  1172. BOOST_ASSERT(!!from);
  1173. convert(from, 0, to, path::codecvt());
  1174. }
  1175. inline void convert(const wchar_t* from, std::string& to)
  1176. {
  1177. BOOST_ASSERT(!!from);
  1178. convert(from, 0, to, path::codecvt());
  1179. }
  1180. } // namespace path_traits
  1181. } // namespace filesystem
  1182. } // namespace boost
  1183. //----------------------------------------------------------------------------//
  1184. #include <boost/filesystem/detail/footer.hpp>
  1185. #endif // BOOST_FILESYSTEM_PATH_HPP