1 #ifndef BOOSTER_NOWIDE_FSTREAM_H 2 #define BOOSTER_NOWIDE_FSTREAM_H 4 #include <booster/config.h> 5 #include <booster/nowide/convert.h> 7 #include <booster/auto_ptr_inc.h> 9 #if defined BOOSTER_WIN_NATIVE || defined BOOSTER_WORKAROUND_BROKEN_GCC_ON_DARWIN 10 #include <booster/streambuf.h> 11 #include <booster/nowide/cstdio.h> 21 #if !defined BOOSTER_WIN_NATIVE && !defined(BOOSTER_WORKAROUND_BROKEN_GCC_ON_DARWIN) 23 using std::basic_ifstream;
24 using std::basic_ofstream;
25 using std::basic_fstream;
26 using std::basic_filebuf;
33 #if defined(BOOSTER_WIN_NATIVE) || defined(BOOSTER_DOXYGEN_DOCS) || defined(BOOSTER_WORKAROUND_BROKEN_GCC_ON_DARWIN) 35 #if defined BOOSTER_MSVC 37 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
38 class basic_filebuf :
public std::basic_filebuf<CharType,Traits> {
40 typedef std::basic_filebuf<CharType,Traits> my_base_type;
41 basic_filebuf *open(
char const *s,std::ios_base::openmode mode)
44 if(my_base_type::open(
convert(s).c_str(),mode)) {
49 catch(bad_utf
const &e) {
61 stdio_iodev(stdio_iodev
const &);
62 void operator=(stdio_iodev
const &);
64 stdio_iodev(FILE *f) :
69 size_t read(
char *p,
size_t n)
71 return fread(p,1,n,file_);
73 size_t write(
char const *p,
size_t n)
75 size_t res = fwrite(p,1,n,file_);
83 fseek(file_,pos,SEEK_CUR);
86 fseek(file_,pos,SEEK_SET);
89 fseek(file_,pos,SEEK_END);
107 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
124 basic_filebuf *open(
char const *s,std::ios_base::openmode mode)
127 bool seek_end = mode & std::ios_base::ate;
129 mode ^= std::ios_base::ate;
130 wchar_t const *smode = get_mode(mode);
140 FILE *f = _wfopen(name.c_str(),smode);
144 if(fseek(f,0,SEEK_END)!=0) {
149 std::auto_ptr<io_device> dev(
new details::stdio_iodev(f));
160 return res ?
this : 0;
167 static wchar_t const *get_mode(std::ios_base::openmode mode)
175 if(mode == (std::ios_base::out))
177 if(mode == (std::ios_base::out | std::ios_base::app))
179 if(mode == (std::ios_base::app))
181 if(mode == (std::ios_base::out | std::ios_base::trunc))
183 if(mode == (std::ios_base::in))
185 if(mode == (std::ios_base::in | std::ios_base::out))
187 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
189 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
191 if(mode == (std::ios_base::in | std::ios_base::app))
193 if(mode == (std::ios_base::binary | std::ios_base::out))
195 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
197 if(mode == (std::ios_base::binary | std::ios_base::app))
199 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
201 if(mode == (std::ios_base::binary | std::ios_base::in))
203 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
205 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
207 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
209 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
222 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
227 typedef std::basic_istream<CharType,Traits> internal_stream_type;
230 internal_stream_type(
new internal_buffer_type())
232 buf_.reset(static_cast<internal_buffer_type *>(internal_stream_type::rdbuf()));
234 explicit basic_ifstream(
char const *file_name,std::ios_base::openmode mode = std::ios_base::in) :
235 internal_stream_type(
new internal_buffer_type())
237 buf_.reset(static_cast<internal_buffer_type *>(internal_stream_type::rdbuf()));
238 open(file_name,mode);
240 void open(
char const *file_name,std::ios_base::openmode mode = std::ios_base::in)
242 if(!buf_->open(file_name,mode | std::ios_base::in)) {
243 this->setstate(std::ios_base::failbit);
251 return buf_->is_open();
255 return buf_->is_open();
260 this->setstate(std::ios_base::failbit);
265 internal_buffer_type *rdbuf()
const 275 std::auto_ptr<internal_buffer_type> buf_;
282 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
287 typedef std::basic_ostream<CharType,Traits> internal_stream_type;
290 internal_stream_type(
new internal_buffer_type())
292 buf_.reset(static_cast<internal_buffer_type *>(internal_stream_type::rdbuf()));
294 explicit basic_ofstream(
char const *file_name,std::ios_base::openmode mode = std::ios_base::out) :
295 internal_stream_type(
new internal_buffer_type())
297 buf_.reset(static_cast<internal_buffer_type *>(internal_stream_type::rdbuf()));
298 open(file_name,mode);
300 void open(
char const *file_name,std::ios_base::openmode mode = std::ios_base::out)
302 if(!buf_->open(file_name,mode | std::ios_base::out)) {
303 this->setstate(std::ios_base::failbit);
311 return buf_->is_open();
315 return buf_->is_open();
320 this->setstate(std::ios_base::failbit);
325 internal_buffer_type *rdbuf()
const 335 std::auto_ptr<internal_buffer_type> buf_;
342 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
347 typedef std::basic_iostream<CharType,Traits> internal_stream_type;
350 internal_stream_type(
new internal_buffer_type())
352 buf_.reset(static_cast<internal_buffer_type *>(internal_stream_type::rdbuf()));
354 explicit basic_fstream(
char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::in) :
355 internal_stream_type(
new internal_buffer_type())
357 buf_.reset(static_cast<internal_buffer_type *>(internal_stream_type::rdbuf()));
358 open(file_name,mode);
360 void open(
char const *file_name,std::ios_base::openmode mode = std::ios_base::out | std::ios_base::out)
362 if(!buf_->open(file_name,mode)) {
363 this->setstate(std::ios_base::failbit);
371 return buf_->is_open();
375 return buf_->is_open();
380 this->setstate(std::ios_base::failbit);
385 internal_buffer_type *rdbuf()
const 395 std::auto_ptr<internal_buffer_type> buf_;
Definition: fstream.h:283
this is an implementation of generic streambuffer
Definition: streambuf.h:70
This class is a base class of generic I/O device that can be used in very simple manner with booster:...
Definition: streambuf.h:19
BOOSTER_API std::string convert(wchar_t const *s)
Definition: fstream.h:343
pos_type
Definition: streambuf.h:24
basic_filebuf< char > filebuf
Definition: fstream.h:402
basic_ofstream< char > ofstream
Definition: fstream.h:410
Definition: fstream.h:108
Definition: fstream.h:115
Definition: fstream.h:223
basic_ifstream< char > ifstream
Definition: fstream.h:406
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
basic_fstream< char > fstream
Definition: fstream.h:414
This exception is thrown if invalid UTF-8 or UTF-16 is given as input.
Definition: convert.h:14