CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/RecoParticleFlow/PFClusterTools/src/IO.cc

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