ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
binstream.h
Go to the documentation of this file.
1#ifndef RWSTREAM_H
2#define RWSTREAM_H
3
4#include <stdio.h>
5#include <cstdlib>
6#include <complex>
7#include <iostream>
8
10
16{
17 public:
18 Binstream() = default;
19 Binstream(const std::string, const char*);
20 ~Binstream();
21 // The destructor closes the owned FILE*, so copying would double-close.
22 Binstream(const Binstream&) = delete;
23 Binstream& operator=(const Binstream&) = delete;
24 FILE* fileptr = nullptr;
25 void close();
26 void open(const std::string, const char*);
27 bool operator!() const;
28 operator bool() const;
29
30 template<class T>
31 Binstream& operator>>(T& data);
32
33 template<class T>
34 Binstream& operator<<(const T& data);
35
36 template<class T>
37 Binstream& read(T* data, const int n);
38
39 template<class T>
40 Binstream& write(const T* data, const int n);
41};
42
43// read a data from file
44template<class T>
46{
47 const int size = sizeof(T);
48 if(this->fileptr == NULL)
49 {
50 ModuleBase::WARNING_QUIT("Binstream::operator>>",
51 "cannot read from an unopened file.");
52 }
53 size_t ch = fread(&data, size, 1, this->fileptr);
54 if(ch < 1)
55 {
56 ModuleBase::WARNING_QUIT("Binstream::operator>>",
57 "Some data couldn't be read. Please make sure you are using op: \"r\".");
58 }
59 return *this;
60}
61
62// write a data into file
63template<class T>
65{
66 const int size = sizeof(T);
67 if(this->fileptr == NULL)
68 {
69 ModuleBase::WARNING_QUIT("Binstream::operator<<",
70 "cannot write to an unopened file.");
71 }
72 size_t ch = fwrite(&data, size, 1, this->fileptr);
73 // fwrite() reports success while data is still buffered; fflush() forces
74 // the buffer out so delayed write errors (e.g. RLIMIT_FSIZE, full disk)
75 // are detected here instead of being silently dropped by fclose().
76 if(ch < 1 || fflush(this->fileptr) != 0)
77 {
78 ModuleBase::WARNING_QUIT("Binstream::operator<<",
79 "Some data couldn't be written.");
80 }
81 return *this;
82}
83
84// read an array of data
85template<class T>
86Binstream& Binstream::read(T* data, const int n)
87{
88 const int size = sizeof(T);
89 if(this->fileptr == NULL)
90 {
91 ModuleBase::WARNING_QUIT("Binstream::read",
92 "cannot read from an unopened file.");
93 }
94 size_t ch = fread(data, size, n, this->fileptr);
95 if(ch < static_cast<size_t>(n))
96 {
97 ModuleBase::WARNING_QUIT("Binstream::read",
98 "Some array elements couldn't be read. Please make sure you are using op: \"r\".");
99 }
100 return *this;
101}
102
103// write an array of data
104template<class T>
105Binstream& Binstream::write(const T* data, const int n)
106{
107 const int size = sizeof(T);
108 if(this->fileptr == NULL)
109 {
110 ModuleBase::WARNING_QUIT("Binstream::write",
111 "cannot write to an unopened file.");
112 }
113 size_t ch = fwrite(data, size, n, this->fileptr);
114 // fwrite() reports success while data is still buffered; fflush() forces
115 // the buffer out so delayed write errors (e.g. RLIMIT_FSIZE, full disk)
116 // are detected here instead of being silently dropped by fclose().
117 if(ch < static_cast<size_t>(n) || fflush(this->fileptr) != 0)
118 {
119 ModuleBase::WARNING_QUIT("Binstream::write",
120 "Some array elements couldn't be written.");
121 }
122 return *this;
123}
124
125#endif
Binstream & write(const T *data, const int n)
Definition binstream.h:105
void open(const std::string, const char *)
Definition binstream.cpp:73
Binstream & operator>>(T &data)
Definition binstream.h:45
bool operator!() const
Definition binstream.cpp:83
~Binstream()
Definition binstream.cpp:37
Binstream & operator=(const Binstream &)=delete
Binstream(const Binstream &)=delete
Binstream & read(T *data, const int n)
Definition binstream.h:86
FILE * fileptr
Definition binstream.h:24
Binstream & operator<<(const T &data)
Definition binstream.h:64
void close()
Definition binstream.cpp:54
Binstream()=default
#define T
Definition exp.cpp:237
void WARNING_QUIT(const std::string &, const std::string &)
Combine the functions of WARNING and QUIT.
Definition test_delley.cpp:15