Boost.Nowide
fstream.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_NOWIDE_FSTREAM_INCLUDED_HPP
9 #define BOOST_NOWIDE_FSTREAM_INCLUDED_HPP
10 
11 #include <iosfwd>
12 #include <boost/config.hpp>
13 #include <boost/nowide/convert.hpp>
14 #include <boost/scoped_ptr.hpp>
15 #include <fstream>
16 #include <memory>
17 #include <boost/nowide/filebuf.hpp>
18 
19 namespace boost {
25 namespace nowide {
26 #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_FSTREAM_TESTS) && !defined(BOOST_NOWIDE_DOXYGEN)
27 
28  using std::basic_ifstream;
29  using std::basic_ofstream;
30  using std::basic_fstream;
31  using std::ifstream;
32  using std::ofstream;
33  using std::fstream;
34 
35 #else
36  template<typename CharType,typename Traits = std::char_traits<CharType> >
40  class basic_ifstream : public std::basic_istream<CharType,Traits>
41  {
42  public:
44  typedef std::basic_istream<CharType,Traits> internal_stream_type;
45 
46  basic_ifstream() :
47  internal_stream_type(0)
48  {
49  buf_.reset(new internal_buffer_type());
50  std::ios::rdbuf(buf_.get());
51  }
52 
53  explicit basic_ifstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::in) :
54  internal_stream_type(0)
55  {
56  buf_.reset(new internal_buffer_type());
57  std::ios::rdbuf(buf_.get());
58  open(file_name,mode);
59  }
60 
61  explicit basic_ifstream(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::in) :
62  internal_stream_type(0)
63  {
64  buf_.reset(new internal_buffer_type());
65  std::ios::rdbuf(buf_.get());
66  open(file_name,mode);
67  }
68 
69 
70  void open(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::in)
71  {
72  open(file_name.c_str(),mode);
73  }
74  void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::in)
75  {
76  if(!buf_->open(file_name,mode | std::ios_base::in)) {
77  this->setstate(std::ios_base::failbit);
78  }
79  else {
80  this->clear();
81  }
82  }
83  bool is_open()
84  {
85  return buf_->is_open();
86  }
87  bool is_open() const
88  {
89  return buf_->is_open();
90  }
91  void close()
92  {
93  if(!buf_->close())
94  this->setstate(std::ios_base::failbit);
95  else
96  this->clear();
97  }
98 
99  internal_buffer_type *rdbuf() const
100  {
101  return buf_.get();
102  }
103  ~basic_ifstream()
104  {
105  buf_->close();
106  }
107 
108  private:
109  boost::scoped_ptr<internal_buffer_type> buf_;
110  };
111 
115 
116  template<typename CharType,typename Traits = std::char_traits<CharType> >
117  class basic_ofstream : public std::basic_ostream<CharType,Traits>
118  {
119  public:
121  typedef std::basic_ostream<CharType,Traits> internal_stream_type;
122 
123  basic_ofstream() :
124  internal_stream_type(0)
125  {
126  buf_.reset(new internal_buffer_type());
127  std::ios::rdbuf(buf_.get());
128  }
129  explicit basic_ofstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::out) :
130  internal_stream_type(0)
131  {
132  buf_.reset(new internal_buffer_type());
133  std::ios::rdbuf(buf_.get());
134  open(file_name,mode);
135  }
136  explicit basic_ofstream(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out) :
137  internal_stream_type(0)
138  {
139  buf_.reset(new internal_buffer_type());
140  std::ios::rdbuf(buf_.get());
141  open(file_name,mode);
142  }
143  void open(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out)
144  {
145  open(file_name.c_str(),mode);
146  }
147  void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out)
148  {
149  if(!buf_->open(file_name,mode | std::ios_base::out)) {
150  this->setstate(std::ios_base::failbit);
151  }
152  else {
153  this->clear();
154  }
155  }
156  bool is_open()
157  {
158  return buf_->is_open();
159  }
160  bool is_open() const
161  {
162  return buf_->is_open();
163  }
164  void close()
165  {
166  if(!buf_->close())
167  this->setstate(std::ios_base::failbit);
168  else
169  this->clear();
170  }
171 
172  internal_buffer_type *rdbuf() const
173  {
174  return buf_.get();
175  }
176  ~basic_ofstream()
177  {
178  buf_->close();
179  }
180 
181  private:
182  boost::scoped_ptr<internal_buffer_type> buf_;
183  };
184 
188 
189  template<typename CharType,typename Traits = std::char_traits<CharType> >
190  class basic_fstream : public std::basic_iostream<CharType,Traits>
191  {
192  public:
194  typedef std::basic_iostream<CharType,Traits> internal_stream_type;
195 
196  basic_fstream() :
197  internal_stream_type(0)
198  {
199  buf_.reset(new internal_buffer_type());
200  std::ios::rdbuf(buf_.get());
201  }
202  explicit basic_fstream(char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::in) :
203  internal_stream_type(0)
204  {
205  buf_.reset(new internal_buffer_type());
206  std::ios::rdbuf(buf_.get());
207  open(file_name,mode);
208  }
209  explicit basic_fstream(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::in) :
210  internal_stream_type(0)
211  {
212  buf_.reset(new internal_buffer_type());
213  std::ios::rdbuf(buf_.get());
214  open(file_name,mode);
215  }
216  void open(std::string const &file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::out)
217  {
218  open(file_name.c_str(),mode);
219  }
220  void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::out)
221  {
222  if(!buf_->open(file_name,mode)) {
223  this->setstate(std::ios_base::failbit);
224  }
225  else {
226  this->clear();
227  }
228  }
229  bool is_open()
230  {
231  return buf_->is_open();
232  }
233  bool is_open() const
234  {
235  return buf_->is_open();
236  }
237  void close()
238  {
239  if(!buf_->close())
240  this->setstate(std::ios_base::failbit);
241  else
242  this->clear();
243  }
244 
245  internal_buffer_type *rdbuf() const
246  {
247  return buf_.get();
248  }
249  ~basic_fstream()
250  {
251  buf_->close();
252  }
253 
254  private:
255  boost::scoped_ptr<internal_buffer_type> buf_;
256  };
257 
258 
275 
276 #endif
277 } // nowide
278 } // namespace boost
279 
280 
281 
282 #endif
283 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Definition: args.hpp:18
basic_ifstream< char > ifstream
Definition: fstream.hpp:266
basic_filebuf< char > filebuf
Convinience typedef.
Definition: filebuf.hpp:401
Same as std::basic_ifstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:40
basic_ofstream< char > ofstream
Definition: fstream.hpp:270
This forward declaration defined the basic_filebuf type.
Definition: filebuf.hpp:38
Same as std::basic_fstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:190
This is implementation of std::filebuf.
Definition: filebuf.hpp:47
Same as std::basic_ofstream<char> but accepts UTF-8 strings under Windows.
Definition: fstream.hpp:117
basic_fstream< char > fstream
Definition: fstream.hpp:274