BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
RawFileUtil.cxx File Reference
#include "RawFile/RawFileUtil.h"
#include "RawFile/raw_ifstream.h"
#include "RawFile/raw_ofstream.h"
#include "IRawFile/RawFileExceptions.h"
#include <cstring>

Go to the source code of this file.

Functions

raw_ifstreamoperator>> (raw_ifstream &is, FileStartRecord &record)
 
raw_ifstreamoperator>> (raw_ifstream &is, FileNameStrings &record)
 
raw_ifstreamoperator>> (raw_ifstream &is, RunParametersRecord &record)
 
raw_ifstreamoperator>> (raw_ifstream &is, DataSeparatorRecord &record)
 
raw_ifstreamoperator>> (raw_ifstream &is, FileEndRecord &record)
 
raw_ifstreammakeEndRecord (DataSeparatorRecord &sep, raw_ifstream &is, FileEndRecord &record)
 
raw_ofstreamoperator<< (raw_ofstream &os, FileStartRecord &record)
 
raw_ofstreamoperator<< (raw_ofstream &os, FileNameStrings &record)
 
raw_ofstreamoperator<< (raw_ofstream &os, RunParametersRecord &record)
 
raw_ofstreamoperator<< (raw_ofstream &os, DataSeparatorRecord &record)
 
raw_ofstreamoperator<< (raw_ofstream &os, FileEndRecord &record)
 

Function Documentation

◆ makeEndRecord()

raw_ifstream & makeEndRecord ( DataSeparatorRecord sep,
raw_ifstream is,
FileEndRecord record 
)

Definition at line 146 of file RawFileUtil.cxx.

146 {
147 // copy file_end_record data that already read by data_separator_record
148 memcpy((void*)&record.m_record, (const void*)&(sep.getRecord()), sizeof(data_separator_record));
149 // read data block
150 if (is.read((char*)(&record.m_record.events_in_file), (
151 sizeof(file_end_record) - sizeof(data_separator_record)
152 )).fail()) {
153 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
154 throw BadInputStream("FileEndRecord");
155 }
156
157 // data validation checks
158 if (record.m_record.marker != 0x1234dddd) {
159 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
160 throw WrongMarker(0x1234dddd, record.m_record.marker);
161 }
162 if (record.m_record.end_marker != 0x1234eeee) {
163 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
164 throw WrongMarker(0x1234eeee, record.m_record.end_marker);
165 }
166
167 if (record.m_record.record_size != 10) {
168 //std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
169 throw UnexpectedRecordSize("FileEndRecord", 10, record.m_record.record_size);
170 }
171
172 return is;
173}
const data_separator_record & getRecord() const

◆ operator<<() [1/5]

raw_ofstream & operator<< ( raw_ofstream os,
DataSeparatorRecord record 
)

Definition at line 212 of file RawFileUtil.cxx.

212 {
213 // write data block
214 if (os.write((char*)(&record.m_record), sizeof(data_separator_record)).fail()) {
215 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
216 throw FailedToWrite("DataSeparatorRecord");
217 }
218
219 return os;
220}

◆ operator<<() [2/5]

raw_ofstream & operator<< ( raw_ofstream os,
FileEndRecord record 
)

Definition at line 222 of file RawFileUtil.cxx.

222 {
223 // write data block
224 if (os.write((char*)(&record.m_record), sizeof(file_end_record)).fail()) {
225 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
226 throw FailedToWrite("FileEndRecord");
227 }
228
229 return os;
230}

◆ operator<<() [3/5]

raw_ofstream & operator<< ( raw_ofstream os,
FileNameStrings record 
)

Definition at line 185 of file RawFileUtil.cxx.

185 {
186 // write data block
187 os.write((char*)(&record.m_record), sizeof(uint32_t)*2);
188 uint32_t sizebyte = record.m_record.length1;
189 uint32_t sizeword = (sizebyte+3)/4;
190 os.write(record.m_appName.c_str(), sizeword*4);
191 os.write((char*)(&record.m_record.length2), sizeof(uint32_t));
192 sizebyte = record.m_record.length2;
193 sizeword = (sizebyte+3)/4;
194 os.write(record.m_usrTag.c_str(), sizeword*4);
195 if ( os.fail() ) {
196 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
197 throw FailedToWrite("FileNameStrings");
198 }
199 return os;
200}

◆ operator<<() [4/5]

raw_ofstream & operator<< ( raw_ofstream os,
FileStartRecord record 
)

Definition at line 175 of file RawFileUtil.cxx.

175 {
176 // write data block
177 if (os.write((char*)(&record.m_record), sizeof(file_start_record)).fail()) {
178 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
179 throw FailedToWrite("FileStartRecord");
180 }
181
182 return os;
183}

◆ operator<<() [5/5]

raw_ofstream & operator<< ( raw_ofstream os,
RunParametersRecord record 
)

Definition at line 202 of file RawFileUtil.cxx.

202 {
203 // write data block
204 if (os.write((char*)(&record.m_record), sizeof(run_parameters_record)).fail()) {
205 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
206 throw FailedToWrite("RunParametersRecord");
207 }
208
209 return os;
210}

◆ operator>>() [1/5]

