VTK  9.7.1
BinaryFile.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
4 #ifndef BinaryFile_h
5 #define BinaryFile_h
6 
7 #include "vtkByteSwap.h"
8 #include <fstream>
9 #include <string>
10 
12 {
13 public:
14  BinaryFile(const char* fname);
15  ~BinaryFile();
16 
17  bool good() const;
18 
19  template <typename T>
20  void ReadArray(T* array, size_t nitems)
21  {
22  this->InFile.read((char*)array, nitems * sizeof(T));
23  if (this->NeedSwap)
24  {
25 #ifdef VTK_WORDS_BIGENDIAN
26  vtkByteSwap::SwapBERange(array, nitems);
27 #else
28  vtkByteSwap::SwapLERange(array, nitems);
29 #endif
30  }
31  }
32 
33  template <typename T>
34  T ReadOne()
35  {
36  T x;
37  this->ReadArray(&x, 1);
38  return x;
39  }
40 
41  int ReadInt();
42 
43  double ReadDouble();
44 
45  void ReadCString(char* s, size_t n = 128);
46 
48 
49  static int SwapInt(int x);
50 
51  void SetSwap(bool val);
52 
53 private:
54  std::ifstream InFile;
55  bool NeedSwap;
56 };
57 
58 #endif // BinaryFile_h
void SetSwap(bool val)
double ReadDouble()
bool good() const
int ReadInt()
void ReadCString(char *s, size_t n=128)
void ReadArray(T *array, size_t nitems)
Definition: BinaryFile.h:20
BinaryFile(const char *fname)
T ReadOne()
Definition: BinaryFile.h:34
static int SwapInt(int x)
std::string ReadStdString()