CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IO.h
Go to the documentation of this file.
1 #ifndef __IO__
2 #define __IO__
3 
4 #include <string>
5 #include <vector>
6 
7 
8 #include <iostream>
9 #include <fstream>
10 // #include <strstream>
11 #include <sstream>
12 #include <iomanip>
13 
14 #ifndef __CINT__
15 #include <fnmatch.h>
16 #endif
17 
18 
28 class IO {
29  private:
31  std::vector< std::pair< std::string, std::string> > fAllLines;
32 
34  bool ParseFile(const char* filename);
35 
37  int fCurline;
38 
40  std::string fCurkey;
41 
43  std::string fCurtag;
44 
45 
46  public:
48  static const unsigned sLinesize;
49 
51  IO(const char* filepattern);
52  ~IO() {fAllLines.clear();}
53 
55  void Dump(std::ostream& out = std::cout) const;
56 
58  friend std::ostream& operator<<(std::ostream& out, IO& io);
59 
61  bool IsZombie() const {return !fAllLines.size();}
62 
63 #ifndef __CINT__
64  template <class T>
66  bool GetOpt(const char* tag, const char* key, std::vector< T >& values) const;
67 
69  template <class T>
70  bool GetOpt(const char* tag, const char* key, T& value) const;
71 
73  bool GetOpt(const char* tag, const char* key, std::string& value) const;
74 
82  template <class T>
83  bool GetAllOpt(const char* tag, const char* key, std::vector< T >& values);
84 
93  template <class T>
94  bool GetAllOpt(const char* tag, const char* key, T& value);
95 
96  std::string GetLineData(const char* tag, const char* key) const;
97  std::string GetNextLineData(const char* tag, const char* key);
98 
99 
100 #endif
101 };
102 
103 #ifndef __CINT__
104 
105 template <class T>
106 bool IO::GetOpt(const char* tag, const char* key, std::vector< T >& values) const {
107  std::string data = GetLineData(tag,key);
108 
109  std::istringstream in(data.c_str());
110  while(1) {
111  T tmp;
112  in>>tmp;
113  if(!in) break;
114  values.push_back(tmp);
115  }
116 
117  return true;
118 }
119 
120 template <class T>
121 bool IO::GetOpt(const char* tag, const char* key, T& value) const {
122  std::string data = GetLineData(tag,key);
123 
124  std::istringstream in(data.c_str());
125  in>>value;
126  if(in.good()) return true;
127  else return false;
128 }
129 
130 template <class T>
131 bool IO::GetAllOpt(const char* tag, const char* key, std::vector< T >& values) {
132  return false;
133 }
134 
135 template <class T>
136 bool IO::GetAllOpt(const char* tag, const char* key, T& value) {
137  std::string data = GetNextLineData(tag, key);
138  if(data.empty()) return false;
139 
140  std::istringstream in(data.c_str());
141  in>>value;
142  if(in) {
143  return true;
144  }
145  else {
146  return false;
147  }
148 }
149 
150 
151 #endif // __CINT__
152 
153 #endif
154 
155 
156 
157 
158 
159 
160 
161 
162 
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::string GetNextLineData(const char *tag, const char *key)
Definition: IO.cc:114
bool GetAllOpt(const char *tag, const char *key, std::vector< T > &values)
reads a vector of T
Definition: IO.h:131
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
~IO()
Definition: IO.h:52
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31
bool IsZombie() const
true if constructor went wrong
Definition: IO.h:61
tuple out
Definition: dbtoconf.py:99
friend std::ostream & operator<<(std::ostream &out, IO &io)
dumps fAllLines
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::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
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
long double T
IO(const char *filepattern)
builds IO from files matching filepattern
Definition: IO.cc:9