00001 #include "RawFile/RawFileUtil.h"
00002 #include "RawFile/raw_ifstream.h"
00003 #include "RawFile/raw_ofstream.h"
00004 #include "IRawFile/RawFileExceptions.h"
00005 #include <cstring>
00006
00007 raw_ifstream& operator>>(raw_ifstream& is, FileStartRecord& record) {
00008
00009 if (is.read((char*)(&record.m_record), sizeof(file_start_record)).fail()) {
00010
00011 throw BadInputStream("FileStartRecord");
00012 }
00013
00014
00015 if (record.m_record.marker != 0x1234aaaa) {
00016
00017 throw WrongMarker(0x1234aaaa, record.m_record.marker);
00018 }
00019
00020 if (record.m_record.record_size != 8) {
00021
00022 throw UnexpectedRecordSize("FileStartRecord", 8, record.m_record.record_size);
00023 }
00024
00025 return is;
00026 }
00027
00028 raw_ifstream& operator>>(raw_ifstream& is, FileNameStrings& record) {
00029
00030 if (is.read((char*)(&record.m_record.marker), sizeof(uint32_t)).fail()) {
00031
00032 throw BadInputStream("FileNameStrings::marker");
00033 }
00034
00035
00036 if (record.m_record.marker != 0x1234aabb) {
00037
00038 throw WrongMarker(0x1234aabb, record.m_record.marker);
00039 }
00040
00041
00042 if (is.read((char*)(&record.m_record.length1), sizeof(uint32_t)).fail()) {
00043
00044 throw BadInputStream("FileNameStrings::length1");
00045 }
00046
00047 uint32_t length1_word = (record.m_record.length1 + 3) / 4;
00048 char* appName = new char[length1_word * 4 + 1];
00049 if (is.read(appName, length1_word*4).fail()) {
00050
00051 throw BadInputStream("FileNameStrings::appName");
00052 }
00053 appName[record.m_record.length1] = '\0';
00054 record.m_appName = appName;
00055 delete[] appName;
00056
00057 if (is.read((char*)(&record.m_record.length2), sizeof(uint32_t)).fail()) {
00058
00059 throw BadInputStream("FileNameStrings::length2");
00060 }
00061
00062 uint32_t length2_word = (record.m_record.length2 + 3) / 4;
00063 char* usrTag = new char[length2_word * 4 + 1];
00064 if (is.read(usrTag, length2_word*4).fail()) {
00065
00066 throw BadInputStream("FileNameStrings::usrTag");
00067 }
00068 usrTag[record.m_record.length2] = '\0';
00069 record.m_usrTag = usrTag;
00070 delete[] usrTag;
00071
00072 return is;
00073 }
00074
00075 raw_ifstream& operator>>(raw_ifstream&is, RunParametersRecord& record) {
00076
00077 if (is.read((char*)(&record.m_record), sizeof(run_parameters_record)).fail()) {
00078
00079 throw BadInputStream("RunParametersRecord");
00080 }
00081
00082
00083 if (record.m_record.marker != 0x1234bbbb) {
00084
00085 throw WrongMarker(0x1234bbbb, record.m_record.marker);
00086 }
00087
00088 if (record.m_record.record_size != 9) {
00089
00090 throw UnexpectedRecordSize("RunParametersRecord", 9, record.m_record.record_size);
00091 }
00092
00093 return is;
00094 }
00095
00096 raw_ifstream& operator>>(raw_ifstream& is, DataSeparatorRecord& record) {
00097
00098 if (is.read((char*)(&record.m_record), sizeof(data_separator_record)).fail()) {
00099
00100 throw BadInputStream("DataSeparatorRecord");
00101
00102 }
00103
00104
00105 if (record.m_record.marker != 0x1234cccc) {
00106 if (record.m_record.marker == 0x1234dddd) {
00107 throw ReachEndOfFile(is.currentFile().c_str());
00108 }
00109
00110 throw WrongMarker(0x1234cccc, record.m_record.marker);
00111 }
00112
00113 if (record.m_record.record_size != 4) {
00114
00115 throw UnexpectedRecordSize("DataSeparatorRecord", 4, record.m_record.record_size);
00116 }
00117
00118 return is;
00119 }
00120
00121 raw_ifstream& operator>>(raw_ifstream& is, FileEndRecord& record) {
00122
00123 if (is.read((char*)(&record.m_record), sizeof(file_end_record)).fail()) {
00124
00125 throw BadInputStream("FileEndRecord");
00126 }
00127
00128
00129 if (record.m_record.marker != 0x1234dddd) {
00130
00131 throw WrongMarker(0x1234dddd, record.m_record.marker);
00132 }
00133 if (record.m_record.end_marker != 0x1234eeee) {
00134
00135 throw WrongMarker(0x1234eeee, record.m_record.end_marker);
00136 }
00137
00138 if (record.m_record.record_size != 10) {
00139
00140 throw UnexpectedRecordSize("FileEndRecord", 10, record.m_record.record_size);
00141 }
00142
00143 return is;
00144 }
00145
00146 raw_ifstream& makeEndRecord(DataSeparatorRecord& sep, raw_ifstream& is, FileEndRecord& record) {
00147
00148 memcpy((void*)&record.m_record, (const void*)&(sep.getRecord()), sizeof(data_separator_record));
00149
00150 if (is.read((char*)(&record.m_record.events_in_file), (
00151 sizeof(file_end_record) - sizeof(data_separator_record)
00152 )).fail()) {
00153
00154 throw BadInputStream("FileEndRecord");
00155 }
00156
00157
00158 if (record.m_record.marker != 0x1234dddd) {
00159
00160 throw WrongMarker(0x1234dddd, record.m_record.marker);
00161 }
00162 if (record.m_record.end_marker != 0x1234eeee) {
00163
00164 throw WrongMarker(0x1234eeee, record.m_record.end_marker);
00165 }
00166
00167 if (record.m_record.record_size != 10) {
00168
00169 throw UnexpectedRecordSize("FileEndRecord", 10, record.m_record.record_size);
00170 }
00171
00172 return is;
00173 }
00174
00175 raw_ofstream& operator<<(raw_ofstream& os, FileStartRecord& record) {
00176
00177 if (os.write((char*)(&record.m_record), sizeof(file_start_record)).fail()) {
00178 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
00179 throw FailedToWrite("FileStartRecord");
00180 }
00181
00182 return os;
00183 }
00184
00185 raw_ofstream& operator<<(raw_ofstream& os, FileNameStrings& record) {
00186
00187 os.write((char*)(&record.m_record), sizeof(uint32_t)*2);
00188 uint32_t sizebyte = record.m_record.length1;
00189 uint32_t sizeword = (sizebyte+3)/4;
00190 os.write(record.m_appName.c_str(), sizeword*4);
00191 os.write((char*)(&record.m_record.length2), sizeof(uint32_t));
00192 sizebyte = record.m_record.length2;
00193 sizeword = (sizebyte+3)/4;
00194 os.write(record.m_usrTag.c_str(), sizeword*4);
00195 if ( os.fail() ) {
00196 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
00197 throw FailedToWrite("FileNameStrings");
00198 }
00199 return os;
00200 }
00201
00202 raw_ofstream& operator<<(raw_ofstream& os, RunParametersRecord& record) {
00203
00204 if (os.write((char*)(&record.m_record), sizeof(run_parameters_record)).fail()) {
00205 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
00206 throw FailedToWrite("RunParametersRecord");
00207 }
00208
00209 return os;
00210 }
00211
00212 raw_ofstream& operator<<(raw_ofstream& os, DataSeparatorRecord& record) {
00213
00214 if (os.write((char*)(&record.m_record), sizeof(data_separator_record)).fail()) {
00215 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
00216 throw FailedToWrite("DataSeparatorRecord");
00217 }
00218
00219 return os;
00220 }
00221
00222 raw_ofstream& operator<<(raw_ofstream& os, FileEndRecord& record) {
00223
00224 if (os.write((char*)(&record.m_record), sizeof(file_end_record)).fail()) {
00225 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
00226 throw FailedToWrite("FileEndRecord");
00227 }
00228
00229 return os;
00230 }
00231
00232 FileStartRecord::FileStartRecord()
00233 {
00234 m_record.marker = 0x1234aaaa;
00235 m_record.record_size = 8;
00236
00237 m_record.version = 0;
00238 m_record.file_number = 0;
00239 m_record.date = 0;
00240 m_record.time = 0;
00241 m_record.sizeLimit_dataBlocks = 0;
00242 m_record.sizeLimit_MB = 0;
00243 }
00244
00245 FileNameStrings::FileNameStrings()
00246 {
00247 m_record.marker = 0x1234aabb;
00248 m_record.length1 = 4;
00249 m_record.length2 = 7;
00250 m_appName = "BOSS";
00251 m_usrTag = "offline";
00252 }
00253
00254 RunParametersRecord::RunParametersRecord()
00255 {
00256 m_record.marker = 0x1234bbbb;
00257 m_record.record_size = 9;
00258
00259 m_record.run_number = 0;
00260 m_record.max_events = 0;
00261 m_record.rec_enable = 0;
00262 m_record.trigger_type = 0;
00263 m_record.detector_mask = 0;
00264 m_record.beam_type = 0;
00265 m_record.beam_energy = 0;
00266 }
00267
00268 DataSeparatorRecord::DataSeparatorRecord()
00269 {
00270 m_record.marker = 0x1234cccc;
00271 m_record.record_size = 4;
00272
00273
00274 }
00275
00276 FileEndRecord::FileEndRecord()
00277 {
00278 m_record.marker = 0x1234dddd;
00279 m_record.record_size = 10;
00280
00281 m_record.date = 0;
00282 m_record.time = 0;
00283
00284 m_record.data_in_file = 0;
00285 m_record.events_in_run = 0;
00286 m_record.data_in_run = 0;
00287 m_record.status = 1;
00288 m_record.end_marker = 0x1234eeee;
00289 }
00290
00291 void FileStartRecord::dump(std::ostream& os) const {
00292 os << "[RawFile] FileStartRecord:" << std::endl << std::hex
00293 << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
00294 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
00295 << "[RawFile] \tversion : 0x" << m_record.version << std::endl
00296 << "[RawFile] \tfile_number : 0x" << m_record.file_number << std::endl
00297 << "[RawFile] \tdate : 0x" << m_record.date << std::endl
00298 << "[RawFile] \ttime : 0x" << m_record.time << std::endl
00299 << "[RawFile] \tsizeLimit_dataBlocks : 0x" << m_record.sizeLimit_dataBlocks << std::endl
00300 << "[RawFile] \tsizeLimit_MB : 0x" << m_record.sizeLimit_MB
00301 << std::dec << std::endl;
00302 }
00303
00304 void FileNameStrings::dump(std::ostream& os) const {
00305 os << "[RawFile] FileNameStrings:" << std::endl << std::hex
00306 << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
00307 << "[RawFile] \tAppName length : 0x" << m_record.length1 << std::endl
00308 << "[RawFile] \tAppName : " << m_appName << std::endl
00309 << "[RawFile] \tUsrTag length : 0x" << m_record.length2 << std::endl
00310 << "[RawFile] \tUsrTag : " << m_usrTag
00311 << std::dec << std::endl;
00312 }
00313
00314 void RunParametersRecord::dump(std::ostream& os) const {
00315 os << "[RawFile] RunParametersRecord:" << std::endl << std::hex
00316 << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
00317 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
00318 << "[RawFile] \trun_number : 0x" << m_record.run_number << std::endl
00319 << "[RawFile] \tmax_events : 0x" << m_record.max_events << std::endl
00320 << "[RawFile] \trec_enable : 0x" << m_record.rec_enable << std::endl
00321 << "[RawFile] \ttrigger_type : 0x" << m_record.trigger_type << std::endl
00322 << "[RawFile] \tdetector_mask : 0x" << m_record.detector_mask << std::endl
00323 << "[RawFile] \tbeam_type : 0x" << m_record.beam_type << std::endl
00324 << "[RawFile] \tbeam_energy : 0x" << m_record.beam_energy
00325 << std::dec << std::endl;
00326 }
00327
00328 void DataSeparatorRecord::dump(std::ostream& os) const {
00329 os << "[RawFile] DataSeparatorRecord:" << std::endl << std::hex
00330 << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
00331 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
00332 << "[RawFile] \tdata_block_number : 0x" << m_record.data_block_number << std::endl
00333 << "[RawFile] \tdata_block_size : 0x" << m_record.data_block_size
00334 << std::dec << std::endl;
00335 }
00336
00337 void FileEndRecord::dump(std::ostream& os) const {
00338 os << "[RawFile] FileEndRecord:" << std::endl << std::hex
00339 << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
00340 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
00341 << "[RawFile] \tdate : 0x" << m_record.date << std::endl
00342 << "[RawFile] \ttime : 0x" << m_record.time << std::endl
00343 << "[RawFile] \tevents_in_file : 0x" << m_record.events_in_file << std::endl
00344 << "[RawFile] \tdata_in_file : 0x" << m_record.data_in_file << std::endl
00345 << "[RawFile] \tevents_in_run : 0x" << m_record.events_in_run << std::endl
00346 << "[RawFile] \tdata_in_run : 0x" << m_record.data_in_run << std::endl
00347 << "[RawFile] \tstatus : 0x" << m_record.status << std::endl
00348 << "[RawFile] \tend_marker : 0x" << m_record.end_marker
00349 << std::dec << std::endl;
00350 }