00001 00031 #ifndef PARSER_H 00032 #define PARSER_H 00033 00034 // #define MAX_STR_LEN 4096 00035 00036 #include <itpp/base/vec.h> 00037 #include <itpp/base/mat.h> 00038 #include <itpp/base/array.h> 00039 #include <iostream> 00040 00041 00042 namespace itpp 00043 { 00044 00102 class Parser 00103 { 00104 public: 00105 00107 Parser(); 00108 00110 Parser(const std::string &filename); 00111 00113 Parser(int argc, char *argv[]); 00114 00116 Parser(const std::string &filename, int argc, char *argv[]); 00117 00119 Parser(const Array<std::string> &setup); 00120 00122 void init(const std::string &filename); 00123 00125 void init(int argc, char *argv[]); 00126 00128 void init(const std::string &filename, int argc, char *argv[]); 00129 00131 void init(const Array<std::string> &setup); 00132 00134 void set_silentmode(bool v = true); 00135 00137 bool exist(const std::string &name); 00138 00140 template<class T> 00141 bool get(T &var, const std::string &name, int num = -1); 00142 00144 bool get_bool(const std::string &name, int num = -1); 00145 00147 int get_int(const std::string &name, int num = -1); 00148 00150 double get_double(const std::string &name, int num = -1); 00151 00153 std::string get_string(const std::string &name, int num = -1); 00154 00156 vec get_vec(const std::string &name, int num = -1); 00157 00159 ivec get_ivec(const std::string &name, int num = -1); 00160 00162 svec get_svec(const std::string &name, int num = -1); 00163 00165 bvec get_bvec(const std::string &name, int num = -1); 00166 00168 mat get_mat(const std::string &name, int num = -1); 00169 00171 imat get_imat(const std::string &name, int num = -1); 00172 00174 smat get_smat(const std::string &name, int num = -1); 00175 00177 bmat get_bmat(const std::string &name, int num = -1); 00178 00179 protected: 00180 00181 private: 00182 00184 std::string findname(const std::string &name, 00185 bool &error_flag, 00186 bool &print_flag, 00187 int num = 0, 00188 bool keep_brackets = false); 00189 00190 void pre_parsing(void); 00191 00192 Array<std::string> SetupStrings; 00193 00194 bool VERBOSE; 00195 }; 00196 00197 // ----------------------- Implementation starts here ----------------------- 00198 00199 template<class T> 00200 bool Parser::get(T &var, const std::string &name, int num) 00201 { 00202 bool error_flag, print_flag; 00203 std::string str = findname(name, error_flag, print_flag, num, true); 00204 std::istringstream buffer(str); 00205 if (error_flag) { 00206 if (VERBOSE) { 00207 std::cout << name << " = " << var << ";" << std::endl; 00208 } 00209 } 00210 else { 00211 buffer >> var; 00212 if (print_flag) { 00213 std::cout << name << " = " << var << std::endl; 00214 } 00215 else if (VERBOSE) { 00216 std::cout << name << " = " << var << ";" << std::endl; 00217 } 00218 } 00219 return !error_flag; 00220 } 00221 00223 template<> 00224 bool Parser::get(std::string &var, const std::string &name, int num); 00226 template<> 00227 bool Parser::get(int &var, const std::string &name, int num); 00229 template<> 00230 bool Parser::get(bool &var, const std::string &name, int num); 00231 00232 } // namespace itpp 00233 00234 #endif // #ifndef PARSER_H
Generated on Wed Jul 27 2011 16:27:04 for IT++ by Doxygen 1.7.4