From 86ecce2072d740679f8cd292028a5b1bae4bdeba Mon Sep 17 00:00:00 2001 From: KWSys Upstream Date: Tue, 2 Feb 2021 09:34:46 -0500 Subject: KWSys 2021-02-02 (c672435e) Code extracted from: https://gitlab.kitware.com/utils/kwsys.git at commit c672435eba0d5b024117cd4f30d8d2ed57f7f338 (master). Upstream Shortlog ----------------- Brad King (1): 6e51fe76 FStream: Add std::fstream wrapper for in/out file streams --- FStream.hxx.in | 46 ++++++++++++++++++++++++++++++++++++++++++++++ testFStream.cxx | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/FStream.hxx.in b/FStream.hxx.in index b424488..55a7fb1 100644 --- a/FStream.hxx.in +++ b/FStream.hxx.in @@ -167,6 +167,50 @@ protected: }; template > +class basic_fstream + : public std::basic_iostream + , public basic_efilebuf +{ +public: + typedef typename basic_efilebuf::internal_buffer_type + internal_buffer_type; + typedef std::basic_iostream internal_stream_type; + + basic_fstream() + : internal_stream_type(new internal_buffer_type()) + { + this->buf_ = + static_cast(internal_stream_type::rdbuf()); + } + explicit basic_fstream(char const* file_name, + std::ios_base::openmode mode = std::ios_base::in | + std::ios_base::out) + : internal_stream_type(new internal_buffer_type()) + { + this->buf_ = + static_cast(internal_stream_type::rdbuf()); + open(file_name, mode); + } + + void open(char const* file_name, + std::ios_base::openmode mode = std::ios_base::in | + std::ios_base::out) + { + this->_set_state(this->_open(file_name, mode), this, this); + } + + bool is_open() { return this->_is_open(); } + + void close() { this->_set_state(this->_close(), this, this); } + + using basic_efilebuf::_is_open; + + internal_buffer_type* rdbuf() const { return this->buf_; } + + ~basic_fstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); } +}; + +template > class basic_ifstream : public std::basic_istream , public basic_efilebuf @@ -251,11 +295,13 @@ public: ~basic_ofstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); } }; +typedef basic_fstream fstream; typedef basic_ifstream ifstream; typedef basic_ofstream ofstream; # undef @KWSYS_NAMESPACE@_FStream_NOEXCEPT #else +using std::fstream; using std::ofstream; using std::ifstream; #endif diff --git a/testFStream.cxx b/testFStream.cxx index afba953..3325e20 100644 --- a/testFStream.cxx +++ b/testFStream.cxx @@ -99,12 +99,50 @@ static int testBOM() return 0; } +static int testBOMIO() +{ + // test various encodings in binary mode + for (int i = 0; i < num_test_files; i++) { + kwsys::fstream f("bomio.txt", + kwsys::fstream::in | kwsys::fstream::out | + kwsys::fstream::binary | kwsys::fstream::trunc); + f.write(reinterpret_cast(expected_bom_data[i] + 1), + *expected_bom_data[i]); + f.write(reinterpret_cast(file_data[i] + 1), file_data[i][0]); + if (!f.good()) { + std::cout << "Unable to write data " << i << std::endl; + return 1; + } + f.seekp(0); + + kwsys::FStream::BOM bom = kwsys::FStream::ReadBOM(f); + if (bom != expected_bom[i]) { + std::cout << "Unexpected BOM " << i << std::endl; + return 1; + } + char data[max_test_file_size]; + f.read(data, file_data[i][0]); + if (!f.good()) { + std::cout << "Unable to read data " << i << std::endl; + return 1; + } + + if (memcmp(data, file_data[i] + 1, file_data[i][0]) != 0) { + std::cout << "Incorrect read data " << i << std::endl; + return 1; + } + } + + return 0; +} + int testFStream(int, char* []) { int ret = 0; ret |= testNoFile(); ret |= testBOM(); + ret |= testBOMIO(); return ret; } -- cgit v0.12