path.hpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. // filesystem path.hpp ---------------------------------------------------------------//
  2. // Copyright Vladimir Prus 2002
  3. // Copyright Beman Dawes 2002-2005, 2009
  4. // Copyright Andrey Semashev 2021-2024
  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/filesystem/config.hpp>
  14. #include <cstddef>
  15. #include <iosfwd>
  16. #include <locale>
  17. #include <string>
  18. #include <iterator>
  19. #include <type_traits>
  20. #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
  21. #include <string_view>
  22. #endif
  23. #include <boost/assert.hpp>
  24. #include <boost/iterator/iterator_facade.hpp>
  25. #include <boost/iterator/iterator_categories.hpp>
  26. #include <boost/io/quoted.hpp>
  27. #include <boost/functional/hash_fwd.hpp>
  28. #include <boost/filesystem/detail/path_traits.hpp>
  29. #include <boost/filesystem/detail/type_traits/negation.hpp>
  30. #include <boost/filesystem/detail/type_traits/conjunction.hpp>
  31. #include <boost/filesystem/detail/type_traits/disjunction.hpp>
  32. #include <boost/filesystem/detail/header.hpp> // must be the last #include
  33. namespace boost {
  34. namespace filesystem {
  35. class path;
  36. namespace path_detail { // intentionally don't use filesystem::detail to not bring internal Boost.Filesystem functions into ADL via path_constants
  37. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  38. struct path_constants
  39. {
  40. typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
  41. typedef Char value_type;
  42. static BOOST_CONSTEXPR_OR_CONST value_type separator = Separator;
  43. static BOOST_CONSTEXPR_OR_CONST value_type preferred_separator = PreferredSeparator;
  44. static BOOST_CONSTEXPR_OR_CONST value_type dot = Dot;
  45. };
  46. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  47. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  48. BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
  49. path_constants< Char, Separator, PreferredSeparator, Dot >::separator;
  50. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  51. BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
  52. path_constants< Char, Separator, PreferredSeparator, Dot >::preferred_separator;
  53. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  54. BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
  55. path_constants< Char, Separator, PreferredSeparator, Dot >::dot;
  56. #endif
  57. class path_iterator;
  58. class path_reverse_iterator;
  59. } // namespace path_detail
  60. namespace detail {
  61. struct path_algorithms
  62. {
  63. // A struct that denotes a contiguous range of characters in a string. A lightweight alternative to string_view.
  64. struct substring
  65. {
  66. std::size_t pos;
  67. std::size_t size;
  68. };
  69. typedef path_traits::path_native_char_type value_type;
  70. typedef std::basic_string< value_type > string_type;
  71. static bool has_filename_v3(path const& p);
  72. static bool has_filename_v4(path const& p);
  73. BOOST_FILESYSTEM_DECL static path filename_v3(path const& p);
  74. static path filename_v4(path const& p);
  75. BOOST_FILESYSTEM_DECL static path stem_v3(path const& p);
  76. BOOST_FILESYSTEM_DECL static path stem_v4(path const& p);
  77. BOOST_FILESYSTEM_DECL static path extension_v3(path const& p);
  78. static path extension_v4(path const& p);
  79. BOOST_FILESYSTEM_DECL static void remove_filename_v3(path& p);
  80. BOOST_FILESYSTEM_DECL static void remove_filename_v4(path& p);
  81. BOOST_FILESYSTEM_DECL static void replace_extension_v3(path& p, path const& new_extension);
  82. BOOST_FILESYSTEM_DECL static void replace_extension_v4(path& p, path const& new_extension);
  83. BOOST_FILESYSTEM_DECL static path lexically_normal_v3(path const& p);
  84. BOOST_FILESYSTEM_DECL static path lexically_normal_v4(path const& p);
  85. BOOST_FILESYSTEM_DECL static path generic_path_v3(path const& p);
  86. BOOST_FILESYSTEM_DECL static path generic_path_v4(path const& p);
  87. #if defined(BOOST_WINDOWS_API)
  88. BOOST_FILESYSTEM_DECL static void make_preferred_v3(path& p);
  89. BOOST_FILESYSTEM_DECL static void make_preferred_v4(path& p);
  90. #endif
  91. BOOST_FILESYSTEM_DECL static int compare_v3(path const& left, path const& right);
  92. BOOST_FILESYSTEM_DECL static int compare_v4(path const& left, path const& right);
  93. BOOST_FILESYSTEM_DECL static void append_v3(path& p, const value_type* b, const value_type* e);
  94. BOOST_FILESYSTEM_DECL static void append_v4(path& p, const value_type* b, const value_type* e);
  95. static void append_v4(path& left, path const& right);
  96. // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0.
  97. // Note: An append is never performed if size()==0, so a returned 0 is unambiguous.
  98. BOOST_FILESYSTEM_DECL static string_type::size_type append_separator_if_needed(path& p);
  99. BOOST_FILESYSTEM_DECL static void erase_redundant_separator(path& p, string_type::size_type sep_pos);
  100. BOOST_FILESYSTEM_DECL static string_type::size_type find_root_name_size(path const& p);
  101. BOOST_FILESYSTEM_DECL static string_type::size_type find_root_path_size(path const& p);
  102. BOOST_FILESYSTEM_DECL static substring find_root_directory(path const& p);
  103. BOOST_FILESYSTEM_DECL static substring find_relative_path(path const& p);
  104. BOOST_FILESYSTEM_DECL static string_type::size_type find_parent_path_size(path const& p);
  105. BOOST_FILESYSTEM_DECL static string_type::size_type find_filename_v4_size(path const& p);
  106. BOOST_FILESYSTEM_DECL static string_type::size_type find_extension_v4_size(path const& p);
  107. BOOST_FILESYSTEM_DECL static int lex_compare_v3
  108. (
  109. path_detail::path_iterator first1, path_detail::path_iterator const& last1,
  110. path_detail::path_iterator first2, path_detail::path_iterator const& last2
  111. );
  112. BOOST_FILESYSTEM_DECL static int lex_compare_v4
  113. (
  114. path_detail::path_iterator first1, path_detail::path_iterator const& last1,
  115. path_detail::path_iterator first2, path_detail::path_iterator const& last2
  116. );
  117. BOOST_FILESYSTEM_DECL static void increment_v3(path_detail::path_iterator& it);
  118. BOOST_FILESYSTEM_DECL static void increment_v4(path_detail::path_iterator& it);
  119. BOOST_FILESYSTEM_DECL static void decrement_v3(path_detail::path_iterator& it);
  120. BOOST_FILESYSTEM_DECL static void decrement_v4(path_detail::path_iterator& it);
  121. };
  122. } // namespace detail
  123. //------------------------------------------------------------------------------------//
  124. // //
  125. // class path //
  126. // //
  127. //------------------------------------------------------------------------------------//
  128. class path :
  129. public filesystem::path_detail::path_constants<
  130. #ifdef BOOST_WINDOWS_API
  131. detail::path_traits::path_native_char_type, L'/', L'\\', L'.'
  132. #else
  133. detail::path_traits::path_native_char_type, '/', '/', '.'
  134. #endif
  135. >
  136. {
  137. friend class path_detail::path_iterator;
  138. friend class path_detail::path_reverse_iterator;
  139. friend struct detail::path_algorithms;
  140. public:
  141. // value_type is the character type used by the operating system API to
  142. // represent paths.
  143. typedef detail::path_algorithms::value_type value_type;
  144. typedef detail::path_algorithms::string_type string_type;
  145. typedef detail::path_traits::codecvt_type codecvt_type;
  146. // ----- character encoding conversions -----
  147. // Following the principle of least astonishment, path input arguments
  148. // passed to or obtained from the operating system via objects of
  149. // class path behave as if they were directly passed to or
  150. // obtained from the O/S API, unless conversion is explicitly requested.
  151. //
  152. // POSIX specfies that path strings are passed unchanged to and from the
  153. // API. Note that this is different from the POSIX command line utilities,
  154. // which convert according to a locale.
  155. //
  156. // Thus for POSIX, char strings do not undergo conversion. wchar_t strings
  157. // are converted to/from char using the path locale or, if a conversion
  158. // argument is given, using a conversion object modeled on
  159. // std::wstring_convert.
  160. //
  161. // The path locale, which is global to the thread, can be changed by the
  162. // imbue() function. It is initialized to an implementation defined locale.
  163. //
  164. // For Windows, wchar_t strings do not undergo conversion. char strings
  165. // are converted using the "ANSI" or "OEM" code pages, as determined by
  166. // the AreFileApisANSI() function, or, if a conversion argument is given,
  167. // using a conversion object modeled on std::wstring_convert.
  168. //
  169. // See m_pathname comments for further important rationale.
  170. // TODO: rules needed for operating systems that use / or .
  171. // differently, or format directory paths differently from file paths.
  172. //
  173. // **********************************************************************************
  174. //
  175. // More work needed: How to handle an operating system that may have
  176. // slash characters or dot characters in valid filenames, either because
  177. // it doesn't follow the POSIX standard, or because it allows MBCS
  178. // filename encodings that may contain slash or dot characters. For
  179. // example, ISO/IEC 2022 (JIS) encoding which allows switching to
  180. // JIS x0208-1983 encoding. A valid filename in this set of encodings is
  181. // 0x1B 0x24 0x42 [switch to X0208-1983] 0x24 0x2F [U+304F Kiragana letter KU]
  182. // ^^^^
  183. // Note that 0x2F is the ASCII slash character
  184. //
  185. // **********************************************************************************
  186. // Supported source arguments: half-open iterator range, container, c-array,
  187. // and single pointer to null terminated string.
  188. // All source arguments except pointers to null terminated byte strings support
  189. // multi-byte character strings which may have embedded nulls. Embedded null
  190. // support is required for some Asian languages on Windows.
  191. // "const codecvt_type& cvt=codecvt()" default arguments are not used because this
  192. // limits the impact of locale("") initialization failures on POSIX systems to programs
  193. // that actually depend on locale(""). It further ensures that exceptions thrown
  194. // as a result of such failues occur after main() has started, so can be caught.
  195. private:
  196. //! Assignment operation
  197. class assign_op
  198. {
  199. private:
  200. path& m_self;
  201. public:
  202. typedef void result_type;
  203. explicit assign_op(path& self) noexcept : m_self(self) {}
  204. result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const
  205. {
  206. m_self.m_pathname.assign(source, source_end);
  207. }
  208. template< typename OtherChar >
  209. result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const
  210. {
  211. m_self.m_pathname.clear();
  212. detail::path_traits::convert(source, source_end, m_self.m_pathname, cvt);
  213. }
  214. };
  215. //! Concatenation operation
  216. class concat_op
  217. {
  218. private:
  219. path& m_self;
  220. public:
  221. typedef void result_type;
  222. explicit concat_op(path& self) noexcept : m_self(self) {}
  223. result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const
  224. {
  225. m_self.m_pathname.append(source, source_end);
  226. }
  227. template< typename OtherChar >
  228. result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const
  229. {
  230. detail::path_traits::convert(source, source_end, m_self.m_pathname, cvt);
  231. }
  232. };
  233. //! Path appending operation
  234. class append_op
  235. {
  236. private:
  237. path& m_self;
  238. public:
  239. typedef void result_type;
  240. explicit append_op(path& self) noexcept : m_self(self) {}
  241. BOOST_FORCEINLINE result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const
  242. {
  243. m_self.append(source, source_end);
  244. }
  245. template< typename OtherChar >
  246. BOOST_FORCEINLINE result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const
  247. {
  248. string_type src;
  249. detail::path_traits::convert(source, source_end, src, cvt);
  250. m_self.append(src.data(), src.data() + src.size());
  251. }
  252. };
  253. //! Path comparison operation
  254. class compare_op
  255. {
  256. private:
  257. path const& m_self;
  258. public:
  259. typedef int result_type;
  260. explicit compare_op(path const& self) noexcept : m_self(self) {}
  261. result_type operator() (const value_type* source, const value_type* source_end, const codecvt_type* = nullptr) const;
  262. template< typename OtherChar >
  263. result_type operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt = nullptr) const;
  264. };
  265. public:
  266. typedef path_detail::path_iterator iterator;
  267. typedef iterator const_iterator;
  268. typedef path_detail::path_reverse_iterator reverse_iterator;
  269. typedef reverse_iterator const_reverse_iterator;
  270. public:
  271. // ----- constructors -----
  272. path() noexcept {}
  273. path(path const& p) : m_pathname(p.m_pathname) {}
  274. path(path const& p, codecvt_type const&) : m_pathname(p.m_pathname) {}
  275. path(const value_type* s) : m_pathname(s) {}
  276. path(const value_type* s, codecvt_type const&) : m_pathname(s) {}
  277. path(string_type const& s) : m_pathname(s) {}
  278. path(string_type const& s, codecvt_type const&) : m_pathname(s) {}
  279. #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
  280. path(std::basic_string_view< value_type > const& s) : m_pathname(s) {}
  281. path(std::basic_string_view< value_type > const& s, codecvt_type const&) : m_pathname(s) {}
  282. #endif
  283. template<
  284. typename Source,
  285. typename = typename std::enable_if<
  286. detail::conjunction<
  287. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >,
  288. detail::negation< detail::path_traits::is_native_path_source< typename std::remove_cv< Source >::type > >
  289. >::value
  290. >::type
  291. >
  292. path(Source const& source)
  293. {
  294. assign(source);
  295. }
  296. template<
  297. typename Source,
  298. typename = typename std::enable_if<
  299. detail::conjunction<
  300. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >,
  301. detail::negation< detail::path_traits::is_native_path_source< typename std::remove_cv< Source >::type > >
  302. >::value
  303. >::type
  304. >
  305. explicit path(Source const& source, codecvt_type const& cvt)
  306. {
  307. assign(source, cvt);
  308. }
  309. path(path&& p) noexcept : m_pathname(static_cast< string_type&& >(p.m_pathname))
  310. {
  311. }
  312. path(path&& p, codecvt_type const&) noexcept : m_pathname(static_cast< string_type&& >(p.m_pathname))
  313. {
  314. }
  315. path& operator=(path&& p) noexcept
  316. {
  317. m_pathname = static_cast< string_type&& >(p.m_pathname);
  318. return *this;
  319. }
  320. path& assign(path&& p) noexcept
  321. {
  322. m_pathname = static_cast< string_type&& >(p.m_pathname);
  323. return *this;
  324. }
  325. path& assign(path&& p, codecvt_type const&) noexcept
  326. {
  327. m_pathname = static_cast< string_type&& >(p.m_pathname);
  328. return *this;
  329. }
  330. path(string_type&& s) noexcept : m_pathname(static_cast< string_type&& >(s))
  331. {
  332. }
  333. path(string_type&& s, codecvt_type const&) noexcept : m_pathname(static_cast< string_type&& >(s))
  334. {
  335. }
  336. path& operator=(string_type&& p) noexcept
  337. {
  338. m_pathname = static_cast< string_type&& >(p);
  339. return *this;
  340. }
  341. path& assign(string_type&& p) noexcept
  342. {
  343. m_pathname = static_cast< string_type&& >(p);
  344. return *this;
  345. }
  346. path& assign(string_type&& p, codecvt_type const&) noexcept
  347. {
  348. m_pathname = static_cast< string_type&& >(p);
  349. return *this;
  350. }
  351. path(const value_type* begin, const value_type* end) : m_pathname(begin, end) {}
  352. path(const value_type* begin, const value_type* end, codecvt_type const&) : m_pathname(begin, end) {}
  353. template<
  354. typename InputIterator,
  355. typename = typename std::enable_if<
  356. detail::conjunction<
  357. detail::path_traits::is_path_source_iterator< InputIterator >,
  358. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  359. >::value
  360. >::type
  361. >
  362. path(InputIterator begin, InputIterator end)
  363. {
  364. if (begin != end)
  365. {
  366. typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
  367. source_t source(begin, end);
  368. assign(static_cast< source_t&& >(source));
  369. }
  370. }
  371. template<
  372. typename InputIterator,
  373. typename = typename std::enable_if<
  374. detail::conjunction<
  375. detail::path_traits::is_path_source_iterator< InputIterator >,
  376. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  377. >::value
  378. >::type
  379. >
  380. path(InputIterator begin, InputIterator end, codecvt_type const& cvt)
  381. {
  382. if (begin != end)
  383. {
  384. typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
  385. source_t source(begin, end);
  386. assign(static_cast< source_t&& >(source), cvt);
  387. }
  388. }
  389. path(std::nullptr_t) = delete;
  390. path& operator= (std::nullptr_t) = delete;
  391. public:
  392. // ----- assignments -----
  393. // We need to explicitly define copy assignment as otherwise it will be implicitly defined as deleted because there is move assignment
  394. path& operator=(path const& p);
  395. template< typename Source >
  396. typename std::enable_if<
  397. detail::disjunction<
  398. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >,
  399. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  400. >::value,
  401. path&
  402. >::type operator=(Source const& source)
  403. {
  404. return assign(source);
  405. }
  406. path& assign(path const& p)
  407. {
  408. m_pathname = p.m_pathname;
  409. return *this;
  410. }
  411. template< typename Source >
  412. typename std::enable_if<
  413. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  414. path&
  415. >::type assign(Source const& source)
  416. {
  417. detail::path_traits::dispatch(source, assign_op(*this));
  418. return *this;
  419. }
  420. template< typename Source >
  421. typename std::enable_if<
  422. detail::conjunction<
  423. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  424. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  425. >::value,
  426. path&
  427. >::type assign(Source const& source)
  428. {
  429. detail::path_traits::dispatch_convertible(source, assign_op(*this));
  430. return *this;
  431. }
  432. path& assign(path const& p, codecvt_type const&)
  433. {
  434. m_pathname = p.m_pathname;
  435. return *this;
  436. }
  437. template< typename Source >
  438. typename std::enable_if<
  439. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  440. path&
  441. >::type assign(Source const& source, codecvt_type const& cvt)
  442. {
  443. detail::path_traits::dispatch(source, assign_op(*this), &cvt);
  444. return *this;
  445. }
  446. template< typename Source >
  447. typename std::enable_if<
  448. detail::conjunction<
  449. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  450. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  451. >::value,
  452. path&
  453. >::type assign(Source const& source, codecvt_type const& cvt)
  454. {
  455. detail::path_traits::dispatch_convertible(source, assign_op(*this), &cvt);
  456. return *this;
  457. }
  458. path& assign(const value_type* begin, const value_type* end)
  459. {
  460. m_pathname.assign(begin, end);
  461. return *this;
  462. }
  463. template< typename InputIterator >
  464. typename std::enable_if<
  465. detail::conjunction<
  466. detail::path_traits::is_path_source_iterator< InputIterator >,
  467. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  468. >::value,
  469. path&
  470. >::type assign(InputIterator begin, InputIterator end)
  471. {
  472. m_pathname.clear();
  473. if (begin != end)
  474. {
  475. typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
  476. source_t source(begin, end);
  477. assign(static_cast< source_t&& >(source));
  478. }
  479. return *this;
  480. }
  481. path& assign(const value_type* begin, const value_type* end, codecvt_type const&)
  482. {
  483. m_pathname.assign(begin, end);
  484. return *this;
  485. }
  486. template< typename InputIterator >
  487. typename std::enable_if<
  488. detail::conjunction<
  489. detail::path_traits::is_path_source_iterator< InputIterator >,
  490. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  491. >::value,
  492. path&
  493. >::type assign(InputIterator begin, InputIterator end, codecvt_type const& cvt)
  494. {
  495. m_pathname.clear();
  496. if (begin != end)
  497. {
  498. typedef std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source_t;
  499. source_t source(begin, end);
  500. assign(static_cast< source_t&& >(source), cvt);
  501. }
  502. return *this;
  503. }
  504. // ----- concatenation -----
  505. path& operator+=(path const& p);
  506. template< typename Source >
  507. typename std::enable_if<
  508. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >::value,
  509. path&
  510. >::type operator+=(Source const& source)
  511. {
  512. return concat(source);
  513. }
  514. path& operator+=(value_type c)
  515. {
  516. m_pathname.push_back(c);
  517. return *this;
  518. }
  519. template< typename CharT >
  520. typename std::enable_if<
  521. detail::path_traits::is_path_char_type< CharT >::value,
  522. path&
  523. >::type operator+=(CharT c)
  524. {
  525. CharT tmp[2];
  526. tmp[0] = c;
  527. tmp[1] = static_cast< CharT >(0);
  528. concat_op(*this)(tmp, tmp + 1);
  529. return *this;
  530. }
  531. path& concat(path const& p)
  532. {
  533. m_pathname.append(p.m_pathname);
  534. return *this;
  535. }
  536. template< typename Source >
  537. typename std::enable_if<
  538. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  539. path&
  540. >::type concat(Source const& source)
  541. {
  542. detail::path_traits::dispatch(source, concat_op(*this));
  543. return *this;
  544. }
  545. template< typename Source >
  546. typename std::enable_if<
  547. detail::conjunction<
  548. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  549. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  550. >::value,
  551. path&
  552. >::type concat(Source const& source)
  553. {
  554. detail::path_traits::dispatch_convertible(source, concat_op(*this));
  555. return *this;
  556. }
  557. path& concat(path const& p, codecvt_type const&)
  558. {
  559. m_pathname.append(p.m_pathname);
  560. return *this;
  561. }
  562. template< typename Source >
  563. typename std::enable_if<
  564. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  565. path&
  566. >::type concat(Source const& source, codecvt_type const& cvt)
  567. {
  568. detail::path_traits::dispatch(source, concat_op(*this), &cvt);
  569. return *this;
  570. }
  571. template< typename Source >
  572. typename std::enable_if<
  573. detail::conjunction<
  574. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  575. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  576. >::value,
  577. path&
  578. >::type concat(Source const& source, codecvt_type const& cvt)
  579. {
  580. detail::path_traits::dispatch_convertible(source, concat_op(*this), &cvt);
  581. return *this;
  582. }
  583. path& concat(const value_type* begin, const value_type* end)
  584. {
  585. m_pathname.append(begin, end);
  586. return *this;
  587. }
  588. template< typename InputIterator >
  589. typename std::enable_if<
  590. detail::conjunction<
  591. detail::path_traits::is_path_source_iterator< InputIterator >,
  592. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  593. >::value,
  594. path&
  595. >::type concat(InputIterator begin, InputIterator end)
  596. {
  597. if (begin != end)
  598. {
  599. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
  600. detail::path_traits::dispatch(source, concat_op(*this));
  601. }
  602. return *this;
  603. }
  604. path& concat(const value_type* begin, const value_type* end, codecvt_type const&)
  605. {
  606. m_pathname.append(begin, end);
  607. return *this;
  608. }
  609. template< typename InputIterator >
  610. typename std::enable_if<
  611. detail::conjunction<
  612. detail::path_traits::is_path_source_iterator< InputIterator >,
  613. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  614. >::value,
  615. path&
  616. >::type concat(InputIterator begin, InputIterator end, codecvt_type const& cvt)
  617. {
  618. if (begin != end)
  619. {
  620. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
  621. detail::path_traits::dispatch(source, concat_op(*this), &cvt);
  622. }
  623. return *this;
  624. }
  625. // ----- appends -----
  626. // if a separator is added, it is the preferred separator for the platform;
  627. // slash for POSIX, backslash for Windows
  628. path& operator/=(path const& p);
  629. template< typename Source >
  630. BOOST_FORCEINLINE typename std::enable_if<
  631. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >::value,
  632. path&
  633. >::type operator/=(Source const& source)
  634. {
  635. return append(source);
  636. }
  637. path& append(path const& p);
  638. template< typename Source >
  639. BOOST_FORCEINLINE typename std::enable_if<
  640. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  641. path&
  642. >::type append(Source const& source)
  643. {
  644. detail::path_traits::dispatch(source, append_op(*this));
  645. return *this;
  646. }
  647. template< typename Source >
  648. BOOST_FORCEINLINE typename std::enable_if<
  649. detail::conjunction<
  650. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  651. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  652. >::value,
  653. path&
  654. >::type append(Source const& source)
  655. {
  656. detail::path_traits::dispatch_convertible(source, append_op(*this));
  657. return *this;
  658. }
  659. path& append(path const& p, codecvt_type const&);
  660. template< typename Source >
  661. BOOST_FORCEINLINE typename std::enable_if<
  662. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  663. path&
  664. >::type append(Source const& source, codecvt_type const& cvt)
  665. {
  666. detail::path_traits::dispatch(source, append_op(*this), &cvt);
  667. return *this;
  668. }
  669. template< typename Source >
  670. BOOST_FORCEINLINE typename std::enable_if<
  671. detail::conjunction<
  672. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  673. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  674. >::value,
  675. path&
  676. >::type append(Source const& source, codecvt_type const& cvt)
  677. {
  678. detail::path_traits::dispatch_convertible(source, append_op(*this), &cvt);
  679. return *this;
  680. }
  681. path& append(const value_type* begin, const value_type* end);
  682. template< typename InputIterator >
  683. BOOST_FORCEINLINE typename std::enable_if<
  684. detail::conjunction<
  685. detail::path_traits::is_path_source_iterator< InputIterator >,
  686. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  687. >::value,
  688. path&
  689. >::type append(InputIterator begin, InputIterator end)
  690. {
  691. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
  692. detail::path_traits::dispatch(source, append_op(*this));
  693. return *this;
  694. }
  695. path& append(const value_type* begin, const value_type* end, codecvt_type const&);
  696. template< typename InputIterator >
  697. BOOST_FORCEINLINE typename std::enable_if<
  698. detail::conjunction<
  699. detail::path_traits::is_path_source_iterator< InputIterator >,
  700. detail::negation< detail::path_traits::is_native_char_ptr< InputIterator > >
  701. >::value,
  702. path&
  703. >::type append(InputIterator begin, InputIterator end, const codecvt_type& cvt)
  704. {
  705. std::basic_string< typename std::iterator_traits< InputIterator >::value_type > source(begin, end);
  706. detail::path_traits::dispatch(source, append_op(*this), &cvt);
  707. return *this;
  708. }
  709. // ----- modifiers -----
  710. void clear() noexcept { m_pathname.clear(); }
  711. path& make_preferred();
  712. path& remove_filename();
  713. BOOST_FILESYSTEM_DECL path& remove_filename_and_trailing_separators();
  714. BOOST_FILESYSTEM_DECL path& remove_trailing_separator();
  715. BOOST_FILESYSTEM_DECL path& replace_filename(path const& replacement);
  716. path& replace_extension(path const& new_extension = path());
  717. void swap(path& rhs) noexcept { m_pathname.swap(rhs.m_pathname); }
  718. // ----- observers -----
  719. // For operating systems that format file paths differently than directory
  720. // paths, return values from observers are formatted as file names unless there
  721. // is a trailing separator, in which case returns are formatted as directory
  722. // paths. POSIX and Windows make no such distinction.
  723. // Implementations are permitted to return const values or const references.
  724. // The string or path returned by an observer are specified as being formatted
  725. // as "native" or "generic".
  726. //
  727. // For POSIX, these are all the same format; slashes and backslashes are as input and
  728. // are not modified.
  729. //
  730. // For Windows, native: as input; slashes and backslashes are not modified;
  731. // this is the format of the internally stored string.
  732. // generic: backslashes are converted to slashes
  733. // ----- native format observers -----
  734. string_type const& native() const noexcept { return m_pathname; }
  735. const value_type* c_str() const noexcept { return m_pathname.c_str(); }
  736. string_type::size_type size() const noexcept { return m_pathname.size(); }
  737. template< typename String >
  738. String string() const;
  739. template< typename String >
  740. String string(codecvt_type const& cvt) const;
  741. #ifdef BOOST_WINDOWS_API
  742. std::string string() const
  743. {
  744. std::string tmp;
  745. if (!m_pathname.empty())
  746. detail::path_traits::convert(m_pathname.data(), m_pathname.data() + m_pathname.size(), tmp);
  747. return tmp;
  748. }
  749. std::string string(codecvt_type const& cvt) const
  750. {
  751. std::string tmp;
  752. if (!m_pathname.empty())
  753. detail::path_traits::convert(m_pathname.data(), m_pathname.data() + m_pathname.size(), tmp, &cvt);
  754. return tmp;
  755. }
  756. // string_type is std::wstring, so there is no conversion
  757. std::wstring const& wstring() const { return m_pathname; }
  758. std::wstring const& wstring(codecvt_type const&) const { return m_pathname; }
  759. #else // BOOST_POSIX_API
  760. // string_type is std::string, so there is no conversion
  761. std::string const& string() const { return m_pathname; }
  762. std::string const& string(codecvt_type const&) const { return m_pathname; }
  763. std::wstring wstring() const
  764. {
  765. std::wstring tmp;
  766. if (!m_pathname.empty())
  767. detail::path_traits::convert(m_pathname.data(), m_pathname.data() + m_pathname.size(), tmp);
  768. return tmp;
  769. }
  770. std::wstring wstring(codecvt_type const& cvt) const
  771. {
  772. std::wstring tmp;
  773. if (!m_pathname.empty())
  774. detail::path_traits::convert(m_pathname.data(), m_pathname.data() + m_pathname.size(), tmp, &cvt);
  775. return tmp;
  776. }
  777. #endif
  778. // ----- generic format observers -----
  779. // Experimental generic function returning generic formatted path (i.e. separators
  780. // are forward slashes). Motivation: simpler than a family of generic_*string
  781. // functions.
  782. path generic_path() const;
  783. template< typename String >
  784. String generic_string() const;
  785. template< typename String >
  786. String generic_string(codecvt_type const& cvt) const;
  787. std::string generic_string() const { return generic_path().string(); }
  788. std::string generic_string(codecvt_type const& cvt) const { return generic_path().string(cvt); }
  789. std::wstring generic_wstring() const { return generic_path().wstring(); }
  790. std::wstring generic_wstring(codecvt_type const& cvt) const { return generic_path().wstring(cvt); }
  791. // ----- compare -----
  792. int compare(path const& p) const; // generic, lexicographical
  793. template< typename Source >
  794. BOOST_FORCEINLINE typename std::enable_if<
  795. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  796. int
  797. >::type compare(Source const& source) const
  798. {
  799. return detail::path_traits::dispatch(source, compare_op(*this));
  800. }
  801. template< typename Source >
  802. BOOST_FORCEINLINE typename std::enable_if<
  803. detail::conjunction<
  804. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  805. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  806. >::value,
  807. int
  808. >::type compare(Source const& source) const
  809. {
  810. return detail::path_traits::dispatch_convertible(source, compare_op(*this));
  811. }
  812. template< typename Source >
  813. BOOST_FORCEINLINE typename std::enable_if<
  814. detail::path_traits::is_path_source< typename std::remove_cv< Source >::type >::value,
  815. int
  816. >::type compare(Source const& source, codecvt_type const& cvt) const
  817. {
  818. return detail::path_traits::dispatch(source, compare_op(*this), &cvt);
  819. }
  820. template< typename Source >
  821. BOOST_FORCEINLINE typename std::enable_if<
  822. detail::conjunction<
  823. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >,
  824. detail::negation< detail::path_traits::is_path_source< typename std::remove_cv< Source >::type > >
  825. >::value,
  826. int
  827. >::type compare(Source const& source, codecvt_type const& cvt) const
  828. {
  829. return detail::path_traits::dispatch_convertible(source, compare_op(*this), &cvt);
  830. }
  831. // ----- decomposition -----
  832. path root_path() const { return path(m_pathname.c_str(), m_pathname.c_str() + detail::path_algorithms::find_root_path_size(*this)); }
  833. // returns 0 or 1 element path even on POSIX, root_name() is non-empty() for network paths
  834. path root_name() const { return path(m_pathname.c_str(), m_pathname.c_str() + detail::path_algorithms::find_root_name_size(*this)); }
  835. // returns 0 or 1 element path
  836. path root_directory() const
  837. {
  838. detail::path_algorithms::substring root_dir = detail::path_algorithms::find_root_directory(*this);
  839. const value_type* p = m_pathname.c_str() + root_dir.pos;
  840. return path(p, p + root_dir.size);
  841. }
  842. path relative_path() const
  843. {
  844. detail::path_algorithms::substring rel_path = detail::path_algorithms::find_relative_path(*this);
  845. const value_type* p = m_pathname.c_str() + rel_path.pos;
  846. return path(p, p + rel_path.size);
  847. }
  848. path parent_path() const { return path(m_pathname.c_str(), m_pathname.c_str() + detail::path_algorithms::find_parent_path_size(*this)); }
  849. path filename() const; // returns 0 or 1 element path
  850. path stem() const; // returns 0 or 1 element path
  851. path extension() const; // returns 0 or 1 element path
  852. // ----- query -----
  853. bool empty() const noexcept { return m_pathname.empty(); }
  854. bool filename_is_dot() const;
  855. bool filename_is_dot_dot() const;
  856. bool has_root_path() const { return detail::path_algorithms::find_root_path_size(*this) > 0; }
  857. bool has_root_name() const { return detail::path_algorithms::find_root_name_size(*this) > 0; }
  858. bool has_root_directory() const { return detail::path_algorithms::find_root_directory(*this).size > 0; }
  859. bool has_relative_path() const { return detail::path_algorithms::find_relative_path(*this).size > 0; }
  860. bool has_parent_path() const { return detail::path_algorithms::find_parent_path_size(*this) > 0; }
  861. bool has_filename() const;
  862. bool has_stem() const { return !stem().empty(); }
  863. bool has_extension() const { return !extension().empty(); }
  864. bool is_relative() const { return !is_absolute(); }
  865. bool is_absolute() const
  866. {
  867. #if defined(BOOST_WINDOWS_API)
  868. return has_root_name() && has_root_directory();
  869. #else
  870. return has_root_directory();
  871. #endif
  872. }
  873. // ----- lexical operations -----
  874. path lexically_normal() const;
  875. BOOST_FILESYSTEM_DECL path lexically_relative(path const& base) const;
  876. path lexically_proximate(path const& base) const;
  877. // ----- iterators -----
  878. BOOST_FILESYSTEM_DECL iterator begin() const;
  879. BOOST_FILESYSTEM_DECL iterator end() const;
  880. reverse_iterator rbegin() const;
  881. reverse_iterator rend() const;
  882. // ----- static member functions -----
  883. static BOOST_FILESYSTEM_DECL std::locale imbue(std::locale const& loc);
  884. static BOOST_FILESYSTEM_DECL codecvt_type const& codecvt();
  885. //--------------------------------------------------------------------------------------//
  886. // class path private members //
  887. //--------------------------------------------------------------------------------------//
  888. private:
  889. /*
  890. * m_pathname has the type, encoding, and format required by the native
  891. * operating system. Thus for POSIX and Windows there is no conversion for
  892. * passing m_pathname.c_str() to the O/S API or when obtaining a path from the
  893. * O/S API. POSIX encoding is unspecified other than for dot and slash
  894. * characters; POSIX just treats paths as a sequence of bytes. Windows
  895. * encoding is UCS-2 or UTF-16 depending on the version.
  896. */
  897. string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes,
  898. // slashes NOT converted to backslashes
  899. };
  900. namespace detail {
  901. BOOST_FILESYSTEM_DECL path const& dot_path();
  902. BOOST_FILESYSTEM_DECL path const& dot_dot_path();
  903. } // namespace detail
  904. namespace path_detail {
  905. //------------------------------------------------------------------------------------//
  906. // class path::iterator //
  907. //------------------------------------------------------------------------------------//
  908. class path_iterator :
  909. public boost::iterator_facade<
  910. path_iterator,
  911. const path,
  912. boost::bidirectional_traversal_tag
  913. >
  914. {
  915. private:
  916. friend class boost::iterator_core_access;
  917. friend class boost::filesystem::path;
  918. friend class path_reverse_iterator;
  919. friend struct boost::filesystem::detail::path_algorithms;
  920. path const& dereference() const { return m_element; }
  921. bool equal(path_iterator const& rhs) const noexcept
  922. {
  923. return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos;
  924. }
  925. void increment();
  926. void decrement();
  927. private:
  928. // current element
  929. path m_element;
  930. // path being iterated over
  931. const path* m_path_ptr;
  932. // position of m_element in m_path_ptr->m_pathname.
  933. // if m_element is implicit dot, m_pos is the
  934. // position of the last separator in the path.
  935. // end() iterator is indicated by
  936. // m_pos == m_path_ptr->m_pathname.size()
  937. path::string_type::size_type m_pos;
  938. };
  939. //------------------------------------------------------------------------------------//
  940. // class path::reverse_iterator //
  941. //------------------------------------------------------------------------------------//
  942. class path_reverse_iterator :
  943. public boost::iterator_facade<
  944. path_reverse_iterator,
  945. const path,
  946. boost::bidirectional_traversal_tag
  947. >
  948. {
  949. public:
  950. explicit path_reverse_iterator(path_iterator itr) :
  951. m_itr(itr)
  952. {
  953. if (itr != itr.m_path_ptr->begin())
  954. m_element = *--itr;
  955. }
  956. private:
  957. friend class boost::iterator_core_access;
  958. friend class boost::filesystem::path;
  959. path const& dereference() const { return m_element; }
  960. bool equal(path_reverse_iterator const& rhs) const noexcept { return m_itr == rhs.m_itr; }
  961. void increment()
  962. {
  963. --m_itr;
  964. if (m_itr != m_itr.m_path_ptr->begin())
  965. {
  966. path_iterator tmp = m_itr;
  967. m_element = *--tmp;
  968. }
  969. }
  970. void decrement()
  971. {
  972. m_element = *m_itr;
  973. ++m_itr;
  974. }
  975. private:
  976. path_iterator m_itr;
  977. path m_element;
  978. };
  979. // std::lexicographical_compare would infinitely recurse because path iterators
  980. // yield paths, so provide a path aware version
  981. bool lexicographical_compare(path_iterator first1, path_iterator const& last1, path_iterator first2, path_iterator const& last2);
  982. } // namespace path_detail
  983. using path_detail::lexicographical_compare;
  984. //------------------------------------------------------------------------------------//
  985. // //
  986. // non-member functions //
  987. // //
  988. //------------------------------------------------------------------------------------//
  989. BOOST_FORCEINLINE bool operator==(path const& lhs, path const& rhs)
  990. {
  991. return lhs.compare(rhs) == 0;
  992. }
  993. template< typename Path, typename Source >
  994. BOOST_FORCEINLINE typename std::enable_if<
  995. detail::conjunction<
  996. std::is_same< Path, path >,
  997. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  998. >::value,
  999. bool
  1000. >::type operator==(Path const& lhs, Source const& rhs)
  1001. {
  1002. return lhs.compare(rhs) == 0;
  1003. }
  1004. template< typename Source, typename Path >
  1005. BOOST_FORCEINLINE typename std::enable_if<
  1006. detail::conjunction<
  1007. std::is_same< Path, path >,
  1008. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1009. >::value,
  1010. bool
  1011. >::type operator==(Source const& lhs, Path const& rhs)
  1012. {
  1013. return rhs.compare(lhs) == 0;
  1014. }
  1015. BOOST_FORCEINLINE bool operator!=(path const& lhs, path const& rhs)
  1016. {
  1017. return lhs.compare(rhs) != 0;
  1018. }
  1019. template< typename Path, typename Source >
  1020. BOOST_FORCEINLINE typename std::enable_if<
  1021. detail::conjunction<
  1022. std::is_same< Path, path >,
  1023. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1024. >::value,
  1025. bool
  1026. >::type operator!=(Path const& lhs, Source const& rhs)
  1027. {
  1028. return lhs.compare(rhs) != 0;
  1029. }
  1030. template< typename Source, typename Path >
  1031. BOOST_FORCEINLINE typename std::enable_if<
  1032. detail::conjunction<
  1033. std::is_same< Path, path >,
  1034. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1035. >::value,
  1036. bool
  1037. >::type operator!=(Source const& lhs, Path const& rhs)
  1038. {
  1039. return rhs.compare(lhs) != 0;
  1040. }
  1041. BOOST_FORCEINLINE bool operator<(path const& lhs, path const& rhs)
  1042. {
  1043. return lhs.compare(rhs) < 0;
  1044. }
  1045. template< typename Path, typename Source >
  1046. BOOST_FORCEINLINE typename std::enable_if<
  1047. detail::conjunction<
  1048. std::is_same< Path, path >,
  1049. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1050. >::value,
  1051. bool
  1052. >::type operator<(Path const& lhs, Source const& rhs)
  1053. {
  1054. return lhs.compare(rhs) < 0;
  1055. }
  1056. template< typename Source, typename Path >
  1057. BOOST_FORCEINLINE typename std::enable_if<
  1058. detail::conjunction<
  1059. std::is_same< Path, path >,
  1060. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1061. >::value,
  1062. bool
  1063. >::type operator<(Source const& lhs, Path const& rhs)
  1064. {
  1065. return rhs.compare(lhs) > 0;
  1066. }
  1067. BOOST_FORCEINLINE bool operator<=(path const& lhs, path const& rhs)
  1068. {
  1069. return lhs.compare(rhs) <= 0;
  1070. }
  1071. template< typename Path, typename Source >
  1072. BOOST_FORCEINLINE typename std::enable_if<
  1073. detail::conjunction<
  1074. std::is_same< Path, path >,
  1075. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1076. >::value,
  1077. bool
  1078. >::type operator<=(Path const& lhs, Source const& rhs)
  1079. {
  1080. return lhs.compare(rhs) <= 0;
  1081. }
  1082. template< typename Source, typename Path >
  1083. BOOST_FORCEINLINE typename std::enable_if<
  1084. detail::conjunction<
  1085. std::is_same< Path, path >,
  1086. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1087. >::value,
  1088. bool
  1089. >::type operator<=(Source const& lhs, Path const& rhs)
  1090. {
  1091. return rhs.compare(lhs) >= 0;
  1092. }
  1093. BOOST_FORCEINLINE bool operator>(path const& lhs, path const& rhs)
  1094. {
  1095. return lhs.compare(rhs) > 0;
  1096. }
  1097. template< typename Path, typename Source >
  1098. BOOST_FORCEINLINE typename std::enable_if<
  1099. detail::conjunction<
  1100. std::is_same< Path, path >,
  1101. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1102. >::value,
  1103. bool
  1104. >::type operator>(Path const& lhs, Source const& rhs)
  1105. {
  1106. return lhs.compare(rhs) > 0;
  1107. }
  1108. template< typename Source, typename Path >
  1109. BOOST_FORCEINLINE typename std::enable_if<
  1110. detail::conjunction<
  1111. std::is_same< Path, path >,
  1112. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1113. >::value,
  1114. bool
  1115. >::type operator>(Source const& lhs, Path const& rhs)
  1116. {
  1117. return rhs.compare(lhs) < 0;
  1118. }
  1119. BOOST_FORCEINLINE bool operator>=(path const& lhs, path const& rhs)
  1120. {
  1121. return lhs.compare(rhs) >= 0;
  1122. }
  1123. template< typename Path, typename Source >
  1124. BOOST_FORCEINLINE typename std::enable_if<
  1125. detail::conjunction<
  1126. std::is_same< Path, path >,
  1127. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1128. >::value,
  1129. bool
  1130. >::type operator>=(Path const& lhs, Source const& rhs)
  1131. {
  1132. return lhs.compare(rhs) >= 0;
  1133. }
  1134. template< typename Source, typename Path >
  1135. BOOST_FORCEINLINE typename std::enable_if<
  1136. detail::conjunction<
  1137. std::is_same< Path, path >,
  1138. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >
  1139. >::value,
  1140. bool
  1141. >::type operator>=(Source const& lhs, Path const& rhs)
  1142. {
  1143. return rhs.compare(lhs) <= 0;
  1144. }
  1145. // Note: Declared as a template to delay binding to Boost.ContainerHash functions and make the dependency optional
  1146. template< typename Path >
  1147. inline typename std::enable_if<
  1148. std::is_same< Path, path >::value,
  1149. std::size_t
  1150. >::type hash_value(Path const& p) noexcept
  1151. {
  1152. #ifdef BOOST_WINDOWS_API
  1153. std::size_t seed = 0u;
  1154. for (typename Path::value_type const* it = p.c_str(); *it; ++it)
  1155. hash_combine(seed, *it == L'/' ? L'\\' : *it);
  1156. return seed;
  1157. #else // BOOST_POSIX_API
  1158. return hash_range(p.native().begin(), p.native().end());
  1159. #endif
  1160. }
  1161. inline void swap(path& lhs, path& rhs) noexcept
  1162. {
  1163. lhs.swap(rhs);
  1164. }
  1165. BOOST_FORCEINLINE path operator/(path lhs, path const& rhs)
  1166. {
  1167. lhs.append(rhs);
  1168. return lhs;
  1169. }
  1170. template< typename Source >
  1171. BOOST_FORCEINLINE typename std::enable_if<
  1172. detail::path_traits::is_convertible_to_path_source< typename std::remove_cv< Source >::type >::value,
  1173. path
  1174. >::type operator/(path lhs, Source const& rhs)
  1175. {
  1176. lhs.append(rhs);
  1177. return lhs;
  1178. }
  1179. // inserters and extractors
  1180. // use boost::io::quoted() to handle spaces in paths
  1181. // use '&' as escape character to ease use for Windows paths
  1182. template< typename Char, typename Traits >
  1183. inline std::basic_ostream< Char, Traits >&
  1184. operator<<(std::basic_ostream< Char, Traits >& os, path const& p)
  1185. {
  1186. return os << boost::io::quoted(p.template string< std::basic_string< Char > >(), static_cast< Char >('&'));
  1187. }
  1188. template< typename Char, typename Traits >
  1189. inline std::basic_istream< Char, Traits >&
  1190. operator>>(std::basic_istream< Char, Traits >& is, path& p)
  1191. {
  1192. std::basic_string< Char > str;
  1193. is >> boost::io::quoted(str, static_cast< Char >('&'));
  1194. p = str;
  1195. return is;
  1196. }
  1197. // name_checks
  1198. // These functions are holdovers from version 1. It isn't clear they have much
  1199. // usefulness, or how to generalize them for later versions.
  1200. BOOST_FILESYSTEM_DECL bool portable_posix_name(std::string const& name);
  1201. BOOST_FILESYSTEM_DECL bool windows_name(std::string const& name);
  1202. BOOST_FILESYSTEM_DECL bool portable_name(std::string const& name);
  1203. BOOST_FILESYSTEM_DECL bool portable_directory_name(std::string const& name);
  1204. BOOST_FILESYSTEM_DECL bool portable_file_name(std::string const& name);
  1205. BOOST_FILESYSTEM_DECL bool native(std::string const& name);
  1206. namespace detail {
  1207. // For POSIX, is_directory_separator() and is_element_separator() are identical since
  1208. // a forward slash is the only valid directory separator and also the only valid
  1209. // element separator. For Windows, forward slash and back slash are the possible
  1210. // directory separators, but colon (example: "c:foo") is also an element separator.
  1211. inline bool is_directory_separator(path::value_type c) noexcept
  1212. {
  1213. return c == path::separator
  1214. #ifdef BOOST_WINDOWS_API
  1215. || c == path::preferred_separator
  1216. #endif
  1217. ;
  1218. }
  1219. inline bool is_element_separator(path::value_type c) noexcept
  1220. {
  1221. return c == path::separator
  1222. #ifdef BOOST_WINDOWS_API
  1223. || c == path::preferred_separator || c == L':'
  1224. #endif
  1225. ;
  1226. }
  1227. } // namespace detail
  1228. //------------------------------------------------------------------------------------//
  1229. // class path miscellaneous function implementations //
  1230. //------------------------------------------------------------------------------------//
  1231. namespace detail {
  1232. inline bool path_algorithms::has_filename_v3(path const& p)
  1233. {
  1234. return !p.m_pathname.empty();
  1235. }
  1236. inline bool path_algorithms::has_filename_v4(path const& p)
  1237. {
  1238. return path_algorithms::find_filename_v4_size(p) > 0;
  1239. }
  1240. inline path path_algorithms::filename_v4(path const& p)
  1241. {
  1242. string_type::size_type filename_size = path_algorithms::find_filename_v4_size(p);
  1243. string_type::size_type pos = p.m_pathname.size() - filename_size;
  1244. const value_type* ptr = p.m_pathname.c_str() + pos;
  1245. return path(ptr, ptr + filename_size);
  1246. }
  1247. inline path path_algorithms::extension_v4(path const& p)
  1248. {
  1249. string_type::size_type extension_size = path_algorithms::find_extension_v4_size(p);
  1250. string_type::size_type pos = p.m_pathname.size() - extension_size;
  1251. const value_type* ptr = p.m_pathname.c_str() + pos;
  1252. return path(ptr, ptr + extension_size);
  1253. }
  1254. inline void path_algorithms::append_v4(path& left, path const& right)
  1255. {
  1256. path_algorithms::append_v4(left, right.m_pathname.c_str(), right.m_pathname.c_str() + right.m_pathname.size());
  1257. }
  1258. } // namespace detail
  1259. // Note: Because of the range constructor in C++23 std::string_view that involves a check for contiguous_range concept,
  1260. // any non-template function call that requires a check whether the source argument (which may be fs::path)
  1261. // is convertible to std::string_view must be made after fs::path::iterator is defined. This includes overload
  1262. // resolution and SFINAE checks. Otherwise, the concept check result formally changes between fs::path::iterator
  1263. // is not defined and defined, which causes compilation errors with gcc 11 and later.
  1264. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106808
  1265. BOOST_FORCEINLINE path::compare_op::result_type path::compare_op::operator() (const value_type* source, const value_type* source_end, const codecvt_type*) const
  1266. {
  1267. path src;
  1268. src.m_pathname.assign(source, source_end);
  1269. return m_self.compare(src);
  1270. }
  1271. template< typename OtherChar >
  1272. BOOST_FORCEINLINE path::compare_op::result_type path::compare_op::operator() (const OtherChar* source, const OtherChar* source_end, const codecvt_type* cvt) const
  1273. {
  1274. path src;
  1275. detail::path_traits::convert(source, source_end, src.m_pathname, cvt);
  1276. return m_self.compare(src);
  1277. }
  1278. inline path& path::operator=(path const& p)
  1279. {
  1280. return assign(p);
  1281. }
  1282. inline path& path::operator+=(path const& p)
  1283. {
  1284. return concat(p);
  1285. }
  1286. BOOST_FORCEINLINE path& path::operator/=(path const& p)
  1287. {
  1288. return append(p);
  1289. }
  1290. inline path path::lexically_proximate(path const& base) const
  1291. {
  1292. path tmp(lexically_relative(base));
  1293. return tmp.empty() ? *this : tmp;
  1294. }
  1295. inline path::reverse_iterator path::rbegin() const
  1296. {
  1297. return reverse_iterator(end());
  1298. }
  1299. inline path::reverse_iterator path::rend() const
  1300. {
  1301. return reverse_iterator(begin());
  1302. }
  1303. inline bool path::filename_is_dot() const
  1304. {
  1305. // implicit dot is tricky, so actually call filename(); see path::filename() example
  1306. // in reference.html
  1307. path p(filename());
  1308. return p.size() == 1 && *p.c_str() == dot;
  1309. }
  1310. inline bool path::filename_is_dot_dot() const
  1311. {
  1312. 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]));
  1313. // use detail::is_element_separator() rather than detail::is_directory_separator
  1314. // to deal with "c:.." edge case on Windows when ':' acts as a separator
  1315. }
  1316. // The following functions are defined differently, depending on Boost.Filesystem version in use.
  1317. // To avoid ODR violation, these functions are not defined when the library itself is built.
  1318. // This makes sure they are not compiled when the library is built, and the only version there is
  1319. // is the one in user's code. Users are supposed to consistently use the same Boost.Filesystem version
  1320. // in all their translation units.
  1321. #if !defined(BOOST_FILESYSTEM_SOURCE)
  1322. BOOST_FORCEINLINE path& path::append(path const& p)
  1323. {
  1324. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(*this, p.m_pathname.data(), p.m_pathname.data() + p.m_pathname.size());
  1325. return *this;
  1326. }
  1327. BOOST_FORCEINLINE path& path::append(path const& p, codecvt_type const&)
  1328. {
  1329. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(*this, p.m_pathname.data(), p.m_pathname.data() + p.m_pathname.size());
  1330. return *this;
  1331. }
  1332. BOOST_FORCEINLINE path& path::append(const value_type* begin, const value_type* end)
  1333. {
  1334. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(*this, begin, end);
  1335. return *this;
  1336. }
  1337. BOOST_FORCEINLINE path& path::append(const value_type* begin, const value_type* end, codecvt_type const&)
  1338. {
  1339. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::append)(*this, begin, end);
  1340. return *this;
  1341. }
  1342. BOOST_FORCEINLINE path& path::remove_filename()
  1343. {
  1344. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::remove_filename)(*this);
  1345. return *this;
  1346. }
  1347. BOOST_FORCEINLINE path& path::replace_extension(path const& new_extension)
  1348. {
  1349. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::replace_extension)(*this, new_extension);
  1350. return *this;
  1351. }
  1352. BOOST_FORCEINLINE int path::compare(path const& p) const
  1353. {
  1354. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::compare)(*this, p);
  1355. }
  1356. BOOST_FORCEINLINE path path::filename() const
  1357. {
  1358. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::filename)(*this);
  1359. }
  1360. BOOST_FORCEINLINE path path::stem() const
  1361. {
  1362. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::stem)(*this);
  1363. }
  1364. BOOST_FORCEINLINE path path::extension() const
  1365. {
  1366. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::extension)(*this);
  1367. }
  1368. BOOST_FORCEINLINE bool path::has_filename() const
  1369. {
  1370. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::has_filename)(*this);
  1371. }
  1372. BOOST_FORCEINLINE path path::lexically_normal() const
  1373. {
  1374. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::lexically_normal)(*this);
  1375. }
  1376. BOOST_FORCEINLINE path path::generic_path() const
  1377. {
  1378. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::generic_path)(*this);
  1379. }
  1380. BOOST_FORCEINLINE path& path::make_preferred()
  1381. {
  1382. // No effect on POSIX
  1383. #if defined(BOOST_WINDOWS_API)
  1384. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::make_preferred)(*this);
  1385. #endif
  1386. return *this;
  1387. }
  1388. namespace path_detail {
  1389. BOOST_FORCEINLINE void path_iterator::increment()
  1390. {
  1391. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::increment)(*this);
  1392. }
  1393. BOOST_FORCEINLINE void path_iterator::decrement()
  1394. {
  1395. BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::decrement)(*this);
  1396. }
  1397. BOOST_FORCEINLINE bool lexicographical_compare(path_iterator first1, path_iterator const& last1, path_iterator first2, path_iterator const& last2)
  1398. {
  1399. return BOOST_FILESYSTEM_VERSIONED_SYM(detail::path_algorithms::lex_compare)(first1, last1, first2, last2) < 0;
  1400. }
  1401. } // namespace path_detail
  1402. #endif // !defined(BOOST_FILESYSTEM_SOURCE)
  1403. //--------------------------------------------------------------------------------------//
  1404. // class path member template specializations //
  1405. //--------------------------------------------------------------------------------------//
  1406. template< >
  1407. inline std::string path::string< std::string >() const
  1408. {
  1409. return string();
  1410. }
  1411. template< >
  1412. inline std::wstring path::string< std::wstring >() const
  1413. {
  1414. return wstring();
  1415. }
  1416. template< >
  1417. inline std::string path::string< std::string >(codecvt_type const& cvt) const
  1418. {
  1419. return string(cvt);
  1420. }
  1421. template< >
  1422. inline std::wstring path::string< std::wstring >(codecvt_type const& cvt) const
  1423. {
  1424. return wstring(cvt);
  1425. }
  1426. template< >
  1427. inline std::string path::generic_string< std::string >() const
  1428. {
  1429. return generic_string();
  1430. }
  1431. template< >
  1432. inline std::wstring path::generic_string< std::wstring >() const
  1433. {
  1434. return generic_wstring();
  1435. }
  1436. template< >
  1437. inline std::string path::generic_string< std::string >(codecvt_type const& cvt) const
  1438. {
  1439. return generic_string(cvt);
  1440. }
  1441. template< >
  1442. inline std::wstring path::generic_string< std::wstring >(codecvt_type const& cvt) const
  1443. {
  1444. return generic_wstring(cvt);
  1445. }
  1446. } // namespace filesystem
  1447. } // namespace boost
  1448. //----------------------------------------------------------------------------//
  1449. #include <boost/filesystem/detail/footer.hpp>
  1450. #endif // BOOST_FILESYSTEM_PATH_HPP