-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathIO.h
196 lines (170 loc) · 4.77 KB
/
IO.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*!
* \file IO.h
* \brief definitions of I/O functions for LibN3L
* \author Jie
*/
#ifndef LIBN3L_IO_H_
#define LIBN3L_IO_H_
#include <stdio.h>
#include "tensor.h"
#include "io.h"
#include "Utiltensor.h"
#include "Utils.h"
class LStream : public IStream {
public:
FILE *fp_;
size_t sz_;
public:
LStream(const string &fname, const char *mode) {
const char *newname = &fname[0];
Open(newname, mode);
}
void Open(const char *fname, const char *mode) {
fp_ = FopenCheck(fname, mode);
fseek(fp_, 0L, SEEK_END);
sz_ = ftell(fp_);
fseek(fp_, 0L, SEEK_SET);
}
size_t Read(void *ptr, size_t size) {
return fread(ptr, size, 1, fp_);
}
void Write(const void *ptr, size_t size) {
fwrite(ptr, size, 1, fp_);
}
// size_t StringRead(string &sentence) {
// char buff[100];
// return fread(ptr, size, 1, fp_);
// }
// void StringWrite(const string &sentence) {
// fputs(sentence,fp_);
// }
inline void Close(void) {
if (fp_ != NULL){
fclose(fp_); fp_ = NULL;
}
}
inline size_t Size() {
return sz_;
}
virtual ~LStream(void) {
this->Close();
}
inline std::FILE *FopenCheck(const char *fname, const char *flag) {
std::FILE *fp = fopen(fname, flag);
Check(fp != NULL, "can not open file \"%s\"\n", fname);
return fp;
}
};
template<typename DType, typename TStream>
inline void WriteBinary(TStream &fo, const DType &target) {
fo.Write(&target, sizeof(target));
}
template<typename DType, typename TStream>
inline void ReadBinary(TStream &fo, DType &target) {
fo.Read(&target, sizeof(DType));
}
template<typename TStream>
inline void WriteString(TStream &fo, const string &target) {
int string_size = target.size();
fo.Write(&string_size, sizeof(string_size));
if (string_size > 0) {
int char_size = sizeof(target[0]);
fo.Write(&char_size, sizeof(char_size));
for (int idx = 0; idx < string_size; idx++) {
fo.Write(&target[idx], sizeof(target[idx]));
}
}
}
template<typename TStream>
inline void ReadString(TStream &fo, string &target) {
int string_size;
fo.Read(&string_size, sizeof(int));
if (string_size > 0) {
int char_size;
fo.Read(&char_size, sizeof(int));
char character[string_size];
for (int idx = 0; idx < string_size; idx++) {
fo.Read(&character[idx], char_size);
}
target = string(character, string_size);
assert(target.size()==string_size);
}
}
template<typename DType, typename TStream>
inline void WriteVector(TStream &fo, vector<DType> &target) {
int vector_size = target.size();
fo.Write(&vector_size, sizeof(vector_size));
if (vector_size > 0) {
int element_size = sizeof(target[0]);
fo.Write(&element_size, sizeof(element_size));
for (int idx = 0; idx < vector_size; idx++) {
fo.Write(&target[idx], sizeof(target[idx]));
// cout << target[idx] << endl;
}
}
}
template<typename DType, typename TStream>
inline void ReadVector(TStream &fo, vector<DType> &target) {
int vector_size;
fo.Read(&vector_size, sizeof(int));
if (vector_size > 0) {
int element_size;
fo.Read(&element_size, sizeof(int));
target.resize(vector_size);
for (int idx = 0; idx < vector_size; idx++) {
fo.Read(&target[idx], element_size);
// cout << target[idx] << endl;
}
assert(target.size()== vector_size);
}
}
template<typename TStream>
inline void WriteVector(TStream &fo, vector<string> &target) {
int vector_size = target.size();
fo.Write(&vector_size, sizeof(vector_size));
if (vector_size > 0) {
for (int idx = 0; idx < vector_size; idx++) {
WriteString(fo, target[idx]);
// cout << target[idx] << endl;
}
}
}
template<typename TStream>
inline void ReadVector(TStream &fo, vector<string> &target) {
target.clear();
int vector_size;
string tmp_target;
fo.Read(&vector_size, sizeof(int));
// cout << "vector_size " << vector_size << endl;
if (vector_size > 0) {
for (int idx = 0; idx < vector_size; idx++) {
ReadString(fo, tmp_target);
target.push_back(tmp_target);
// cout << target[idx] << endl;
}
}
assert(target.size()== vector_size);
}
template<typename DType, typename TStream>
inline void WriteVector(TStream &fo, NRVec<DType> &target) {
int vector_size = target.size();
WriteBinary(fo, vector_size);
if (vector_size > 0) {
for (int idx = 0; idx < vector_size; idx++) {
WriteBinary(fo, target[idx]);
}
}
}
template<typename DType, typename TStream>
inline void ReadVector(TStream &fo, NRVec<DType> &target) {
int vector_size;
ReadBinary(fo, vector_size);
if (vector_size > 0) {
target.resize(vector_size);
for (int idx = 0; idx < vector_size; idx++) {
ReadBinary(fo, target[idx]);
}
assert(target.size()== vector_size);
}
}
#endif // LIBN3L_IO_H_