-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryFile.cpp
More file actions
250 lines (201 loc) · 5.82 KB
/
BinaryFile.cpp
File metadata and controls
250 lines (201 loc) · 5.82 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "file_io.h"
#include <string.h>
using namespace std;
#if 0
#include <vector>
#include <string.h>
using namespace std;
BinaryFile::BinaryFile(const File& file)
: File(file.getN(), file.getM(), file.getIsClassification()) {
init();
}
BinaryFile::BinaryFile(int N1, int M1, int isClassification1)
: File(N1, M1, isClassification1) {
init();
}
void BinaryFile::init() {
CONSTRUCTOR_MSG("BinaryFile");
counter = 0;
data.clear();
if (isClassification) {
pattern_size = N+1;
} else {
pattern_size = N+M;
}
//data.capacity(128*pattern_size);
state = WRITING;
}
void BinaryFile::beginReading() {
counter = 0;
state = READING;
}
void BinaryFile::beginWriting() {
counter = data.size()/pattern_size;
state = WRITING;
}
void BinaryFile::endReading() {
assert(state == READING);
// nothing to do
state = CLOSED;
}
void BinaryFile::endWriting() {
assert(state == WRITING);
// nothing to do
state = CLOSED;
}
int BinaryFile::getNextPattern(Array &arr) {
assert(state == READING);
assert((isClassification && arr.size() >= (size_t) (N + 1)) || (!isClassification && arr.size() >= (size_t) (N + M))); // arr is not of sufficient size
if ((counter+1)*pattern_size>data.size()) // end of file
return 0;
memcpy(&arr[0], &data[counter*pattern_size], sizeof (REAL)*pattern_size);
counter++;
// more checks needed.
return 1;
}
void BinaryFile::putPattern(const Array &arr) {
assert(state == WRITING);
assert((isClassification && arr.size() >= (size_t) (N + 1)) || (!isClassification && arr.size() >= (size_t) (N + M))); // arr is not of sufficient size
for(int i=0; i<pattern_size; i++) {
data.push_back(arr[i]);
}
counter++;
}
BinaryFile::~BinaryFile() {
DESTRUCTOR_MSG("BinaryFile");
data.clear();
counter = 0;
}
#else
BinaryFile::BinaryFile(const File& file)
: File(file.getN(), file.getM(), file.getIsClassification()) {
init();
}
BinaryFile::BinaryFile(int N1, int M1, int isClassification1)
: File(N1, M1, isClassification1) {
init();
}
void BinaryFile::init() {
CONSTRUCTOR_MSG("BinaryFile");
// Obtain path to the "temp" folder // todo make this static member
string path_to_temp = ".";
#ifdef unix
const char *temp_env_var = "TMPDIR";
const char *default_tempdir = "/tmp";
#else
char temp_env_var[] = "TEMP";
char default_tempdir[] = ".";
#endif
if (getenv(temp_env_var) == 0) {
path_to_temp = default_tempdir;
} else {
path_to_temp = getenv(temp_env_var);
}
// Open a transient binary file
#ifdef unix
string templatestr = path_to_temp + "/PlnTmpXXXXXX";
char *templatechr = new char[templatestr.length()+8];
strcpy(templatechr, templatestr.c_str());
int fdescriptor = mkstemp(templatechr);
fp = fdopen(fdescriptor, "wb+");
fileName = string(templatechr);
#else
char *tempnam_result = _tempnam(path_to_temp.c_str(), "PlnTmp");
fileName = string(tempnam_result);
if (tempnam_result) // fixed a leak detected by valgrind
free(tempnam_result);
fp = fopen(fileName.c_str(), "wb+");
#endif
if (fp == NULL) {
perror(fileName.c_str());
exit(0);
}
state = WRITING;
}
void BinaryFile::beginReading() {
assert(fp != NULL);
fseek(fp, 0L, ios::beg);
state = READING;
}
void BinaryFile::beginWriting() {
assert(fp != NULL);
fseek(fp, 0L, ios::end);
state = WRITING;
}
void BinaryFile::endReading() {
assert(state == READING);
// nothing to do
state = CLOSED;
}
void BinaryFile::endWriting() {
assert(state == WRITING);
// nothing to do
state = CLOSED;
}
int BinaryFile::getNextPattern(Array &arr) {
assert(state == READING);
assert((isClassification && arr.size() >= (size_t) (N + 1)) || (!isClassification && arr.size() >= (size_t) (N + M))); // arr is not of sufficient size
if (fp == NULL)
return 0;
if (feof(fp)) // end of file
return 0;
if (isClassification) {
if (fread(&arr[0], sizeof (REAL), N + 1, fp) != N + 1) {
return 0; // bad pattern
}
} else {
if (fread(&arr[0], sizeof (REAL), N + M, fp) != N + M) {
return 0; // bad pattern
}
}
// more checks needed.
return 1;
}
void BinaryFile::putPattern(const Array &arr) {
assert(state == WRITING);
assert((isClassification && arr.size() >= (size_t) (N + 1)) || (!isClassification && arr.size() >= (size_t) (N + M))); // arr is not of sufficient size
if (isClassification) {
fwrite(&arr[0], sizeof (REAL), N + 1, fp);
} else {
fwrite(&arr[0], sizeof (REAL), N + M, fp);
}
}
BinaryFile::~BinaryFile() {
DESTRUCTOR_MSG("BinaryFile");
if (fp != NULL)
fclose(fp);
if (remove(fileName.c_str()) != 0) {
perror(fileName.c_str());
}
}
void BinaryFile::truncate() {
fp = freopen(NULL, "wb+", fp);
state = WRITING;
if (fp == NULL) {
perror(fileName.c_str());
exit(0);
}
}
void BinaryFile::truncate(int N1, int M1, int isClassification1) {
truncate();
N = N1;
M = M1;
isClassification = isClassification1;
if (isClassification)
patternSize = N + 1;
else
patternSize = N + M;
}
void BinaryFile::dump(string dumpName) {
Array pattern;
pattern.resize(getPatternSize());
beginReading();
TextFile out(dumpName, N, M, isClassification, 1);
out.beginWriting();
while(getNextPattern(pattern)) {
out.putPattern(pattern);
}
out.endWriting();
endReading();
}
#endif