IT++ Logo

parser.h

Go to the documentation of this file.
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 
00102   class Parser {
00103   public:
00104 
00106     Parser();
00107 
00109     Parser(const std::string &filename);
00110 
00112     Parser(int argc, char *argv[]);
00113 
00115     Parser(const std::string &filename, int argc, char *argv[]);
00116 
00118     Parser(const Array<std::string> &setup);
00119 
00121     void init(const std::string &filename);
00122 
00124     void init(int argc, char *argv[]);
00125 
00127     void init(const std::string &filename, int argc, char *argv[]);
00128 
00130     void init(const Array<std::string> &setup);
00131 
00133     void set_silentmode(bool v=true);
00134 
00136     bool exist(const std::string &name);
00137 
00139     template<class T>
00140     bool get(T &var, const std::string &name, int num=-1);
00141 
00143     bool get_bool(const std::string &name,  int num=-1);
00144 
00146     int get_int(const std::string &name,  int num=-1);
00147 
00149     double get_double(const std::string &name, int num=-1);
00150 
00152     std::string get_string(const std::string &name, int num=-1);
00153 
00155     vec get_vec(const std::string &name, int num=-1);
00156 
00158     ivec get_ivec(const std::string &name, int num=-1);
00159 
00161     svec get_svec(const std::string &name, int num=-1);
00162 
00164     bvec get_bvec(const std::string &name, int num=-1);
00165 
00167     mat get_mat(const std::string &name, int num=-1);
00168 
00170     imat get_imat(const std::string &name, int num=-1);
00171 
00173     smat get_smat(const std::string &name, int num=-1);
00174 
00176     bmat get_bmat(const std::string &name, int num=-1);
00177 
00178   protected:
00179 
00180   private:
00181 
00183     std::string findname(const std::string &name,
00184        bool &error_flag,
00185        bool &print_flag,
00186        int num=0,
00187        bool keep_brackets=false);
00188 
00189     void pre_parsing(void);
00190 
00191     Array<std::string> SetupStrings;
00192 
00193     bool VERBOSE;
00194   };
00195 
00196   // ----------------------- Implementation starts here -----------------------
00197 
00198   template<class T>
00199   bool Parser::get(T &var, const std::string &name, int num)
00200   {
00201     bool error_flag, print_flag;
00202     std::string str = findname(name, error_flag, print_flag, num, true);
00203     std::istringstream buffer(str);
00204     if (error_flag) {
00205       if (VERBOSE) {
00206   std::cout << name << " = " << var << ";" << std::endl;
00207       }
00208     } else {
00209       buffer >> var;
00210       if (print_flag) {
00211   std::cout << name << " = " << var << std::endl;
00212       } else if (VERBOSE) {
00213   std::cout << name << " = " << var << ";" << std::endl;
00214       }
00215     }
00216     return !error_flag;
00217   }
00218 
00220   template<>
00221   bool Parser::get(std::string &var, const std::string &name, int num);
00223   template<>
00224   bool Parser::get(int &var, const std::string &name, int num);
00226   template<>
00227   bool Parser::get(bool &var, const std::string &name, int num);
00228 
00229 } // namespace itpp
00230 
00231 #endif // #ifndef PARSER_H
SourceForge Logo

Generated on Sun Dec 9 17:31:00 2007 for IT++ by Doxygen 1.5.4