Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:50

0001 /*
0002  * Distributed under the Boost Software License, Version 1.0.(See accompanying 
0003  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
0004  * 
0005  * See http://www.boost.org/libs/iostreams for documentation.
0006  *
0007  * File:        boost/iostreams/detail/path.hpp
0008  * Date:        Sat Jun 21 21:24:05 MDT 2008
0009  * Copyright:   2008 CodeRage, LLC
0010  * Author:      Jonathan Turkanis
0011  * Contact:     turkanis at coderage dot com
0012  *
0013  * Defines the class boost::iostreams::detail::path, for storing a 
0014  * a std::string or std::wstring.
0015  *
0016  * This class allows interoperability with Boost.Filesystem without
0017  * creating a dependence on Boost.Filesystem headers or implementation.
0018  */
0019 
0020 #ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED
0021 #define BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED
0022 
0023 #include <cstring>
0024 #include <string>
0025 #include <boost/iostreams/detail/config/wide_streams.hpp>
0026 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
0027 # include <cwchar>
0028 #endif
0029 #include <boost/static_assert.hpp>
0030 #include <boost/type.hpp>
0031 #include <boost/type_traits/is_same.hpp>
0032 
0033 namespace boost { namespace iostreams { namespace detail {
0034 
0035 #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //------------------------------------//
0036 
0037 class path {
0038     template<typename T, typename V>
0039     struct sfinae
0040     {
0041         typedef V type;
0042     };
0043 public:
0044 
0045     // Default constructor
0046     path() : narrow_(), wide_(), is_wide_(false) { }
0047 
0048     // Constructor taking a std::string
0049     path(const std::string& p) : narrow_(p), wide_(), is_wide_(false) { }
0050 
0051     // Constructor taking a C-style string
0052     path(const char* p) : narrow_(p), wide_(), is_wide_(false) { }
0053 
0054     // Constructor taking a boost::filesystem2::path or
0055     // boost::filesystem2::wpath
0056     template<typename Path>
0057     explicit path(const Path& p, typename Path::external_string_type* = 0)
0058     {
0059         init(p.external_file_string());
0060     }
0061 
0062     // Constructor taking a boost::filesystem3::path (boost filesystem v3)
0063     template<typename Path>
0064     explicit path(const Path& p, typename Path::codecvt_type* = 0)
0065     {
0066         init(p.native());
0067     }
0068 
0069     // Copy constructor
0070     path(const path& p) 
0071         : narrow_(p.narrow_), wide_(p.wide_), is_wide_(p.is_wide_) 
0072         { }
0073 
0074     // Assignment operator taking another path
0075     path& operator=(const path& p)
0076     {
0077         narrow_ = p.narrow_;
0078         wide_ = p.wide_;
0079         is_wide_ = p.is_wide_;
0080         return *this;
0081     }
0082 
0083     // Assignment operator taking a std::string
0084     path& operator=(const std::string& p)
0085     {
0086         narrow_ = p;
0087         wide_.clear();
0088         is_wide_ = false;
0089         return *this;
0090     }
0091 
0092     // Assignment operator taking a C-style string
0093     path& operator=(const char* p)
0094     {
0095         narrow_.assign(p);
0096         wide_.clear();
0097         is_wide_ = false;
0098         return *this;
0099     }
0100 
0101 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1400)
0102     // Assignment operator taking a boost::filesystem2::path or
0103     // boost::filesystem2::wpath
0104     // (not on Visual C++ 7.1/8.0, as it seems to have problems with
0105     // SFINAE functions with the same parameters, doesn't seem
0106     // worth working around).
0107     template<typename Path>
0108     typename sfinae<typename Path::external_string_type, path&>::type
0109         operator=(const Path& p)
0110     {
0111         init(p.external_file_string());
0112         return *this;
0113     }
0114 #endif
0115 
0116     // Assignment operator taking a boost::filesystem3::path
0117     template<typename Path>
0118     typename sfinae<typename Path::codecvt_type, path&>::type
0119         operator=(const Path& p)
0120     {
0121         init(p.native());
0122         return *this;
0123     }
0124 
0125     bool is_wide() const { return is_wide_; }
0126 
0127     // Returns a representation of the underlying path as a std::string
0128     // Requires: is_wide() returns false
0129     const char* c_str() const { return narrow_.c_str(); }
0130 
0131     // Returns a representation of the underlying path as a std::wstring
0132     // Requires: is_wide() returns true
0133     const wchar_t* c_wstr() const { return wide_.c_str(); }
0134 private:
0135     
0136     // For wide-character paths, use a boost::filesystem::wpath instead of a
0137     // std::wstring
0138     path(const std::wstring&);
0139     path& operator=(const std::wstring&);
0140 
0141     void init(std::string const& file_path)
0142     {
0143         narrow_ = file_path;
0144         wide_.clear();
0145         is_wide_ = false;
0146     }
0147 
0148     void init(std::wstring const& file_path)
0149     {
0150         narrow_.clear();
0151         wide_ = file_path;
0152         is_wide_ = true;
0153     }
0154 
0155     std::string   narrow_;
0156     std::wstring  wide_;
0157     bool          is_wide_;
0158 };
0159 
0160 inline bool operator==(const path& lhs, const path& rhs)
0161 {
0162     return lhs.is_wide() ?
0163         rhs.is_wide() && std::wcscmp(lhs.c_wstr(), rhs.c_wstr()) == 0 :
0164         !rhs.is_wide() && std::strcmp(lhs.c_str(), rhs.c_str()) == 0;
0165 }
0166 
0167 #else // #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //---------------------------//
0168 
0169 class path {
0170 public:
0171     path() { }
0172     path(const std::string& p) : path_(p) { }
0173     path(const char* p) : path_(p) { }
0174     template<typename Path>
0175         path(const Path& p) : path_(p.external_file_string()) { }
0176     path(const path& p) : path_(p.path_) { }
0177     path& operator=(const path& other) 
0178     {
0179         path_ = other.path_;
0180         return *this;
0181     }
0182     path& operator=(const std::string& p) 
0183     {
0184         path_ = p;
0185         return *this;
0186     }
0187     path& operator=(const char* p) 
0188     {
0189         path_ = p;
0190         return *this;
0191     }
0192     template<typename Path>
0193         path& operator=(const Path& p)
0194         {
0195             path_ = p.external_file_string();
0196             return *this;
0197         }
0198     bool is_wide() const { return false; }
0199     const char* c_str() const { return path_.c_str(); }
0200     const wchar_t* c_wstr() const { return 0; }
0201 private:
0202     std::string path_;
0203 };
0204 
0205 inline bool operator==(const path& lhs, const path& rhs)
0206 {
0207     return std::strcmp(lhs.c_str(), rhs.c_str()) == 0 ;
0208 }
0209 
0210 #endif // #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS //--------------------------//
0211 
0212 } } } // End namespaces detail, iostreams, boost.
0213 
0214 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_PATH_HPP_INCLUDED