Procházet zdrojové kódy

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
KWSys Upstream před 4 roky
rodič
revize
86ecce2072
2 změnil soubory, kde provedl 84 přidání a 0 odebrání
  1. 46 0
      FStream.hxx.in
  2. 38 0
      testFStream.cxx

+ 46 - 0
FStream.hxx.in

@@ -166,6 +166,50 @@ protected:
   FILE* file_;
 };
 
+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>
@@ -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

+ 38 - 0
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;
 }