-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataFile.cxx
80 lines (64 loc) · 1.48 KB
/
DataFile.cxx
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
// Interface for reading a data file for parallelization test
#include "DataFile.h"
#include <iostream>
#include <memory>
using namespace std;
DataFile::DataFile( string fname )
: filename{std::move(fname)}
, filep{nullptr}
, buffer{make_unique<evbuf_t[]>(MAX_EVTSIZE)}
{
// Constructor
buffer[0] = 0;
}
DataFile::~DataFile()
{
// Destructor
Close();
}
int DataFile::Open( const string& fname )
{
if( !fname.empty() )
filename = fname;
filep = fopen( filename.c_str(), "r" );
if( !filep ) {
cerr << "Error opening file " << filename << endl;
return 1;
}
return 0;
}
int DataFile::Close()
{
if( filep ) {
fclose(filep);
filep = nullptr;
}
return 0;
}
int DataFile::ReadEvent()
{
if( int status; !IsOpen() && (status = Open()) != 0 )
return status;
clearerr(filep);
const size_t wordsize = sizeof(buffer[0]);
evbuf_t* bufptr = buffer.get();
// Read header
if( fread( bufptr, 1, wordsize, filep ) != wordsize ) {
if( feof(filep) )
return -1;
cerr << "Error reading event header from file " << filename << endl;
return 2;
}
evbuf_t evsize = buffer[0];
if( evsize > MAX_EVTSIZE*wordsize ) {
cerr << "Event too large, size = " << evsize << endl;
return 3;
}
// Read data
if( fread( bufptr+1, 1, evsize-wordsize, filep ) != evsize-wordsize ) {
// EOF should never occur in the midst of the data
cerr << "Error reading event data from file " << filename << endl;
return 4;
}
return 0;
}