diff --git a/Source/kwsys/FStream.hxx.in b/Source/kwsys/FStream.hxx.in index b424488920..55a7fb195a 100644 --- a/Source/kwsys/FStream.hxx.in +++ b/Source/kwsys/FStream.hxx.in @@ -166,6 +166,50 @@ protected: FILE* file_; }; +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 @@ -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/Source/kwsys/testFStream.cxx b/Source/kwsys/testFStream.cxx index afba9530e4..3325e2046e 100644 --- a/Source/kwsys/testFStream.cxx +++ b/Source/kwsys/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; }