BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
RawFileUtil.cxx File Reference
#include "RawFile/RawFileUtil.h"
#include "IRawFile/RawFileExceptions.h"
#include "RawFile/raw_ifstream.h"
#include "RawFile/raw_ofstream.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)
std::ofstream & operator<< (std::ofstream &os, FileStartRecord &record)
std::ofstream & operator<< (std::ofstream &os, FileNameStrings &record)
std::ofstream & operator<< (std::ofstream &os, RunParametersRecord &record)
std::ofstream & operator<< (std::ofstream &os, DataSeparatorRecord &record)
std::ofstream & operator<< (std::ofstream &os, FileEndRecord &record)

Function Documentation

◆ makeEndRecord()

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

Definition at line 164 of file RawFileUtil.cxx.

165 {
166 // copy file_end_record data that already read by data_separator_record
167 memcpy( (void*)&record.m_record, (const void*)&( sep.getRecord() ),
168 sizeof( data_separator_record ) );
169 // read data block
170 if ( is.read( (char*)( &record.m_record.events_in_file ),
171 ( sizeof( file_end_record ) - sizeof( data_separator_record ) ) )
172 .fail() )
173 {
174 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
175 throw BadInputStream( "FileEndRecord" );
176 }
177
178 // data validation checks
179 if ( record.m_record.marker != 0x1234dddd )
180 {
181 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
182 throw WrongMarker( 0x1234dddd, record.m_record.marker );
183 }
184 if ( record.m_record.end_marker != 0x1234eeee )
185 {
186 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
187 throw WrongMarker( 0x1234eeee, record.m_record.end_marker );
188 }
189
190 if ( record.m_record.record_size != 10 )
191 {
192 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
193 throw UnexpectedRecordSize( "FileEndRecord", 10, record.m_record.record_size );
194 }
195
196 return is;
197}
const data_separator_record & getRecord() const

◆ operator<<() [1/5]

std::ofstream & operator<< ( std::ofstream & os,
DataSeparatorRecord & record )

Definition at line 239 of file RawFileUtil.cxx.

239 {
240 // write data block
241 if ( os.write( (char*)( &record.m_record ), sizeof( data_separator_record ) ).fail() )
242 {
243 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
244 throw FailedToWrite( "DataSeparatorRecord" );
245 }
246
247 return os;
248}

◆ operator<<() [2/5]

std::ofstream & operator<< ( std::ofstream & os,
FileEndRecord & record )

Definition at line 250 of file RawFileUtil.cxx.

250 {
251 // write data block
252 if ( os.write( (char*)( &record.m_record ), sizeof( file_end_record ) ).fail() )
253 {
254 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
255 throw FailedToWrite( "FileEndRecord" );
256 }
257
258 return os;
259}

◆ operator<<() [3/5]

std::ofstream & operator<< ( std::ofstream & os,
FileNameStrings & record )

Definition at line 210 of file RawFileUtil.cxx.

210 {
211 // write data block
212 os.write( (char*)( &record.m_record ), sizeof( uint32_t ) * 2 );
213 uint32_t sizebyte = record.m_record.length1;
214 uint32_t sizeword = ( sizebyte + 3 ) / 4;
215 os.write( record.m_appName.c_str(), sizeword * 4 );
216 os.write( (char*)( &record.m_record.length2 ), sizeof( uint32_t ) );
217 sizebyte = record.m_record.length2;
218 sizeword = ( sizebyte + 3 ) / 4;
219 os.write( record.m_usrTag.c_str(), sizeword * 4 );
220 if ( os.fail() )
221 {
222 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
223 throw FailedToWrite( "FileNameStrings" );
224 }
225 return os;
226}

◆ operator<<() [4/5]

std::ofstream & operator<< ( std::ofstream & os,
FileStartRecord & record )

Definition at line 199 of file RawFileUtil.cxx.

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

◆ operator<<() [5/5]

std::ofstream & operator<< ( std::ofstream & os,
RunParametersRecord & record )

Definition at line 228 of file RawFileUtil.cxx.

228 {
229 // write data block
230 if ( os.write( (char*)( &record.m_record ), sizeof( run_parameters_record ) ).fail() )
231 {
232 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
233 throw FailedToWrite( "RunParametersRecord" );
234 }
235
236 return os;
237}

◆ operator>>() [1/5]

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

Definition at line 108 of file RawFileUtil.cxx.

108 {
109 // read data block
110 if ( is.read( (char*)( &record.m_record ), sizeof( data_separator_record ) ).fail() )
111 {
112 // std::cerr << "[RawFile] Error occurred while reading file" << std::endl;
113 throw BadInputStream( "DataSeparatorRecord" );
114 // throw ReachEndOfFile();
115 }
116
117 // data validation checks
118 if ( record.m_record.marker != 0x1234cccc )
119 {
120 if ( record.m_record.marker == 0x1234dddd )
121 { throw ReachEndOfFile( is.currentFile().c_str() ); }
122 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
123 throw WrongMarker( 0x1234cccc, record.m_record.marker );
124 }
125
126 if ( record.m_record.record_size != 4 )
127 {
128 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
129 throw UnexpectedRecordSize( "DataSeparatorRecord", 4, record.m_record.record_size );
130 }
131
132 return is;
133}

◆ operator>>() [2/5]

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

Definition at line 135 of file RawFileUtil.cxx.

135 {
136 // read data block
137 if ( is.read( (char*)( &record.m_record ), sizeof( file_end_record ) ).fail() )
138 {
139 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
140 throw BadInputStream( "FileEndRecord" );
141 }
142
143 // data validation checks
144 if ( record.m_record.marker != 0x1234dddd )
145 {
146 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
147 throw WrongMarker( 0x1234dddd, record.m_record.marker );
148 }
149 if ( record.m_record.end_marker != 0x1234eeee )
150 {
151 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
152 throw WrongMarker( 0x1234eeee, record.m_record.end_marker );
153 }
154
155 if ( record.m_record.record_size != 10 )
156 {
157 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
158 throw UnexpectedRecordSize( "FileEndRecord", 10, record.m_record.record_size );
159 }
160
161 return is;
162}

◆ operator>>() [3/5]

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

Definition at line 31 of file RawFileUtil.cxx.

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

◆ 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 {
11 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
12 throw BadInputStream( "FileStartRecord" );
13 }
14
15 // data validation checks
16 if ( record.m_record.marker != 0x1234aaaa )
17 {
18 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
19 throw WrongMarker( 0x1234aaaa, record.m_record.marker );
20 }
21
22 if ( record.m_record.record_size != 8 )
23 {
24 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
25 throw UnexpectedRecordSize( "FileStartRecord", 8, record.m_record.record_size );
26 }
27
28 return is;
29}

◆ operator>>() [5/5]

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

Definition at line 84 of file RawFileUtil.cxx.

84 {
85 // read data block
86 if ( is.read( (char*)( &record.m_record ), sizeof( run_parameters_record ) ).fail() )
87 {
88 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
89 throw BadInputStream( "RunParametersRecord" );
90 }
91
92 // data validation checks
93 if ( record.m_record.marker != 0x1234bbbb )
94 {
95 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
96 throw WrongMarker( 0x1234bbbb, record.m_record.marker );
97 }
98
99 if ( record.m_record.record_size != 9 )
100 {
101 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
102 throw UnexpectedRecordSize( "RunParametersRecord", 9, record.m_record.record_size );
103 }
104
105 return is;
106}