filebuf.hpp 19 KB

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