path.hpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. // filesystem path.hpp ---------------------------------------------------------------//
  2. // Copyright Beman Dawes 2002-2005, 2009
  3. // Copyright Vladimir Prus 2002
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. // Library home page: http://www.boost.org/libs/filesystem
  7. // path::stem(), extension(), and replace_extension() are based on
  8. // basename(), extension(), and change_extension() from the original
  9. // filesystem/convenience.hpp header by Vladimir Prus.
  10. #ifndef BOOST_FILESYSTEM_PATH_HPP
  11. #define BOOST_FILESYSTEM_PATH_HPP
  12. #include <boost/config.hpp>
  13. # if defined( BOOST_NO_STD_WSTRING )
  14. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  15. # endif
  16. #include <boost/assert.hpp>
  17. #include <boost/filesystem/config.hpp>
  18. #include <boost/filesystem/path_traits.hpp> // includes <cwchar>
  19. #include <boost/system/error_code.hpp>
  20. #include <boost/system/system_error.hpp>
  21. #include <boost/iterator/iterator_facade.hpp>
  22. #include <boost/core/enable_if.hpp>
  23. #include <boost/io/detail/quoted_manip.hpp>
  24. #include <boost/functional/hash_fwd.hpp>
  25. #include <boost/type_traits/is_integral.hpp>
  26. #include <string>
  27. #include <iterator>
  28. #include <cstring>
  29. #include <iosfwd>
  30. #include <stdexcept>
  31. #include <cassert>
  32. #include <locale>
  33. #include <algorithm>
  34. #include <boost/config/abi_prefix.hpp> // must be the last #include
  35. namespace boost
  36. {
  37. namespace filesystem
  38. {
  39. namespace path_detail // intentionally don't use filesystem::detail to not bring internal Boost.Filesystem functions into ADL via path_constants
  40. {
  41. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  42. struct path_constants
  43. {
  44. typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
  45. typedef Char value_type;
  46. static BOOST_CONSTEXPR_OR_CONST value_type separator = Separator;
  47. static BOOST_CONSTEXPR_OR_CONST value_type preferred_separator = PreferredSeparator;
  48. static BOOST_CONSTEXPR_OR_CONST value_type dot = Dot;
  49. };
  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 >::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 >::preferred_separator;
  56. template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
  57. BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
  58. path_constants< Char, Separator, PreferredSeparator, Dot >::dot;
  59. } // namespace path_detail
  60. //------------------------------------------------------------------------------------//
  61. // //
  62. // class path //
  63. // //
  64. //------------------------------------------------------------------------------------//
  65. class path :
  66. public filesystem::path_detail::path_constants<
  67. #ifdef BOOST_WINDOWS_API
  68. wchar_t, L'/', L'\\', L'.'
  69. #else
  70. char, '/', '/', '.'
  71. #endif
  72. >
  73. {
  74. public:
  75. // value_type is the character type used by the operating system API to
  76. // represent paths.
  77. typedef path_constants_base::value_type value_type;
  78. typedef std::basic_string<value_type> string_type;
  79. typedef std::codecvt<wchar_t, char,
  80. std::mbstate_t> codecvt_type;
  81. // ----- character encoding conversions -----
  82. // Following the principle of least astonishment, path input arguments
  83. // passed to or obtained from the operating system via objects of
  84. // class path behave as if they were directly passed to or
  85. // obtained from the O/S API, unless conversion is explicitly requested.
  86. //
  87. // POSIX specfies that path strings are passed unchanged to and from the
  88. // API. Note that this is different from the POSIX command line utilities,
  89. // which convert according to a locale.
  90. //
  91. // Thus for POSIX, char strings do not undergo conversion. wchar_t strings
  92. // are converted to/from char using the path locale or, if a conversion
  93. // argument is given, using a conversion object modeled on
  94. // std::wstring_convert.
  95. //
  96. // The path locale, which is global to the thread, can be changed by the
  97. // imbue() function. It is initialized to an implementation defined locale.
  98. //
  99. // For Windows, wchar_t strings do not undergo conversion. char strings
  100. // are converted using the "ANSI" or "OEM" code pages, as determined by
  101. // the AreFileApisANSI() function, or, if a conversion argument is given,
  102. // using a conversion object modeled on std::wstring_convert.
  103. //
  104. // See m_pathname comments for further important rationale.
  105. // TODO: rules needed for operating systems that use / or .
  106. // differently, or format directory paths differently from file paths.
  107. //
  108. // **********************************************************************************
  109. //
  110. // More work needed: How to handle an operating system that may have
  111. // slash characters or dot characters in valid filenames, either because
  112. // it doesn't follow the POSIX standard, or because it allows MBCS
  113. // filename encodings that may contain slash or dot characters. For
  114. // example, ISO/IEC 2022 (JIS) encoding which allows switching to
  115. // JIS x0208-1983 encoding. A valid filename in this set of encodings is
  116. // 0x1B 0x24 0x42 [switch to X0208-1983] 0x24 0x2F [U+304F Kiragana letter KU]
  117. // ^^^^
  118. // Note that 0x2F is the ASCII slash character
  119. //
  120. // **********************************************************************************
  121. // Supported source arguments: half-open iterator range, container, c-array,
  122. // and single pointer to null terminated string.
  123. // All source arguments except pointers to null terminated byte strings support
  124. // multi-byte character strings which may have embedded nulls. Embedded null
  125. // support is required for some Asian languages on Windows.
  126. // "const codecvt_type& cvt=codecvt()" default arguments are not used because this
  127. // limits the impact of locale("") initialization failures on POSIX systems to programs
  128. // that actually depend on locale(""). It further ensures that exceptions thrown
  129. // as a result of such failues occur after main() has started, so can be caught.
  130. // ----- constructors -----
  131. path() BOOST_NOEXCEPT {}
  132. path(const path& p) : m_pathname(p.m_pathname) {}
  133. template <class Source>
  134. path(Source const& source,
  135. typename boost::enable_if<path_traits::is_pathable<
  136. typename boost::decay<Source>::type> >::type* =0)
  137. {
  138. path_traits::dispatch(source, m_pathname);
  139. }
  140. path(const value_type* s) : m_pathname(s) {}
  141. path(value_type* s) : m_pathname(s) {}
  142. path(const string_type& s) : m_pathname(s) {}
  143. path(string_type& s) : m_pathname(s) {}
  144. // As of October 2015 the interaction between noexcept and =default is so troublesome
  145. // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
  146. // functions. GCC is not even consistent for the same release on different platforms.
  147. # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  148. path(path&& p) BOOST_NOEXCEPT : m_pathname(std::move(p.m_pathname)) {}
  149. path& operator=(path&& p) BOOST_NOEXCEPT
  150. { m_pathname = std::move(p.m_pathname); return *this; }
  151. # endif
  152. template <class Source>
  153. path(Source const& source, const codecvt_type& cvt)
  154. {
  155. path_traits::dispatch(source, m_pathname, cvt);
  156. }
  157. template <class InputIterator>
  158. path(InputIterator begin, InputIterator end)
  159. {
  160. if (begin != end)
  161. {
  162. // convert requires contiguous string, so copy
  163. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  164. seq(begin, end);
  165. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
  166. }
  167. }
  168. template <class InputIterator>
  169. path(InputIterator begin, InputIterator end, const codecvt_type& cvt)
  170. {
  171. if (begin != end)
  172. {
  173. // convert requires contiguous string, so copy
  174. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  175. seq(begin, end);
  176. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
  177. }
  178. }
  179. // ----- assignments -----
  180. path& operator=(const path& p)
  181. {
  182. m_pathname = p.m_pathname;
  183. return *this;
  184. }
  185. template <class Source>
  186. typename boost::enable_if<path_traits::is_pathable<
  187. typename boost::decay<Source>::type>, path&>::type
  188. operator=(Source const& source)
  189. {
  190. m_pathname.clear();
  191. path_traits::dispatch(source, m_pathname);
  192. return *this;
  193. }
  194. // value_type overloads
  195. path& operator=(const value_type* ptr) // required in case ptr overlaps *this
  196. {m_pathname = ptr; return *this;}
  197. path& operator=(value_type* ptr) // required in case ptr overlaps *this
  198. {m_pathname = ptr; return *this;}
  199. path& operator=(const string_type& s) {m_pathname = s; return *this;}
  200. path& operator=(string_type& s) {m_pathname = s; return *this;}
  201. path& assign(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
  202. {m_pathname = ptr; return *this;}
  203. template <class Source>
  204. path& assign(Source const& source, const codecvt_type& cvt)
  205. {
  206. m_pathname.clear();
  207. path_traits::dispatch(source, m_pathname, cvt);
  208. return *this;
  209. }
  210. template <class InputIterator>
  211. path& assign(InputIterator begin, InputIterator end)
  212. {
  213. m_pathname.clear();
  214. if (begin != end)
  215. {
  216. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  217. seq(begin, end);
  218. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
  219. }
  220. return *this;
  221. }
  222. template <class InputIterator>
  223. path& assign(InputIterator begin, InputIterator end, const codecvt_type& cvt)
  224. {
  225. m_pathname.clear();
  226. if (begin != end)
  227. {
  228. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  229. seq(begin, end);
  230. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
  231. }
  232. return *this;
  233. }
  234. // ----- concatenation -----
  235. template <class Source>
  236. typename boost::enable_if<path_traits::is_pathable<
  237. typename boost::decay<Source>::type>, path&>::type
  238. operator+=(Source const& source)
  239. {
  240. return concat(source);
  241. }
  242. // value_type overloads. Same rationale as for constructors above
  243. path& operator+=(const path& p) { m_pathname += p.m_pathname; return *this; }
  244. path& operator+=(const value_type* ptr) { m_pathname += ptr; return *this; }
  245. path& operator+=(value_type* ptr) { m_pathname += ptr; return *this; }
  246. path& operator+=(const string_type& s) { m_pathname += s; return *this; }
  247. path& operator+=(string_type& s) { m_pathname += s; return *this; }
  248. path& operator+=(value_type c) { m_pathname += c; return *this; }
  249. template <class CharT>
  250. typename boost::enable_if<boost::is_integral<CharT>, path&>::type
  251. operator+=(CharT c)
  252. {
  253. CharT tmp[2];
  254. tmp[0] = c;
  255. tmp[1] = 0;
  256. return concat(tmp);
  257. }
  258. template <class Source>
  259. path& concat(Source const& source)
  260. {
  261. path_traits::dispatch(source, m_pathname);
  262. return *this;
  263. }
  264. template <class Source>
  265. path& concat(Source const& source, const codecvt_type& cvt)
  266. {
  267. path_traits::dispatch(source, m_pathname, cvt);
  268. return *this;
  269. }
  270. template <class InputIterator>
  271. path& concat(InputIterator begin, InputIterator end)
  272. {
  273. if (begin == end)
  274. return *this;
  275. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  276. seq(begin, end);
  277. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
  278. return *this;
  279. }
  280. template <class InputIterator>
  281. path& concat(InputIterator begin, InputIterator end, const codecvt_type& cvt)
  282. {
  283. if (begin == end)
  284. return *this;
  285. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  286. seq(begin, end);
  287. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
  288. return *this;
  289. }
  290. // ----- appends -----
  291. // if a separator is added, it is the preferred separator for the platform;
  292. // slash for POSIX, backslash for Windows
  293. BOOST_FILESYSTEM_DECL path& operator/=(const path& p);
  294. template <class Source>
  295. typename boost::enable_if<path_traits::is_pathable<
  296. typename boost::decay<Source>::type>, path&>::type
  297. operator/=(Source const& source)
  298. {
  299. return append(source);
  300. }
  301. BOOST_FILESYSTEM_DECL path& operator/=(const value_type* ptr);
  302. path& operator/=(value_type* ptr)
  303. {
  304. return this->operator/=(const_cast<const value_type*>(ptr));
  305. }
  306. path& operator/=(const string_type& s) { return this->operator/=(path(s)); }
  307. path& operator/=(string_type& s) { return this->operator/=(path(s)); }
  308. path& append(const value_type* ptr) // required in case ptr overlaps *this
  309. {
  310. this->operator/=(ptr);
  311. return *this;
  312. }
  313. path& append(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
  314. {
  315. this->operator/=(ptr);
  316. return *this;
  317. }
  318. template <class Source>
  319. path& append(Source const& source);
  320. template <class Source>
  321. path& append(Source const& source, const codecvt_type& cvt);
  322. template <class InputIterator>
  323. path& append(InputIterator begin, InputIterator end);
  324. template <class InputIterator>
  325. path& append(InputIterator begin, InputIterator end, const codecvt_type& cvt);
  326. // ----- modifiers -----
  327. void clear() BOOST_NOEXCEPT { m_pathname.clear(); }
  328. # ifdef BOOST_POSIX_API
  329. path& make_preferred() { return *this; } // POSIX no effect
  330. # else // BOOST_WINDOWS_API
  331. BOOST_FILESYSTEM_DECL path& make_preferred(); // change slashes to backslashes
  332. # endif
  333. BOOST_FILESYSTEM_DECL path& remove_filename();
  334. BOOST_FILESYSTEM_DECL path& remove_trailing_separator();
  335. BOOST_FILESYSTEM_DECL path& replace_extension(const path& new_extension = path());
  336. void swap(path& rhs) BOOST_NOEXCEPT { m_pathname.swap(rhs.m_pathname); }
  337. // ----- observers -----
  338. // For operating systems that format file paths differently than directory
  339. // paths, return values from observers are formatted as file names unless there
  340. // is a trailing separator, in which case returns are formatted as directory
  341. // paths. POSIX and Windows make no such distinction.
  342. // Implementations are permitted to return const values or const references.
  343. // The string or path returned by an observer are specified as being formatted
  344. // as "native" or "generic".
  345. //
  346. // For POSIX, these are all the same format; slashes and backslashes are as input and
  347. // are not modified.
  348. //
  349. // For Windows, native: as input; slashes and backslashes are not modified;
  350. // this is the format of the internally stored string.
  351. // generic: backslashes are converted to slashes
  352. // ----- native format observers -----
  353. const string_type& native() const BOOST_NOEXCEPT { return m_pathname; }
  354. const value_type* c_str() const BOOST_NOEXCEPT { return m_pathname.c_str(); }
  355. string_type::size_type size() const BOOST_NOEXCEPT { return m_pathname.size(); }
  356. template <class String>
  357. String string() const;
  358. template <class String>
  359. String string(const codecvt_type& cvt) const;
  360. # ifdef BOOST_WINDOWS_API
  361. const std::string string() const
  362. {
  363. std::string tmp;
  364. if (!m_pathname.empty())
  365. path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
  366. tmp);
  367. return tmp;
  368. }
  369. const std::string string(const codecvt_type& cvt) const
  370. {
  371. std::string tmp;
  372. if (!m_pathname.empty())
  373. path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
  374. tmp, cvt);
  375. return tmp;
  376. }
  377. // string_type is std::wstring, so there is no conversion
  378. const std::wstring& wstring() const { return m_pathname; }
  379. const std::wstring& wstring(const codecvt_type&) const { return m_pathname; }
  380. # else // BOOST_POSIX_API
  381. // string_type is std::string, so there is no conversion
  382. const std::string& string() const { return m_pathname; }
  383. const std::string& string(const codecvt_type&) const { return m_pathname; }
  384. const std::wstring wstring() const
  385. {
  386. std::wstring tmp;
  387. if (!m_pathname.empty())
  388. path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
  389. tmp);
  390. return tmp;
  391. }
  392. const std::wstring wstring(const codecvt_type& cvt) const
  393. {
  394. std::wstring tmp;
  395. if (!m_pathname.empty())
  396. path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
  397. tmp, cvt);
  398. return tmp;
  399. }
  400. # endif
  401. // ----- generic format observers -----
  402. // Experimental generic function returning generic formatted path (i.e. separators
  403. // are forward slashes). Motivation: simpler than a family of generic_*string
  404. // functions.
  405. # ifdef BOOST_WINDOWS_API
  406. BOOST_FILESYSTEM_DECL path generic_path() const;
  407. # else
  408. path generic_path() const { return path(*this); }
  409. # endif
  410. template <class String>
  411. String generic_string() const;
  412. template <class String>
  413. String generic_string(const codecvt_type& cvt) const;
  414. # ifdef BOOST_WINDOWS_API
  415. const std::string generic_string() const { return generic_path().string(); }
  416. const std::string generic_string(const codecvt_type& cvt) const { return generic_path().string(cvt); }
  417. const std::wstring generic_wstring() const { return generic_path().wstring(); }
  418. const std::wstring generic_wstring(const codecvt_type&) const { return generic_wstring(); }
  419. # else // BOOST_POSIX_API
  420. // On POSIX-like systems, the generic format is the same as the native format
  421. const std::string& generic_string() const { return m_pathname; }
  422. const std::string& generic_string(const codecvt_type&) const { return m_pathname; }
  423. const std::wstring generic_wstring() const { return this->wstring(); }
  424. const std::wstring generic_wstring(const codecvt_type& cvt) const { return this->wstring(cvt); }
  425. # endif
  426. // ----- compare -----
  427. BOOST_FILESYSTEM_DECL int compare(const path& p) const BOOST_NOEXCEPT; // generic, lexicographical
  428. int compare(const std::string& s) const { return compare(path(s)); }
  429. int compare(const value_type* s) const { return compare(path(s)); }
  430. // ----- decomposition -----
  431. BOOST_FILESYSTEM_DECL path root_path() const;
  432. BOOST_FILESYSTEM_DECL path root_name() const; // returns 0 or 1 element path
  433. // even on POSIX, root_name() is non-empty() for network paths
  434. BOOST_FILESYSTEM_DECL path root_directory() const; // returns 0 or 1 element path
  435. BOOST_FILESYSTEM_DECL path relative_path() const;
  436. BOOST_FILESYSTEM_DECL path parent_path() const;
  437. BOOST_FILESYSTEM_DECL path filename() const; // returns 0 or 1 element path
  438. BOOST_FILESYSTEM_DECL path stem() const; // returns 0 or 1 element path
  439. BOOST_FILESYSTEM_DECL path extension() const; // returns 0 or 1 element path
  440. // ----- query -----
  441. bool empty() const BOOST_NOEXCEPT { return m_pathname.empty(); }
  442. bool filename_is_dot() const;
  443. bool filename_is_dot_dot() const;
  444. bool has_root_path() const { return has_root_directory() || has_root_name(); }
  445. bool has_root_name() const { return !root_name().empty(); }
  446. bool has_root_directory() const { return !root_directory().empty(); }
  447. bool has_relative_path() const { return !relative_path().empty(); }
  448. bool has_parent_path() const { return !parent_path().empty(); }
  449. bool has_filename() const { return !m_pathname.empty(); }
  450. bool has_stem() const { return !stem().empty(); }
  451. bool has_extension() const { return !extension().empty(); }
  452. bool is_relative() const { return !is_absolute(); }
  453. bool is_absolute() const
  454. {
  455. # ifdef BOOST_WINDOWS_API
  456. return has_root_name() && has_root_directory();
  457. # else
  458. return has_root_directory();
  459. # endif
  460. }
  461. // ----- lexical operations -----
  462. BOOST_FILESYSTEM_DECL path lexically_normal() const;
  463. BOOST_FILESYSTEM_DECL path lexically_relative(const path& base) const;
  464. path lexically_proximate(const path& base) const
  465. {
  466. path tmp(lexically_relative(base));
  467. return tmp.empty() ? *this : tmp;
  468. }
  469. // ----- iterators -----
  470. class iterator;
  471. typedef iterator const_iterator;
  472. class reverse_iterator;
  473. typedef reverse_iterator const_reverse_iterator;
  474. BOOST_FILESYSTEM_DECL iterator begin() const;
  475. BOOST_FILESYSTEM_DECL iterator end() const;
  476. reverse_iterator rbegin() const;
  477. reverse_iterator rend() const;
  478. // ----- static member functions -----
  479. static BOOST_FILESYSTEM_DECL std::locale imbue(const std::locale& loc);
  480. static BOOST_FILESYSTEM_DECL const codecvt_type& codecvt();
  481. // ----- deprecated functions -----
  482. # if defined(BOOST_FILESYSTEM_DEPRECATED) && defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  483. # error both BOOST_FILESYSTEM_DEPRECATED and BOOST_FILESYSTEM_NO_DEPRECATED are defined
  484. # endif
  485. # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  486. // recently deprecated functions supplied by default
  487. path& normalize() {
  488. path tmp(lexically_normal());
  489. m_pathname.swap(tmp.m_pathname);
  490. return *this;
  491. }
  492. path& remove_leaf() { return remove_filename(); }
  493. path leaf() const { return filename(); }
  494. path branch_path() const { return parent_path(); }
  495. path generic() const { return generic_path(); }
  496. bool has_leaf() const { return !m_pathname.empty(); }
  497. bool has_branch_path() const { return !parent_path().empty(); }
  498. bool is_complete() const { return is_absolute(); }
  499. # endif
  500. # if defined(BOOST_FILESYSTEM_DEPRECATED)
  501. // deprecated functions with enough signature or semantic changes that they are
  502. // not supplied by default
  503. const std::string file_string() const { return string(); }
  504. const std::string directory_string() const { return string(); }
  505. const std::string native_file_string() const { return string(); }
  506. const std::string native_directory_string() const { return string(); }
  507. const string_type external_file_string() const { return native(); }
  508. const string_type external_directory_string() const { return native(); }
  509. // older functions no longer supported
  510. //typedef bool (*name_check)(const std::string & name);
  511. //basic_path(const string_type& str, name_check) { operator/=(str); }
  512. //basic_path(const typename string_type::value_type* s, name_check)
  513. // { operator/=(s);}
  514. //static bool default_name_check_writable() { return false; }
  515. //static void default_name_check(name_check) {}
  516. //static name_check default_name_check() { return 0; }
  517. //basic_path& canonize();
  518. # endif
  519. //--------------------------------------------------------------------------------------//
  520. // class path private members //
  521. //--------------------------------------------------------------------------------------//
  522. private:
  523. # if defined(_MSC_VER)
  524. # pragma warning(push) // Save warning settings
  525. # pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>'
  526. # endif // needs to have dll-interface...
  527. /*
  528. m_pathname has the type, encoding, and format required by the native
  529. operating system. Thus for POSIX and Windows there is no conversion for
  530. passing m_pathname.c_str() to the O/S API or when obtaining a path from the
  531. O/S API. POSIX encoding is unspecified other than for dot and slash
  532. characters; POSIX just treats paths as a sequence of bytes. Windows
  533. encoding is UCS-2 or UTF-16 depending on the version.
  534. */
  535. string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes,
  536. // slashes NOT converted to backslashes
  537. # if defined(_MSC_VER)
  538. # pragma warning(pop) // restore warning settings.
  539. # endif
  540. // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0.
  541. // Note: An append is never performed if size()==0, so a returned 0 is unambiguous.
  542. BOOST_FILESYSTEM_DECL string_type::size_type m_append_separator_if_needed();
  543. BOOST_FILESYSTEM_DECL void m_erase_redundant_separator(string_type::size_type sep_pos);
  544. BOOST_FILESYSTEM_DECL string_type::size_type m_parent_path_end() const;
  545. // Was qualified; como433beta8 reports:
  546. // warning #427-D: qualified name is not allowed in member declaration
  547. friend class iterator;
  548. friend bool operator<(const path& lhs, const path& rhs);
  549. // see path::iterator::increment/decrement comment below
  550. static BOOST_FILESYSTEM_DECL void m_path_iterator_increment(path::iterator& it);
  551. static BOOST_FILESYSTEM_DECL void m_path_iterator_decrement(path::iterator& it);
  552. }; // class path
  553. namespace detail
  554. {
  555. BOOST_FILESYSTEM_DECL
  556. int lex_compare(path::iterator first1, path::iterator last1,
  557. path::iterator first2, path::iterator last2);
  558. BOOST_FILESYSTEM_DECL
  559. const path& dot_path();
  560. BOOST_FILESYSTEM_DECL
  561. const path& dot_dot_path();
  562. }
  563. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  564. typedef path wpath;
  565. # endif
  566. //------------------------------------------------------------------------------------//
  567. // class path::iterator //
  568. //------------------------------------------------------------------------------------//
  569. class path::iterator
  570. : public boost::iterator_facade<
  571. path::iterator,
  572. path const,
  573. boost::bidirectional_traversal_tag >
  574. {
  575. private:
  576. friend class boost::iterator_core_access;
  577. friend class boost::filesystem::path;
  578. friend class boost::filesystem::path::reverse_iterator;
  579. friend void m_path_iterator_increment(path::iterator & it);
  580. friend void m_path_iterator_decrement(path::iterator & it);
  581. const path& dereference() const { return m_element; }
  582. bool equal(const iterator & rhs) const
  583. {
  584. return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos;
  585. }
  586. // iterator_facade derived classes don't seem to like implementations in
  587. // separate translation unit dll's, so forward to class path static members
  588. void increment() { m_path_iterator_increment(*this); }
  589. void decrement() { m_path_iterator_decrement(*this); }
  590. path m_element; // current element
  591. const path* m_path_ptr; // path being iterated over
  592. string_type::size_type m_pos; // position of m_element in
  593. // m_path_ptr->m_pathname.
  594. // if m_element is implicit dot, m_pos is the
  595. // position of the last separator in the path.
  596. // end() iterator is indicated by
  597. // m_pos == m_path_ptr->m_pathname.size()
  598. }; // path::iterator
  599. //------------------------------------------------------------------------------------//
  600. // class path::reverse_iterator //
  601. //------------------------------------------------------------------------------------//
  602. class path::reverse_iterator
  603. : public boost::iterator_facade<
  604. path::reverse_iterator,
  605. path const,
  606. boost::bidirectional_traversal_tag >
  607. {
  608. public:
  609. explicit reverse_iterator(iterator itr) : m_itr(itr)
  610. {
  611. if (itr != itr.m_path_ptr->begin())
  612. m_element = *--itr;
  613. }
  614. private:
  615. friend class boost::iterator_core_access;
  616. friend class boost::filesystem::path;
  617. const path& dereference() const { return m_element; }
  618. bool equal(const reverse_iterator& rhs) const { return m_itr == rhs.m_itr; }
  619. void increment()
  620. {
  621. --m_itr;
  622. if (m_itr != m_itr.m_path_ptr->begin())
  623. {
  624. iterator tmp = m_itr;
  625. m_element = *--tmp;
  626. }
  627. }
  628. void decrement()
  629. {
  630. m_element = *m_itr;
  631. ++m_itr;
  632. }
  633. iterator m_itr;
  634. path m_element;
  635. }; // path::reverse_iterator
  636. //------------------------------------------------------------------------------------//
  637. // //
  638. // non-member functions //
  639. // //
  640. //------------------------------------------------------------------------------------//
  641. // std::lexicographical_compare would infinitely recurse because path iterators
  642. // yield paths, so provide a path aware version
  643. inline bool lexicographical_compare(path::iterator first1, path::iterator last1,
  644. path::iterator first2, path::iterator last2)
  645. { return detail::lex_compare(first1, last1, first2, last2) < 0; }
  646. inline bool operator==(const path& lhs, const path& rhs) {return lhs.compare(rhs) == 0;}
  647. inline bool operator==(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) == 0;}
  648. inline bool operator==(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
  649. inline bool operator==(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) == 0;}
  650. inline bool operator==(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
  651. inline bool operator!=(const path& lhs, const path& rhs) {return lhs.compare(rhs) != 0;}
  652. inline bool operator!=(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) != 0;}
  653. inline bool operator!=(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
  654. inline bool operator!=(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) != 0;}
  655. inline bool operator!=(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
  656. // TODO: why do == and != have additional overloads, but the others don't?
  657. inline bool operator<(const path& lhs, const path& rhs) {return lhs.compare(rhs) < 0;}
  658. inline bool operator<=(const path& lhs, const path& rhs) {return !(rhs < lhs);}
  659. inline bool operator> (const path& lhs, const path& rhs) {return rhs < lhs;}
  660. inline bool operator>=(const path& lhs, const path& rhs) {return !(lhs < rhs);}
  661. inline std::size_t hash_value(const path& x)
  662. {
  663. # ifdef BOOST_WINDOWS_API
  664. std::size_t seed = 0;
  665. for(const path::value_type* it = x.c_str(); *it; ++it)
  666. hash_combine(seed, *it == L'/' ? L'\\' : *it);
  667. return seed;
  668. # else // BOOST_POSIX_API
  669. return hash_range(x.native().begin(), x.native().end());
  670. # endif
  671. }
  672. inline void swap(path& lhs, path& rhs) BOOST_NOEXCEPT { lhs.swap(rhs); }
  673. inline path operator/(const path& lhs, const path& rhs) { return path(lhs) /= rhs; }
  674. # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  675. inline path&& operator/(path&& lhs, const path& rhs) { lhs /= rhs; return std::move(lhs); }
  676. # endif
  677. // inserters and extractors
  678. // use boost::io::quoted() to handle spaces in paths
  679. // use '&' as escape character to ease use for Windows paths
  680. template <class Char, class Traits>
  681. inline std::basic_ostream<Char, Traits>&
  682. operator<<(std::basic_ostream<Char, Traits>& os, const path& p)
  683. {
  684. return os
  685. << boost::io::quoted(p.template string<std::basic_string<Char> >(), static_cast<Char>('&'));
  686. }
  687. template <class Char, class Traits>
  688. inline std::basic_istream<Char, Traits>&
  689. operator>>(std::basic_istream<Char, Traits>& is, path& p)
  690. {
  691. std::basic_string<Char> str;
  692. is >> boost::io::quoted(str, static_cast<Char>('&'));
  693. p = str;
  694. return is;
  695. }
  696. // name_checks
  697. // These functions are holdovers from version 1. It isn't clear they have much
  698. // usefulness, or how to generalize them for later versions.
  699. BOOST_FILESYSTEM_DECL bool portable_posix_name(const std::string & name);
  700. BOOST_FILESYSTEM_DECL bool windows_name(const std::string & name);
  701. BOOST_FILESYSTEM_DECL bool portable_name(const std::string & name);
  702. BOOST_FILESYSTEM_DECL bool portable_directory_name(const std::string & name);
  703. BOOST_FILESYSTEM_DECL bool portable_file_name(const std::string & name);
  704. BOOST_FILESYSTEM_DECL bool native(const std::string & name);
  705. namespace detail
  706. {
  707. // For POSIX, is_directory_separator() and is_element_separator() are identical since
  708. // a forward slash is the only valid directory separator and also the only valid
  709. // element separator. For Windows, forward slash and back slash are the possible
  710. // directory separators, but colon (example: "c:foo") is also an element separator.
  711. inline bool is_directory_separator(path::value_type c) BOOST_NOEXCEPT
  712. {
  713. return c == path::separator
  714. # ifdef BOOST_WINDOWS_API
  715. || c == path::preferred_separator
  716. # endif
  717. ;
  718. }
  719. inline bool is_element_separator(path::value_type c) BOOST_NOEXCEPT
  720. {
  721. return c == path::separator
  722. # ifdef BOOST_WINDOWS_API
  723. || c == path::preferred_separator || c == L':'
  724. # endif
  725. ;
  726. }
  727. } // namespace detail
  728. //------------------------------------------------------------------------------------//
  729. // class path miscellaneous function implementations //
  730. //------------------------------------------------------------------------------------//
  731. inline path::reverse_iterator path::rbegin() const { return reverse_iterator(end()); }
  732. inline path::reverse_iterator path::rend() const { return reverse_iterator(begin()); }
  733. inline bool path::filename_is_dot() const
  734. {
  735. // implicit dot is tricky, so actually call filename(); see path::filename() example
  736. // in reference.html
  737. path p(filename());
  738. return p.size() == 1 && *p.c_str() == dot;
  739. }
  740. inline bool path::filename_is_dot_dot() const
  741. {
  742. return size() >= 2 && m_pathname[size()-1] == dot && m_pathname[size()-2] == dot
  743. && (m_pathname.size() == 2 || detail::is_element_separator(m_pathname[size()-3]));
  744. // use detail::is_element_separator() rather than detail::is_directory_separator
  745. // to deal with "c:.." edge case on Windows when ':' acts as a separator
  746. }
  747. //--------------------------------------------------------------------------------------//
  748. // class path member template implementation //
  749. //--------------------------------------------------------------------------------------//
  750. template <class InputIterator>
  751. path& path::append(InputIterator begin, InputIterator end)
  752. {
  753. if (begin == end)
  754. return *this;
  755. string_type::size_type sep_pos(m_append_separator_if_needed());
  756. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  757. seq(begin, end);
  758. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
  759. if (sep_pos)
  760. m_erase_redundant_separator(sep_pos);
  761. return *this;
  762. }
  763. template <class InputIterator>
  764. path& path::append(InputIterator begin, InputIterator end, const codecvt_type& cvt)
  765. {
  766. if (begin == end)
  767. return *this;
  768. string_type::size_type sep_pos(m_append_separator_if_needed());
  769. std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
  770. seq(begin, end);
  771. path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
  772. if (sep_pos)
  773. m_erase_redundant_separator(sep_pos);
  774. return *this;
  775. }
  776. template <class Source>
  777. path& path::append(Source const& source)
  778. {
  779. if (path_traits::empty(source))
  780. return *this;
  781. string_type::size_type sep_pos(m_append_separator_if_needed());
  782. path_traits::dispatch(source, m_pathname);
  783. if (sep_pos)
  784. m_erase_redundant_separator(sep_pos);
  785. return *this;
  786. }
  787. template <class Source>
  788. path& path::append(Source const& source, const codecvt_type& cvt)
  789. {
  790. if (path_traits::empty(source))
  791. return *this;
  792. string_type::size_type sep_pos(m_append_separator_if_needed());
  793. path_traits::dispatch(source, m_pathname, cvt);
  794. if (sep_pos)
  795. m_erase_redundant_separator(sep_pos);
  796. return *this;
  797. }
  798. //--------------------------------------------------------------------------------------//
  799. // class path member template specializations //
  800. //--------------------------------------------------------------------------------------//
  801. template <> inline
  802. std::string path::string<std::string>() const
  803. { return string(); }
  804. template <> inline
  805. std::wstring path::string<std::wstring>() const
  806. { return wstring(); }
  807. template <> inline
  808. std::string path::string<std::string>(const codecvt_type& cvt) const
  809. { return string(cvt); }
  810. template <> inline
  811. std::wstring path::string<std::wstring>(const codecvt_type& cvt) const
  812. { return wstring(cvt); }
  813. template <> inline
  814. std::string path::generic_string<std::string>() const
  815. { return generic_string(); }
  816. template <> inline
  817. std::wstring path::generic_string<std::wstring>() const
  818. { return generic_wstring(); }
  819. template <> inline
  820. std::string path::generic_string<std::string>(const codecvt_type& cvt) const
  821. { return generic_string(cvt); }
  822. template <> inline
  823. std::wstring path::generic_string<std::wstring>(const codecvt_type& cvt) const
  824. { return generic_wstring(cvt); }
  825. //--------------------------------------------------------------------------------------//
  826. // path_traits convert function implementations //
  827. // requiring path::codecvt() be visable //
  828. //--------------------------------------------------------------------------------------//
  829. namespace path_traits
  830. { // without codecvt
  831. inline
  832. void convert(const char* from,
  833. const char* from_end, // 0 for null terminated MBCS
  834. std::wstring & to)
  835. {
  836. convert(from, from_end, to, path::codecvt());
  837. }
  838. inline
  839. void convert(const wchar_t* from,
  840. const wchar_t* from_end, // 0 for null terminated MBCS
  841. std::string & to)
  842. {
  843. convert(from, from_end, to, path::codecvt());
  844. }
  845. inline
  846. void convert(const char* from,
  847. std::wstring & to)
  848. {
  849. BOOST_ASSERT(!!from);
  850. convert(from, 0, to, path::codecvt());
  851. }
  852. inline
  853. void convert(const wchar_t* from,
  854. std::string & to)
  855. {
  856. BOOST_ASSERT(!!from);
  857. convert(from, 0, to, path::codecvt());
  858. }
  859. } // namespace path_traits
  860. } // namespace filesystem
  861. } // namespace boost
  862. //----------------------------------------------------------------------------//
  863. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  864. #endif // BOOST_FILESYSTEM_PATH_HPP