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             buffer_size_ = (n >= 0) ? static_cast<size_t>(n) : 0;
   222         int overflow(
int c = EOF)
 override   224             if(!(mode_ & (std::ios_base::out | std::ios_base::app)))
   230             size_t n = pptr() - pbase();
   233                 if(std::fwrite(pbase(), 1, n, file_) != n)
   235                 setp(buffer_, buffer_ + buffer_size_);
   238                     *buffer_ = Traits::to_char_type(c);
   246                     setp(buffer_, buffer_ + buffer_size_);
   247                     *buffer_ = Traits::to_char_type(c);
   249                 } 
else if(std::fputc(c, file_) == EOF)
   255                     setp(last_char_, last_char_);
   258             return Traits::not_eof(c);
   268                 result = overflow() != EOF;
   270                 if(std::fflush(file_) != 0)
   271                     return result = 
false;
   273                 result = stop_reading();
   274             return result ? 0 : -1;
   277         int underflow()
 override   279             if(!(mode_ & std::ios_base::in))
   287             if(buffer_size_ == 0 || !(mode_ & std::ios_base::binary))
   289                 const int c = std::fgetc(file_);
   292                 last_char_[0] = Traits::to_char_type(c);
   293                 setg(last_char_, last_char_, last_char_ + 1);
   297                 const size_t n = std::fread(buffer_, 1, buffer_size_, file_);
   298                 setg(buffer_, buffer_, buffer_ + n);
   302             return Traits::to_int_type(*gptr());
   305         int pbackfail(
int c = EOF)
 override   307             if(!(mode_ & std::ios_base::in))
   313             else if(seekoff(-1, std::ios_base::cur) != std::streampos(std::streamoff(-1)))
   315                 if(underflow() == EOF)
   322                 return Traits::not_eof(c);
   326                 *gptr() = Traits::to_char_type(c);
   327             return Traits::not_eof(c);
   330         std::streampos seekoff(std::streamoff off,
   331                                std::ios_base::seekdir seekdir,
   332                                std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
 override   345             case std::ios_base::beg: whence = SEEK_SET; 
break;
   346             case std::ios_base::cur: whence = SEEK_CUR; 
break;
   347             case std::ios_base::end: whence = SEEK_END; 
break;
   348             default: assert(
false); 
return EOF;
   350             if(detail::fseek(file_, off, whence) != 0)
   352             return detail::ftell(file_);
   354         std::streampos seekpos(std::streampos pos,
   355                                std::ios_base::openmode m = std::ios_base::in | std::ios_base::out)
 override   358             return seekoff(pos, std::ios_base::beg, m);
   360         void imbue(
const std::locale& loc)
 override   372             const auto off = gptr() - egptr();
   376 #if defined(__clang__)   377 #pragma clang diagnostic push   378 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"   381             if(off > std::numeric_limits<std::streamoff>::max())
   383 #if defined(__clang__)   384 #pragma clang diagnostic pop   386             return detail::fseek(file_, static_cast<std::streamoff>(off), SEEK_CUR) == 0;
   395                 const char* 
const base = pbase();
   396                 const size_t n = pptr() - base;
   398                 if(n && std::fwrite(base, 1, n, file_) != n)
   404         void reset(FILE* f = 0)
   415         static const wchar_t* get_mode(std::ios_base::openmode mode)
   423             if(mode == (std::ios_base::out))
   425             if(mode == (std::ios_base::out | std::ios_base::app))
   427             if(mode == (std::ios_base::app))
   429             if(mode == (std::ios_base::out | std::ios_base::trunc))
   431             if(mode == (std::ios_base::in))
   433             if(mode == (std::ios_base::in | std::ios_base::out))
   435             if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
   437             if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
   439             if(mode == (std::ios_base::in | std::ios_base::app))
   441             if(mode == (std::ios_base::binary | std::ios_base::out))
   443             if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
   445             if(mode == (std::ios_base::binary | std::ios_base::app))
   447             if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
   449             if(mode == (std::ios_base::binary | std::ios_base::in))
   451             if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
   453             if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
   455             if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
   457             if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
   467         std::ios::openmode mode_;
   476     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:477
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