-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawDataReader.cxx
More file actions
109 lines (91 loc) · 2.24 KB
/
RawDataReader.cxx
File metadata and controls
109 lines (91 loc) · 2.24 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
#include "DataReader.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <fcntl.h>
RawDataReader::RawDataReader(const char* fname, int bufsize, int chunksize)
{
// Note that read() may read incomplete chunks. We make the buffer one
// chunk larger than requested to have space for this.
fChunksize = chunksize;
fNChunks = bufsize + 1;
fBufsize = fNChunks * chunksize;
fHead = -1;
fBufPos = 0;
fBuf = new char[fBufsize];
fFd = open(fname, O_RDONLY | O_NONBLOCK);
if(fFd < 0)
perror("open()");
}
RawDataReader::~RawDataReader()
{
delete[] fBuf;
}
void RawDataReader::reset(int bufsize)
{
if(bufsize >= 0 && (bufsize + 1) != fNChunks) {
delete[] fBuf;
fNChunks = bufsize + 1;
fBufsize = fNChunks * fChunksize;
fBuf = new char[fBufsize];
}
fHead = -1;
fBufPos = 0;
}
int RawDataReader::do_read(char* buf, int len)
{
if(fFd < 0)
return -1;
fd_set fds;
struct timeval tv;
int res;
FD_ZERO(&fds);
FD_SET(fFd, &fds);
tv.tv_sec = 0;
tv.tv_usec = 0;
res = select(fFd+1, &fds, NULL, NULL, &tv);
if(res < 0) {
perror("select()");
return -1;
}
if(!FD_ISSET(fFd, &fds))
return 0;
res = read(fFd, buf, len);
if(res < 0) {
perror("read()");
return -1;
} else if(res == 0) {
fHadEOF = true;
}
return res;
}
bool RawDataReader::readAll()
{
int n_read = 0;
while(1) {
n_read = do_read(&fBuf[fBufPos], fBufsize - fBufPos);
if(n_read <= 0)
break;
if(fHadEOF) {
// new data has arrived after end of file
fHead = -1;
fHadReset = true;
fHadEOF = false;
}
fHead += (n_read + fBufPos % fChunksize) / fChunksize;
fBufPos += n_read;
fBufPos %= fBufsize;
}
if(fHadEOF) {
// end of file, start anew when next data arrives
fBufPos = 0;
}
return (n_read >= 0);
}
char* RawDataReader::atRaw(int t)
{
if(t > fHead || t <= (fHead - fNChunks + 1) || t < 0)
return NULL;
return &fBuf[(t % fNChunks)*fChunksize];
}