General option file parser. More...
#include <IO.h>
Public Member Functions | |
void | Dump (std::ostream &out=std::cout) const |
dumps fAllLines | |
template<class T > | |
bool | GetAllOpt (const char *tag, const char *key, std::vector< T > &values) |
reads a vector of T | |
template<class T > | |
bool | GetAllOpt (const char *tag, const char *key, T &value) |
reads a T | |
std::string | GetLineData (const char *tag, const char *key) const |
std::string | GetNextLineData (const char *tag, const char *key) |
bool | GetOpt (const char *tag, const char *key, std::string &value) const |
reads a string | |
template<class T > | |
bool | GetOpt (const char *tag, const char *key, std::vector< T > &values) const |
reads a vector of T | |
template<class T > | |
bool | GetOpt (const char *tag, const char *key, T &value) const |
reads a T | |
IO (const char *filepattern) | |
builds IO from files matching filepattern | |
bool | IsZombie () const |
true if constructor went wrong | |
~IO () | |
Static Public Attributes | |
static const unsigned | sLinesize = 1000 |
maximum line size | |
Private Member Functions | |
bool | ParseFile (const char *filename) |
parse one file | |
Private Attributes | |
std::vector< std::pair < std::string, std::string > > | fAllLines |
all non empty, uncommented lines | |
std::string | fCurkey |
current key | |
int | fCurline |
counter | |
std::string | fCurtag |
current tag | |
Friends | |
std::ostream & | operator<< (std::ostream &out, IO &io) |
dumps fAllLines |
General option file parser.
IO io("myfile.opt"); fECALthresh = 0.05; io.GetOpt("ECAL", "threshold", fECALthresh);
IO::IO | ( | const char * | filepattern | ) |
builds IO from files matching filepattern
Definition at line 9 of file IO.cc.
References gather_cfg::cout, linker::files, Utils::Glob(), i, and ParseFile().
: fCurline(0) { // loop on all files matching pattern, store each non empty line in // fAllLines for efficient searches later. cout<<endl; cout<<"------ Reading User Parameters : "<<filepattern<<endl; vector<string> files = Utils::Glob(filepattern); if( files.empty() ) { string err = "IO::IO : no files verify pattern "; err += filepattern; throw err; } for(unsigned i=0; i<files.size(); i++) { ParseFile(files[i].c_str()); } cout<<"------ Reading User Parameters : DONE ---------"<<endl; cout<<endl; }
void IO::Dump | ( | std::ostream & | out = std::cout | ) | const |
dumps fAllLines
Referenced by operator<<().
bool IO::GetAllOpt | ( | const char * | tag, |
const char * | key, | ||
T & | value | ||
) |
reads a T
this function allows to read several lines with the same tag/key:
Definition at line 136 of file IO.h.
References runTheMatrix::data, GetNextLineData(), recoMuon::in, and relativeConstraints::value.
bool IO::GetAllOpt | ( | const char * | tag, |
const char * | key, | ||
std::vector< T > & | values | ||
) |
string IO::GetLineData | ( | const char * | tag, |
const char * | key | ||
) | const |
Definition at line 89 of file IO.cc.
References runTheMatrix::data, fAllLines, first, newFWLiteAna::found, i, recoMuon::in, edm::second(), sLinesize, and GlobalPosition_Frontier_DevDB_cff::tag.
Referenced by GetOpt().
{ // if tag matches several option lines, data is the data corresponding // to the last tag char data[sLinesize]; bool found = false; for(unsigned i=0; i<fAllLines.size(); i++) { if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { istringstream in(fAllLines[i].second.c_str()); string readkey; in>>readkey; if(readkey == key) { // data.erase(); // string skey = key; // int start = pos+skey.size(); // data.assign(fAllLines[i].second, start, data.size()-start); found=true; in.get(data,sLinesize); } } } if(found) return string(data); else return string(); }
string IO::GetNextLineData | ( | const char * | tag, |
const char * | key | ||
) |
Definition at line 114 of file IO.cc.
References runTheMatrix::data, fAllLines, fCurkey, fCurline, fCurtag, first, newFWLiteAna::found, i, recoMuon::in, combine::key, edm::second(), sLinesize, and GlobalPosition_Frontier_DevDB_cff::tag.
Referenced by GetAllOpt().
{ if(fCurtag != tag || fCurkey != key) { // not the same request fCurline = 0; fCurtag = tag; fCurkey = key; } // cout<<fCurline<<" "<<fCurtag<<" "<<fCurkey<<endl; char data[sLinesize]; bool found = false; for(unsigned i=fCurline; i<fAllLines.size(); i++) { if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { istringstream in(fAllLines[i].second.c_str()); string readkey; in>>readkey; if(readkey == key) { found=true; in.get(data,sLinesize); fCurline=i+1; break; } } } if(found) return string(data); else return string(); }
bool IO::GetOpt | ( | const char * | tag, |
const char * | key, | ||
std::string & | value | ||
) | const |
reads a string
bool IO::GetOpt | ( | const char * | tag, |
const char * | key, | ||
T & | value | ||
) | const |
reads a T
Definition at line 121 of file IO.h.
References runTheMatrix::data, GetLineData(), recoMuon::in, and relativeConstraints::value.
bool IO::GetOpt | ( | const char * | tag, |
const char * | key, | ||
std::vector< T > & | values | ||
) | const |
reads a vector of T
Definition at line 106 of file IO.h.
References runTheMatrix::data, GetLineData(), recoMuon::in, and tmp.
Referenced by PFRootEventManager::connect(), PFRootEventManager::readOptions(), DisplayManager::readOptions(), and PFRootEventManagerColin::readSpecificOptions().
bool IO::IsZombie | ( | ) | const [inline] |
bool IO::ParseFile | ( | const char * | filename | ) | [private] |
parse one file
Definition at line 31 of file IO.cc.
References gather_cfg::cout, runTheMatrix::data, relativeConstraints::empty, fAllLines, recoMuon::in, pos, asciidump::s, sLinesize, and GlobalPosition_Frontier_DevDB_cff::tag.
Referenced by IO().
{ cout<<"file : "<<filename<<"\t\t"; ifstream in(filename); if( !in.good() ) { cout<<"unreadable"<<endl; return false; } char data[sLinesize]; char s[sLinesize]; int pos=0; do { in.seekg(pos); in.getline(s,sLinesize); pos = in.tellg(); if(string(s).empty()) { continue; // remove empty lines } istringstream lin(s); string tag; lin>>tag; if(!strncmp(tag.c_str(),"//",2)) continue; // remove commented lines can be done better ... lin.get(data,sLinesize); fAllLines.push_back(pair<string, string>(tag, data)); } while(in.good()); if(in.eof()) { cout<<"ok"<<endl; return true; } else { cout<<"error"<<endl; return false; } }
std::ostream& operator<< | ( | std::ostream & | out, |
IO & | io | ||
) | [friend] |
dumps fAllLines
std::vector< std::pair< std::string, std::string> > IO::fAllLines [private] |
all non empty, uncommented lines
Definition at line 31 of file IO.h.
Referenced by GetLineData(), GetNextLineData(), IsZombie(), ParseFile(), and ~IO().
std::string IO::fCurkey [private] |
int IO::fCurline [private] |
std::string IO::fCurtag [private] |
const unsigned IO::sLinesize = 1000 [static] |
maximum line size
Definition at line 48 of file IO.h.
Referenced by GetLineData(), GetNextLineData(), and ParseFile().