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> 31 BOOST_NOWIDE_DECL std::streampos ftell(FILE* file);
33 BOOST_NOWIDE_DECL
int fseek(FILE* file, std::streamoff offset,
int origin);
36 #if !BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT && !defined(BOOST_NOWIDE_DOXYGEN) 37 using std::basic_filebuf;
40 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
58 using Traits = std::char_traits<char>;
63 #pragma warning(disable : 4351) // new behavior : elements of array will be default initialized 69 buffer_size_(BUFSIZ), buffer_(0), file_(0), owns_buffer_(
false), last_char_(),
70 mode_(std::ios_base::openmode(0))
91 std::basic_streambuf<char>::swap(rhs);
93 swap(buffer_size_, rhs.buffer_size_);
94 swap(buffer_, rhs.buffer_);
95 swap(file_, rhs.file_);
96 swap(owns_buffer_, rhs.owns_buffer_);
97 swap(last_char_[0], rhs.last_char_[0]);
98 swap(mode_, rhs.mode_);
100 if(epptr() == rhs.last_char_)
101 setp(last_char_, last_char_);
102 if(egptr() == rhs.last_char_)
103 rhs.setg(last_char_, gptr() == rhs.last_char_ ? last_char_ : last_char_ + 1, last_char_ + 1);
104 if(rhs.epptr() == last_char_)
105 setp(rhs.last_char_, rhs.last_char_);
106 if(rhs.egptr() == rhs.last_char_)
108 rhs.setg(rhs.last_char_,
109 rhs.gptr() == last_char_ ? rhs.last_char_ : rhs.last_char_ + 1,
124 return open(s.c_str(), mode);
132 return open(name.
get(), mode);
139 validate_cvt(this->getloc());
140 const bool ate = (mode & std::ios_base::ate) != 0;
142 mode &= ~std::ios_base::ate;
143 const wchar_t* smode = get_mode(mode);
146 file_ = detail::wfopen(s, smode);
149 if(ate && detail::fseek(file_, 0, SEEK_END) != 0)
164 bool res = sync() == 0;
165 if(std::fclose(file_) != 0)
168 mode_ = std::ios_base::openmode(0);
173 owns_buffer_ =
false;
175 return res ? this : NULL;
182 return file_ != NULL;
192 buffer_ =
new char[buffer_size_];
196 void validate_cvt(
const std::locale& loc)
198 if(!std::use_facet<std::codecvt<char, char, std::mbstate_t>>(loc).always_noconv())
199 throw std::runtime_error(
"Converting codecvts are not supported");
203 std::streambuf* setbuf(
char* s, std::streamsize n)
override 208 setg(NULL, NULL, NULL);
213 buffer_size_ = (n >= 0) ? static_cast<size_t>(n) : 0;
217 int overflow(
int c = EOF)
override 219 if(!(mode_ & std::ios_base::out))
225 size_t n = pptr() - pbase();
228 if(std::fwrite(pbase(), 1, n, file_) != n)
230 setp(buffer_, buffer_ + buffer_size_);
233 *buffer_ = Traits::to_char_type(c);
241 setp(buffer_, buffer_ + buffer_size_);
242 *buffer_ = Traits::to_char_type(c);
244 }
else if(std::fputc(c, file_) == EOF)
250 setp(last_char_, last_char_);
253 return Traits::not_eof(c);
263 result = overflow() != EOF;
265 if(std::fflush(file_) != 0)
266 return result =
false;
268 result = stop_reading();
269 return result ? 0 : -1;
272 int underflow()
override 274 if(!(mode_ & std::ios_base::in))
278 if(buffer_size_ == 0)
280 const int c = std::fgetc(file_);
283 last_char_[0] = Traits::to_char_type(c);
284 setg(last_char_, last_char_, last_char_ + 1);
288 const size_t n = std::fread(buffer_, 1, buffer_size_, file_);
289 setg(buffer_, buffer_, buffer_ + n);
293 return Traits::to_int_type(*gptr());
296 int pbackfail(
int c = EOF)
override 298 if(!(mode_ & std::ios_base::in))
304 else if(seekoff(-1, std::ios_base::cur) != std::streampos(std::streamoff(-1)))
306 if(underflow() == EOF)
313 return Traits::not_eof(c);
317 *gptr() = Traits::to_char_type(c);
318 return Traits::not_eof(c);
321 std::streampos seekoff(std::streamoff off,
322 std::ios_base::seekdir seekdir,
323 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
override 336 case std::ios_base::beg: whence = SEEK_SET;
break;
337 case std::ios_base::cur: whence = SEEK_CUR;
break;
338 case std::ios_base::end: whence = SEEK_END;
break;
339 default: assert(
false);
return EOF;
341 if(detail::fseek(file_, off, whence) != 0)
343 return detail::ftell(file_);
345 std::streampos seekpos(std::streampos pos,
346 std::ios_base::openmode m = std::ios_base::in | std::ios_base::out)
override 349 return seekoff(pos, std::ios_base::beg, m);
351 void imbue(
const std::locale& loc)
override 363 const auto off = gptr() - egptr();
367 #if defined(__clang__) 368 #pragma clang diagnostic push 369 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" 372 if(off > std::numeric_limits<std::streamoff>::max())
374 #if defined(__clang__) 375 #pragma clang diagnostic pop 377 return detail::fseek(file_, static_cast<std::streamoff>(off), SEEK_CUR) == 0;
386 const char*
const base = pbase();
387 const size_t n = pptr() - base;
389 if(n && std::fwrite(base, 1, n, file_) != n)
395 void reset(FILE* f = 0)
406 static const wchar_t* get_mode(std::ios_base::openmode mode)
414 if(mode == (std::ios_base::out))
416 if(mode == (std::ios_base::out | std::ios_base::app))
418 if(mode == (std::ios_base::app))
420 if(mode == (std::ios_base::out | std::ios_base::trunc))
422 if(mode == (std::ios_base::in))
424 if(mode == (std::ios_base::in | std::ios_base::out))
426 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
428 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
430 if(mode == (std::ios_base::in | std::ios_base::app))
432 if(mode == (std::ios_base::binary | std::ios_base::out))
434 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
436 if(mode == (std::ios_base::binary | std::ios_base::app))
438 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
440 if(mode == (std::ios_base::binary | std::ios_base::in))
442 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
444 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
446 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
448 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
458 std::ios::openmode mode_;
basic_filebuf * close()
Definition: filebuf.hpp:160
bool is_open() const
Definition: filebuf.hpp:180
This forward declaration defines the basic_filebuf type.
Definition: filebuf.hpp:47
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:135
basic_filebuf * open(const char *s, std::ios_base::openmode mode)
Definition: filebuf.hpp:129
This is the implementation of std::filebuf.
Definition: filebuf.hpp:56
A class that allows to create a temporary wide or narrow UTF strings from wide or narrow UTF source.
Definition: stackstring.hpp:32
basic_filebuf * open(const std::string &s, std::ios_base::openmode mode)
Definition: filebuf.hpp:122
output_char * get()
Return the converted, NULL-terminated string or NULL if no string was converted.
Definition: stackstring.hpp:127