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