diff options
author | Brad King <brad.king@kitware.com> | 2021-02-02 14:37:05 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2021-02-02 14:37:05 (GMT) |
commit | 7d46310cf8546f6594a6704034a0915ed46189cc (patch) | |
tree | c1498a60be6706b1f0e02f5342723404fac85d90 /Source/kwsys | |
parent | 8387aa20f2c8ba870a8349665e2469f0e064f0b2 (diff) | |
parent | 86ecce2072d740679f8cd292028a5b1bae4bdeba (diff) | |
download | CMake-7d46310cf8546f6594a6704034a0915ed46189cc.zip CMake-7d46310cf8546f6594a6704034a0915ed46189cc.tar.gz CMake-7d46310cf8546f6594a6704034a0915ed46189cc.tar.bz2 |
Merge branch 'upstream-KWSys' into update-kwsys
# By KWSys Upstream
* upstream-KWSys:
KWSys 2021-02-02 (c672435e)
Diffstat (limited to 'Source/kwsys')
-rw-r--r-- | Source/kwsys/FStream.hxx.in | 46 | ||||
-rw-r--r-- | Source/kwsys/testFStream.cxx | 38 |
2 files changed, 84 insertions, 0 deletions
diff --git a/Source/kwsys/FStream.hxx.in b/Source/kwsys/FStream.hxx.in index b424488..55a7fb1 100644 --- a/Source/kwsys/FStream.hxx.in +++ b/Source/kwsys/FStream.hxx.in @@ -167,6 +167,50 @@ protected: }; template <typename CharType, typename Traits = std::char_traits<CharType> > +class basic_fstream + : public std::basic_iostream<CharType, Traits> + , public basic_efilebuf<CharType, Traits> +{ +public: + typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type + internal_buffer_type; + typedef std::basic_iostream<CharType, Traits> internal_stream_type; + + basic_fstream() + : internal_stream_type(new internal_buffer_type()) + { + this->buf_ = + static_cast<internal_buffer_type*>(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_buffer_type*>(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<CharType, Traits>::_is_open; + + internal_buffer_type* rdbuf() const { return this->buf_; } + + ~basic_fstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); } +}; + +template <typename CharType, typename Traits = std::char_traits<CharType> > class basic_ifstream : public std::basic_istream<CharType, Traits> , public basic_efilebuf<CharType, Traits> @@ -251,11 +295,13 @@ public: ~basic_ofstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); } }; +typedef basic_fstream<char> fstream; typedef basic_ifstream<char> ifstream; typedef basic_ofstream<char> 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 afba953..3325e20 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<const char*>(expected_bom_data[i] + 1), + *expected_bom_data[i]); + f.write(reinterpret_cast<const char*>(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; } |