#include <RecoParticleFlow/PFClusterTools/interface/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, T &value) |
reads a T | |
template<class T> | |
bool | GetAllOpt (const char *tag, const char *key, std::vector< T > &values) |
reads a vector of 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, T &value) const |
reads a T | |
template<class T> | |
bool | GetOpt (const char *tag, const char *key, std::vector< T > &values) const |
reads a vector of 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 |
Definition at line 29 of file IO.h.
IO::IO | ( | const char * | filepattern | ) |
builds IO from files matching filepattern
Definition at line 9 of file IO.cc.
References GenMuonPlsPt100GeV_cfg::cout, lat::endl(), err, translate::files, Utils::Glob(), i, and ParseFile().
00009 : fCurline(0) { 00010 // loop on all files matching pattern, store each non empty line in 00011 // fAllLines for efficient searches later. 00012 cout<<endl; 00013 cout<<"------ Reading User Parameters : "<<filepattern<<endl; 00014 00015 vector<string> files = Utils::Glob(filepattern); 00016 00017 if( files.empty() ) { 00018 string err = "IO::IO : no files verify pattern "; 00019 err += filepattern; 00020 throw err; 00021 } 00022 00023 for(unsigned i=0; i<files.size(); i++) { 00024 ParseFile(files[i].c_str()); 00025 } 00026 cout<<"------ Reading User Parameters : DONE ---------"<<endl; 00027 cout<<endl; 00028 00029 }
pftools::IO::~IO | ( | ) | [inline] |
void pftools::IO::Dump | ( | std::ostream & | out = std::cout |
) | const |
bool IO::GetAllOpt | ( | const char * | tag, | |
const char * | key, | |||
T & | value | |||
) | [inline] |
reads a T
this function allows to read several lines with the same tag/key:
Definition at line 138 of file IO.h.
References data, GetNextLineData(), and in.
00138 { 00139 std::string data = GetNextLineData(tag, key); 00140 if(data.empty()) return false; 00141 00142 std::istringstream in(data.c_str()); 00143 in>>value; 00144 if(in) { 00145 return true; 00146 } 00147 else { 00148 return false; 00149 } 00150 }
bool IO::GetAllOpt | ( | const char * | tag, | |
const char * | key, | |||
std::vector< T > & | values | |||
) | [inline] |
string IO::GetLineData | ( | const char * | tag, | |
const char * | key | |||
) | const |
Definition at line 89 of file IO.cc.
References data, fAllLines, first, i, in, edm::second(), and sLinesize.
Referenced by GetOpt().
00089 { 00090 // if tag matches several option lines, data is the data corresponding 00091 // to the last tag 00092 00093 char data[sLinesize]; 00094 bool found = false; 00095 for(unsigned i=0; i<fAllLines.size(); i++) { 00096 if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { 00097 istringstream in(fAllLines[i].second.c_str()); 00098 string readkey; in>>readkey; 00099 00100 if(readkey == key) { 00101 // data.erase(); 00102 // string skey = key; 00103 // int start = pos+skey.size(); 00104 // data.assign(fAllLines[i].second, start, data.size()-start); 00105 found=true; 00106 in.get(data,sLinesize); 00107 } 00108 } 00109 } 00110 if(found) return string(data); 00111 else return string(); 00112 }
string IO::GetNextLineData | ( | const char * | tag, | |
const char * | key | |||
) |
Definition at line 114 of file IO.cc.
References data, fAllLines, fCurkey, fCurline, fCurtag, first, i, in, edm::second(), and sLinesize.
Referenced by GetAllOpt().
00114 { 00115 00116 if(fCurtag != tag || fCurkey != key) { 00117 // not the same request 00118 fCurline = 0; 00119 fCurtag = tag; 00120 fCurkey = key; 00121 } 00122 // cout<<fCurline<<" "<<fCurtag<<" "<<fCurkey<<endl; 00123 00124 char data[sLinesize]; 00125 bool found = false; 00126 for(unsigned i=fCurline; i<fAllLines.size(); i++) { 00127 if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { 00128 istringstream in(fAllLines[i].second.c_str()); 00129 string readkey; in>>readkey; 00130 00131 if(readkey == key) { 00132 found=true; 00133 in.get(data,sLinesize); 00134 fCurline=i+1; 00135 break; 00136 } 00137 } 00138 } 00139 if(found) return string(data); 00140 else return string(); 00141 }
bool pftools::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 [inline] |
bool IO::GetOpt | ( | const char * | tag, | |
const char * | key, | |||
std::vector< T > & | values | |||
) | const [inline] |
reads a vector of T
Definition at line 108 of file IO.h.
References data, GetLineData(), in, and tmp.
Referenced by pftools::Exercises2::calibrateCalibratables(), pftools::Exercises3::calibrateCalibratables(), pftools::Exercises2::determineCorrection(), pftools::Exercises3::evaluateCalibrator(), pftools::Exercises2::evaluateCalibrator(), pftools::Exercises3::evaluateSpaceManager(), pftools::Exercises2::evaluateSpaceManager(), pftools::Exercises2::Exercises2(), and pftools::Exercises3::Exercises3().
00108 { 00109 std::string data = GetLineData(tag,key); 00110 00111 std::istringstream in(data.c_str()); 00112 while(1) { 00113 T tmp; 00114 in>>tmp; 00115 if(!in) break; 00116 values.push_back(tmp); 00117 } 00118 00119 return true; 00120 }
bool pftools::IO::IsZombie | ( | ) | const [inline] |
bool IO::ParseFile | ( | const char * | filename | ) | [private] |
parse one file
Definition at line 31 of file IO.cc.
References GenMuonPlsPt100GeV_cfg::cout, data, empty, lat::endl(), fAllLines, in, s, sLinesize, and ecalRecalibSequence_cff::tag.
Referenced by IO().
00031 { 00032 cout<<"file : "<<filename<<"\t\t"; 00033 00034 ifstream in(filename); 00035 if( !in.good() ) { 00036 cout<<"unreadable"<<endl; 00037 return false; 00038 } 00039 00040 char data[sLinesize]; 00041 char s[sLinesize]; 00042 int pos=0; 00043 00044 do { 00045 in.seekg(pos); 00046 in.getline(s,sLinesize); 00047 00048 pos = in.tellg(); 00049 00050 if(string(s).empty()) { 00051 continue; // remove empty lines 00052 } 00053 00054 istringstream lin(s); 00055 00056 string tag; 00057 lin>>tag; 00058 00059 if(!strncmp(tag.c_str(),"//",2)) continue; // remove commented lines can be done better ... 00060 00061 lin.get(data,sLinesize); 00062 00063 fAllLines.push_back(pair<string, string>(tag, data)); 00064 } while(in.good()); 00065 00066 if(in.eof()) { 00067 cout<<"ok"<<endl; 00068 return true; 00069 } 00070 else { 00071 cout<<"error"<<endl; 00072 return false; 00073 } 00074 }
std::ostream& operator<< | ( | std::ostream & | out, | |
IO & | io | |||
) | [friend] |
dumps fAllLines
std::vector< std::pair< std::string, std::string> > pftools::IO::fAllLines [private] |
all non empty, uncommented lines
Definition at line 32 of file IO.h.
Referenced by GetLineData(), GetNextLineData(), IsZombie(), ParseFile(), and ~IO().
std::string pftools::IO::fCurkey [private] |
int pftools::IO::fCurline [private] |
std::string pftools::IO::fCurtag [private] |
const unsigned IO::sLinesize = 1000 [static] |
maximum line size
Definition at line 49 of file IO.h.
Referenced by GetLineData(), GetNextLineData(), and ParseFile().