SatAndLight  2.2.2-hubble
Simulation toolkit for space telescopes
IO.h
Go to the documentation of this file.
1 #ifndef __IO__
2 #define __IO__
3 
10 #include "CUtils.h"
11 
12 using namespace std;
13 
19 class IO {
20 
21  public:
22 
43  IO(const string aFilePattern);
44 
48  virtual ~IO(void) {fAllLines.clear();}
49 
60  void Dump(ostream& out = cout) const;
61 
66  inline bool IsZombie(void) const {return !fAllLines.size();}
67 
68  friend ostream& operator<<(ostream& out, IO& io);
69 
78  template <class T>
79  bool GetOpt(const char* aTag, const char* aKey, vector< T >& aValues) const;
80 
89  template <class T>
90  bool GetOpt(const char* aTag, const char* aKey, T& aValue) const;
91 
100  bool GetOpt(const char* aTag, const char* aKey, string& aValue) const;
101 
108  string GetLineData(const char* aTag, const char* aKey) const;
109 
118  template <class T>
119  bool GetAllOpt(const char* aTag, const char* aKey, vector< T >& aValues);
128  template <class T>
129  bool GetAllOpt(const char* aTag, const char* aKey, T& aValue);
130 
131 
138  string GetNextLineData(const char* aTag, const char* aKey);
139 
140  private:
141 
142  vector< pair<string,string> > fAllLines;
143  bool ParseFile(const char* filename);
144  int fCurline;
145  string fCurkey;
146  string fCurtag;
147  static const unsigned sLinesize;
148 };
149 
150 template <class T>
151 bool IO::GetOpt(const char* tag, const char* key, vector< T >& values) const {
152  string data = GetLineData(tag,key);
153  values.clear();
154  istringstream in(data.c_str());
155  while(1) {
156  T tmp;
157  in>>tmp;
158  if(!in) break;
159  values.push_back(tmp);
160  }
161 
162  return (bool)values.size();
163 }
164 
165 template <class T>
166 bool IO::GetOpt(const char* tag, const char* key, T& value) const {
167  string data = GetLineData(tag,key);
168 
169  istringstream in(data.c_str());
170  in>>value;
171  return (!!in)&&data.size();
172 }
173 
174 template <class T>
175 bool IO::GetAllOpt(const char* tag, const char* key, vector< T >& values) {
176 
177  string data;
178  T tmp;
179  values.clear();
180  while(1) {// loop over lines
181  data=GetNextLineData(tag, key);
182  if(!data.compare("")) break;
183  istringstream inval(data.c_str());
184  while(1) {// loop over values
185  inval>>tmp;
186  if(!inval) break;
187  values.push_back(tmp);
188  }
189  inval.clear();
190  }
191 
192  return (bool)values.size();
193 }
194 
195 template <class T>
196 bool IO::GetAllOpt(const char* tag, const char* key, T& value) {
197  string data = GetNextLineData(tag, key);
198  if(data.empty()) return false;
199 
200  istringstream in(data.c_str());
201  in>>value;
202  if(in) {
203  return true;
204  }
205  else {
206  return false;
207  }
208 }
209 
210 #endif
This module offers C++ utility functions.
ostream & operator<<(ostream &out, IO &io)
Definition: IO.cc:66
Parse option files.
Definition: IO.h:19
string fCurtag
current tag
Definition: IO.h:146
bool GetOpt(const char *aTag, const char *aKey, vector< T > &aValues) const
Gets an option vector.
Definition: IO.h:151
bool IsZombie(void) const
Flags the parsing sequence.
Definition: IO.h:66
bool GetAllOpt(const char *aTag, const char *aKey, vector< T > &aValues)
Gets a vector of all options.
Definition: IO.h:175
string fCurkey
current key
Definition: IO.h:145
int fCurline
line counter
Definition: IO.h:144
static const unsigned sLinesize
maximum number of characters in a line
Definition: IO.h:147
vector< pair< string, string > > fAllLines
option lines
Definition: IO.h:142
virtual ~IO(void)
Destructor of the IO class.
Definition: IO.h:48