-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbin_file.cpp
267 lines (221 loc) · 7.16 KB
/
bin_file.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <stdint.h>
#include <stdexcept>
#include "bin_file.hpp"
#include "byte_swap.h"
#include "prenexustypes.hpp"
using std::ifstream;
using std::runtime_error;
using std::size_t;
using std::string;
using std::stringstream;
using std::vector;
namespace {
static const size_t BLOCK_SIZE = 1024;
}
BinFile::BinFile(const string &filename) : byteSwap(false)
{
// this should never trip
if (filename.empty())
throw runtime_error("Cannot specify empty filename");
this->filename = filename;
// open the file
this->handle = new ifstream(this->filename.c_str(), std::ios::binary);
if (!(this->handle->is_open()))
throw runtime_error("Failed opening file");
// determine the file size in bytes
this->handle->seekg(0,std::ios::end);
this->size_bytes = this->handle->tellg();
this->seek(0);
}
BinFile::~BinFile()
{
if (this->handle != NULL)
delete this->handle;
this->handle = NULL;
}
size_t BinFile::size_in_bytes()
{
return this->size_bytes;
}
size_t BinFile::num_items(const size_t data_size, const std::size_t items)
{
size_t fileStartPos = this->handle->tellg();
size_t size = (this->size_bytes - fileStartPos) / data_size;
if ((items > 0) && (items < size))
size = items;
return size;
}
void BinFile::seek(const size_t bytes)
{
if (this->handle == NULL)
throw runtime_error("File handle is null");
this->handle->seekg(bytes, std::ios::beg);
}
void BinFile::setByteSwap(const bool swap)
{
this->byteSwap = swap;
}
namespace { // anonymous namespace hides from other files
void byte_swap(char &){
// do nothing
}
void byte_swap(int8_t &number){
// do nothing
(void)number;
}
void byte_swap(int16_t &number){
swap_bytes<2>(&number);
}
void byte_swap(int32_t &number){
swap_bytes<4>(&number);
}
void byte_swap(int64_t &number){
swap_bytes<8>(&number);
}
void byte_swap(uint8_t &number){
// do nothing
(void)number;
}
void byte_swap(uint16_t &number){
swap_bytes<2>(&number);
}
void byte_swap(uint32_t &number){
swap_bytes<4>(&number);
}
void byte_swap(uint64_t &number){
swap_bytes<8>(&number);
}
void byte_swap(float &number){
swap_bytes<4>(&number);
}
void byte_swap(double &number){
swap_bytes<8>(&number);
}
} // namespace anonymous
template <typename NumT>
void BinFile::read_block(NumT *buffer, const std::size_t buffer_size) {
size_t data_size=sizeof(NumT);
this->handle->read(reinterpret_cast<char *>(buffer),buffer_size*data_size);
if(this->byteSwap) {
for( size_t i=0 ; i<buffer_size ; ++i ) {
byte_swap(buffer[i]);
}
}
/* REMOVE
for( size_t i=0 ; i<buffer_size ; ++i ) {
{
if(config.show_data)
{
print_data_item<NumT>(buffer,offset,i,config);
}
if(config.sum_block)
{
sum_data_item<NumT>(buffer,i,config,sum,counter);
}
}
if(!config.integrate.empty())
{
sum_events(buffer, buffer_size, event_count);
}
*/
}
template <>
void BinFile::read_block<prenexus::DasEvent>(prenexus::DasEvent *buffer, const std::size_t buffer_size) {
size_t data_size=sizeof(prenexus::DasEvent);
this->handle->read(reinterpret_cast<char *>(buffer),buffer_size*data_size);
}
template <>
void BinFile::read_block<prenexus::Pulse>(prenexus::Pulse *buffer, const std::size_t buffer_size) {
size_t data_size=sizeof(prenexus::Pulse);
this->handle->read(reinterpret_cast<char *>(buffer),buffer_size*data_size);
}
template <>
void BinFile::read_block<prenexus::OldPulse>(prenexus::OldPulse *buffer, const std::size_t buffer_size) {
size_t data_size=sizeof(prenexus::OldPulse);
this->handle->read(reinterpret_cast<char *>(buffer),buffer_size*data_size);
}
template <>
void BinFile::read_block<prenexus::Rtdl>(prenexus::Rtdl *buffer, const std::size_t buffer_size) {
size_t data_size=sizeof(prenexus::Rtdl);
this->handle->read(reinterpret_cast<char *>(buffer),buffer_size*data_size);
}
template <typename NumT>
void BinFile::read(vector<NumT> & data, const size_t items)
{
data.clear();
// cache the size of the fundamental data item
size_t data_size = sizeof(NumT);
// determine how many items to read
size_t size = this->num_items(data_size, items);
// set up the buffer for reading
// how many items to read
size_t num_read = BLOCK_SIZE;
if (size < num_read)
num_read = size;
NumT * buffer = new NumT[num_read];
// the current position in the file (in items)
size_t pos = 0;
#ifdef DEBUG
std::cout << "data_size: " << data_size << " bytes" << std::endl;
std::cout << "fileStartPos: " << fileStartPos << " bytes" << std::endl;
std::cout << "size: " << size << " items " << std::endl;
std::cout << "num_read: " << num_read << " items" << std::endl;
#endif
// read in the data using the calculated blocks. Appending to the
// i/o vector along the way
while (pos < size) {
#ifdef DEBUG
std::cout << "pos: " << pos << " num_read: " << num_read
<< " size: " << size << std::endl;
#endif
read_block(buffer, num_read);
data.insert(data.end(), buffer, buffer+num_read);
// calculate the next chunk to read
pos += num_read;
if (pos + num_read > size)
num_read = size - pos;
}
delete[] buffer;
}
void BinFile::read(stringstream & data, const size_t items)
{
data.clear();
// cache the size of the fundamental data item
size_t data_size = sizeof(char);
// determine how many items to read
size_t size = this->num_items(data_size, items);
// set up the buffer for reading
// how many items to read
size_t num_read = BLOCK_SIZE;
if (size < num_read)
num_read = size;
char * buffer = new char[num_read];
// the current position in the file (in items)
size_t pos = 0;
// read in the data using the calculated blocks. Appending to the
// i/o vector along the way
while (pos < size) {
read_block(buffer, num_read);
data << string(buffer, buffer + num_read);
// calculate the next chunk to read
pos += num_read;
if (pos + num_read > size)
num_read = size - pos;
}
delete[] buffer;
}
// concrete instantiations to make the compiler happy
template void BinFile::read<uint8_t>(vector<uint8_t> & data, const size_t items);
template void BinFile::read<int8_t>(vector<int8_t> & data, const size_t items);
template void BinFile::read<uint16_t>(vector<uint16_t> & data, const size_t items);
template void BinFile::read<int16_t>(vector<int16_t> & data, const size_t items);
template void BinFile::read<uint32_t>(vector<uint32_t> & data, const size_t items);
template void BinFile::read<int32_t>(vector<int32_t> & data, const size_t items);
template void BinFile::read<uint64_t>(vector<uint64_t> & data, const size_t items);
template void BinFile::read<int64_t>(vector<int64_t> & data, const size_t items);
template void BinFile::read<float>(vector<float> & data, const size_t items);
template void BinFile::read<double>(vector<double> & data, const size_t items);
template void BinFile::read<prenexus::DasEvent>(vector<prenexus::DasEvent> & data, const size_t items);
template void BinFile::read<prenexus::Pulse>(vector<prenexus::Pulse> & data, const size_t items);
template void BinFile::read<prenexus::OldPulse>(vector<prenexus::OldPulse> & data, const size_t items);
template void BinFile::read<prenexus::Rtdl>(vector<prenexus::Rtdl> & data, const size_t items);