Boost.Nowide
|
00001 // 00002 // Copyright (c) 2012 Artyom Beilis (Tonkikh) 00003 // 00004 // Distributed under the Boost Software License, Version 1.0. (See 00005 // accompanying file LICENSE_1_0.txt or copy at 00006 // http://www.boost.org/LICENSE_1_0.txt) 00007 // 00008 #ifndef BOOST_NOWIDE_FSTREAM_INCLUDED_HPP 00009 #define BOOST_NOWIDE_FSTREAM_INCLUDED_HPP 00010 00011 #include <iosfwd> 00012 #include <boost/config.hpp> 00013 #include <boost/nowide/convert.hpp> 00014 #include <boost/scoped_ptr.hpp> 00015 #include <fstream> 00016 #include <memory> 00017 #include <boost/nowide/filebuf.hpp> 00018 00019 namespace boost { 00025 namespace nowide { 00026 #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_FSTREAM_TESTS) && !defined(BOOST_NOWIDE_DOXYGEN) 00027 00028 using std::basic_ifstream; 00029 using std::basic_ofstream; 00030 using std::basic_fstream; 00031 using std::ifstream; 00032 using std::ofstream; 00033 using std::fstream; 00034 00035 #else 00036 00037 00038 00039 template<typename CharType,typename Traits = std::char_traits<CharType> > 00040 class basic_ifstream : public std::basic_istream<CharType,Traits> 00041 { 00042 public: 00043 typedef basic_filebuf<CharType,Traits> internal_buffer_type; 00044 typedef std::basic_istream<CharType,Traits> internal_stream_type; 00045 00046 basic_ifstream() : 00047 internal_stream_type(0) 00048 { 00049 buf_.reset(new internal_buffer_type()); 00050 std::ios::rdbuf(buf_.get()); 00051 } 00052 00053 explicit basic_ifstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::in) : 00054 internal_stream_type(0) 00055 { 00056 buf_.reset(new internal_buffer_type()); 00057 std::ios::rdbuf(buf_.get()); 00058 open(file_name,mode); 00059 } 00060 00061 void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::in) 00062 { 00063 if(!buf_->open(file_name,mode | std::ios_base::in)) { 00064 this->setstate(std::ios_base::failbit); 00065 } 00066 else { 00067 this->clear(); 00068 } 00069 } 00070 bool is_open() 00071 { 00072 return buf_->is_open(); 00073 } 00074 bool is_open() const 00075 { 00076 return buf_->is_open(); 00077 } 00078 void close() 00079 { 00080 if(!buf_->close()) 00081 this->setstate(std::ios_base::failbit); 00082 else 00083 this->clear(); 00084 } 00085 00086 internal_buffer_type *rdbuf() const 00087 { 00088 return buf_.get(); 00089 } 00090 ~basic_ifstream() 00091 { 00092 buf_->close(); 00093 } 00094 00095 private: 00096 boost::scoped_ptr<internal_buffer_type> buf_; 00097 }; 00098 00102 00103 template<typename CharType,typename Traits = std::char_traits<CharType> > 00104 class basic_ofstream : public std::basic_ostream<CharType,Traits> 00105 { 00106 public: 00107 typedef basic_filebuf<CharType,Traits> internal_buffer_type; 00108 typedef std::basic_ostream<CharType,Traits> internal_stream_type; 00109 00110 basic_ofstream() : 00111 internal_stream_type(0) 00112 { 00113 buf_.reset(new internal_buffer_type()); 00114 std::ios::rdbuf(buf_.get()); 00115 } 00116 explicit basic_ofstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::out) : 00117 internal_stream_type(0) 00118 { 00119 buf_.reset(new internal_buffer_type()); 00120 std::ios::rdbuf(buf_.get()); 00121 open(file_name,mode); 00122 } 00123 void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out) 00124 { 00125 if(!buf_->open(file_name,mode | std::ios_base::out)) { 00126 this->setstate(std::ios_base::failbit); 00127 } 00128 else { 00129 this->clear(); 00130 } 00131 } 00132 bool is_open() 00133 { 00134 return buf_->is_open(); 00135 } 00136 bool is_open() const 00137 { 00138 return buf_->is_open(); 00139 } 00140 void close() 00141 { 00142 if(!buf_->close()) 00143 this->setstate(std::ios_base::failbit); 00144 else 00145 this->clear(); 00146 } 00147 00148 internal_buffer_type *rdbuf() const 00149 { 00150 return buf_.get(); 00151 } 00152 ~basic_ofstream() 00153 { 00154 buf_->close(); 00155 } 00156 00157 private: 00158 boost::scoped_ptr<internal_buffer_type> buf_; 00159 }; 00160 00164 00165 template<typename CharType,typename Traits = std::char_traits<CharType> > 00166 class basic_fstream : public std::basic_iostream<CharType,Traits> 00167 { 00168 public: 00169 typedef basic_filebuf<CharType,Traits> internal_buffer_type; 00170 typedef std::basic_iostream<CharType,Traits> internal_stream_type; 00171 00172 basic_fstream() : 00173 internal_stream_type(0) 00174 { 00175 buf_.reset(new internal_buffer_type()); 00176 std::ios::rdbuf(buf_.get()); 00177 } 00178 explicit basic_fstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::in) : 00179 internal_stream_type(0) 00180 { 00181 buf_.reset(new internal_buffer_type()); 00182 std::ios::rdbuf(buf_.get()); 00183 open(file_name,mode); 00184 } 00185 void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::out) 00186 { 00187 if(!buf_->open(file_name,mode)) { 00188 this->setstate(std::ios_base::failbit); 00189 } 00190 else { 00191 this->clear(); 00192 } 00193 } 00194 bool is_open() 00195 { 00196 return buf_->is_open(); 00197 } 00198 bool is_open() const 00199 { 00200 return buf_->is_open(); 00201 } 00202 void close() 00203 { 00204 if(!buf_->close()) 00205 this->setstate(std::ios_base::failbit); 00206 else 00207 this->clear(); 00208 } 00209 00210 internal_buffer_type *rdbuf() const 00211 { 00212 return buf_.get(); 00213 } 00214 ~basic_fstream() 00215 { 00216 buf_->close(); 00217 } 00218 00219 private: 00220 boost::scoped_ptr<internal_buffer_type> buf_; 00221 }; 00222 00223 00227 typedef basic_filebuf<char> filebuf; 00231 typedef basic_ifstream<char> ifstream; 00235 typedef basic_ofstream<char> ofstream; 00239 typedef basic_fstream<char> fstream; 00240 00241 #endif 00242 } // nowide 00243 } // namespace boost 00244 00245 00246 00247 #endif 00248 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4