IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parser.h
Go to the documentation of this file.
1 
31 #ifndef PARSER_H
32 #define PARSER_H
33 
34 // #define MAX_STR_LEN 4096
35 
36 #include <itpp/base/vec.h>
37 #include <itpp/base/mat.h>
38 #include <itpp/base/array.h>
39 #include <iostream>
40 
41 
42 namespace itpp
43 {
44 
102 class Parser
103 {
104 public:
105 
107  Parser();
108 
110  Parser(const std::string &filename);
111 
113  Parser(int argc, char *argv[]);
114 
116  Parser(const std::string &filename, int argc, char *argv[]);
117 
119  Parser(const Array<std::string> &setup);
120 
122  void init(const std::string &filename);
123 
125  void init(int argc, char *argv[]);
126 
128  void init(const std::string &filename, int argc, char *argv[]);
129 
131  void init(const Array<std::string> &setup);
132 
134  void set_silentmode(bool v = true);
135 
137  bool exist(const std::string &name);
138 
140  template<class T>
141  bool get(T &var, const std::string &name, int num = -1);
142 
144  bool get_bool(const std::string &name, int num = -1);
145 
147  int get_int(const std::string &name, int num = -1);
148 
150  double get_double(const std::string &name, int num = -1);
151 
153  std::string get_string(const std::string &name, int num = -1);
154 
156  vec get_vec(const std::string &name, int num = -1);
157 
159  ivec get_ivec(const std::string &name, int num = -1);
160 
162  svec get_svec(const std::string &name, int num = -1);
163 
165  bvec get_bvec(const std::string &name, int num = -1);
166 
168  mat get_mat(const std::string &name, int num = -1);
169 
171  imat get_imat(const std::string &name, int num = -1);
172 
174  smat get_smat(const std::string &name, int num = -1);
175 
177  bmat get_bmat(const std::string &name, int num = -1);
178 
179 protected:
180 
181 private:
182 
184  std::string findname(const std::string &name,
185  bool &error_flag,
186  bool &print_flag,
187  int num = 0,
188  bool keep_brackets = false);
189 
190  void pre_parsing(void);
191 
192  Array<std::string> SetupStrings;
193 
194  bool VERBOSE;
195 };
196 
197 // ----------------------- Implementation starts here -----------------------
198 
199 template<class T>
200 bool Parser::get(T &var, const std::string &name, int num)
201 {
202  bool error_flag, print_flag;
203  std::string str = findname(name, error_flag, print_flag, num, true);
204  std::istringstream buffer(str);
205  if (error_flag) {
206  if (VERBOSE) {
207  std::cout << name << " = " << var << ";" << std::endl;
208  }
209  }
210  else {
211  buffer >> var;
212  if (print_flag) {
213  std::cout << name << " = " << var << std::endl;
214  }
215  else if (VERBOSE) {
216  std::cout << name << " = " << var << ";" << std::endl;
217  }
218  }
219  return !error_flag;
220 }
221 
223 template<>
224 bool Parser::get(std::string &var, const std::string &name, int num);
226 template<>
227 bool Parser::get(int &var, const std::string &name, int num);
229 template<>
230 bool Parser::get(bool &var, const std::string &name, int num);
231 
232 } // namespace itpp
233 
234 #endif // #ifndef PARSER_H
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:13 for IT++ by Doxygen 1.8.1.2