Go to the documentation of this file.00001 #include "RecoParticleFlow/PFRootEvent/interface/IO.h"
00002 #include "RecoParticleFlow/PFRootEvent/interface/Utils.h"
00003 #include <cstring>
00004
00005 using namespace std;
00006
00007 const unsigned IO::sLinesize = 1000;
00008
00009 IO::IO(const char* filepattern) : fCurline(0) {
00010
00011
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 }
00030
00031 bool IO::ParseFile(const char* filename) {
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;
00052 }
00053
00054 istringstream lin(s);
00055
00056 string tag;
00057 lin>>tag;
00058
00059 if(!strncmp(tag.c_str(),"//",2)) continue;
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 }
00075
00076 void IO::Dump(ostream& out) const {
00077 for (unsigned i=0; i<fAllLines.size(); i++) {
00078 out<<fAllLines[i].first<< "\t" << fAllLines[i].second << endl;
00079 }
00080 }
00081
00082 ostream& operator<<(ostream& out, IO& io) {
00083 if(!out) return out;
00084 io.Dump(out);
00085 return out;
00086 }
00087
00088
00089 string IO::GetLineData(const char* tag, const char* key) const {
00090
00091
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
00102
00103
00104
00105 found=true;
00106 in.get(data,sLinesize);
00107 }
00108 }
00109 }
00110 if(found) return string(data);
00111 else return string();
00112 }
00113
00114 string IO::GetNextLineData(const char* tag, const char* key) {
00115
00116 if(fCurtag != tag || fCurkey != key) {
00117
00118 fCurline = 0;
00119 fCurtag = tag;
00120 fCurkey = key;
00121 }
00122
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 }
00142
00143
00144 bool IO::GetOpt(const char* tag, const char* key, string& value) const {
00145 string data = GetLineData(tag,key);
00146
00147 char cstr[sLinesize];
00148 istringstream in(data.c_str());
00149 in.get(cstr,sLinesize);
00150
00151
00152 value = cstr;
00153 if(!value.empty()) {
00154
00155 int pos = value.find_first_not_of(" \t");
00156 value = value.substr(pos);
00157
00158 pos = value.find_last_not_of(" \t");
00159 value = value.substr(0,pos+1);
00160 }
00161 if(!value.empty()) return true;
00162 else return false;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174