8 #ifndef BOOST_NOWIDE_FILEBUF_HPP 9 #define BOOST_NOWIDE_FILEBUF_HPP 12 #include <boost/config.hpp> 13 #include <boost/nowide/stackstring.hpp> 19 # pragma warning(push) 20 # pragma warning(disable : 4996 4244 4800) 26 #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_FSTREAM_TESTS) && !defined(BOOST_NOWIDE_DOXYGEN) 27 using std::basic_filebuf;
37 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
57 mode_(std::ios::in | std::ios::out)
78 return open(s.c_str(),mode);
90 bool ate = bool(mode & std::ios_base::ate);
92 mode = mode ^ std::ios_base::ate;
93 wchar_t const *smode = get_mode(mode);
99 #ifdef BOOST_NOWIDE_FSTREAM_TESTS 100 FILE *f =
::fopen(s,boost::nowide::convert(smode).c_str());
102 FILE *f = ::_wfopen(name.c_str(),smode);
106 if(ate && fseek(f,0,SEEK_END)!=0) {
118 bool res = sync() == 0;
120 if(::fclose(file_)!=0)
124 return res ?
this : 0;
139 if(buffer_size_ > 0) {
140 buffer_ =
new char [buffer_size_];
146 virtual std::streambuf *setbuf(
char *s,std::streamsize n)
148 if(!buffer_ && n>=0) {
156 #ifdef BOOST_NOWIDE_DEBUG_FILEBUF 158 void print_buf(
char *b,
char *p,
char *e)
160 std::cerr <<
"-- Is Null: " << (b==0) << std::endl;;
164 std::cerr <<
"-- Total: " << e - b <<
" offset from start " << p - b << std::endl;
166 std::cerr <<
"-- Total: " << p - b << std::endl;
169 for(
char *ptr = b;ptr<p;ptr++)
173 for(
char *ptr = p;ptr<e;ptr++)
176 std::cerr <<
"]" << std::endl;
182 std::cerr <<
"- Output:" << std::endl;
183 print_buf(pbase(),pptr(),0);
184 std::cerr <<
"- Input:" << std::endl;
185 print_buf(eback(),gptr(),egptr());
186 std::cerr <<
"- fpos: " << (file_ ? ftell(file_) : -1L) << std::endl;
195 std::cerr <<
"In: " << f << std::endl;
200 std::cerr <<
"Out: " << f << std::endl;
211 #ifdef BOOST_NOWIDE_DEBUG_FILEBUF 212 print_guard g(
this,__FUNCTION__);
220 size_t n = pptr() - pbase();
222 if(::fwrite(pbase(),1,n,file_) < n)
227 if(buffer_size_ > 0) {
229 setp(buffer_,buffer_+buffer_size_);
234 if(::fputc(c,file_)==EOF)
244 return overflow(EOF);
249 #ifdef BOOST_NOWIDE_DEBUG_FILEBUF 250 print_guard g(
this,__FUNCTION__);
256 if(buffer_size_ == 0) {
257 int c = ::fgetc(file_);
262 setg(&last_char_,&last_char_,&last_char_ + 1);
266 size_t n = ::fread(buffer_,1,buffer_size_,file_);
267 setg(buffer_,buffer_,buffer_+n);
270 return std::char_traits<char>::to_int_type(*gptr());
275 return pubseekoff(-1,std::ios::cur);
278 std::streampos seekoff(std::streamoff off,
279 std::ios_base::seekdir seekdir,
280 std::ios_base::openmode )
282 #ifdef BOOST_NOWIDE_DEBUG_FILEBUF 283 print_guard g(
this,__FUNCTION__);
287 if(fixp() < 0 || fixg() < 0)
289 if(seekdir == std::ios_base::cur) {
290 if( ::fseek(file_,off,SEEK_CUR) < 0)
293 else if(seekdir == std::ios_base::beg) {
294 if( ::fseek(file_,off,SEEK_SET) < 0)
297 else if(seekdir == std::ios_base::end) {
298 if( ::fseek(file_,off,SEEK_END) < 0)
305 std::streampos seekpos(std::streampos off,std::ios_base::openmode m)
307 return seekoff(std::streamoff(off),std::ios_base::beg,m);
312 if(gptr()!=egptr()) {
313 std::streamsize off = gptr() - egptr();
315 if(fseek(file_,off,SEEK_CUR) != 0)
332 void reset(FILE *f = 0)
343 static wchar_t const *get_mode(std::ios_base::openmode mode)
351 if(mode == (std::ios_base::out))
353 if(mode == (std::ios_base::out | std::ios_base::app))
355 if(mode == (std::ios_base::app))
357 if(mode == (std::ios_base::out | std::ios_base::trunc))
359 if(mode == (std::ios_base::in))
361 if(mode == (std::ios_base::in | std::ios_base::out))
363 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
365 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
367 if(mode == (std::ios_base::in | std::ios_base::app))
369 if(mode == (std::ios_base::binary | std::ios_base::out))
371 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
373 if(mode == (std::ios_base::binary | std::ios_base::app))
375 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
377 if(mode == (std::ios_base::binary | std::ios_base::in))
379 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
381 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
383 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
385 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
395 std::ios::openmode mode_;
409 # pragma warning(pop)
basic_filebuf * close()
Definition: filebuf.hpp:116
FILE * fopen(char const *file_name, char const *mode)
Same as fopen but file_name and mode are UTF-8 strings.
Definition: cstdio.hpp:53
basic_filebuf< char > filebuf
Convinience typedef.
Definition: filebuf.hpp:401
bool is_open() const
Definition: filebuf.hpp:129
basic_filebuf * open(char const *s, std::ios_base::openmode mode)
Definition: filebuf.hpp:83
basic_filebuf()
Definition: filebuf.hpp:52
This forward declaration defined the basic_filebuf type.
Definition: filebuf.hpp:38
This is implementation of std::filebuf.
Definition: filebuf.hpp:47
basic_filebuf * open(std::string const &s, std::ios_base::openmode mode)
Definition: filebuf.hpp:76
A class that allows to create a temporary wide or narrow UTF strings from wide or narrow UTF source...
Definition: stackstring.hpp:25