00001 #include "RawFile/raw_ifstream.h" 00002 #include "IRawFile/RawFileExceptions.h" 00003 #include <cstdlib> 00004 00005 //static data members 00006 int raw_ifstream::_nHandler = 0; 00007 raw_ifstream* raw_ifstream::_instance = 0; 00008 pthread_mutex_t raw_ifstream::_pthread_lock = PTHREAD_MUTEX_INITIALIZER; 00009 00010 00011 raw_ifstream* raw_ifstream::instance(const std::vector<std::string>& fnames) 00012 { 00013 lock(); 00014 00015 if ( _instance == 0 ) { 00016 _instance = new raw_ifstream(fnames); 00017 } 00018 00019 ++_nHandler; 00020 00021 unlock(); 00022 00023 return _instance; 00024 } 00025 00026 void raw_ifstream::release() 00027 { 00028 lock(); 00029 00030 if ( _nHandler > 0 && --_nHandler == 0 ) { 00031 if ( _instance->m_isOpen ) { 00032 std::cout << "[RawFile] Closing: " << *(_instance->m_curFile) << std::endl; 00033 _instance->close(); 00034 } 00035 delete _instance; 00036 _instance = 0; 00037 } 00038 00039 unlock(); 00040 } 00041 00042 raw_ifstream::raw_ifstream(const std::vector<std::string>& fnames) 00043 : m_isOpen(false), m_fnames(fnames) 00044 { 00045 if ( m_fnames.empty() ) { 00046 throw RawExMessage("[RawFile] Empty input file list!"); 00047 } 00048 00049 m_curFile = m_fnames.begin(); 00050 init_fstream(); 00051 } 00052 00053 raw_ifstream::~raw_ifstream() 00054 { 00055 } 00056 00057 void raw_ifstream::next_file() 00058 { 00059 std::cout << "[RawFile] Closing: " << *m_curFile << std::endl; 00060 close(); 00061 m_isOpen = false; 00062 00063 if ( ++m_curFile == m_fnames.end() ) { 00064 --m_curFile; 00065 throw ReachEndOfFileList(); 00066 } 00067 00068 init_fstream(); 00069 } 00070 00071 uint32_t raw_ifstream::runNo() 00072 { 00073 return m_runParametersRecord.getRecord().run_number; 00074 } 00075 00076 void raw_ifstream::init_fstream() 00077 { 00078 if ( access( m_curFile->c_str(), F_OK ) < 0 ) { 00079 std::cerr << "[RawFile] Invalid file: " << *m_curFile << std::endl; 00080 exit(1); 00081 } 00082 00083 std::cout << "[RawFile] Prepare for reading: " << *m_curFile << std::endl; 00084 00085 open( m_curFile->c_str(), std::ios::binary ); 00086 m_isOpen = true; 00087 00088 try { 00089 (*this) >> m_fileStartRecord >> m_fileNameStrings >> m_runParametersRecord; 00090 } 00091 catch (RawFileException& e) { 00092 e.print(); 00093 exit(1); 00094 } 00095 }