1 // filesystem path.hpp ---------------------------------------------------------------//
3 // Copyright Beman Dawes 2002-2005, 2009
4 // Copyright Vladimir Prus 2002
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
9 // Library home page: http://www.boost.org/libs/filesystem
11 // path::stem(), extension(), and replace_extension() are based on
12 // basename(), extension(), and change_extension() from the original
13 // filesystem/convenience.hpp header by Vladimir Prus.
15 #ifndef BOOST_FILESYSTEM_PATH_HPP
16 #define BOOST_FILESYSTEM_PATH_HPP
18 #include <boost/config.hpp>
20 # if defined( BOOST_NO_STD_WSTRING )
21 # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
24 #include <boost/assert.hpp>
25 #include <boost/filesystem/config.hpp>
26 #include <boost/filesystem/path_traits.hpp> // includes <cwchar>
27 #include <boost/system/error_code.hpp>
28 #include <boost/system/system_error.hpp>
29 #include <boost/iterator/iterator_facade.hpp>
30 #include <boost/iterator/iterator_categories.hpp>
31 #include <boost/core/enable_if.hpp>
32 #include <boost/io/detail/quoted_manip.hpp>
33 #include <boost/functional/hash_fwd.hpp>
34 #include <boost/type_traits/is_integral.hpp>
44 #include <boost/config/abi_prefix.hpp> // must be the last #include
50 namespace path_detail // intentionally don't use filesystem::detail to not bring internal Boost.Filesystem functions into ADL via path_constants
53 template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
56 typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
57 typedef Char value_type;
58 static BOOST_CONSTEXPR_OR_CONST value_type separator = Separator;
59 static BOOST_CONSTEXPR_OR_CONST value_type preferred_separator = PreferredSeparator;
60 static BOOST_CONSTEXPR_OR_CONST value_type dot = Dot;
63 #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
64 template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
65 BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
66 path_constants< Char, Separator, PreferredSeparator, Dot >::separator;
67 template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
68 BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
69 path_constants< Char, Separator, PreferredSeparator, Dot >::preferred_separator;
70 template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
71 BOOST_CONSTEXPR_OR_CONST typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
72 path_constants< Char, Separator, PreferredSeparator, Dot >::dot;
75 } // namespace path_detail
77 //------------------------------------------------------------------------------------//
81 //------------------------------------------------------------------------------------//
84 public filesystem::path_detail::path_constants<
85 #ifdef BOOST_WINDOWS_API
86 wchar_t, L'/', L'\\', L'.'
94 // value_type is the character type used by the operating system API to
97 typedef path_constants_base::value_type value_type;
98 typedef std::basic_string<value_type> string_type;
99 typedef std::codecvt<wchar_t, char,
100 std::mbstate_t> codecvt_type;
103 // ----- character encoding conversions -----
105 // Following the principle of least astonishment, path input arguments
106 // passed to or obtained from the operating system via objects of
107 // class path behave as if they were directly passed to or
108 // obtained from the O/S API, unless conversion is explicitly requested.
110 // POSIX specfies that path strings are passed unchanged to and from the
111 // API. Note that this is different from the POSIX command line utilities,
112 // which convert according to a locale.
114 // Thus for POSIX, char strings do not undergo conversion. wchar_t strings
115 // are converted to/from char using the path locale or, if a conversion
116 // argument is given, using a conversion object modeled on
117 // std::wstring_convert.
119 // The path locale, which is global to the thread, can be changed by the
120 // imbue() function. It is initialized to an implementation defined locale.
122 // For Windows, wchar_t strings do not undergo conversion. char strings
123 // are converted using the "ANSI" or "OEM" code pages, as determined by
124 // the AreFileApisANSI() function, or, if a conversion argument is given,
125 // using a conversion object modeled on std::wstring_convert.
127 // See m_pathname comments for further important rationale.
129 // TODO: rules needed for operating systems that use / or .
130 // differently, or format directory paths differently from file paths.
132 // **********************************************************************************
134 // More work needed: How to handle an operating system that may have
135 // slash characters or dot characters in valid filenames, either because
136 // it doesn't follow the POSIX standard, or because it allows MBCS
137 // filename encodings that may contain slash or dot characters. For
138 // example, ISO/IEC 2022 (JIS) encoding which allows switching to
139 // JIS x0208-1983 encoding. A valid filename in this set of encodings is
140 // 0x1B 0x24 0x42 [switch to X0208-1983] 0x24 0x2F [U+304F Kiragana letter KU]
142 // Note that 0x2F is the ASCII slash character
144 // **********************************************************************************
146 // Supported source arguments: half-open iterator range, container, c-array,
147 // and single pointer to null terminated string.
149 // All source arguments except pointers to null terminated byte strings support
150 // multi-byte character strings which may have embedded nulls. Embedded null
151 // support is required for some Asian languages on Windows.
153 // "const codecvt_type& cvt=codecvt()" default arguments are not used because this
154 // limits the impact of locale("") initialization failures on POSIX systems to programs
155 // that actually depend on locale(""). It further ensures that exceptions thrown
156 // as a result of such failues occur after main() has started, so can be caught.
158 // ----- constructors -----
160 path() BOOST_NOEXCEPT {}
161 path(const path& p) : m_pathname(p.m_pathname) {}
163 template <class Source>
164 path(Source const& source,
165 typename boost::enable_if<path_traits::is_pathable<
166 typename boost::decay<Source>::type> >::type* =0)
168 path_traits::dispatch(source, m_pathname);
171 path(const value_type* s) : m_pathname(s) {}
172 path(value_type* s) : m_pathname(s) {}
173 path(const string_type& s) : m_pathname(s) {}
174 path(string_type& s) : m_pathname(s) {}
176 // As of October 2015 the interaction between noexcept and =default is so troublesome
177 // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
178 // functions. GCC is not even consistent for the same release on different platforms.
180 # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
181 path(path&& p) BOOST_NOEXCEPT : m_pathname(std::move(p.m_pathname)) {}
182 path& operator=(path&& p) BOOST_NOEXCEPT
183 { m_pathname = std::move(p.m_pathname); return *this; }
186 template <class Source>
187 path(Source const& source, const codecvt_type& cvt)
189 path_traits::dispatch(source, m_pathname, cvt);
192 template <class InputIterator>
193 path(InputIterator begin, InputIterator end)
197 // convert requires contiguous string, so copy
198 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
200 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
204 template <class InputIterator>
205 path(InputIterator begin, InputIterator end, const codecvt_type& cvt)
209 // convert requires contiguous string, so copy
210 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
212 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
216 // ----- assignments -----
218 path& operator=(const path& p)
220 m_pathname = p.m_pathname;
224 template <class Source>
225 typename boost::enable_if<path_traits::is_pathable<
226 typename boost::decay<Source>::type>, path&>::type
227 operator=(Source const& source)
230 path_traits::dispatch(source, m_pathname);
234 // value_type overloads
236 path& operator=(const value_type* ptr) // required in case ptr overlaps *this
237 {m_pathname = ptr; return *this;}
238 path& operator=(value_type* ptr) // required in case ptr overlaps *this
239 {m_pathname = ptr; return *this;}
240 path& operator=(const string_type& s) {m_pathname = s; return *this;}
241 path& operator=(string_type& s) {m_pathname = s; return *this;}
243 path& assign(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
244 {m_pathname = ptr; return *this;}
245 template <class Source>
246 path& assign(Source const& source, const codecvt_type& cvt)
249 path_traits::dispatch(source, m_pathname, cvt);
253 template <class InputIterator>
254 path& assign(InputIterator begin, InputIterator end)
259 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
261 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
266 template <class InputIterator>
267 path& assign(InputIterator begin, InputIterator end, const codecvt_type& cvt)
272 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
274 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
279 // ----- concatenation -----
281 template <class Source>
282 typename boost::enable_if<path_traits::is_pathable<
283 typename boost::decay<Source>::type>, path&>::type
284 operator+=(Source const& source)
286 return concat(source);
289 // value_type overloads. Same rationale as for constructors above
290 path& operator+=(const path& p) { m_pathname += p.m_pathname; return *this; }
291 path& operator+=(const value_type* ptr) { m_pathname += ptr; return *this; }
292 path& operator+=(value_type* ptr) { m_pathname += ptr; return *this; }
293 path& operator+=(const string_type& s) { m_pathname += s; return *this; }
294 path& operator+=(string_type& s) { m_pathname += s; return *this; }
295 path& operator+=(value_type c) { m_pathname += c; return *this; }
297 template <class CharT>
298 typename boost::enable_if<boost::is_integral<CharT>, path&>::type
307 template <class Source>
308 path& concat(Source const& source)
310 path_traits::dispatch(source, m_pathname);
314 template <class Source>
315 path& concat(Source const& source, const codecvt_type& cvt)
317 path_traits::dispatch(source, m_pathname, cvt);
321 template <class InputIterator>
322 path& concat(InputIterator begin, InputIterator end)
326 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
328 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
332 template <class InputIterator>
333 path& concat(InputIterator begin, InputIterator end, const codecvt_type& cvt)
337 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
339 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
343 // ----- appends -----
345 // if a separator is added, it is the preferred separator for the platform;
346 // slash for POSIX, backslash for Windows
348 BOOST_FILESYSTEM_DECL path& operator/=(const path& p);
350 template <class Source>
351 typename boost::enable_if<path_traits::is_pathable<
352 typename boost::decay<Source>::type>, path&>::type
353 operator/=(Source const& source)
355 return append(source);
358 BOOST_FILESYSTEM_DECL path& operator/=(const value_type* ptr);
359 path& operator/=(value_type* ptr)
361 return this->operator/=(const_cast<const value_type*>(ptr));
363 path& operator/=(const string_type& s) { return this->operator/=(path(s)); }
364 path& operator/=(string_type& s) { return this->operator/=(path(s)); }
366 path& append(const value_type* ptr) // required in case ptr overlaps *this
368 this->operator/=(ptr);
372 path& append(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this
374 this->operator/=(ptr);
378 template <class Source>
379 path& append(Source const& source);
381 template <class Source>
382 path& append(Source const& source, const codecvt_type& cvt);
384 template <class InputIterator>
385 path& append(InputIterator begin, InputIterator end);
387 template <class InputIterator>
388 path& append(InputIterator begin, InputIterator end, const codecvt_type& cvt);
390 // ----- modifiers -----
392 void clear() BOOST_NOEXCEPT { m_pathname.clear(); }
393 # ifdef BOOST_POSIX_API
394 path& make_preferred() { return *this; } // POSIX no effect
395 # else // BOOST_WINDOWS_API
396 BOOST_FILESYSTEM_DECL path& make_preferred(); // change slashes to backslashes
398 BOOST_FILESYSTEM_DECL path& remove_filename();
399 BOOST_FILESYSTEM_DECL path& remove_trailing_separator();
400 BOOST_FILESYSTEM_DECL path& replace_extension(const path& new_extension = path());
401 void swap(path& rhs) BOOST_NOEXCEPT { m_pathname.swap(rhs.m_pathname); }
403 // ----- observers -----
405 // For operating systems that format file paths differently than directory
406 // paths, return values from observers are formatted as file names unless there
407 // is a trailing separator, in which case returns are formatted as directory
408 // paths. POSIX and Windows make no such distinction.
410 // Implementations are permitted to return const values or const references.
412 // The string or path returned by an observer are specified as being formatted
413 // as "native" or "generic".
415 // For POSIX, these are all the same format; slashes and backslashes are as input and
418 // For Windows, native: as input; slashes and backslashes are not modified;
419 // this is the format of the internally stored string.
420 // generic: backslashes are converted to slashes
422 // ----- native format observers -----
424 const string_type& native() const BOOST_NOEXCEPT { return m_pathname; }
425 const value_type* c_str() const BOOST_NOEXCEPT { return m_pathname.c_str(); }
426 string_type::size_type size() const BOOST_NOEXCEPT { return m_pathname.size(); }
428 template <class String>
429 String string() const;
431 template <class String>
432 String string(const codecvt_type& cvt) const;
434 # ifdef BOOST_WINDOWS_API
435 const std::string string() const
438 if (!m_pathname.empty())
439 path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
443 const std::string string(const codecvt_type& cvt) const
446 if (!m_pathname.empty())
447 path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
452 // string_type is std::wstring, so there is no conversion
453 const std::wstring& wstring() const { return m_pathname; }
454 const std::wstring& wstring(const codecvt_type&) const { return m_pathname; }
455 # else // BOOST_POSIX_API
456 // string_type is std::string, so there is no conversion
457 const std::string& string() const { return m_pathname; }
458 const std::string& string(const codecvt_type&) const { return m_pathname; }
460 const std::wstring wstring() const
463 if (!m_pathname.empty())
464 path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
468 const std::wstring wstring(const codecvt_type& cvt) const
471 if (!m_pathname.empty())
472 path_traits::convert(m_pathname.c_str(), m_pathname.c_str()+m_pathname.size(),
478 // ----- generic format observers -----
480 // Experimental generic function returning generic formatted path (i.e. separators
481 // are forward slashes). Motivation: simpler than a family of generic_*string
483 # ifdef BOOST_WINDOWS_API
484 BOOST_FILESYSTEM_DECL path generic_path() const;
486 path generic_path() const { return path(*this); }
489 template <class String>
490 String generic_string() const;
492 template <class String>
493 String generic_string(const codecvt_type& cvt) const;
495 # ifdef BOOST_WINDOWS_API
496 const std::string generic_string() const { return generic_path().string(); }
497 const std::string generic_string(const codecvt_type& cvt) const { return generic_path().string(cvt); }
498 const std::wstring generic_wstring() const { return generic_path().wstring(); }
499 const std::wstring generic_wstring(const codecvt_type&) const { return generic_wstring(); }
500 # else // BOOST_POSIX_API
501 // On POSIX-like systems, the generic format is the same as the native format
502 const std::string& generic_string() const { return m_pathname; }
503 const std::string& generic_string(const codecvt_type&) const { return m_pathname; }
504 const std::wstring generic_wstring() const { return this->wstring(); }
505 const std::wstring generic_wstring(const codecvt_type& cvt) const { return this->wstring(cvt); }
508 // ----- compare -----
510 BOOST_FILESYSTEM_DECL int compare(const path& p) const BOOST_NOEXCEPT; // generic, lexicographical
511 int compare(const std::string& s) const { return compare(path(s)); }
512 int compare(const value_type* s) const { return compare(path(s)); }
514 // ----- decomposition -----
516 BOOST_FILESYSTEM_DECL path root_path() const;
517 BOOST_FILESYSTEM_DECL path root_name() const; // returns 0 or 1 element path
518 // even on POSIX, root_name() is non-empty() for network paths
519 BOOST_FILESYSTEM_DECL path root_directory() const; // returns 0 or 1 element path
520 BOOST_FILESYSTEM_DECL path relative_path() const;
521 BOOST_FILESYSTEM_DECL path parent_path() const;
522 BOOST_FILESYSTEM_DECL path filename() const; // returns 0 or 1 element path
523 BOOST_FILESYSTEM_DECL path stem() const; // returns 0 or 1 element path
524 BOOST_FILESYSTEM_DECL path extension() const; // returns 0 or 1 element path
528 bool empty() const BOOST_NOEXCEPT { return m_pathname.empty(); }
529 bool filename_is_dot() const;
530 bool filename_is_dot_dot() const;
531 bool has_root_path() const { return has_root_directory() || has_root_name(); }
532 bool has_root_name() const { return !root_name().empty(); }
533 bool has_root_directory() const { return !root_directory().empty(); }
534 bool has_relative_path() const { return !relative_path().empty(); }
535 bool has_parent_path() const { return !parent_path().empty(); }
536 bool has_filename() const { return !m_pathname.empty(); }
537 bool has_stem() const { return !stem().empty(); }
538 bool has_extension() const { return !extension().empty(); }
539 bool is_relative() const { return !is_absolute(); }
540 bool is_absolute() const
542 // Windows CE has no root name (aka drive letters)
543 # if defined(BOOST_WINDOWS_API) && !defined(UNDER_CE)
544 return has_root_name() && has_root_directory();
546 return has_root_directory();
550 // ----- lexical operations -----
552 BOOST_FILESYSTEM_DECL path lexically_normal() const;
553 BOOST_FILESYSTEM_DECL path lexically_relative(const path& base) const;
554 path lexically_proximate(const path& base) const
556 path tmp(lexically_relative(base));
557 return tmp.empty() ? *this : tmp;
560 // ----- iterators -----
563 typedef iterator const_iterator;
564 class reverse_iterator;
565 typedef reverse_iterator const_reverse_iterator;
567 BOOST_FILESYSTEM_DECL iterator begin() const;
568 BOOST_FILESYSTEM_DECL iterator end() const;
569 reverse_iterator rbegin() const;
570 reverse_iterator rend() const;
572 // ----- static member functions -----
574 static BOOST_FILESYSTEM_DECL std::locale imbue(const std::locale& loc);
575 static BOOST_FILESYSTEM_DECL const codecvt_type& codecvt();
577 // ----- deprecated functions -----
579 # if defined(BOOST_FILESYSTEM_DEPRECATED) && defined(BOOST_FILESYSTEM_NO_DEPRECATED)
580 # error both BOOST_FILESYSTEM_DEPRECATED and BOOST_FILESYSTEM_NO_DEPRECATED are defined
583 # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
584 // recently deprecated functions supplied by default
586 path tmp(lexically_normal());
587 m_pathname.swap(tmp.m_pathname);
590 path& remove_leaf() { return remove_filename(); }
591 path leaf() const { return filename(); }
592 path branch_path() const { return parent_path(); }
593 path generic() const { return generic_path(); }
594 bool has_leaf() const { return !m_pathname.empty(); }
595 bool has_branch_path() const { return !parent_path().empty(); }
596 bool is_complete() const { return is_absolute(); }
599 # if defined(BOOST_FILESYSTEM_DEPRECATED)
600 // deprecated functions with enough signature or semantic changes that they are
601 // not supplied by default
602 const std::string file_string() const { return string(); }
603 const std::string directory_string() const { return string(); }
604 const std::string native_file_string() const { return string(); }
605 const std::string native_directory_string() const { return string(); }
606 const string_type external_file_string() const { return native(); }
607 const string_type external_directory_string() const { return native(); }
609 // older functions no longer supported
610 //typedef bool (*name_check)(const std::string & name);
611 //basic_path(const string_type& str, name_check) { operator/=(str); }
612 //basic_path(const typename string_type::value_type* s, name_check)
614 //static bool default_name_check_writable() { return false; }
615 //static void default_name_check(name_check) {}
616 //static name_check default_name_check() { return 0; }
617 //basic_path& canonize();
620 //--------------------------------------------------------------------------------------//
621 // class path private members //
622 //--------------------------------------------------------------------------------------//
626 # if defined(_MSC_VER)
627 # pragma warning(push) // Save warning settings
628 # pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>'
629 # endif // needs to have dll-interface...
631 m_pathname has the type, encoding, and format required by the native
632 operating system. Thus for POSIX and Windows there is no conversion for
633 passing m_pathname.c_str() to the O/S API or when obtaining a path from the
634 O/S API. POSIX encoding is unspecified other than for dot and slash
635 characters; POSIX just treats paths as a sequence of bytes. Windows
636 encoding is UCS-2 or UTF-16 depending on the version.
638 string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes,
639 // slashes NOT converted to backslashes
640 # if defined(_MSC_VER)
641 # pragma warning(pop) // restore warning settings.
644 // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0.
645 // Note: An append is never performed if size()==0, so a returned 0 is unambiguous.
646 BOOST_FILESYSTEM_DECL string_type::size_type m_append_separator_if_needed();
648 BOOST_FILESYSTEM_DECL void m_erase_redundant_separator(string_type::size_type sep_pos);
649 BOOST_FILESYSTEM_DECL string_type::size_type m_parent_path_end() const;
651 // Was qualified; como433beta8 reports:
652 // warning #427-D: qualified name is not allowed in member declaration
653 friend class iterator;
654 friend bool operator<(const path& lhs, const path& rhs);
656 // see path::iterator::increment/decrement comment below
657 static BOOST_FILESYSTEM_DECL void m_path_iterator_increment(path::iterator& it);
658 static BOOST_FILESYSTEM_DECL void m_path_iterator_decrement(path::iterator& it);
664 BOOST_FILESYSTEM_DECL
665 int lex_compare(path::iterator first1, path::iterator last1,
666 path::iterator first2, path::iterator last2);
667 BOOST_FILESYSTEM_DECL
668 const path& dot_path();
669 BOOST_FILESYSTEM_DECL
670 const path& dot_dot_path();
673 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
677 //------------------------------------------------------------------------------------//
678 // class path::iterator //
679 //------------------------------------------------------------------------------------//
682 : public boost::iterator_facade<
685 boost::bidirectional_traversal_tag >
688 friend class boost::iterator_core_access;
689 friend class boost::filesystem::path;
690 friend class boost::filesystem::path::reverse_iterator;
691 friend void m_path_iterator_increment(path::iterator & it);
692 friend void m_path_iterator_decrement(path::iterator & it);
694 const path& dereference() const { return m_element; }
696 bool equal(const iterator & rhs) const
698 return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos;
701 // iterator_facade derived classes don't seem to like implementations in
702 // separate translation unit dll's, so forward to class path static members
703 void increment() { m_path_iterator_increment(*this); }
704 void decrement() { m_path_iterator_decrement(*this); }
706 path m_element; // current element
707 const path* m_path_ptr; // path being iterated over
708 string_type::size_type m_pos; // position of m_element in
709 // m_path_ptr->m_pathname.
710 // if m_element is implicit dot, m_pos is the
711 // position of the last separator in the path.
712 // end() iterator is indicated by
713 // m_pos == m_path_ptr->m_pathname.size()
716 //------------------------------------------------------------------------------------//
717 // class path::reverse_iterator //
718 //------------------------------------------------------------------------------------//
720 class path::reverse_iterator
721 : public boost::iterator_facade<
722 path::reverse_iterator,
724 boost::bidirectional_traversal_tag >
727 explicit reverse_iterator(iterator itr) : m_itr(itr)
729 if (itr != itr.m_path_ptr->begin())
734 friend class boost::iterator_core_access;
735 friend class boost::filesystem::path;
737 const path& dereference() const { return m_element; }
738 bool equal(const reverse_iterator& rhs) const { return m_itr == rhs.m_itr; }
742 if (m_itr != m_itr.m_path_ptr->begin())
744 iterator tmp = m_itr;
757 }; // path::reverse_iterator
759 //------------------------------------------------------------------------------------//
761 // non-member functions //
763 //------------------------------------------------------------------------------------//
765 // std::lexicographical_compare would infinitely recurse because path iterators
766 // yield paths, so provide a path aware version
767 inline bool lexicographical_compare(path::iterator first1, path::iterator last1,
768 path::iterator first2, path::iterator last2)
769 { return detail::lex_compare(first1, last1, first2, last2) < 0; }
771 inline bool operator==(const path& lhs, const path& rhs) {return lhs.compare(rhs) == 0;}
772 inline bool operator==(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) == 0;}
773 inline bool operator==(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
774 inline bool operator==(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) == 0;}
775 inline bool operator==(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
777 inline bool operator!=(const path& lhs, const path& rhs) {return lhs.compare(rhs) != 0;}
778 inline bool operator!=(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) != 0;}
779 inline bool operator!=(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
780 inline bool operator!=(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) != 0;}
781 inline bool operator!=(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
783 // TODO: why do == and != have additional overloads, but the others don't?
785 inline bool operator<(const path& lhs, const path& rhs) {return lhs.compare(rhs) < 0;}
786 inline bool operator<=(const path& lhs, const path& rhs) {return !(rhs < lhs);}
787 inline bool operator> (const path& lhs, const path& rhs) {return rhs < lhs;}
788 inline bool operator>=(const path& lhs, const path& rhs) {return !(lhs < rhs);}
790 inline std::size_t hash_value(const path& x) BOOST_NOEXCEPT
792 # ifdef BOOST_WINDOWS_API
793 std::size_t seed = 0;
794 for(const path::value_type* it = x.c_str(); *it; ++it)
795 hash_combine(seed, *it == L'/' ? L'\\' : *it);
797 # else // BOOST_POSIX_API
798 return hash_range(x.native().begin(), x.native().end());
802 inline void swap(path& lhs, path& rhs) BOOST_NOEXCEPT { lhs.swap(rhs); }
804 inline path operator/(const path& lhs, const path& rhs)
810 # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
811 inline path operator/(path&& lhs, const path& rhs)
814 return std::move(lhs);
818 // inserters and extractors
819 // use boost::io::quoted() to handle spaces in paths
820 // use '&' as escape character to ease use for Windows paths
822 template <class Char, class Traits>
823 inline std::basic_ostream<Char, Traits>&
824 operator<<(std::basic_ostream<Char, Traits>& os, const path& p)
827 << boost::io::quoted(p.template string<std::basic_string<Char> >(), static_cast<Char>('&'));
830 template <class Char, class Traits>
831 inline std::basic_istream<Char, Traits>&
832 operator>>(std::basic_istream<Char, Traits>& is, path& p)
834 std::basic_string<Char> str;
835 is >> boost::io::quoted(str, static_cast<Char>('&'));
842 // These functions are holdovers from version 1. It isn't clear they have much
843 // usefulness, or how to generalize them for later versions.
845 BOOST_FILESYSTEM_DECL bool portable_posix_name(const std::string & name);
846 BOOST_FILESYSTEM_DECL bool windows_name(const std::string & name);
847 BOOST_FILESYSTEM_DECL bool portable_name(const std::string & name);
848 BOOST_FILESYSTEM_DECL bool portable_directory_name(const std::string & name);
849 BOOST_FILESYSTEM_DECL bool portable_file_name(const std::string & name);
850 BOOST_FILESYSTEM_DECL bool native(const std::string & name);
854 // For POSIX, is_directory_separator() and is_element_separator() are identical since
855 // a forward slash is the only valid directory separator and also the only valid
856 // element separator. For Windows, forward slash and back slash are the possible
857 // directory separators, but colon (example: "c:foo") is also an element separator.
859 inline bool is_directory_separator(path::value_type c) BOOST_NOEXCEPT
861 return c == path::separator
862 # ifdef BOOST_WINDOWS_API
863 || c == path::preferred_separator
867 inline bool is_element_separator(path::value_type c) BOOST_NOEXCEPT
869 return c == path::separator
870 # ifdef BOOST_WINDOWS_API
871 || c == path::preferred_separator || c == L':'
875 } // namespace detail
877 //------------------------------------------------------------------------------------//
878 // class path miscellaneous function implementations //
879 //------------------------------------------------------------------------------------//
881 inline path::reverse_iterator path::rbegin() const { return reverse_iterator(end()); }
882 inline path::reverse_iterator path::rend() const { return reverse_iterator(begin()); }
884 inline bool path::filename_is_dot() const
886 // implicit dot is tricky, so actually call filename(); see path::filename() example
889 return p.size() == 1 && *p.c_str() == dot;
892 inline bool path::filename_is_dot_dot() const
894 return size() >= 2 && m_pathname[size()-1] == dot && m_pathname[size()-2] == dot
895 && (m_pathname.size() == 2 || detail::is_element_separator(m_pathname[size()-3]));
896 // use detail::is_element_separator() rather than detail::is_directory_separator
897 // to deal with "c:.." edge case on Windows when ':' acts as a separator
900 //--------------------------------------------------------------------------------------//
901 // class path member template implementation //
902 //--------------------------------------------------------------------------------------//
904 template <class InputIterator>
905 path& path::append(InputIterator begin, InputIterator end)
909 string_type::size_type sep_pos(m_append_separator_if_needed());
910 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
912 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname);
914 m_erase_redundant_separator(sep_pos);
918 template <class InputIterator>
919 path& path::append(InputIterator begin, InputIterator end, const codecvt_type& cvt)
923 string_type::size_type sep_pos(m_append_separator_if_needed());
924 std::basic_string<typename std::iterator_traits<InputIterator>::value_type>
926 path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt);
928 m_erase_redundant_separator(sep_pos);
932 template <class Source>
933 path& path::append(Source const& source)
935 if (path_traits::empty(source))
937 string_type::size_type sep_pos(m_append_separator_if_needed());
938 path_traits::dispatch(source, m_pathname);
940 m_erase_redundant_separator(sep_pos);
944 template <class Source>
945 path& path::append(Source const& source, const codecvt_type& cvt)
947 if (path_traits::empty(source))
949 string_type::size_type sep_pos(m_append_separator_if_needed());
950 path_traits::dispatch(source, m_pathname, cvt);
952 m_erase_redundant_separator(sep_pos);
956 //--------------------------------------------------------------------------------------//
957 // class path member template specializations //
958 //--------------------------------------------------------------------------------------//
961 std::string path::string<std::string>() const
965 std::wstring path::string<std::wstring>() const
966 { return wstring(); }
969 std::string path::string<std::string>(const codecvt_type& cvt) const
970 { return string(cvt); }
973 std::wstring path::string<std::wstring>(const codecvt_type& cvt) const
974 { return wstring(cvt); }
977 std::string path::generic_string<std::string>() const
978 { return generic_string(); }
981 std::wstring path::generic_string<std::wstring>() const
982 { return generic_wstring(); }
985 std::string path::generic_string<std::string>(const codecvt_type& cvt) const
986 { return generic_string(cvt); }
989 std::wstring path::generic_string<std::wstring>(const codecvt_type& cvt) const
990 { return generic_wstring(cvt); }
992 //--------------------------------------------------------------------------------------//
993 // path_traits convert function implementations //
994 // requiring path::codecvt() be visable //
995 //--------------------------------------------------------------------------------------//
997 namespace path_traits
1001 void convert(const char* from,
1002 const char* from_end, // 0 for null terminated MBCS
1005 convert(from, from_end, to, path::codecvt());
1009 void convert(const wchar_t* from,
1010 const wchar_t* from_end, // 0 for null terminated MBCS
1013 convert(from, from_end, to, path::codecvt());
1017 void convert(const char* from,
1020 BOOST_ASSERT(!!from);
1021 convert(from, 0, to, path::codecvt());
1025 void convert(const wchar_t* from,
1028 BOOST_ASSERT(!!from);
1029 convert(from, 0, to, path::codecvt());
1031 } // namespace path_traits
1032 } // namespace filesystem
1033 } // namespace boost
1035 //----------------------------------------------------------------------------//
1037 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
1039 #endif // BOOST_FILESYSTEM_PATH_HPP