bin_io_handler.cc

Go to the documentation of this file.
00001 #ifndef BIN_IO_HANDLER_CC
00002 #define BIN_IO_HANDLER_CC
00003 
00004 #include "bin_io_handler.hh"
00005 
00006 bin_io_handler::bin_io_handler(FORMAT::INPUT_FILE type)
00007   : decoder_base(),  _format(type)
00008 {
00009   _name="bin_io_handler";
00010   _status=INIT;
00011   reset();
00012 }
00013 
00014 
00015 void bin_io_handler::reset(){
00016   
00017   //
00018   // Only function that can assign INIT status other 
00019   // than the constructor.
00020   //
00021   switch(_status){
00022   case INIT:
00023   case CLOSED:
00024     break;
00025   case OPENED:
00026   case OPERATING:
00027     close();
00028     break;
00029   }  
00030   _filename="";
00031   _nwords_in_file=0;
00032   _mode=READ;
00033   _status=INIT;
00034   _format=FORMAT::UNDEFINED;
00035   _eof=false;
00036   _single_word[0]=0x0;
00037 }
00038 
00039 bool bin_io_handler::open(){
00040   //
00041   // Only function that can assign OPENED status
00042   //
00043   if(_filename.size()==0) {
00044     Message::send(MSG::ERROR,__FUNCTION__,
00045           "File name is empty. Provide a file path+name.");
00046   }else if(_status==OPENED || _status==OPERATING) {
00047     sprintf(_buf,
00048         "File is open: %s ... you must close it first.",
00049         _filename.c_str());
00050     Message::send(MSG::ERROR,__FUNCTION__,_buf);
00051   }else{
00052     switch(_format){
00053     case FORMAT::UNDEFINED:
00054       Message::send(MSG::ERROR, __FUNCTION__,
00055             "File format unspecified. Cannot open a file.");
00056       break;
00057     case FORMAT::BINARY:
00058       if(_mode==READ) _handler=fopen(_filename.c_str(),"rb");
00059       else            _handler=fopen(_filename.c_str(),"wb");
00060       break;
00061     case FORMAT::ASCII:
00062       if(_mode==READ) _handler=fopen(_filename.c_str(),"r");
00063       else            _handler=fopen(_filename.c_str(),"w");
00064       break;
00065     }
00066   }
00067 
00068   if(!(_handler)){
00069     sprintf(_buf,"Failed to open a file: %s",_filename.c_str());
00070     Message::send(MSG::ERROR,__FUNCTION__,_buf);
00071     return false;
00072   }
00073 
00074   sprintf(_buf,"Opened a file: %s",_filename.c_str());
00075   Message::send(MSG::NORMAL,__PRETTY_FUNCTION__,_buf);
00076 
00077   _eof = false;
00078   _status=OPENED;
00079   return true;
00080 }
00081 
00082 bool bin_io_handler::is_open() const{
00083   switch(_status){
00084   case OPENED:
00085   case OPERATING:
00086     return true;
00087   case INIT:
00088   case CLOSED:
00089     break;
00090   }
00091   return false;
00092 }
00093 
00094 void bin_io_handler::close(){
00095   //
00096   // Only function that can assign CLOSED status.
00097   //
00098   switch(_status){
00099   case INIT:
00100     Message::send(MSG::ERROR,__FUNCTION__,"File not yet opened!");
00101     break;
00102   case CLOSED:
00103     Message::send(MSG::ERROR,__FUNCTION__,"File already closed!!");
00104     break;
00105   case OPENED:
00106     sprintf(_buf,"Closing: %s ... no I/O operation done.",_filename.c_str());
00107     Message::send(MSG::WARNING,__FUNCTION__,_buf);
00108     fclose(_handler);
00109     break;
00110   case OPERATING:
00111     sprintf(_buf,"Closing: %s ... %d words processed",
00112         _filename.c_str(),
00113         _nwords_in_file);
00114     Message::send(MSG::NORMAL,__FUNCTION__,_buf);
00115     fclose(_handler);
00116     break;
00117   }
00118   _handler=0;
00119   _status=CLOSED;
00120 }
00121 
00122 bool bin_io_handler::write_word(const PMT::word_t word) {
00123 
00124   _single_word[0]=word;
00125 
00126   return write_multi_word(_single_word,1);
00127 }
00128 
00129 bool bin_io_handler::write_multi_word(const PMT::word_t* words, const size_t entries){
00130 
00131   //
00132   // Only function that can assign OPERATING status in WRITE mode.
00133   //
00134   if(_status!=OPENED && _status!=OPERATING) {
00135     Message::send(MSG::ERROR,__FUNCTION__,"Invalid file I/O status. Cannot write output!");
00136     return false;
00137   }
00138 
00139   if(_mode!=WRITE){
00140     Message::send(MSG::ERROR,__FUNCTION__,
00141           "Not allowed as this is output file stream!");
00142     return false;
00143   }
00144 
00145   if(_format==FORMAT::ASCII) {
00146 
00147     _nchars=0;
00148 
00149     for(UInt_t index=0; index<entries; ++index) {
00150       
00151       _nchars+=fprintf(_handler,"%x ",words[index]);
00152       
00153       _nwords_in_file++;
00154       
00155       _checksum+=words[index];
00156 
00157     }
00158 
00159     fprintf(_handler,"\n");
00160   }
00161   else {
00162     
00163     fwrite(&words[0],sizeof(PMT::word_t),entries,_handler);
00164 
00165   }
00166 
00167   return true;
00168 }
00169 
00170 PMT::word_t bin_io_handler::read_multi_word(size_t length){
00171 
00172   _word = PMT::EMPTY_WORD;
00173   if(_status!=OPENED && _status!=OPERATING) {
00174     Message::send(MSG::ERROR,__FUNCTION__,
00175           "Invalid file I/O status. Cannot read!");
00176     return _word;
00177   }
00178 
00179   if(_mode!=READ){
00180     Message::send(MSG::ERROR,__FUNCTION__,
00181           "Not allowed as this is input file stream!");    
00182     return _word;
00183   }
00184 
00185   _status=OPERATING;
00186 
00187   // Check if a buffer holds words to be read out
00188   if(_multi_word_index < _read_word_buffer.size()) {
00189 
00190     _word = _read_word_buffer.at(_multi_word_index);
00191 
00192     _multi_word_index++;
00193 
00194     _nwords_in_file++;
00195 
00196     return _word;
00197   }
00198 
00199   if(feof(_handler)){
00200     Message::send(MSG::DEBUG,__FUNCTION__,"Reached EOF!");
00201     _eof=true;
00202     return PMT::EMPTY_WORD;
00203   }
00204 
00205   size_t nwords_to_read = length;
00206   size_t nwords_read    = 0;
00207   
00208   if(!(nwords_to_read)) nwords_to_read = read_word();
00209  
00210   if(_format==FORMAT::ASCII){
00211 
00212     _read_word_buffer.clear();
00213 
00214     _read_word_buffer.reserve(nwords_to_read);
00215 
00216     for(nwords_read=0; nwords_read<nwords_to_read && !_eof; nwords_read++){
00217 
00218       if(fscanf(_handler,"%x%n",&_word,&_nchars)<1) {
00219     Message::send(MSG::DEBUG,__FUNCTION__,"Reached EOF!");
00220     _eof=true;
00221       }
00222 
00223       while(!_eof && _nchars<8  ) {
00224     if(_verbosity_level<=MSG::WARNING){
00225       sprintf(_buf,"Encountered none 32-bit word expression: %x (%d chars)",_word,_nchars);
00226       Message::send(MSG::WARNING,__FUNCTION__,_buf);
00227     }
00228     
00229     _eof=feof(_handler);
00230     
00231     if(fscanf(_handler,"%x%n",&_word,&_nchars)<1) {
00232       _eof=true;
00233       break;
00234     }
00235       }
00236       
00237       if(!_eof) _read_word_buffer.push_back(_word);
00238     }
00239 
00240   }
00241   else{
00242 
00243     _read_word_buffer.clear();
00244     _read_word_buffer.resize(nwords_to_read,0);
00245     
00246     nwords_read=fread(&_read_word_buffer[0],sizeof(_word),nwords_to_read,_handler);
00247 
00248     if(nwords_read < nwords_to_read) {
00249 
00250       _eof = true;
00251 
00252       _read_word_buffer.resize(nwords_read);
00253 
00254     }
00255   }
00256 
00257   if(_verbosity[MSG::INFO]){
00258     
00259     sprintf(_buf,"Read-in %zu/%zu words from the file...",nwords_read,nwords_to_read);
00260     Message::send(MSG::INFO,__FUNCTION__,_buf);
00261   }
00262 
00263   _multi_word_index = 0;
00264 
00265   _word = _read_word_buffer[_multi_word_index];
00266 
00267   _multi_word_index++;
00268 
00269   return _word;
00270 }
00271 
00272 PMT::word_t bin_io_handler::read_word() {
00273 
00274   if(_eof) return PMT::EMPTY_WORD;
00275 
00276   _word=PMT::EMPTY_WORD;
00277   _nchars=PMT::EMPTY_WORD;
00278 
00279   if(_status!=OPENED && _status!=OPERATING) {
00280     Message::send(MSG::ERROR,__FUNCTION__,
00281           "Invalid file I/O status. Cannot read!");
00282     return _word;
00283   }
00284 
00285   if(_mode!=READ){
00286     Message::send(MSG::ERROR,__FUNCTION__,
00287           "Not allowed as this is input file stream!");    
00288     return _word;
00289   }
00290 
00291   _status=OPERATING;
00292 
00293   //
00294   // Different handling for ASCII v.s. BIN
00295   //
00296   if(feof(_handler)){
00297     Message::send(MSG::DEBUG,__FUNCTION__,"Reached EOF!");
00298     _eof=true;
00299     return PMT::EMPTY_WORD;
00300   }
00301 
00302   // Check if fscanf returns < 1 ... then it is EOF
00303   if(_format==FORMAT::ASCII){
00304 
00305     if(fscanf(_handler,"%x%n",&_word,&_nchars)<1) {
00306       Message::send(MSG::DEBUG,__FUNCTION__,"Reached EOF!");
00307       _eof=true;
00308     }
00309 
00310     while(!_eof && _nchars<8  ) {
00311       if(_verbosity_level<=MSG::WARNING){
00312     sprintf(_buf,"Encountered none 32-bit word expression: %x (%d chars)",_word,_nchars);
00313     Message::send(MSG::WARNING,__FUNCTION__,_buf);
00314       }
00315 
00316       _eof=feof(_handler);
00317       
00318       if(fscanf(_handler,"%x%n",&_word,&_nchars)<1) {
00319     _eof=true;
00320     break;
00321       }
00322     }
00323   }
00324   else
00325     _eof=!(fread(&_word,sizeof(_word),1,_handler));
00326   
00327   if(_eof)
00328     _word=PMT::EMPTY_WORD;
00329   else{
00330     _checksum+=_word;
00331     _nwords_in_file+=1;
00332   }
00333 
00334   return _word;
00335 }
00336 
00337 #endif
00338 
00339 

Generated on Mon Apr 7 15:35:12 2014 for MyProject by  doxygen 1.4.7