raw_ifstream & operator>> ( raw_ifstream is,
DataSeparatorRecord record 
)

Definition at line 96 of file RawFileUtil.cxx.

96 {
97 // read data block
98 if (is.read((char*)(&record.m_record), sizeof(data_separator_record)).fail()) {
99 //std::cerr << "[RawFile] Error occurred while reading file" << std::endl;
100 throw BadInputStream("DataSeparatorRecord");
101 //throw ReachEndOfFile();
102 }
103
104 // data validation checks
105 if (record.m_record.marker != 0x1234cccc) {
106 if (record.m_record.marker == 0x1234dddd) {
107 throw ReachEndOfFile(is.currentFile().c_str());
108 }
109 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
110 throw WrongMarker(0x1234cccc, record.m_record.marker);
111 }
112
113 if (record.m_record.record_size != 4) {
114 //std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
115 throw UnexpectedRecordSize("DataSeparatorRecord", 4, record.m_record.record_size);
116 }
117
118 return is;
119}

◆ operator>>() [2/5]

raw_ifstream & operator>> ( raw_ifstream is,
FileEndRecord record 
)

Definition at line 121 of file RawFileUtil.cxx.

121 {
122 // read data block
123 if (is.read((char*)(&record.m_record), sizeof(file_end_record)).fail()) {
124 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
125 throw BadInputStream("FileEndRecord");
126 }
127
128 // data validation checks
129 if (record.m_record.marker != 0x1234dddd) {
130 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
131 throw WrongMarker(0x1234dddd, record.m_record.marker);
132 }
133 if (record.m_record.end_marker != 0x1234eeee) {
134 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
135 throw WrongMarker(0x1234eeee, record.m_record.end_marker);
136 }
137
138 if (record.m_record.record_size != 10) {
139 //std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
140 throw UnexpectedRecordSize("FileEndRecord", 10, record.m_record.record_size);
141 }
142
143 return is;
144}

◆ operator>>() [3/5]

raw_ifstream & operator>> ( raw_ifstream is,
FileNameStrings record 
)

Definition at line 28 of file RawFileUtil.cxx.

28 {
29 // read marker
30 if (is.read((char*)(&record.m_record.marker), sizeof(uint32_t)).fail()) {
31 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
32 throw BadInputStream("FileNameStrings::marker");
33 }
34
35 // marker validation
36 if (record.m_record.marker != 0x1234aabb) {
37 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
38 throw WrongMarker(0x1234aabb, record.m_record.marker);
39 }
40
41 // read length and strings
42 if (is.read((char*)(&record.m_record.length1), sizeof(uint32_t)).fail()) {
43 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
44 throw BadInputStream("FileNameStrings::length1");
45 }
46
47 uint32_t length1_word = (record.m_record.length1 + 3) / 4;
48 char* appName = new char[length1_word * 4 + 1];
49 if (is.read(appName, length1_word*4).fail()) {
50 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
51 throw BadInputStream("FileNameStrings::appName");
52 }
53 appName[record.m_record.length1] = '\0';
54 record.m_appName = appName;
55 delete[] appName;
56
57 if (is.read((char*)(&record.m_record.length2), sizeof(uint32_t)).fail()) {
58 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
59 throw BadInputStream("FileNameStrings::length2");
60 }
61
62 uint32_t length2_word = (record.m_record.length2 + 3) / 4;
63 char* usrTag = new char[length2_word * 4 + 1];
64 if (is.read(usrTag, length2_word*4).fail()) {
65 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
66 throw BadInputStream("FileNameStrings::usrTag");
67 }
68 usrTag[record.m_record.length2] = '\0';
69 record.m_usrTag = usrTag;
70 delete[] usrTag;
71
72 return is;
73}

◆ operator>>() [4/5]

raw_ifstream & operator>> ( raw_ifstream is,
FileStartRecord record 
)

Definition at line 7 of file RawFileUtil.cxx.

7 {
8 // read data block
9 if (is.read((char*)(&record.m_record), sizeof(file_start_record)).fail()) {
10 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
11 throw BadInputStream("FileStartRecord");
12 }
13
14 // data validation checks
15 if (record.m_record.marker != 0x1234aaaa) {
16 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
17 throw WrongMarker(0x1234aaaa, record.m_record.marker);
18 }
19
20 if (record.m_record.record_size != 8) {
21 //std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
22 throw UnexpectedRecordSize("FileStartRecord", 8, record.m_record.record_size);
23 }
24
25 return is;
26}

◆ operator>>() [5/5]

raw_ifstream & operator>> ( raw_ifstream is,
RunParametersRecord record 
)

Definition at line 75 of file RawFileUtil.cxx.

75 {
76 // read data block
77 if (is.read((char*)(&record.m_record), sizeof(run_parameters_record)).fail()) {
78 //std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
79 throw BadInputStream("RunParametersRecord");
80 }
81
82 // data validation checks
83 if (record.m_record.marker != 0x1234bbbb) {
84 //std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
85 throw WrongMarker(0x1234bbbb, record.m_record.marker);
86 }
87
88 if (record.m_record.record_size != 9) {
89 //std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
90 throw UnexpectedRecordSize("RunParametersRecord", 9, record.m_record.record_size);
91 }
92
93 return is;
94}