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/detail/is_path.hpp> 16 #include <boost/nowide/stackstring.hpp> 32 BOOST_NOWIDE_DECL std::streampos ftell(FILE* file);
34 BOOST_NOWIDE_DECL
int fseek(FILE* file, std::streamoff offset,
int origin);
37 #if !BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT && !defined(BOOST_NOWIDE_DOXYGEN) 38 using std::basic_filebuf;
41 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
61 using Traits = std::char_traits<char>;
66 #pragma warning(disable : 4351) // new behavior : elements of array will be default initialized 72 file_(
nullptr), buffer_(
nullptr), buffer_size_(BUFSIZ), owns_buffer_(
false), unbuffered_read_(
false),
73 last_char_(), mode_(std::ios_base::openmode(0))
75 setg(
nullptr,
nullptr,
nullptr);
76 setp(
nullptr,
nullptr);
95 std::basic_streambuf<char>::swap(rhs);
97 swap(file_, rhs.file_);
98 swap(buffer_, rhs.buffer_);
99 swap(buffer_size_, rhs.buffer_size_);
100 swap(owns_buffer_, rhs.owns_buffer_);
101 swap(unbuffered_read_, rhs.unbuffered_read_);
102 swap(last_char_[0], rhs.last_char_[0]);
103 swap(mode_, rhs.mode_);
106 if(pbase() == rhs.last_char_)
107 setp(last_char_, (pptr() == epptr()) ? last_char_ : last_char_ + 1);
108 if(eback() == rhs.last_char_)
109 setg(last_char_, (gptr() == rhs.last_char_) ? last_char_ : last_char_ + 1, last_char_ + 1);
111 if(rhs.pbase() == last_char_)
112 rhs.setp(rhs.last_char_, (rhs.pptr() == rhs.epptr()) ? rhs.last_char_ : rhs.last_char_ + 1);
113 if(rhs.eback() == last_char_)
115 rhs.setg(rhs.last_char_,
116 (rhs.gptr() == last_char_) ? rhs.last_char_ : rhs.last_char_ + 1,
131 return open(s.c_str(), mode);
139 return open(name.
get(), mode);
146 validate_cvt(this->getloc());
147 const bool ate = (mode & std::ios_base::ate) != 0;
149 mode &= ~std::ios_base::ate;
150 const wchar_t* smode = get_mode(mode);
153 file_ = detail::wfopen(s, smode);
156 if(ate && detail::fseek(file_, 0, SEEK_END) != 0)
162 set_unbuffered_read();
165 template<
typename Path>
166 detail::enable_if_path_t<Path, basic_filebuf*> open(
const Path& file_name, std::ios_base::openmode mode)
168 return open(file_name.c_str(), mode);
177 bool res = sync() == 0;
178 if(std::fclose(file_) != 0)
181 mode_ = std::ios_base::openmode(0);
186 owns_buffer_ =
false;
188 setg(
nullptr,
nullptr,
nullptr);
189 setp(
nullptr,
nullptr);
190 return res ? this :
nullptr;
197 return file_ !=
nullptr;
201 std::streambuf* setbuf(
char* s, std::streamsize n)
override 206 setg(
nullptr,
nullptr,
nullptr);
207 setp(
nullptr,
nullptr);
211 owns_buffer_ =
false;
214 buffer_size_ = (n >= 0) ? static_cast<size_t>(n) : 0;
215 set_unbuffered_read();
229 const bool has_prev_write = pptr() != buffer_;
230 result = overflow() != EOF;
231 if(has_prev_write && std::fflush(file_) != 0)
234 result = stop_reading();
235 return result ? 0 : -1;
238 int overflow(
int c = EOF)
override 240 if(!(mode_ & (std::ios_base::out | std::ios_base::app)))
246 size_t n = pptr() - pbase();
249 if(std::fwrite(pbase(), 1, n, file_) != n)
252 setp(buffer_, buffer_ + buffer_size_);
255 *buffer_ = Traits::to_char_type(c);
263 setp(buffer_, buffer_ + buffer_size_);
264 *buffer_ = Traits::to_char_type(c);
266 }
else if(std::fputc(c, file_) == EOF)
272 setp(last_char_, last_char_);
275 return Traits::not_eof(c);
278 std::streamsize xsputn(
const char* s, std::streamsize n)
override 281 if(n <= static_cast<std::streamsize>(buffer_size_))
282 return std::basic_streambuf<char>::xsputn(s, n);
283 if(!(mode_ & (std::ios_base::out | std::ios_base::app)) || !stop_reading())
288 const char*
const base = pbase();
289 const size_t num_buffered = pptr() - base;
290 if(num_buffered != 0)
292 const auto num_written = std::fwrite(base, 1, num_buffered, file_);
293 setp(const_cast<char*>(base + num_written), epptr());
294 if(num_written != num_buffered)
298 const auto num_written = std::fwrite(s, 1, static_cast<size_t>(n), file_);
299 if(num_written > 0u && base != last_char_)
300 setp(last_char_, last_char_);
304 int underflow()
override 306 if(!(mode_ & std::ios_base::in) || !stop_writing())
310 const int c = std::fgetc(file_);
313 last_char_[0] = Traits::to_char_type(c);
314 setg(last_char_, last_char_, last_char_ + 1);
318 const size_t n = std::fread(buffer_, 1, buffer_size_, file_);
319 setg(buffer_, buffer_, buffer_ + n);
323 return Traits::to_int_type(*gptr());
326 std::streamsize xsgetn(
char* s, std::streamsize n)
override 329 if(n <= static_cast<std::streamsize>(unbuffered_read_ ? 1u : buffer_size_))
330 return std::basic_streambuf<char>::xsgetn(s, n);
331 if(!(mode_ & std::ios_base::in) || !stop_writing())
334 std::streamsize num_copied = 0;
336 const auto num_buffered = egptr() - gptr();
337 if(num_buffered != 0)
339 const auto num_read = num_buffered > n ? n : num_buffered;
340 traits_type::copy(s, gptr(), static_cast<size_t>(num_read));
343 num_copied = num_read;
344 setg(eback(), gptr() + num_read, egptr());
349 const auto num_read = std::fread(s, 1, static_cast<size_t>(n), file_);
354 num_copied += num_read;
359 int pbackfail(
int c = EOF)
override 369 if(c != EOF && *gptr() != Traits::to_char_type(c))
370 *gptr() = Traits::to_char_type(c);
371 return Traits::not_eof(c);
374 std::streampos seekoff(std::streamoff off,
375 std::ios_base::seekdir seekdir,
376 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
override 389 case std::ios_base::beg: whence = SEEK_SET;
break;
390 case std::ios_base::cur: whence = SEEK_CUR;
break;
391 case std::ios_base::end: whence = SEEK_END;
break;
392 default: assert(
false);
return EOF;
394 if(detail::fseek(file_, off, whence) != 0)
396 return detail::ftell(file_);
398 std::streampos seekpos(std::streampos pos,
399 std::ios_base::openmode m = std::ios_base::in | std::ios_base::out)
override 402 return seekoff(pos, std::ios_base::beg, m);
404 void imbue(
const std::locale& loc)
override 416 buffer_ =
new char[buffer_size_];
421 void set_unbuffered_read()
427 unbuffered_read_ = !(mode_ & std::ios_base::binary) || buffer_size_ == 0u;
430 void validate_cvt(
const std::locale& loc)
432 if(!std::use_facet<std::codecvt<char, char, std::mbstate_t>>(loc).always_noconv())
433 throw std::runtime_error(
"Converting codecvts are not supported");
442 const auto off = gptr() - egptr();
443 setg(
nullptr,
nullptr,
nullptr);
446 #if defined(__clang__) 447 #pragma clang diagnostic push 448 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" 451 if(off < std::numeric_limits<std::streamoff>::min())
453 #if defined(__clang__) 454 #pragma clang diagnostic pop 456 return detail::fseek(file_, static_cast<std::streamoff>(off), SEEK_CUR) == 0;
465 const char*
const base = pbase();
466 const size_t n = pptr() - base;
467 setp(
nullptr,
nullptr);
468 if(n && std::fwrite(base, 1, n, file_) != n)
474 static const wchar_t* get_mode(std::ios_base::openmode mode)
482 if(mode == (std::ios_base::out))
484 if(mode == (std::ios_base::out | std::ios_base::app))
486 if(mode == (std::ios_base::app))
488 if(mode == (std::ios_base::out | std::ios_base::trunc))
490 if(mode == (std::ios_base::in))
492 if(mode == (std::ios_base::in | std::ios_base::out))
494 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
496 if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
498 if(mode == (std::ios_base::in | std::ios_base::app))
500 if(mode == (std::ios_base::binary | std::ios_base::out))
502 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
504 if(mode == (std::ios_base::binary | std::ios_base::app))
506 if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
508 if(mode == (std::ios_base::binary | std::ios_base::in))
510 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
512 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
514 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
516 if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
525 bool unbuffered_read_;
527 std::ios::openmode mode_;
536 template<
typename CharType,
typename Traits>
basic_filebuf * close()
Definition: filebuf.hpp:173
bool is_open() const
Definition: filebuf.hpp:195
This forward declaration defines the basic_filebuf type which is used when BOOST_NOWIDE_USE_FILEBUF_R...
Definition: filebuf.hpp:49
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:142
basic_filebuf * open(const char *s, std::ios_base::openmode mode)
Definition: filebuf.hpp:136
This is the implementation of std::filebuf which is used when BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT is...
Definition: filebuf.hpp:59
void swap(basic_filebuf< CharType, Traits > &lhs, basic_filebuf< CharType, Traits > &rhs)
Swap the basic_filebuf instances.
Definition: filebuf.hpp:537
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:129
output_char * get()
Return the converted, NULL-terminated string or NULL if no string was converted.
Definition: stackstring.hpp:127