Chris@16: // filesystem path.hpp ---------------------------------------------------------------// Chris@16: Chris@16: // Copyright Beman Dawes 2002-2005, 2009 Chris@16: // Copyright Vladimir Prus 2002 Chris@16: Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // See http://www.boost.org/LICENSE_1_0.txt Chris@16: Chris@16: // Library home page: http://www.boost.org/libs/filesystem Chris@16: Chris@16: // path::stem(), extension(), and replace_extension() are based on Chris@16: // basename(), extension(), and change_extension() from the original Chris@16: // filesystem/convenience.hpp header by Vladimir Prus. Chris@16: Chris@16: #ifndef BOOST_FILESYSTEM_PATH_HPP Chris@16: #define BOOST_FILESYSTEM_PATH_HPP Chris@16: Chris@16: #include Chris@16: Chris@16: # if defined( BOOST_NO_STD_WSTRING ) Chris@16: # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support Chris@16: # endif Chris@16: Chris@16: #include Chris@16: #include // includes Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include // must be the last #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace filesystem Chris@16: { Chris@16: //------------------------------------------------------------------------------------// Chris@16: // // Chris@16: // class path // Chris@16: // // Chris@16: //------------------------------------------------------------------------------------// Chris@16: Chris@16: class BOOST_FILESYSTEM_DECL path Chris@16: { Chris@16: public: Chris@16: Chris@16: // value_type is the character type used by the operating system API to Chris@16: // represent paths. Chris@16: Chris@16: # ifdef BOOST_WINDOWS_API Chris@16: typedef wchar_t value_type; Chris@16: BOOST_STATIC_CONSTEXPR value_type preferred_separator = L'\\'; Chris@16: # else Chris@16: typedef char value_type; Chris@16: BOOST_STATIC_CONSTEXPR value_type preferred_separator = '/'; Chris@16: # endif Chris@16: typedef std::basic_string string_type; Chris@16: typedef std::codecvt codecvt_type; Chris@16: Chris@16: Chris@16: // ----- character encoding conversions ----- Chris@16: Chris@16: // Following the principle of least astonishment, path input arguments Chris@16: // passed to or obtained from the operating system via objects of Chris@16: // class path behave as if they were directly passed to or Chris@16: // obtained from the O/S API, unless conversion is explicitly requested. Chris@16: // Chris@16: // POSIX specfies that path strings are passed unchanged to and from the Chris@16: // API. Note that this is different from the POSIX command line utilities, Chris@16: // which convert according to a locale. Chris@16: // Chris@16: // Thus for POSIX, char strings do not undergo conversion. wchar_t strings Chris@16: // are converted to/from char using the path locale or, if a conversion Chris@16: // argument is given, using a conversion object modeled on Chris@16: // std::wstring_convert. Chris@16: // Chris@16: // The path locale, which is global to the thread, can be changed by the Chris@16: // imbue() function. It is initialized to an implementation defined locale. Chris@16: // Chris@16: // For Windows, wchar_t strings do not undergo conversion. char strings Chris@16: // are converted using the "ANSI" or "OEM" code pages, as determined by Chris@16: // the AreFileApisANSI() function, or, if a conversion argument is given, Chris@16: // using a conversion object modeled on std::wstring_convert. Chris@16: // Chris@16: // See m_pathname comments for further important rationale. Chris@16: Chris@16: // TODO: rules needed for operating systems that use / or . Chris@16: // differently, or format directory paths differently from file paths. Chris@16: // Chris@16: // ********************************************************************************** Chris@16: // Chris@16: // More work needed: How to handle an operating system that may have Chris@16: // slash characters or dot characters in valid filenames, either because Chris@16: // it doesn't follow the POSIX standard, or because it allows MBCS Chris@16: // filename encodings that may contain slash or dot characters. For Chris@16: // example, ISO/IEC 2022 (JIS) encoding which allows switching to Chris@16: // JIS x0208-1983 encoding. A valid filename in this set of encodings is Chris@16: // 0x1B 0x24 0x42 [switch to X0208-1983] 0x24 0x2F [U+304F Kiragana letter KU] Chris@16: // ^^^^ Chris@16: // Note that 0x2F is the ASCII slash character Chris@16: // Chris@16: // ********************************************************************************** Chris@16: Chris@16: // Supported source arguments: half-open iterator range, container, c-array, Chris@16: // and single pointer to null terminated string. Chris@16: Chris@16: // All source arguments except pointers to null terminated byte strings support Chris@16: // multi-byte character strings which may have embedded nulls. Embedded null Chris@16: // support is required for some Asian languages on Windows. Chris@16: Chris@101: // "const codecvt_type& cvt=codecvt()" default arguments are not used because this Chris@101: // limits the impact of locale("") initialization failures on POSIX systems to programs Chris@101: // that actually depend on locale(""). It further ensures that exceptions thrown Chris@101: // as a result of such failues occur after main() has started, so can be caught. Chris@16: Chris@16: // ----- constructors ----- Chris@16: Chris@16: path(){} Chris@16: Chris@16: path(const path& p) : m_pathname(p.m_pathname) {} Chris@16: Chris@16: template Chris@16: path(Source const& source, Chris@16: typename boost::enable_if::type> >::type* =0) Chris@16: { Chris@101: path_traits::dispatch(source, m_pathname); Chris@16: } Chris@16: Chris@16: path(const value_type* s) : m_pathname(s) {} Chris@101: path(value_type* s) : m_pathname(s) {} Chris@101: path(const string_type& s) : m_pathname(s) {} Chris@101: path(string_type& s) : m_pathname(s) {} Chris@16: Chris@16: template Chris@16: path(Source const& source, const codecvt_type& cvt) Chris@16: { Chris@16: path_traits::dispatch(source, m_pathname, cvt); Chris@16: } Chris@16: Chris@16: template Chris@16: path(InputIterator begin, InputIterator end) Chris@16: { Chris@16: if (begin != end) Chris@16: { Chris@101: // convert requires contiguous string, so copy Chris@16: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: path(InputIterator begin, InputIterator end, const codecvt_type& cvt) Chris@16: { Chris@16: if (begin != end) Chris@16: { Chris@101: // convert requires contiguous string, so copy Chris@16: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt); Chris@16: } Chris@16: } Chris@16: Chris@16: // ----- assignments ----- Chris@16: Chris@16: path& operator=(const path& p) Chris@16: { Chris@16: m_pathname = p.m_pathname; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: typename boost::enable_if::type>, path&>::type Chris@16: operator=(Source const& source) Chris@16: { Chris@16: m_pathname.clear(); Chris@101: path_traits::dispatch(source, m_pathname); Chris@16: return *this; Chris@16: } Chris@16: Chris@101: // value_type overloads Chris@101: Chris@101: path& operator=(const value_type* ptr) // required in case ptr overlaps *this Chris@101: {m_pathname = ptr; return *this;} Chris@101: path& operator=(value_type* ptr) // required in case ptr overlaps *this Chris@101: {m_pathname = ptr; return *this;} Chris@101: path& operator=(const string_type& s) {m_pathname = s; return *this;} Chris@101: path& operator=(string_type& s) {m_pathname = s; return *this;} Chris@101: Chris@16: path& assign(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this Chris@101: {m_pathname = ptr; return *this;} Chris@16: template Chris@16: path& assign(Source const& source, const codecvt_type& cvt) Chris@16: { Chris@16: m_pathname.clear(); Chris@16: path_traits::dispatch(source, m_pathname, cvt); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: path& assign(InputIterator begin, InputIterator end) Chris@16: { Chris@101: m_pathname.clear(); Chris@101: if (begin != end) Chris@101: { Chris@101: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname); Chris@101: } Chris@101: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: path& assign(InputIterator begin, InputIterator end, const codecvt_type& cvt) Chris@16: { Chris@16: m_pathname.clear(); Chris@16: if (begin != end) Chris@16: { Chris@16: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // ----- concatenation ----- Chris@16: Chris@16: template Chris@16: typename boost::enable_if::type>, path&>::type Chris@16: operator+=(Source const& source) Chris@16: { Chris@101: return concat(source); Chris@16: } Chris@16: Chris@101: // value_type overloads. Same rationale as for constructors above Chris@101: path& operator+=(const path& p) { m_pathname += p.m_pathname; return *this; } Chris@101: path& operator+=(const value_type* ptr) { m_pathname += ptr; return *this; } Chris@101: path& operator+=(value_type* ptr) { m_pathname += ptr; return *this; } Chris@101: path& operator+=(const string_type& s) { m_pathname += s; return *this; } Chris@101: path& operator+=(string_type& s) { m_pathname += s; return *this; } Chris@101: path& operator+=(value_type c) { m_pathname += c; return *this; } Chris@101: Chris@16: template Chris@16: typename boost::enable_if, path&>::type Chris@16: operator+=(CharT c) Chris@16: { Chris@16: CharT tmp[2]; Chris@16: tmp[0] = c; Chris@16: tmp[1] = 0; Chris@101: return concat(tmp); Chris@101: } Chris@101: Chris@101: template Chris@101: path& concat(Source const& source) Chris@101: { Chris@101: path_traits::dispatch(source, m_pathname); Chris@101: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: path& concat(Source const& source, const codecvt_type& cvt) Chris@16: { Chris@16: path_traits::dispatch(source, m_pathname, cvt); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: path& concat(InputIterator begin, InputIterator end) Chris@16: { Chris@101: if (begin == end) Chris@101: return *this; Chris@101: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname); Chris@101: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: path& concat(InputIterator begin, InputIterator end, const codecvt_type& cvt) Chris@16: { Chris@16: if (begin == end) Chris@16: return *this; Chris@16: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // ----- appends ----- Chris@16: Chris@16: // if a separator is added, it is the preferred separator for the platform; Chris@16: // slash for POSIX, backslash for Windows Chris@16: Chris@16: path& operator/=(const path& p); Chris@16: Chris@16: template Chris@16: typename boost::enable_if::type>, path&>::type Chris@16: operator/=(Source const& source) Chris@16: { Chris@101: return append(source); Chris@101: } Chris@101: Chris@101: path& operator/=(const value_type* ptr); Chris@101: path& operator/=(value_type* ptr) Chris@101: { Chris@101: return this->operator/=(const_cast(ptr)); Chris@101: } Chris@101: path& operator/=(const string_type& s) { return this->operator/=(path(s)); } Chris@101: path& operator/=(string_type& s) { return this->operator/=(path(s)); } Chris@101: Chris@101: path& append(const value_type* ptr) // required in case ptr overlaps *this Chris@101: { Chris@101: this->operator/=(ptr); Chris@101: return *this; Chris@16: } Chris@16: Chris@16: path& append(const value_type* ptr, const codecvt_type&) // required in case ptr overlaps *this Chris@16: { Chris@16: this->operator/=(ptr); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: path& append(Source const& source); Chris@101: Chris@101: template Chris@16: path& append(Source const& source, const codecvt_type& cvt); Chris@16: Chris@16: template Chris@101: path& append(InputIterator begin, InputIterator end); Chris@16: Chris@16: template Chris@16: path& append(InputIterator begin, InputIterator end, const codecvt_type& cvt); Chris@16: Chris@16: // ----- modifiers ----- Chris@16: Chris@16: void clear() { m_pathname.clear(); } Chris@16: path& make_preferred() Chris@16: # ifdef BOOST_POSIX_API Chris@16: { return *this; } // POSIX no effect Chris@16: # else // BOOST_WINDOWS_API Chris@16: ; // change slashes to backslashes Chris@16: # endif Chris@16: path& remove_filename(); Chris@101: path& remove_trailing_separator(); Chris@16: path& replace_extension(const path& new_extension = path()); Chris@16: void swap(path& rhs) { m_pathname.swap(rhs.m_pathname); } Chris@16: Chris@16: // ----- observers ----- Chris@16: Chris@16: // For operating systems that format file paths differently than directory Chris@16: // paths, return values from observers are formatted as file names unless there Chris@16: // is a trailing separator, in which case returns are formatted as directory Chris@16: // paths. POSIX and Windows make no such distinction. Chris@16: Chris@16: // Implementations are permitted to return const values or const references. Chris@16: Chris@16: // The string or path returned by an observer are specified as being formatted Chris@16: // as "native" or "generic". Chris@16: // Chris@16: // For POSIX, these are all the same format; slashes and backslashes are as input and Chris@16: // are not modified. Chris@16: // Chris@16: // For Windows, native: as input; slashes and backslashes are not modified; Chris@16: // this is the format of the internally stored string. Chris@16: // generic: backslashes are converted to slashes Chris@16: Chris@16: // ----- native format observers ----- Chris@16: Chris@16: const string_type& native() const { return m_pathname; } // Throws: nothing Chris@16: const value_type* c_str() const { return m_pathname.c_str(); } // Throws: nothing Chris@16: Chris@16: template Chris@16: String string() const; Chris@16: Chris@16: template Chris@16: String string(const codecvt_type& cvt) const; Chris@16: Chris@16: # ifdef BOOST_WINDOWS_API Chris@101: const std::string string() const Chris@101: { Chris@101: std::string tmp; Chris@101: if (!m_pathname.empty()) Chris@101: path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(), Chris@101: tmp); Chris@101: return tmp; Chris@101: } Chris@16: const std::string string(const codecvt_type& cvt) const Chris@16: { Chris@16: std::string tmp; Chris@16: if (!m_pathname.empty()) Chris@16: path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(), Chris@16: tmp, cvt); Chris@16: return tmp; Chris@16: } Chris@16: Chris@16: // string_type is std::wstring, so there is no conversion Chris@16: const std::wstring& wstring() const { return m_pathname; } Chris@16: const std::wstring& wstring(const codecvt_type&) const { return m_pathname; } Chris@16: Chris@16: # else // BOOST_POSIX_API Chris@16: // string_type is std::string, so there is no conversion Chris@16: const std::string& string() const { return m_pathname; } Chris@16: const std::string& string(const codecvt_type&) const { return m_pathname; } Chris@16: Chris@101: const std::wstring wstring() const Chris@101: { Chris@101: std::wstring tmp; Chris@101: if (!m_pathname.empty()) Chris@101: path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(), Chris@101: tmp); Chris@101: return tmp; Chris@101: } Chris@16: const std::wstring wstring(const codecvt_type& cvt) const Chris@16: { Chris@16: std::wstring tmp; Chris@16: if (!m_pathname.empty()) Chris@16: path_traits::convert(&*m_pathname.begin(), &*m_pathname.begin()+m_pathname.size(), Chris@16: tmp, cvt); Chris@16: return tmp; Chris@16: } Chris@16: Chris@16: # endif Chris@16: Chris@16: // ----- generic format observers ----- Chris@16: Chris@16: template Chris@16: String generic_string() const; Chris@16: Chris@16: template Chris@16: String generic_string(const codecvt_type& cvt) const; Chris@16: Chris@16: # ifdef BOOST_WINDOWS_API Chris@101: const std::string generic_string() const; Chris@16: const std::string generic_string(const codecvt_type& cvt) const; Chris@16: const std::wstring generic_wstring() const; Chris@16: const std::wstring generic_wstring(const codecvt_type&) const { return generic_wstring(); }; Chris@16: Chris@16: # else // BOOST_POSIX_API Chris@16: // On POSIX-like systems, the generic format is the same as the native format Chris@16: const std::string& generic_string() const { return m_pathname; } Chris@16: const std::string& generic_string(const codecvt_type&) const { return m_pathname; } Chris@101: const std::wstring generic_wstring() const { return wstring(); } Chris@16: const std::wstring generic_wstring(const codecvt_type& cvt) const { return wstring(cvt); } Chris@16: Chris@16: # endif Chris@16: Chris@16: // ----- compare ----- Chris@16: Chris@16: int compare(const path& p) const BOOST_NOEXCEPT; // generic, lexicographical Chris@16: int compare(const std::string& s) const { return compare(path(s)); } Chris@16: int compare(const value_type* s) const { return compare(path(s)); } Chris@16: Chris@16: // ----- decomposition ----- Chris@16: Chris@16: path root_path() const; Chris@16: path root_name() const; // returns 0 or 1 element path Chris@16: // even on POSIX, root_name() is non-empty() for network paths Chris@16: path root_directory() const; // returns 0 or 1 element path Chris@16: path relative_path() const; Chris@16: path parent_path() const; Chris@16: path filename() const; // returns 0 or 1 element path Chris@16: path stem() const; // returns 0 or 1 element path Chris@16: path extension() const; // returns 0 or 1 element path Chris@16: Chris@16: // ----- query ----- Chris@16: Chris@16: bool empty() const { return m_pathname.empty(); } // name consistent with std containers Chris@16: bool has_root_path() const { return has_root_directory() || has_root_name(); } Chris@16: bool has_root_name() const { return !root_name().empty(); } Chris@16: bool has_root_directory() const { return !root_directory().empty(); } Chris@16: bool has_relative_path() const { return !relative_path().empty(); } Chris@16: bool has_parent_path() const { return !parent_path().empty(); } Chris@16: bool has_filename() const { return !m_pathname.empty(); } Chris@16: bool has_stem() const { return !stem().empty(); } Chris@16: bool has_extension() const { return !extension().empty(); } Chris@16: bool is_absolute() const Chris@16: { Chris@16: # ifdef BOOST_WINDOWS_API Chris@16: return has_root_name() && has_root_directory(); Chris@16: # else Chris@16: return has_root_directory(); Chris@16: # endif Chris@16: } Chris@16: bool is_relative() const { return !is_absolute(); } Chris@16: Chris@16: // ----- iterators ----- Chris@16: Chris@16: class iterator; Chris@16: typedef iterator const_iterator; Chris@16: Chris@16: iterator begin() const; Chris@16: iterator end() const; Chris@16: Chris@16: // ----- static member functions ----- Chris@16: Chris@16: static std::locale imbue(const std::locale& loc); Chris@16: static const codecvt_type& codecvt(); Chris@16: Chris@16: // ----- deprecated functions ----- Chris@16: Chris@16: # if defined(BOOST_FILESYSTEM_DEPRECATED) && defined(BOOST_FILESYSTEM_NO_DEPRECATED) Chris@16: # error both BOOST_FILESYSTEM_DEPRECATED and BOOST_FILESYSTEM_NO_DEPRECATED are defined Chris@16: # endif Chris@16: Chris@16: # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED) Chris@16: // recently deprecated functions supplied by default Chris@16: path& normalize() { return m_normalize(); } Chris@16: path& remove_leaf() { return remove_filename(); } Chris@16: path leaf() const { return filename(); } Chris@16: path branch_path() const { return parent_path(); } Chris@16: bool has_leaf() const { return !m_pathname.empty(); } Chris@16: bool has_branch_path() const { return !parent_path().empty(); } Chris@16: bool is_complete() const { return is_absolute(); } Chris@16: # endif Chris@16: Chris@16: # if defined(BOOST_FILESYSTEM_DEPRECATED) Chris@16: // deprecated functions with enough signature or semantic changes that they are Chris@16: // not supplied by default Chris@16: const std::string file_string() const { return string(); } Chris@16: const std::string directory_string() const { return string(); } Chris@16: const std::string native_file_string() const { return string(); } Chris@16: const std::string native_directory_string() const { return string(); } Chris@16: const string_type external_file_string() const { return native(); } Chris@16: const string_type external_directory_string() const { return native(); } Chris@16: Chris@16: // older functions no longer supported Chris@16: //typedef bool (*name_check)(const std::string & name); Chris@16: //basic_path(const string_type& str, name_check) { operator/=(str); } Chris@16: //basic_path(const typename string_type::value_type* s, name_check) Chris@16: // { operator/=(s);} Chris@16: //static bool default_name_check_writable() { return false; } Chris@16: //static void default_name_check(name_check) {} Chris@16: //static name_check default_name_check() { return 0; } Chris@16: //basic_path& canonize(); Chris@16: # endif Chris@16: Chris@16: //--------------------------------------------------------------------------------------// Chris@16: // class path private members // Chris@16: //--------------------------------------------------------------------------------------// Chris@16: Chris@16: private: Chris@16: # if defined(_MSC_VER) Chris@16: # pragma warning(push) // Save warning settings Chris@16: # pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>' Chris@16: # endif // needs to have dll-interface... Chris@16: /* Chris@16: m_pathname has the type, encoding, and format required by the native Chris@16: operating system. Thus for POSIX and Windows there is no conversion for Chris@16: passing m_pathname.c_str() to the O/S API or when obtaining a path from the Chris@16: O/S API. POSIX encoding is unspecified other than for dot and slash Chris@16: characters; POSIX just treats paths as a sequence of bytes. Windows Chris@16: encoding is UCS-2 or UTF-16 depending on the version. Chris@16: */ Chris@16: string_type m_pathname; // Windows: as input; backslashes NOT converted to slashes, Chris@16: // slashes NOT converted to backslashes Chris@16: # if defined(_MSC_VER) Chris@16: # pragma warning(pop) // restore warning settings. Chris@16: # endif Chris@16: Chris@16: string_type::size_type m_append_separator_if_needed(); Chris@16: // Returns: If separator is to be appended, m_pathname.size() before append. Otherwise 0. Chris@16: // Note: An append is never performed if size()==0, so a returned 0 is unambiguous. Chris@16: Chris@16: void m_erase_redundant_separator(string_type::size_type sep_pos); Chris@16: string_type::size_type m_parent_path_end() const; Chris@16: Chris@16: path& m_normalize(); Chris@16: Chris@16: // Was qualified; como433beta8 reports: Chris@16: // warning #427-D: qualified name is not allowed in member declaration Chris@16: friend class iterator; Chris@16: friend bool operator<(const path& lhs, const path& rhs); Chris@16: Chris@16: // see path::iterator::increment/decrement comment below Chris@16: static void m_path_iterator_increment(path::iterator & it); Chris@16: static void m_path_iterator_decrement(path::iterator & it); Chris@16: Chris@16: }; // class path Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: BOOST_FILESYSTEM_DECL Chris@16: int lex_compare(path::iterator first1, path::iterator last1, Chris@16: path::iterator first2, path::iterator last2); Chris@101: BOOST_FILESYSTEM_DECL Chris@101: const path& dot_path(); Chris@101: BOOST_FILESYSTEM_DECL Chris@101: const path& dot_dot_path(); Chris@16: } Chris@16: Chris@16: # ifndef BOOST_FILESYSTEM_NO_DEPRECATED Chris@16: typedef path wpath; Chris@16: # endif Chris@16: Chris@16: //------------------------------------------------------------------------------------// Chris@16: // class path::iterator // Chris@16: //------------------------------------------------------------------------------------// Chris@16: Chris@16: class path::iterator Chris@16: : public boost::iterator_facade< Chris@16: path::iterator, Chris@16: path const, Chris@16: boost::bidirectional_traversal_tag > Chris@16: { Chris@16: private: Chris@16: friend class boost::iterator_core_access; Chris@16: friend class boost::filesystem::path; Chris@16: friend void m_path_iterator_increment(path::iterator & it); Chris@16: friend void m_path_iterator_decrement(path::iterator & it); Chris@16: Chris@16: const path& dereference() const { return m_element; } Chris@16: Chris@16: bool equal(const iterator & rhs) const Chris@16: { Chris@16: return m_path_ptr == rhs.m_path_ptr && m_pos == rhs.m_pos; Chris@16: } Chris@16: Chris@16: // iterator_facade derived classes don't seem to like implementations in Chris@16: // separate translation unit dll's, so forward to class path static members Chris@16: void increment() { m_path_iterator_increment(*this); } Chris@16: void decrement() { m_path_iterator_decrement(*this); } Chris@16: Chris@16: path m_element; // current element Chris@16: const path* m_path_ptr; // path being iterated over Chris@16: string_type::size_type m_pos; // position of m_element in Chris@16: // m_path_ptr->m_pathname. Chris@16: // if m_element is implicit dot, m_pos is the Chris@16: // position of the last separator in the path. Chris@16: // end() iterator is indicated by Chris@16: // m_pos == m_path_ptr->m_pathname.size() Chris@16: }; // path::iterator Chris@16: Chris@16: //------------------------------------------------------------------------------------// Chris@16: // // Chris@16: // non-member functions // Chris@16: // // Chris@16: //------------------------------------------------------------------------------------// Chris@16: Chris@16: // std::lexicographical_compare would infinately recurse because path iterators Chris@16: // yield paths, so provide a path aware version Chris@16: inline bool lexicographical_compare(path::iterator first1, path::iterator last1, Chris@16: path::iterator first2, path::iterator last2) Chris@16: { return detail::lex_compare(first1, last1, first2, last2) < 0; } Chris@16: Chris@16: inline bool operator==(const path& lhs, const path& rhs) {return lhs.compare(rhs) == 0;} Chris@16: inline bool operator==(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) == 0;} Chris@16: inline bool operator==(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) == 0;} Chris@16: inline bool operator==(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) == 0;} Chris@16: inline bool operator==(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) == 0;} Chris@16: Chris@16: inline bool operator!=(const path& lhs, const path& rhs) {return lhs.compare(rhs) != 0;} Chris@16: inline bool operator!=(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) != 0;} Chris@16: inline bool operator!=(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) != 0;} Chris@16: inline bool operator!=(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) != 0;} Chris@16: inline bool operator!=(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) != 0;} Chris@16: Chris@16: // TODO: why do == and != have additional overloads, but the others don't? Chris@16: Chris@16: inline bool operator<(const path& lhs, const path& rhs) {return lhs.compare(rhs) < 0;} Chris@16: inline bool operator<=(const path& lhs, const path& rhs) {return !(rhs < lhs);} Chris@16: inline bool operator> (const path& lhs, const path& rhs) {return rhs < lhs;} Chris@16: inline bool operator>=(const path& lhs, const path& rhs) {return !(lhs < rhs);} Chris@16: Chris@16: inline std::size_t hash_value(const path& x) Chris@16: { Chris@16: # ifdef BOOST_WINDOWS_API Chris@16: std::size_t seed = 0; Chris@16: for(const path::value_type* it = x.c_str(); *it; ++it) Chris@16: hash_combine(seed, *it == '/' ? L'\\' : *it); Chris@16: return seed; Chris@16: # else // BOOST_POSIX_API Chris@16: return hash_range(x.native().begin(), x.native().end()); Chris@16: # endif Chris@16: } Chris@16: Chris@16: inline void swap(path& lhs, path& rhs) { lhs.swap(rhs); } Chris@16: Chris@16: inline path operator/(const path& lhs, const path& rhs) { return path(lhs) /= rhs; } Chris@16: Chris@16: // inserters and extractors Chris@16: // use boost::io::quoted() to handle spaces in paths Chris@16: // use '&' as escape character to ease use for Windows paths Chris@16: Chris@16: template Chris@16: inline std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, const path& p) Chris@16: { Chris@16: return os Chris@16: << boost::io::quoted(p.template string >(), static_cast('&')); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::basic_istream& Chris@16: operator>>(std::basic_istream& is, path& p) Chris@16: { Chris@16: std::basic_string str; Chris@16: is >> boost::io::quoted(str, static_cast('&')); Chris@16: p = str; Chris@16: return is; Chris@16: } Chris@16: Chris@16: // name_checks Chris@16: Chris@16: // These functions are holdovers from version 1. It isn't clear they have much Chris@16: // usefulness, or how to generalize them for later versions. Chris@16: Chris@16: BOOST_FILESYSTEM_DECL bool portable_posix_name(const std::string & name); Chris@16: BOOST_FILESYSTEM_DECL bool windows_name(const std::string & name); Chris@16: BOOST_FILESYSTEM_DECL bool portable_name(const std::string & name); Chris@16: BOOST_FILESYSTEM_DECL bool portable_directory_name(const std::string & name); Chris@16: BOOST_FILESYSTEM_DECL bool portable_file_name(const std::string & name); Chris@16: BOOST_FILESYSTEM_DECL bool native(const std::string & name); Chris@16: Chris@16: //--------------------------------------------------------------------------------------// Chris@16: // class path member template implementation // Chris@16: //--------------------------------------------------------------------------------------// Chris@16: Chris@16: template Chris@101: path& path::append(InputIterator begin, InputIterator end) Chris@101: { Chris@16: if (begin == end) Chris@16: return *this; Chris@16: string_type::size_type sep_pos(m_append_separator_if_needed()); Chris@16: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname); Chris@101: if (sep_pos) Chris@101: m_erase_redundant_separator(sep_pos); Chris@101: return *this; Chris@101: } Chris@101: Chris@101: template Chris@101: path& path::append(InputIterator begin, InputIterator end, const codecvt_type& cvt) Chris@101: { Chris@101: if (begin == end) Chris@101: return *this; Chris@101: string_type::size_type sep_pos(m_append_separator_if_needed()); Chris@101: std::basic_string::value_type> Chris@101: seq(begin, end); Chris@101: path_traits::convert(seq.c_str(), seq.c_str()+seq.size(), m_pathname, cvt); Chris@101: if (sep_pos) Chris@101: m_erase_redundant_separator(sep_pos); Chris@101: return *this; Chris@101: } Chris@101: Chris@101: template Chris@101: path& path::append(Source const& source) Chris@101: { Chris@101: if (path_traits::empty(source)) Chris@101: return *this; Chris@101: string_type::size_type sep_pos(m_append_separator_if_needed()); Chris@101: path_traits::dispatch(source, m_pathname); Chris@16: if (sep_pos) Chris@16: m_erase_redundant_separator(sep_pos); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: path& path::append(Source const& source, const codecvt_type& cvt) Chris@16: { Chris@16: if (path_traits::empty(source)) Chris@16: return *this; Chris@16: string_type::size_type sep_pos(m_append_separator_if_needed()); Chris@16: path_traits::dispatch(source, m_pathname, cvt); Chris@16: if (sep_pos) Chris@16: m_erase_redundant_separator(sep_pos); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //--------------------------------------------------------------------------------------// Chris@16: // class path member template specializations // Chris@16: //--------------------------------------------------------------------------------------// Chris@16: Chris@16: template <> inline Chris@16: std::string path::string() const Chris@16: { return string(); } Chris@16: Chris@16: template <> inline Chris@16: std::wstring path::string() const Chris@16: { return wstring(); } Chris@16: Chris@16: template <> inline Chris@16: std::string path::string(const codecvt_type& cvt) const Chris@16: { return string(cvt); } Chris@16: Chris@16: template <> inline Chris@16: std::wstring path::string(const codecvt_type& cvt) const Chris@16: { return wstring(cvt); } Chris@16: Chris@16: template <> inline Chris@16: std::string path::generic_string() const Chris@16: { return generic_string(); } Chris@16: Chris@16: template <> inline Chris@16: std::wstring path::generic_string() const Chris@16: { return generic_wstring(); } Chris@16: Chris@16: template <> inline Chris@16: std::string path::generic_string(const codecvt_type& cvt) const Chris@16: { return generic_string(cvt); } Chris@16: Chris@16: template <> inline Chris@16: std::wstring path::generic_string(const codecvt_type& cvt) const Chris@16: { return generic_wstring(cvt); } Chris@16: Chris@101: //--------------------------------------------------------------------------------------// Chris@101: // path_traits convert function implementations // Chris@101: // requiring path::codecvt() be visable // Chris@101: //--------------------------------------------------------------------------------------// Chris@16: Chris@101: namespace path_traits Chris@101: { // without codecvt Chris@101: Chris@101: inline Chris@101: void convert(const char* from, Chris@101: const char* from_end, // 0 for null terminated MBCS Chris@101: std::wstring & to) Chris@101: { Chris@101: convert(from, from_end, to, path::codecvt()); Chris@101: } Chris@101: Chris@101: inline Chris@101: void convert(const wchar_t* from, Chris@101: const wchar_t* from_end, // 0 for null terminated MBCS Chris@101: std::string & to) Chris@101: { Chris@101: convert(from, from_end, to, path::codecvt()); Chris@101: } Chris@101: Chris@101: inline Chris@101: void convert(const char* from, Chris@101: std::wstring & to) Chris@101: { Chris@101: BOOST_ASSERT(from); Chris@101: convert(from, 0, to, path::codecvt()); Chris@101: } Chris@101: Chris@101: inline Chris@101: void convert(const wchar_t* from, Chris@101: std::string & to) Chris@101: { Chris@101: BOOST_ASSERT(from); Chris@101: convert(from, 0, to, path::codecvt()); Chris@101: } Chris@101: } // namespace path_traits Chris@16: } // namespace filesystem Chris@16: } // namespace boost Chris@16: Chris@16: //----------------------------------------------------------------------------// Chris@16: Chris@16: #include // pops abi_prefix.hpp pragmas Chris@16: Chris@16: #endif // BOOST_FILESYSTEM_PATH_HPP