IT++ Logo Newcom Logo

parser.h

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

Generated on Thu Apr 19 14:19:52 2007 for IT++ by Doxygen 1.4.6