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 namespace pftools {
29 class IO {
30  private:
32  std::vector< std::pair< std::string, std::string> > fAllLines;
33 
35  bool ParseFile(const char* filename);
36 
38  int fCurline;
39 
42 
45 
46 
47  public:
49  static const unsigned sLinesize;
50 
52  IO(const char* filepattern);
53  ~IO() {fAllLines.clear();}
54 
56  void Dump(std::ostream& out = std::cout) const;
57 
59  friend std::ostream& operator<<(std::ostream& out, IO& io);
60 
62  bool IsZombie() const {return !fAllLines.size();}
63 
64 #ifndef __CINT__
65  template <class T>
67  bool GetOpt(const char* tag, const char* key, std::vector< T >& values) const;
68 
70  template <class T>
71  bool GetOpt(const char* tag, const char* key, T& value) const;
72 
74  bool GetOpt(const char* tag, const char* key, std::string& value) const;
75 
83  template <class T>
84  bool GetAllOpt(const char* tag, const char* key, std::vector< T >& values);
85 
94  template <class T>
95  bool GetAllOpt(const char* tag, const char* key, T& value);
96 
97  std::string GetLineData(const char* tag, const char* key) const;
98  std::string GetNextLineData(const char* tag, const char* key);
99 
100 
101 #endif
102 };
103 }
104 
105 #ifndef __CINT__
106 
107 template <class T>
108 bool pftools::IO::GetOpt(const char* tag, const char* key, std::vector< T >& values) const {
109  std::string data = GetLineData(tag,key);
110 
111  std::istringstream in(data.c_str());
112  while(1) {
113  T tmp;
114  in>>tmp;
115  if(!in) break;
116  values.push_back(tmp);
117  }
118 
119  return true;
120 }
121 
122 template <class T>
123 bool pftools::IO::GetOpt(const char* tag, const char* key, T& value) const {
124  std::string data = GetLineData(tag,key);
125 
126  std::istringstream in(data.c_str());
127  in>>value;
128  if(in.good()) return true;
129  else return false;
130 }
131 
132 template <class T>
133 bool pftools::IO::GetAllOpt(const char* tag, const char* key, std::vector< T >& values) {
134  return false;
135 }
136 
137 template <class T>
138 bool pftools::IO::GetAllOpt(const char* tag, const char* key, T& value) {
139  std::string data = GetNextLineData(tag, key);
140  if(data.empty()) return false;
141 
142  std::istringstream in(data.c_str());
143  in>>value;
144  if(in) {
145  return true;
146  }
147  else {
148  return false;
149  }
150 }
151 
152 
153 #endif // __CINT__
154 
155 #endif
156 
157 
158 
159 
160 
161 
162 
163 
164 
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:32
bool IsZombie() const
true if constructor went wrong
Definition: IO.h:62
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
bool GetOpt(const char *tag, const char *key, std::vector< T > &values) const
reads a vector of T
Definition: IO.h:108
bool ParseFile(const char *filename)
parse one file
Definition: IO.cc:32
int fCurline
counter
Definition: IO.h:38
bool GetAllOpt(const char *tag, const char *key, std::vector< T > &values)
reads a vector of T
Definition: IO.h:133
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
friend std::ostream & operator<<(std::ostream &out, IO &io)
dumps fAllLines
Definition: IO.h:29
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::string GetLineData(const char *tag, const char *key) const
Definition: IO.cc:90
tuple filename
Definition: lut2db_cfg.py:20
~IO()
Definition: IO.h:53
void Dump(std::ostream &out=std::cout) const
dumps fAllLines
Definition: IO.cc:77
tuple cout
Definition: gather_cfg.py:145
long double T
IO(const char *filepattern)
builds IO from files matching filepattern
Definition: IO.cc:10
std::string GetNextLineData(const char *tag, const char *key)
Definition: IO.cc:115