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