Chris@16: /* Chris@16: * Distributed under the Boost Software License, Version 1.0.(See accompanying Chris@16: * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) Chris@16: * Chris@16: * See http://www.boost.org/libs/iostreams for documentation. Chris@16: * Chris@16: * File: boost/iostreams/detail/path.hpp Chris@16: * Date: Sat Jun 21 21:24:05 MDT 2008 Chris@16: * Copyright: 2008 CodeRage, LLC Chris@16: * Author: Jonathan Turkanis Chris@16: * Contact: turkanis at coderage dot com Chris@16: * Chris@16: * Defines the class boost::iostreams::detail::path, for storing a Chris@16: * a std::string or std::wstring. Chris@16: * Chris@16: * This class allows interoperability with Boost.Filesystem without Chris@16: * creating a dependence on Boost.Filesystem headers or implementation. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED Chris@16: #define BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS Chris@16: # include Chris@16: #endif Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace iostreams { namespace detail { Chris@16: Chris@16: #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //------------------------------------// Chris@16: Chris@16: class path { Chris@16: template Chris@16: struct sfinae Chris@16: { Chris@16: typedef V type; Chris@16: }; Chris@16: public: Chris@16: Chris@16: // Default constructor Chris@16: path() : narrow_(), wide_(), is_wide_(false) { } Chris@16: Chris@16: // Constructor taking a std::string Chris@16: path(const std::string& p) : narrow_(p), wide_(), is_wide_(false) { } Chris@16: Chris@16: // Constructor taking a C-style string Chris@16: path(const char* p) : narrow_(p), wide_(), is_wide_(false) { } Chris@16: Chris@16: // Constructor taking a boost::filesystem2::path or Chris@16: // boost::filesystem2::wpath Chris@16: template Chris@16: explicit path(const Path& p, typename Path::external_string_type* = 0) Chris@16: { Chris@16: init(p.external_file_string()); Chris@16: } Chris@16: Chris@16: // Constructor taking a boost::filesystem3::path (boost filesystem v3) Chris@16: template Chris@16: explicit path(const Path& p, typename Path::codecvt_type* = 0) Chris@16: { Chris@16: init(p.native()); Chris@16: } Chris@16: Chris@16: // Copy constructor Chris@16: path(const path& p) Chris@16: : narrow_(p.narrow_), wide_(p.wide_), is_wide_(p.is_wide_) Chris@16: { } Chris@16: Chris@16: // Assignment operator taking another path Chris@16: path& operator=(const path& p) Chris@16: { Chris@16: narrow_ = p.narrow_; Chris@16: wide_ = p.wide_; Chris@16: is_wide_ = p.is_wide_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Assignment operator taking a std::string Chris@16: path& operator=(const std::string& p) Chris@16: { Chris@16: narrow_ = p; Chris@16: wide_.clear(); Chris@16: is_wide_ = false; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Assignment operator taking a C-style string Chris@16: path& operator=(const char* p) Chris@16: { Chris@16: narrow_.assign(p); Chris@16: wide_.clear(); Chris@16: is_wide_ = false; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1400) Chris@16: // Assignment operator taking a boost::filesystem2::path or Chris@16: // boost::filesystem2::wpath Chris@16: // (not on Visual C++ 7.1/8.0, as it seems to have problems with Chris@16: // SFINAE functions with the same parameters, doesn't seem Chris@16: // worth working around). Chris@16: template Chris@16: typename sfinae::type Chris@16: operator=(const Path& p) Chris@16: { Chris@16: init(p.external_file_string()); Chris@16: return *this; Chris@16: } Chris@16: #endif Chris@16: Chris@16: // Assignment operator taking a boost::filesystem3::path Chris@16: template Chris@16: typename sfinae::type Chris@16: operator=(const Path& p) Chris@16: { Chris@16: init(p.native()); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: bool is_wide() const { return is_wide_; } Chris@16: Chris@16: // Returns a representation of the underlying path as a std::string Chris@16: // Requires: is_wide() returns false Chris@16: const char* c_str() const { return narrow_.c_str(); } Chris@16: Chris@16: // Returns a representation of the underlying path as a std::wstring Chris@16: // Requires: is_wide() returns true Chris@16: const wchar_t* c_wstr() const { return wide_.c_str(); } Chris@16: private: Chris@16: Chris@16: // For wide-character paths, use a boost::filesystem::wpath instead of a Chris@16: // std::wstring Chris@16: path(const std::wstring&); Chris@16: path& operator=(const std::wstring&); Chris@16: Chris@16: void init(std::string const& file_path) Chris@16: { Chris@16: narrow_ = file_path; Chris@16: wide_.clear(); Chris@16: is_wide_ = false; Chris@16: } Chris@16: Chris@16: void init(std::wstring const& file_path) Chris@16: { Chris@16: narrow_.clear(); Chris@16: wide_ = file_path; Chris@16: is_wide_ = true; Chris@16: } Chris@16: Chris@16: std::string narrow_; Chris@16: std::wstring wide_; Chris@16: bool is_wide_; Chris@16: }; Chris@16: Chris@16: inline bool operator==(const path& lhs, const path& rhs) Chris@16: { Chris@16: return lhs.is_wide() ? Chris@16: rhs.is_wide() && std::wcscmp(lhs.c_wstr(), rhs.c_wstr()) == 0 : Chris@16: !rhs.is_wide() && std::strcmp(lhs.c_str(), rhs.c_str()) == 0; Chris@16: } Chris@16: Chris@16: #else // #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //---------------------------// Chris@16: Chris@16: class path { Chris@16: public: Chris@16: path() { } Chris@16: path(const std::string& p) : path_(p) { } Chris@16: path(const char* p) : path_(p) { } Chris@16: template Chris@16: path(const Path& p) : path_(p.external_file_string()) { } Chris@16: path(const path& p) : path_(p.path_) { } Chris@16: path& operator=(const path& other) Chris@16: { Chris@16: path_ = other.path_; Chris@16: return *this; Chris@16: } Chris@16: path& operator=(const std::string& p) Chris@16: { Chris@16: path_ = p; Chris@16: return *this; Chris@16: } Chris@16: path& operator=(const char* p) Chris@16: { Chris@16: path_ = p; Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: path& operator=(const Path& p) Chris@16: { Chris@16: path_ = p.external_file_string(); Chris@16: return *this; Chris@16: } Chris@16: bool is_wide() const { return false; } Chris@16: const char* c_str() const { return path_.c_str(); } Chris@16: const wchar_t* c_wstr() const { return 0; } Chris@16: private: Chris@16: std::string path_; Chris@16: }; Chris@16: Chris@16: inline bool operator==(const path& lhs, const path& rhs) Chris@16: { Chris@16: return std::strcmp(lhs.c_str(), rhs.c_str()) == 0 ; Chris@16: } Chris@16: Chris@16: #endif // #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //--------------------------// Chris@16: Chris@16: } } } // End namespaces detail, iostreams, boost. Chris@16: Chris@16: #endif // #ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED