CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IO.cc
Go to the documentation of this file.
3 #include <cstring>
4 
5 using namespace std;
6 
7 const unsigned IO::sLinesize = 1000;
8 
9 IO::IO(const char* filepattern) : fCurline(0) {
10  // loop on all files matching pattern, store each non empty line in
11  // fAllLines for efficient searches later.
12  cout<<endl;
13  cout<<"------ Reading User Parameters : "<<filepattern<<endl;
14 
15  vector<string> files = Utils::Glob(filepattern);
16 
17  if( files.empty() ) {
18  string err = "IO::IO : no files verify pattern ";
19  err += filepattern;
20  throw err;
21  }
22 
23  for(unsigned i=0; i<files.size(); i++) {
24  ParseFile(files[i].c_str());
25  }
26  cout<<"------ Reading User Parameters : DONE ---------"<<endl;
27  cout<<endl;
28 
29 }
30 
31 bool IO::ParseFile(const char* filename) {
32  cout<<"file : "<<filename<<"\t\t";
33 
34  ifstream in(filename);
35  if( !in.good() ) {
36  cout<<"unreadable"<<endl;
37  return false;
38  }
39 
40  char data[sLinesize];
41  char s[sLinesize];
42  int pos=0;
43 
44  do {
45  in.seekg(pos);
46  in.getline(s,sLinesize);
47 
48  pos = in.tellg();
49 
50  if(string(s).empty()) {
51  continue; // remove empty lines
52  }
53 
54  istringstream lin(s);
55 
56  string tag;
57  lin>>tag;
58 
59  if(!strncmp(tag.c_str(),"//",2)) continue; // remove commented lines can be done better ...
60 
61  lin.get(data,sLinesize);
62 
63  fAllLines.push_back(pair<string, string>(tag, data));
64  } while(in.good());
65 
66  if(in.eof()) {
67  cout<<"ok"<<endl;
68  return true;
69  }
70  else {
71  cout<<"error"<<endl;
72  return false;
73  }
74 }
75 
76 void IO::Dump(ostream& out) const {
77  for (unsigned i=0; i<fAllLines.size(); i++) {
78  out<<fAllLines[i].first<< "\t" << fAllLines[i].second << endl;
79  }
80 }
81 
82 ostream& operator<<(ostream& out, IO& io) {
83  if(!out) return out;
84  io.Dump(out);
85  return out;
86 }
87 
88 
89 string IO::GetLineData(const char* tag, const char* key) const {
90  // if tag matches several option lines, data is the data corresponding
91  // to the last tag
92 
93  char data[sLinesize];
94  bool found = false;
95  for(unsigned i=0; i<fAllLines.size(); i++) {
96  if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) {
97  istringstream in(fAllLines[i].second.c_str());
98  string readkey; in>>readkey;
99 
100  if(readkey == key) {
101  // data.erase();
102  // string skey = key;
103  // int start = pos+skey.size();
104  // data.assign(fAllLines[i].second, start, data.size()-start);
105  found=true;
106  in.get(data,sLinesize);
107  }
108  }
109  }
110  if(found) return string(data);
111  else return string();
112 }
113 
114 string IO::GetNextLineData(const char* tag, const char* key) {
115 
116  if(fCurtag != tag || fCurkey != key) {
117  // not the same request
118  fCurline = 0;
119  fCurtag = tag;
120  fCurkey = key;
121  }
122  // cout<<fCurline<<" "<<fCurtag<<" "<<fCurkey<<endl;
123 
124  char data[sLinesize];
125  bool found = false;
126  for(unsigned i=fCurline; i<fAllLines.size(); i++) {
127  if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) {
128  istringstream in(fAllLines[i].second.c_str());
129  string readkey; in>>readkey;
130 
131  if(readkey == key) {
132  found=true;
133  in.get(data,sLinesize);
134  fCurline=i+1;
135  break;
136  }
137  }
138  }
139  if(found) return string(data);
140  else return string();
141 }
142 
143 
144 bool IO::GetOpt(const char* tag, const char* key, string& value) const {
145  string data = GetLineData(tag,key);
146 
147  char cstr[sLinesize];
148  istringstream in(data.c_str());
149  in.get(cstr,sLinesize);
150 
151 
152  value = cstr;
153  if(!value.empty()) {
154  // remove leading spaces
155  int pos = value.find_first_not_of(" \t");
156  value = value.substr(pos);
157  // remove trailing spaces
158  pos = value.find_last_not_of(" \t");
159  value = value.substr(0,pos+1);
160  }
161  if(!value.empty()) return true;
162  else return false;
163 }
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
int i
Definition: DBlmapReader.cc:9
static std::vector< std::string > Glob(const char *pattern)
get all files matching pattern
Definition: Utils.cc:46
void Dump(std::ostream &out=std::cout) const
dumps fAllLines
Definition: IO.cc:76
General option file parser.
Definition: IO.h:28
static const unsigned sLinesize
maximum line size
Definition: IO.h:48
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
std::string GetNextLineData(const char *tag, const char *key)
Definition: IO.cc:114
int fCurline
counter
Definition: IO.h:37
bool GetOpt(const char *tag, const char *key, std::vector< T > &values) const
reads a vector of T
Definition: IO.h:106
U second(std::pair< T, U > const &p)
bool first
Definition: L1TdeRCT.cc:79
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31
tuple out
Definition: dbtoconf.py:99
std::string fCurtag
current tag
Definition: IO.h:43
bool ParseFile(const char *filename)
parse one file
Definition: IO.cc:31
std::string fCurkey
current key
Definition: IO.h:40
std::string GetLineData(const char *tag, const char *key) const
Definition: IO.cc:89
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
list key
Definition: combine.py:13
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:121
IO(const char *filepattern)
builds IO from files matching filepattern
Definition: IO.cc:9