CMS 3D CMS Logo

IO.cc

Go to the documentation of this file.
00001 #include "RecoParticleFlow/PFRootEvent/interface/IO.h"
00002 #include "RecoParticleFlow/PFRootEvent/interface/Utils.h"
00003 
00004 using namespace std;
00005 
00006 const unsigned IO::sLinesize = 1000;
00007 
00008 IO::IO(const char* filepattern) :  fCurline(0) {
00009   // loop on all files matching pattern, store each non empty line in 
00010   // fAllLines for efficient searches later.
00011   cout<<endl;
00012   cout<<"------ Reading User Parameters : "<<filepattern<<endl;
00013 
00014   vector<string> files = Utils::Glob(filepattern);
00015   
00016   if( files.empty() ) {
00017     string err = "IO::IO : no files verify pattern ";
00018     err += filepattern;
00019     throw err;
00020   }
00021 
00022   for(unsigned i=0; i<files.size(); i++) {
00023     ParseFile(files[i].c_str());
00024   }
00025   cout<<"------ Reading User Parameters : DONE ---------"<<endl;
00026   cout<<endl;
00027 
00028 }
00029 
00030 bool IO::ParseFile(const char* filename) {
00031   cout<<"file : "<<filename<<"\t\t";
00032 
00033   ifstream in(filename);
00034   if( !in.good() ) {
00035     cout<<"unreadable"<<endl;
00036     return false;
00037   }
00038 
00039   char data[sLinesize];
00040   char s[sLinesize];
00041   int pos=0;
00042 
00043   do { 
00044     in.seekg(pos);
00045     in.getline(s,sLinesize);
00046     
00047     pos = in.tellg();     
00048  
00049     if(string(s).empty()) {
00050       continue; // remove empty lines
00051     }
00052 
00053     istringstream lin(s);  
00054 
00055     string tag;
00056     lin>>tag;
00057 
00058     if(!strncmp(tag.c_str(),"//",2)) continue; // remove commented lines can be done better ...
00059 
00060     lin.get(data,sLinesize);
00061     
00062     fAllLines.push_back(pair<string, string>(tag, data));
00063   } while(in.good());
00064   
00065   if(in.eof()) {
00066     cout<<"ok"<<endl;
00067     return true;
00068   }
00069   else {
00070     cout<<"error"<<endl;
00071     return false;
00072   }
00073 }
00074 
00075 void IO::Dump(ostream& out) const {
00076   for (unsigned i=0; i<fAllLines.size(); i++) {
00077     out<<fAllLines[i].first<< "\t" << fAllLines[i].second << endl; 
00078   } 
00079 }
00080 
00081 ostream& operator<<(ostream& out, IO& io) {
00082   if(!out) return out;
00083   io.Dump(out);
00084   return out;
00085 }
00086 
00087 
00088 string IO::GetLineData(const char* tag, const char* key) const {
00089   // if tag matches several option lines, data is the data corresponding
00090   // to the last tag
00091 
00092   char data[sLinesize];
00093   bool found = false;
00094   for(unsigned i=0; i<fAllLines.size(); i++) {
00095     if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { 
00096       istringstream in(fAllLines[i].second.c_str());
00097       string readkey; in>>readkey;
00098       
00099       if(readkey == key) {
00100         //      data.erase();
00101         //      string skey = key;
00102         //      int start = pos+skey.size();
00103         //      data.assign(fAllLines[i].second, start, data.size()-start);
00104         found=true;
00105         in.get(data,sLinesize);
00106       }
00107     }
00108   }
00109   if(found) return string(data);
00110   else return string();
00111 }
00112 
00113 string IO::GetNextLineData(const char* tag, const char* key)  {
00114 
00115   if(fCurtag != tag || fCurkey != key) {
00116     // not the same request
00117     fCurline = 0;
00118     fCurtag = tag;
00119     fCurkey = key;
00120   }
00121   // cout<<fCurline<<" "<<fCurtag<<" "<<fCurkey<<endl;
00122 
00123   char data[sLinesize];
00124   bool found = false;
00125   for(unsigned i=fCurline; i<fAllLines.size(); i++) {
00126     if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { 
00127       istringstream in(fAllLines[i].second.c_str());
00128       string readkey; in>>readkey;
00129       
00130       if(readkey == key) {
00131         found=true;
00132         in.get(data,sLinesize);
00133         fCurline=i+1;
00134         break;
00135       }
00136     }
00137   }
00138   if(found) return string(data);
00139   else return string();
00140 }
00141 
00142 
00143 bool IO::GetOpt(const char* tag, const char* key, string& value) const {
00144   string data = GetLineData(tag,key);
00145   
00146   char cstr[sLinesize];
00147   istringstream in(data.c_str());  
00148   in.get(cstr,sLinesize);
00149 
00150 
00151   value = cstr;
00152   if(!value.empty()) {
00153     // remove leading spaces
00154     int pos = value.find_first_not_of(" \t");
00155     value = value.substr(pos);
00156     // remove trailing spaces
00157     pos = value.find_last_not_of(" \t");
00158     value = value.substr(0,pos+1);
00159   }
00160   if(!value.empty()) return true;
00161   else return false;
00162 }
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 

Generated on Tue Jun 9 17:44:41 2009 for CMSSW by  doxygen 1.5.4