NeoPZ
TPZBFileStream.cpp
Go to the documentation of this file.
1 #include "TPZBFileStream.h"
2 #ifdef _AUTODIFF
3 #include "fad.h"
4 #include "tfad.h"
5 #endif
6 
8 
10  CloseWrite();
11  CloseRead();
12 }
13 
14 void TPZBFileStream::OpenRead(const std::string &fileName) {
15 #ifdef PZDEBUG
16  if (AmIOpenForRead()) {
17  PZError << "TPZBFileStream: File is already opened" << std::endl;
18  DebugStop();
19  }
20 #endif
21  fIn.open(fileName.c_str(), std::ifstream::binary);
22 #ifdef PZDEBUG
23  if (!AmIOpenForRead()) {
24  PZError << "TPZBFileStream: Could not open file" << std::endl;
25  DebugStop();
26  }
27 #endif
28 }
29 void TPZBFileStream::OpenWrite(const std::string &fileName) {
30 #ifdef PZDEBUG
31  if (AmIOpenForWrite()) {
32  PZError << "TPZBFileStream: File is already opened" << std::endl;
33  DebugStop();
34  }
35 #endif
36  fOut.open(fileName.c_str(), std::ofstream::binary | std::ofstream::trunc);
37 #ifdef PZDEBUG
38  if (!AmIOpenForWrite()) {
39  PZError << "TPZBFileStream: Could not open file" << std::endl;
40  DebugStop();
41  }
42 #endif
43 // std::string fileInfo("FileVersion");
44 // fOut.write(fileInfo.c_str(), fileInfo.length());
45 // const uint64_t temp = fCurrentVersion;
46 // fOut.write(reinterpret_cast<const char *>(&temp), sizeof(temp));
47 }
48 
50  return fIn.is_open();
51 }
52 
54  return fOut.is_open();
55 }
56 
57 
59  if (fIn.is_open()) {
60  fIn.close();
61  }
62 }
63 
65  if (fOut.is_open()) {
66  fOut.close();
67  }
68 }
69 
71 template <class T> void TPZBFileStream::ReadData(T *p, int howMany) {
72  fIn.read(reinterpret_cast<char *>(p), howMany * sizeof(T));
73 #ifdef PZDEBUG
74  if (fIn.bad()) {
75  PZError << "TBFileStream:Could not read from stream" << std::endl;
76  DebugStop();
77  }
78 #endif
79 }
80 
82 template <class T> void TPZBFileStream::WriteData(const T *p, int howMany) {
83  fOut.write(reinterpret_cast<const char *>(p), howMany * sizeof(T));
84 #ifdef PZDEBUG
85  if (fOut.bad()) {
86  PZError << "TBFileStream:Could not write to stream" << std::endl;
87  DebugStop();
88  }
89 #endif
90 }
91 
92 template
93 void TPZBFileStream::ReadData<double>(double *p, int howMany);
94 
95 template
96 void TPZBFileStream::WriteData<double>(const double *p, int howMany);
97 
void WriteData(const T *p, int howMany)
Reads howMany objects of the class T from pointer location p.
virtual bool AmIOpenForRead()
std::ofstream fOut
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
void OpenRead(const std::string &fileName)
std::ifstream fIn
void OpenWrite(const std::string &fileName)
virtual bool AmIOpenForWrite()
void ReadData(T *p, int howMany)
Reads howMany objects of the class T from pointer location p.
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15