// hepTupleFileType.cc #include "HepTuple/hepTupleFileType.h" #include "HepTuple/HepFileManager.h" #include "HepTuple/Exceptions.h" #include "Exceptions/ZMthrow.h" #include "ZMutility/fstream" ZM_BEGIN_NAMESPACE( zmht ) /* namespace zmht { */ // ------------------------- // Ways of identifying files: // -------------------------- bool fileByKey ( std::ifstream & infile , const std::string & key) { int keylen = key.length(); char * c = new char[keylen+1]; infile.get ( c, keylen+1 ); return ( std::string (c) == key ); } bool fileIsRoot ( std::ifstream & infile ) { return fileByKey ( infile , "root" ); } bool fileIsHistoscope ( std::ifstream & infile ) { return fileByKey ( infile , "HISTOSCOPE" ); } bool fileIsHbook ( std::ifstream & infile ) { // Not knowing how to identify an Hbook file, we place the test last, // and just assume any file that is neither root nor histoscope and // that passes our Hbook plausibility test is Hbook. char c[17]; infile.get ( c, 17 ); for (int i=0;i<16;i++) { if ( (c[i] != 0) && ( (i != 7) || (c[i] != 4) ) ) return false; } return true; } // ------------------------- // hepTupleFileType // -------------------------- HepTupleFileFormat hepTupleFileType (const std::string & name) { { std::ifstream in(name.c_str(),std::ios::in ); if ( (!in) || (!in.good()) ) { ZMthrow ( ZMxHepCantOpenFile(name) ); return HEPTUPLE_CANTOPEN; } } // // Detect the file format. // Since the ifstream goes out of scope after each // instantiation, the file closes after each open. // { std::ifstream in(name.c_str() ); if ( fileIsRoot ( in ) ) { return HEPTUPLE_ROOT; } } { std::ifstream in(name.c_str() ); if ( fileIsHistoscope ( in ) ) { return HEPTUPLE_HISTOSCOPE; } } { // Place test for HBOOK last since we have no positive identification std::ifstream in(name.c_str() ); if ( fileIsHbook ( in ) ) { return HEPTUPLE_HBOOK; } } return HEPTUPLE_UNRECOGNIZED; } // ------------------------- // hepTupleFileType (manager) // -------------------------- HepTupleFileFormat hepTupleFileType (const HepFileManager * manager) { return manager->hepFileFormat(); } HepTupleFileFormat hepTupleFileType (const HepFileManager & manager) { return manager.hepFileFormat(); } ZM_END_NAMESPACE( zmht ) /* } // namespace zmht */