forked from bgamari/timetag-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.cpp
152 lines (127 loc) · 4.12 KB
/
record.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
// vim: set fileencoding=utf-8 noet :
/* timetag-tools - Tools for UMass FPGA timetagger
*
* Copyright © 2010 Ben Gamari
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*
* Author: Ben Gamari <[email protected]>
*/
#include "record.h"
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <cassert>
#ifdef __APPLE__
#if BYTE_ORDER == LITTLE_ENDIAN
uint64_t htobe64(uint64_t a)
{
uint64_t b;
char* bb = (char*) &b;
char* aa = (char*) &a;
for (unsigned int i=0; i<8; i++)
bb[i] = aa[7-i];
return b;
}
#else
uint64_t htobe64(uint64_t a) { return a; }
#endif
#endif
record::type record::get_type() const {
return (data & REC_TYPE_MASK) ? DELTA : STROBE;
}
uint64_t record::get_time() const {
return get_raw_time() + time_offset;
}
uint64_t record::get_raw_time() const {
return data & TIME_MASK;
}
bool record::get_wrap_flag() const {
return (data & TIMER_WRAP_MASK) != 0;
}
bool record::get_lost_flag() const {
return (data & LOST_SAMPLE_MASK) != 0;
}
std::bitset<4> record::get_channels() const {
return std::bitset<4>((data & CHANNEL_MASK) >> TIME_BITS);
}
record_stream::record_stream(FILE* file) : record_stream(file, 0) { }
record_stream::record_stream(FILE* file, unsigned int drop_wraps) : time_offset(0), rec_idx(0), file(file) {
assert(file != NULL);
unsigned int i=0;
while (i < drop_wraps) {
record rec = get_record();
if (rec.get_wrap_flag())
i++;
}
time_offset = 0;
}
unsigned int get_file_length(const char* path) {
struct stat buf;
int res;
res = stat(path, &buf);
if (res)
throw std::runtime_error("Error in stat()");
return buf.st_size / RECORD_LENGTH;
}
record record_stream::get_record() {
record_t data;
int res = fread(&data, 1, RECORD_LENGTH, file);
if (res == 0)
throw end_stream();
else if (res < RECORD_LENGTH)
throw std::runtime_error("Incomplete record");
rec_idx++;
#if defined(LITTLE_ENDIAN)
uint8_t* d = (uint8_t*) &data;
std::swap(d[0], d[5]);
std::swap(d[1], d[4]);
std::swap(d[2], d[3]);
#elif defined(BIG_ENDIAN)
#else
#error Either LITTLE_ENDIAN or BIG_ENDIAN must be defined.
#endif
record rec(data);
if (rec_idx > 1 && rec.get_wrap_flag())
time_offset += (1ULL<<TIME_BITS) - 1;
rec.time_offset = time_offset;
return rec;
}
std::vector<parsed_record> record_stream::parse_records(unsigned int n) {
std::vector<parsed_record> buf;
for (unsigned int i=0; i<n; i++) {
record r = get_record();
parsed_record pr;
pr.time = r.get_time();
pr.type = r.get_type();
pr.wrap = r.get_wrap_flag();
pr.lost = r.get_lost_flag();
std::bitset<4> ch = r.get_channels();
for (unsigned int j=0; j<4; j++)
pr.channels[j] = ch[j];
buf.push_back(pr);
}
return buf;
}
void write_record(FILE* fout, record r) {
record_t data;
data = r.data;
data = htobe64(data << 16);
int res = fwrite(&data, 1, RECORD_LENGTH, fout);
if (res == 0)
throw end_stream();
else if (res < RECORD_LENGTH)
throw std::runtime_error("Incomplete record written");
}