9 #ifndef BOOST_NOWIDE_FILEBUF_HPP_INCLUDED 10 #define BOOST_NOWIDE_FILEBUF_HPP_INCLUDED 13 #if BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT 14 #include <boost/nowide/cstdio.hpp> 15 #include <boost/nowide/stackstring.hpp> 29 #if !BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT && !defined(BOOST_NOWIDE_DOXYGEN) 30 using std::basic_filebuf;
33 template<
typename CharType,
typename Traits = std::
char_traits<CharType> >
51 typedef std::char_traits<char> Traits;
56 #pragma warning(disable : 4351) // new behavior : elements of array will be default initialized 62 buffer_size_(BUFSIZ), buffer_(0), file_(0), owns_buffer_(
false), last_char_(),
63 mode_(std::ios_base::openmode(0))
71 #if !BOOST_NOWIDE_CXX11 92 std::basic_streambuf<char>::swap(rhs);
94 swap(buffer_size_, rhs.buffer_size_);
95 swap(buffer_, rhs.buffer_);
96 swap(file_, rhs.file_);
97 swap(owns_buffer_, rhs.owns_buffer_);
98 swap(last_char_[0], rhs.last_char_[0]);
99 swap(mode_, rhs.mode_);
101 if(epptr() == rhs.last_char_)
102 setp(last_char_, last_char_);
103 if(egptr() == rhs.last_char_)
104 rhs.setg(last_char_, gptr() == rhs.last_char_ ? last_char_ : last_char_ + 1, last_char_ + 1);
105 if(rhs.epptr() == last_char_)
106 setp(rhs.last_char_, rhs.last_char_);
107 if(rhs.egptr() == rhs.last_char_)
109 rhs.setg(rhs.last_char_,
110 rhs.gptr() == last_char_ ? rhs.last_char_ : rhs.last_char_ + 1,
125 return open(s.c_str(), mode);
133 return open(name.
get(), mode);
140 validate_cvt(this->getloc());
141 const bool ate = (mode & std::ios_base::ate) != 0;
143 mode &= ~std::ios_base::ate;
144 const wchar_t* smode = get_mode(mode);
147 file_ = detail::wfopen(s, smode);
150 if(ate && std::fseek(file_, 0, SEEK_END) != 0)
165 bool res = sync() == 0;
166 if(std::fclose(file_) != 0)
169 mode_ = std::ios_base::openmode(0);
174 owns_buffer_ =
false;
176 return res ? this : NULL;
183 return file_ != NULL;
193 buffer_ =
new char[buffer_size_];
197 void validate_cvt(
const std::locale& loc)
199 if(!std::use_facet<std::codecvt<char, char, std::mbstate_t> >(loc).always_noconv())
200 throw std::runtime_error(
"Converting codecvts are not supported");
204 virtual std::streambuf* setbuf(
char* s, std::streamsize n)
209 setg(NULL, NULL, NULL);
214 buffer_size_ = (n >= 0) ? static_cast<size_t>(n) : 0;
218 virtual int overflow(
int c = EOF)
220 if(!(mode_ & std::ios_base::out))
226 size_t n = pptr() - pbase();
229 if(std::fwrite(pbase(), 1, n, file_) != n)
231 setp(buffer_, buffer_ + buffer_size_);
234 *buffer_ = Traits::to_char_type(c);
242 setp(buffer_, buffer_ + buffer_size_);
243 *buffer_ = Traits::to_char_type(c);
245 }
else if(std::fputc(c, file_) == EOF)
251 setp(last_char_, last_char_);
254 return Traits::not_eof(c);
264 result = overflow() != EOF;
266 if(std::fflush(file_) != 0)
267 return result =
false;
269 result = stop_reading();
270 return result ? 0 : -1;
273 virtual int underflow()
275 if(!(mode_ & std::ios_base::in))
279 if(buffer_size_ == 0)
281 const int c = std::fgetc(file_);
284 last_char_[0] = Traits::to_char_type(c);
285 setg(last_char_, last_char_, last_char_ + 1);
289 const size_t n = std::fread(buffer_, 1, buffer_size_, file_);
290 setg(buffer_, buffer_, buffer_ + n);
294 return Traits::to_int_type(*gptr());
297 virtual int pbackfail(
int c = EOF)
299 if(!(mode_ & std::ios_base::in))
305 else if(seekoff(-1, std::ios_base::cur) != std::streampos(std::streamoff(-1)))
307 if(underflow() == EOF)
314 return Traits::not_eof(c);
318 *gptr() = Traits::to_char_type(c);
319 return Traits::not_eof(c);
322 virtual std::streampos seekoff(std::streamoff off,
323 std::ios_base::seekdir seekdir,
324 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
337 case std::ios_base::beg: whence = SEEK_SET;
break;
338 case std::ios_base::cur: whence = SEEK_CUR;
break;
339 case std::ios_base::end: whence = SEEK_END;
break;
340 default: assert(
false);
return EOF;
342 assert(off <= std::numeric_limits<long>::max());
343 if(std::fseek(file_, static_cast<long>(off), whence) != 0)
345 return std::ftell(file_);
347 virtual std::streampos seekpos(std::streampos pos,
348 std::ios_base::openmode m = std::ios_base::in | std::ios_base::out)
351 return seekoff(pos, std::ios_base::beg, m);
353 virtual void imbue(
const std::locale& loc)
365 const std::streamsize off = gptr() - egptr();
367 assert(off <= std::numeric_limits<long>::max());
368 if(off && std::fseek(file_, static_cast<long>(off), SEEK_CUR) != 0)
380 const char*
const base = pbase();
381 const size_t n = pptr() - base;
383 if(n && std::fwrite(base, 1, n, file_) != n)
389 void reset(FILE* f = 0)
400 static const wchar_t* get_mode(std::ios_base::openmode mode)
408 if(mode == (std::ios_base::out))
410 if(mode == (std::ios_base::out | std::ios_base::app))
412 if(mode == (std::ios_base::app))
414 if(mode == (std::ios_base::out | std::ios_base::trunc))
416 if(mode == (std::ios_base::in))
418 if(mode == (std::ios_base::in | std::ios_base::out))
420 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
422 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
424 if(mode == (std::ios_base::in | std::ios_base::app))
426 if(mode == (std::ios_base::binary | std::ios_base::out))
428 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
430 if(mode == (std::ios_base::binary | std::ios_base::app))
432 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
434 if(mode == (std::ios_base::binary | std::ios_base::in))
436 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
438 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
440 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
442 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
452 std::ios::openmode mode_;
basic_filebuf * close()
Definition: filebuf.hpp:161
bool is_open() const
Definition: filebuf.hpp:181
basic_filebuf< char > filebuf
Convenience typedef.
Definition: filebuf.hpp:458
This forward declaration defines the basic_filebuf type.
Definition: filebuf.hpp:40
basic_filebuf * open(const wchar_t *s, std::ios_base::openmode mode)
Opens the file with the given name, see std::filebuf::open.
Definition: filebuf.hpp:136
basic_filebuf * open(const char *s, std::ios_base::openmode mode)
Definition: filebuf.hpp:130
This is the implementation of std::filebuf.
Definition: filebuf.hpp:49
A class that allows to create a temporary wide or narrow UTF strings from wide or narrow UTF source.
Definition: stackstring.hpp:31
basic_filebuf * open(const std::string &s, std::ios_base::openmode mode)
Definition: filebuf.hpp:123
output_char * get()
Return the converted, NULL-terminated string or NULL if no string was converted.
Definition: stackstring.hpp:126