Boost.Nowide
filebuf.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 // Copyright (c) 2019-2020 Alexander Grund
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 #ifndef BOOST_NOWIDE_FILEBUF_HPP_INCLUDED
10 #define BOOST_NOWIDE_FILEBUF_HPP_INCLUDED
11 
12 #include <boost/nowide/config.hpp>
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>
17 #include <cassert>
18 #include <cstdio>
19 #include <ios>
20 #include <limits>
21 #include <locale>
22 #include <stdexcept>
23 #include <streambuf>
24 #else
25 #include <fstream>
26 #endif
27 
28 namespace boost {
29 namespace nowide {
30  namespace detail {
32  BOOST_NOWIDE_DECL std::streampos ftell(FILE* file);
34  BOOST_NOWIDE_DECL int fseek(FILE* file, std::streamoff offset, int origin);
35  } // namespace detail
36 
37 #if !BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT && !defined(BOOST_NOWIDE_DOXYGEN)
38  using std::basic_filebuf;
39  using std::filebuf;
40 #else // Windows
41  template<typename CharType, typename Traits = std::char_traits<CharType>>
50 
58  template<>
59  class basic_filebuf<char> : public std::basic_streambuf<char>
60  {
61  using Traits = std::char_traits<char>;
62 
63  public:
64 #ifdef BOOST_MSVC
65 #pragma warning(push)
66 #pragma warning(disable : 4351) // new behavior : elements of array will be default initialized
67 #endif
68  basic_filebuf() :
72  file_(nullptr), buffer_(nullptr), buffer_size_(BUFSIZ), owns_buffer_(false), unbuffered_read_(false),
73  last_char_(), mode_(std::ios_base::openmode(0))
74  {
75  setg(nullptr, nullptr, nullptr);
76  setp(nullptr, nullptr);
77  }
78 #ifdef BOOST_MSVC
79 #pragma warning(pop)
80 #endif
81  basic_filebuf(const basic_filebuf&) = delete;
82  basic_filebuf& operator=(const basic_filebuf&) = delete;
83  basic_filebuf(basic_filebuf&& other) noexcept : basic_filebuf()
84  {
85  swap(other);
86  }
87  basic_filebuf& operator=(basic_filebuf&& other) noexcept
88  {
89  close();
90  swap(other);
91  return *this;
92  }
93  void swap(basic_filebuf& rhs)
94  {
95  std::basic_streambuf<char>::swap(rhs);
96  using std::swap;
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_);
104 
105  // Fixup last_char references
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);
110 
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_)
114  {
115  rhs.setg(rhs.last_char_,
116  (rhs.gptr() == last_char_) ? rhs.last_char_ : rhs.last_char_ + 1,
117  rhs.last_char_ + 1);
118  }
119  }
120 
121  virtual ~basic_filebuf()
122  {
123  close();
124  }
125 
129  basic_filebuf* open(const std::string& s, std::ios_base::openmode mode)
130  {
131  return open(s.c_str(), mode);
132  }
136  basic_filebuf* open(const char* s, std::ios_base::openmode mode)
137  {
138  const wstackstring name(s);
139  return open(name.get(), mode);
140  }
142  basic_filebuf* open(const wchar_t* s, std::ios_base::openmode mode)
143  {
144  if(is_open())
145  return nullptr;
146  validate_cvt(this->getloc());
147  const bool ate = (mode & std::ios_base::ate) != 0;
148  if(ate)
149  mode &= ~std::ios_base::ate;
150  const wchar_t* smode = get_mode(mode);
151  if(!smode)
152  return nullptr;
153  file_ = detail::wfopen(s, smode);
154  if(!file_)
155  return nullptr;
156  if(ate && detail::fseek(file_, 0, SEEK_END) != 0)
157  {
158  close();
159  return nullptr;
160  }
161  mode_ = mode;
162  set_unbuffered_read();
163  return this;
164  }
165  template<typename Path>
166  detail::enable_if_path_t<Path, basic_filebuf*> open(const Path& file_name, std::ios_base::openmode mode)
167  {
168  return open(file_name.c_str(), mode);
169  }
174  {
175  if(!is_open())
176  return nullptr;
177  bool res = sync() == 0;
178  if(std::fclose(file_) != 0)
179  res = false;
180  file_ = nullptr;
181  mode_ = std::ios_base::openmode(0);
182  if(owns_buffer_)
183  {
184  delete[] buffer_;
185  buffer_ = nullptr;
186  owns_buffer_ = false;
187  }
188  setg(nullptr, nullptr, nullptr);
189  setp(nullptr, nullptr);
190  return res ? this : nullptr;
191  }
195  bool is_open() const
196  {
197  return file_ != nullptr;
198  }
199 
200  protected:
201  std::streambuf* setbuf(char* s, std::streamsize n) override
202  {
203  assert(n >= 0);
204  // Maximum compatibility: Discard all local buffers and use user-provided values
205  // Users should call sync() before or better use it before any IO is done or any file is opened
206  setg(nullptr, nullptr, nullptr);
207  setp(nullptr, nullptr);
208  if(owns_buffer_)
209  {
210  delete[] buffer_;
211  owns_buffer_ = false;
212  }
213  buffer_ = s;
214  buffer_size_ = (n >= 0) ? static_cast<size_t>(n) : 0;
215  set_unbuffered_read();
216  return this;
217  }
218 
219  int sync() override
220  {
221  if(!file_)
222  return 0;
223  bool result;
224  if(pptr())
225  {
226  // Only flush if anything was written, otherwise behavior of fflush is undefined. I.e.:
227  // - Buffered mode: pptr was set to buffer_ and advanced
228  // - Unbuffered mode: pptr set to last_char_
229  const bool has_prev_write = pptr() != buffer_;
230  result = overflow() != EOF;
231  if(has_prev_write && std::fflush(file_) != 0)
232  result = false;
233  } else
234  result = stop_reading();
235  return result ? 0 : -1;
236  }
237 
238  int overflow(int c = EOF) override
239  {
240  if(!(mode_ & (std::ios_base::out | std::ios_base::app)))
241  return EOF;
242 
243  if(!stop_reading())
244  return EOF;
245 
246  size_t n = pptr() - pbase();
247  if(n > 0)
248  {
249  if(std::fwrite(pbase(), 1, n, file_) != n)
250  return EOF;
251  assert(buffer_);
252  setp(buffer_, buffer_ + buffer_size_);
253  if(c != EOF)
254  {
255  *buffer_ = Traits::to_char_type(c);
256  pbump(1);
257  }
258  } else if(c != EOF)
259  {
260  if(buffer_size_ > 0)
261  {
262  make_buffer();
263  setp(buffer_, buffer_ + buffer_size_);
264  *buffer_ = Traits::to_char_type(c);
265  pbump(1);
266  } else if(std::fputc(c, file_) == EOF)
267  {
268  return EOF;
269  } else if(!pptr())
270  {
271  // Set to dummy value so we know we have written something
272  setp(last_char_, last_char_);
273  }
274  }
275  return Traits::not_eof(c);
276  }
277 
278  std::streamsize xsputn(const char* s, std::streamsize n) override
279  {
280  // Only optimize when writing more than a buffer worth of data
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())
284  return 0;
285 
286  assert(n >= 0);
287  // First empty the remaining put area, if any
288  const char* const base = pbase();
289  const size_t num_buffered = pptr() - base;
290  if(num_buffered != 0)
291  {
292  const auto num_written = std::fwrite(base, 1, num_buffered, file_);
293  setp(const_cast<char*>(base + num_written), epptr()); // i.e. pbump(num_written)
294  if(num_written != num_buffered)
295  return 0; // Error writing buffered chars
296  }
297  // Then write directly to file
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_); // Mark as "written" if not done yet
301  return num_written;
302  }
303 
304  int underflow() override
305  {
306  if(!(mode_ & std::ios_base::in) || !stop_writing())
307  return EOF;
308  if(unbuffered_read_)
309  {
310  const int c = std::fgetc(file_);
311  if(c == EOF)
312  return EOF;
313  last_char_[0] = Traits::to_char_type(c);
314  setg(last_char_, last_char_, last_char_ + 1);
315  } else
316  {
317  make_buffer();
318  const size_t n = std::fread(buffer_, 1, buffer_size_, file_);
319  setg(buffer_, buffer_, buffer_ + n);
320  if(n == 0)
321  return EOF;
322  }
323  return Traits::to_int_type(*gptr());
324  }
325 
326  std::streamsize xsgetn(char* s, std::streamsize n) override
327  {
328  // Only optimize when reading more than a buffer worth of data
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())
332  return 0;
333  assert(n >= 0);
334  std::streamsize num_copied = 0;
335  // First empty the remaining get area, if any
336  const auto num_buffered = egptr() - gptr();
337  if(num_buffered != 0)
338  {
339  const auto num_read = num_buffered > n ? n : num_buffered;
340  traits_type::copy(s, gptr(), static_cast<size_t>(num_read));
341  s += num_read;
342  n -= num_read;
343  num_copied = num_read;
344  setg(eback(), gptr() + num_read, egptr()); // i.e. gbump(num_read)
345  }
346  // Then read directly from file (loop as number of bytes read may be less than requested)
347  while(n > 0)
348  {
349  const auto num_read = std::fread(s, 1, static_cast<size_t>(n), file_);
350  if(num_read == 0) // EOF or error
351  break;
352  s += num_read;
353  n -= num_read;
354  num_copied += num_read;
355  }
356  return num_copied;
357  }
358 
359  int pbackfail(int c = EOF) override
360  {
361  // For simplicity we only allow putting back into our read buffer
362  // So putting back more chars than we have read from the buffer will fail
363  if(gptr() > eback())
364  gbump(-1);
365  else
366  return EOF;
367 
368  // Assign the new value if requested
369  if(c != EOF && *gptr() != Traits::to_char_type(c))
370  *gptr() = Traits::to_char_type(c);
371  return Traits::not_eof(c);
372  }
373 
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
377  {
378  if(!file_)
379  return EOF;
380  // Switching between input<->output requires a seek
381  // So do NOT optimize for seekoff(0, cur) as No-OP
382 
383  // On some implementations a seek also flushes, so do a full sync
384  if(sync() != 0)
385  return EOF;
386  int whence;
387  switch(seekdir)
388  {
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;
393  }
394  if(detail::fseek(file_, off, whence) != 0)
395  return EOF;
396  return detail::ftell(file_);
397  }
398  std::streampos seekpos(std::streampos pos,
399  std::ios_base::openmode m = std::ios_base::in | std::ios_base::out) override
400  {
401  // Standard mandates "as-if fsetpos", but assume the effect is the same as fseek
402  return seekoff(pos, std::ios_base::beg, m);
403  }
404  void imbue(const std::locale& loc) override
405  {
406  validate_cvt(loc);
407  }
408 
409  private:
410  void make_buffer()
411  {
412  if(buffer_)
413  return;
414  if(buffer_size_ > 0)
415  {
416  buffer_ = new char[buffer_size_];
417  owns_buffer_ = true;
418  }
419  }
420 
421  void set_unbuffered_read()
422  {
423  // In text mode we cannot use buffering as we are required to know the (file) position of each
424  // char in the get area and to seek back in case of a sync to "put back" unread chars.
425  // However std::fseek with non-zero offsets is unsupported for text files and the (file) offset
426  // to seek back is unknown anyway due to newlines which may got converted.
427  unbuffered_read_ = !(mode_ & std::ios_base::binary) || buffer_size_ == 0u;
428  }
429 
430  void validate_cvt(const std::locale& loc)
431  {
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");
434  }
435 
438  bool stop_reading()
439  {
440  if(!gptr())
441  return true;
442  const auto off = gptr() - egptr();
443  setg(nullptr, nullptr, nullptr);
444  if(!off)
445  return true;
446 #if defined(__clang__)
447 #pragma clang diagnostic push
448 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
449 #endif
450  // coverity[result_independent_of_operands]
451  if(off < std::numeric_limits<std::streamoff>::min())
452  return false;
453 #if defined(__clang__)
454 #pragma clang diagnostic pop
455 #endif
456  return detail::fseek(file_, static_cast<std::streamoff>(off), SEEK_CUR) == 0;
457  }
458 
461  bool stop_writing()
462  {
463  if(pptr())
464  {
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)
469  return false;
470  }
471  return true;
472  }
473 
474  static const wchar_t* get_mode(std::ios_base::openmode mode)
475  {
476  //
477  // done according to n2914 table 106 27.9.1.4
478  //
479 
480  // note can't use switch case as overload operator can't be used
481  // in constant expression
482  if(mode == (std::ios_base::out))
483  return L"w";
484  if(mode == (std::ios_base::out | std::ios_base::app))
485  return L"a";
486  if(mode == (std::ios_base::app))
487  return L"a";
488  if(mode == (std::ios_base::out | std::ios_base::trunc))
489  return L"w";
490  if(mode == (std::ios_base::in))
491  return L"r";
492  if(mode == (std::ios_base::in | std::ios_base::out))
493  return L"r+";
494  if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
495  return L"w+";
496  if(mode == (std::ios_base::in | std::ios_base::out | std::ios_base::app))
497  return L"a+";
498  if(mode == (std::ios_base::in | std::ios_base::app))
499  return L"a+";
500  if(mode == (std::ios_base::binary | std::ios_base::out))
501  return L"wb";
502  if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::app))
503  return L"ab";
504  if(mode == (std::ios_base::binary | std::ios_base::app))
505  return L"ab";
506  if(mode == (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc))
507  return L"wb";
508  if(mode == (std::ios_base::binary | std::ios_base::in))
509  return L"rb";
510  if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out))
511  return L"r+b";
512  if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc))
513  return L"w+b";
514  if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app))
515  return L"a+b";
516  if(mode == (std::ios_base::binary | std::ios_base::in | std::ios_base::app))
517  return L"a+b";
518  return nullptr;
519  }
520 
521  FILE* file_;
522  char* buffer_;
523  size_t buffer_size_;
524  bool owns_buffer_;
525  bool unbuffered_read_; // True to read char by char
526  char last_char_[1];
527  std::ios::openmode mode_;
528  };
529 
534 
536  template<typename CharType, typename Traits>
538  {
539  lhs.swap(rhs);
540  }
541 
542 #endif // windows
543 
544 } // namespace nowide
545 } // namespace boost
546 
547 #endif
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