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))
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_);
102 if(pbase() == rhs.last_char_)
103 setp(last_char_, (pptr() == epptr()) ? last_char_ : last_char_ + 1);
104 if(eback() == rhs.last_char_)
105 setg(last_char_, (gptr() == rhs.last_char_) ? last_char_ : last_char_ + 1, last_char_ + 1);
107 if(rhs.pbase() == last_char_)
108 rhs.setp(rhs.last_char_, (rhs.pptr() == rhs.epptr()) ? rhs.last_char_ : rhs.last_char_ + 1);
109 if(rhs.eback() == last_char_)
111 rhs.setg(rhs.last_char_,
112 (rhs.gptr() == last_char_) ? rhs.last_char_ : rhs.last_char_ + 1,
127 return open(s.c_str(), mode);
135 return open(name.
get(), mode);
142 validate_cvt(this->getloc());
143 const bool ate = (mode & std::ios_base::ate) != 0;
145 mode &= ~std::ios_base::ate;
146 const wchar_t* smode = get_mode(mode);
149 file_ = detail::wfopen(s, smode);
152 if(ate && detail::fseek(file_, 0, SEEK_END) != 0)
167 bool res = sync() == 0;
168 if(std::fclose(file_) != 0)
171 mode_ = std::ios_base::openmode(0);
176 owns_buffer_ =
false;
180 return res ? this : NULL;
187 return file_ != NULL;
197 buffer_ =
new char[buffer_size_];
201 void validate_cvt(
const std::locale& loc)
203 if(!std::use_facet<std::codecvt<char, char, std::mbstate_t>>(loc).always_noconv())
204 throw std::runtime_error(
"Converting codecvts are not supported");
208 std::streambuf* setbuf(
char* s, std::streamsize n)
override 213 setg(NULL, NULL, NULL);
218 owns_buffer_ =
false;
221 buffer_size_ = (n >= 0) ? static_cast<size_t>(n) : 0;
225 int overflow(
int c = EOF)
override 227 if(!(mode_ & (std::ios_base::out | std::ios_base::app)))
233 size_t n = pptr() - pbase();
236 if(std::fwrite(pbase(), 1, n, file_) != n)
238 setp(buffer_, buffer_ + buffer_size_);
241 *buffer_ = Traits::to_char_type(c);
249 setp(buffer_, buffer_ + buffer_size_);
250 *buffer_ = Traits::to_char_type(c);
252 }
else if(std::fputc(c, file_) == EOF)
258 setp(last_char_, last_char_);
261 return Traits::not_eof(c);
271 result = overflow() != EOF;
273 if(std::fflush(file_) != 0)
274 return result =
false;
276 result = stop_reading();
277 return result ? 0 : -1;
280 int underflow()
override 282 if(!(mode_ & std::ios_base::in))
290 if(buffer_size_ == 0 || !(mode_ & std::ios_base::binary))
292 const int c = std::fgetc(file_);
295 last_char_[0] = Traits::to_char_type(c);
296 setg(last_char_, last_char_, last_char_ + 1);
300 const size_t n = std::fread(buffer_, 1, buffer_size_, file_);
301 setg(buffer_, buffer_, buffer_ + n);
305 return Traits::to_int_type(*gptr());
308 int pbackfail(
int c = EOF)
override 310 if(!(mode_ & std::ios_base::in))
316 else if(seekoff(-1, std::ios_base::cur) != std::streampos(std::streamoff(-1)))
318 if(underflow() == EOF)
325 return Traits::not_eof(c);
329 *gptr() = Traits::to_char_type(c);
330 return Traits::not_eof(c);
333 std::streampos seekoff(std::streamoff off,
334 std::ios_base::seekdir seekdir,
335 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
override 348 case std::ios_base::beg: whence = SEEK_SET;
break;
349 case std::ios_base::cur: whence = SEEK_CUR;
break;
350 case std::ios_base::end: whence = SEEK_END;
break;
351 default: assert(
false);
return EOF;
353 if(detail::fseek(file_, off, whence) != 0)
355 return detail::ftell(file_);
357 std::streampos seekpos(std::streampos pos,
358 std::ios_base::openmode m = std::ios_base::in | std::ios_base::out)
override 361 return seekoff(pos, std::ios_base::beg, m);
363 void imbue(
const std::locale& loc)
override 375 const auto off = gptr() - egptr();
379 #if defined(__clang__) 380 #pragma clang diagnostic push 381 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" 384 if(off > std::numeric_limits<std::streamoff>::max())
386 #if defined(__clang__) 387 #pragma clang diagnostic pop 389 return detail::fseek(file_, static_cast<std::streamoff>(off), SEEK_CUR) == 0;
398 const char*
const base = pbase();
399 const size_t n = pptr() - base;
401 if(n && std::fwrite(base, 1, n, file_) != n)
407 void reset(FILE* f = 0)
418 static const wchar_t* get_mode(std::ios_base::openmode mode)
426 if(mode == (std::ios_base::out))
428 if(mode == (std::ios_base::out | std::ios_base::app))
430 if(mode == (std::ios_base::app))
432 if(mode == (std::ios_base::out | std::ios_base::trunc))
434 if(mode == (std::ios_base::in))
436 if(mode == (std::ios_base::in | std::ios_base::out))
438 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
440 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
442 if(mode == (std::ios_base::in | std::ios_base::app))
444 if(mode == (std::ios_base::binary | std::ios_base::out))
446 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
448 if(mode == (std::ios_base::binary | std::ios_base::app))
450 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
452 if(mode == (std::ios_base::binary | std::ios_base::in))
454 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
456 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
458 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
460 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
470 std::ios::openmode mode_;
479 template<
typename CharType,
typename Traits>
basic_filebuf * close()
Definition: filebuf.hpp:163
bool is_open() const
Definition: filebuf.hpp:185
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:138
basic_filebuf * open(const char *s, std::ios_base::openmode mode)
Definition: filebuf.hpp:132
This is the implementation of std::filebuf.
Definition: filebuf.hpp:56
void swap(basic_filebuf< CharType, Traits > &lhs, basic_filebuf< CharType, Traits > &rhs)
Swap the basic_filebuf instances.
Definition: filebuf.hpp:480
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:125
output_char * get()
Return the converted, NULL-terminated string or NULL if no string was converted.
Definition: stackstring.hpp:127