00001 00032 #ifndef PARSER_H 00033 #define PARSER_H 00034 00035 // #define MAX_STR_LEN 4096 00036 00037 #include <itpp/base/vec.h> 00038 #include <itpp/base/mat.h> 00039 #include <itpp/base/array.h> 00040 #include <iostream> 00041 00042 00043 namespace itpp 00044 { 00045 00103 class Parser 00104 { 00105 public: 00106 00108 Parser(); 00109 00111 Parser(const std::string &filename); 00112 00114 Parser(int argc, char *argv[]); 00115 00117 Parser(const std::string &filename, int argc, char *argv[]); 00118 00120 Parser(const Array<std::string> &setup); 00121 00123 void init(const std::string &filename); 00124 00126 void init(int argc, char *argv[]); 00127 00129 void init(const std::string &filename, int argc, char *argv[]); 00130 00132 void init(const Array<std::string> &setup); 00133 00135 void set_silentmode(bool v = true); 00136 00138 bool exist(const std::string &name); 00139 00141 template<class T> 00142 bool get(T &var, const std::string &name, int num = -1); 00143 00145 bool get_bool(const std::string &name, int num = -1); 00146 00148 int get_int(const std::string &name, int num = -1); 00149 00151 double get_double(const std::string &name, int num = -1); 00152 00154 std::string get_string(const std::string &name, int num = -1); 00155 00157 vec get_vec(const std::string &name, int num = -1); 00158 00160 ivec get_ivec(const std::string &name, int num = -1); 00161 00163 svec get_svec(const std::string &name, int num = -1); 00164 00166 bvec get_bvec(const std::string &name, int num = -1); 00167 00169 mat get_mat(const std::string &name, int num = -1); 00170 00172 imat get_imat(const std::string &name, int num = -1); 00173 00175 smat get_smat(const std::string &name, int num = -1); 00176 00178 bmat get_bmat(const std::string &name, int num = -1); 00179 00180 protected: 00181 00182 private: 00183 00185 std::string findname(const std::string &name, 00186 bool &error_flag, 00187 bool &print_flag, 00188 int num = 0, 00189 bool keep_brackets = false); 00190 00191 void pre_parsing(void); 00192 00193 Array<std::string> SetupStrings; 00194 00195 bool VERBOSE; 00196 }; 00197 00198 // ----------------------- Implementation starts here ----------------------- 00199 00200 template<class T> 00201 bool Parser::get(T &var, const std::string &name, int num) 00202 { 00203 bool error_flag, print_flag; 00204 std::string str = findname(name, error_flag, print_flag, num, true); 00205 std::istringstream buffer(str); 00206 if (error_flag) { 00207 if (VERBOSE) { 00208 std::cout << name << " = " << var << ";" << std::endl; 00209 } 00210 } 00211 else { 00212 buffer >> var; 00213 if (print_flag) { 00214 std::cout << name << " = " << var << std::endl; 00215 } 00216 else if (VERBOSE) { 00217 std::cout << name << " = " << var << ";" << std::endl; 00218 } 00219 } 00220 return !error_flag; 00221 } 00222 00224 template<> 00225 bool Parser::get(std::string &var, const std::string &name, int num); 00227 template<> 00228 bool Parser::get(int &var, const std::string &name, int num); 00230 template<> 00231 bool Parser::get(bool &var, const std::string &name, int num); 00232 00233 } // namespace itpp 00234 00235 #endif // #ifndef PARSER_H
Generated on Thu Apr 23 20:04:04 2009 for IT++ by Doxygen 1.5.8