Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

colvarparse.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 
00003 // This file is part of the Collective Variables module (Colvars).
00004 // The original version of Colvars and its updates are located at:
00005 // https://github.com/Colvars/colvars
00006 // Please update all Colvars source files before making any changes.
00007 // If you wish to distribute your changes, please submit them to the
00008 // Colvars repository at GitHub.
00009 
00010 #ifndef COLVARPARSE_H
00011 #define COLVARPARSE_H
00012 
00013 #include <cstring>
00014 #include <string>
00015 
00016 #include "colvarmodule.h"
00017 #include "colvarvalue.h"
00018 #include "colvarparams.h"
00019 
00020 
00022 
00023 
00026 class colvarparse : public colvarparams {
00027 
00028 public:
00029 
00031   colvarparse();
00032 
00034   colvarparse(const std::string& conf);
00035 
00037   void clear();
00038 
00040   void set_string(std::string const &conf);
00041 
00043   virtual ~colvarparse();
00044 
00046   inline std::string const & get_config() const
00047   {
00048     return config_string;
00049   }
00050 
00052   enum Parse_Mode {
00054     parse_null = 0,
00056     parse_echo = (1<<1),
00058     parse_echo_default = (1<<2),
00060     parse_deprecation_warning = (1<<3),
00062     parse_silent = 0,
00064     parse_required = (1<<16),
00067     parse_override = (1<<17),
00069     parse_restart = (1<<18),
00071     parse_normal = (1<<1) | (1<<2) | (1<<17),
00073     parse_deprecated = (1<<1) | (1<<3) | (1<<17)
00074   };
00075 
00079   int check_keywords(std::string &conf, char const *key);
00080 
00082   void clear_keyword_registry();
00083 
00108 
00109   bool get_keyval(std::string const &conf,
00110                   char const *key,
00111                   int &value,
00112                   int const &def_value = (int)0,
00113                   Parse_Mode const parse_mode = parse_normal);
00114   bool get_keyval(std::string const &conf,
00115                   char const *key,
00116                   size_t &value,
00117                   size_t const &def_value = (size_t)0,
00118                   Parse_Mode const parse_mode = parse_normal);
00119   bool get_keyval(std::string const &conf,
00120                   char const *key,
00121                   long &value,
00122                   long const &def_value = 0,
00123                   Parse_Mode const parse_mode = parse_normal);
00124   bool get_keyval(std::string const &conf,
00125                   char const *key,
00126                   cvm::step_number &value,
00127                   cvm::step_number const &def_value = 0,
00128                   Parse_Mode const parse_mode = parse_normal);
00129   bool get_keyval(std::string const &conf,
00130                   char const *key,
00131                   std::string &value,
00132                   std::string const &def_value = std::string(""),
00133                   Parse_Mode const parse_mode = parse_normal);
00134   bool get_keyval(std::string const &conf,
00135                   char const *key,
00136                   cvm::real &value,
00137                   cvm::real const &def_value = (cvm::real)0.0,
00138                   Parse_Mode const parse_mode = parse_normal);
00139   bool get_keyval(std::string const &conf,
00140                   char const *key,
00141                   cvm::rvector &value,
00142                   cvm::rvector const &def_value = cvm::rvector(),
00143                   Parse_Mode const parse_mode = parse_normal);
00144   bool get_keyval(std::string const &conf,
00145                   char const *key,
00146                   cvm::quaternion &value,
00147                   cvm::quaternion const &def_value = cvm::quaternion(),
00148                   Parse_Mode const parse_mode = parse_normal);
00149   bool get_keyval(std::string const &conf,
00150                   char const *key,
00151                   colvarvalue &value,
00152                   colvarvalue const &def_value = colvarvalue(colvarvalue::type_notset),
00153                   Parse_Mode const parse_mode = parse_normal);
00154   bool get_keyval(std::string const &conf,
00155                   char const *key,
00156                   bool &value,
00157                   bool const &def_value = false,
00158                   Parse_Mode const parse_mode = parse_normal);
00159   bool get_keyval(std::string const &conf,
00160                   char const *key,
00161                   std::vector<int> &values,
00162                   std::vector<int> const &def_values = std::vector<int>(0, (int)0),
00163                   Parse_Mode const parse_mode = parse_normal);
00164   bool get_keyval(std::string const &conf,
00165                   char const *key,
00166                   std::vector<size_t> &values,
00167                   std::vector<size_t> const &def_values = std::vector<size_t>(0, (size_t)0),
00168                   Parse_Mode const parse_mode = parse_normal);
00169   bool get_keyval(std::string const &conf,
00170                   char const *key,
00171                   std::vector<long> &values,
00172                   std::vector<long> const &def_values = std::vector<long>(0, (long)0),
00173                   Parse_Mode const parse_mode = parse_normal);
00174   bool get_keyval(std::string const &conf,
00175                   char const *key,
00176                   std::vector<std::string> &values,
00177                   std::vector<std::string> const &def_values = std::vector<std::string>(0, std::string("")),
00178                   Parse_Mode const parse_mode = parse_normal);
00179   bool get_keyval(std::string const &conf,
00180                   char const *key,
00181                   std::vector<cvm::real> &values,
00182                   std::vector<cvm::real> const &def_values = std::vector<cvm::real>(0, (cvm::real)0.0),
00183                   Parse_Mode const parse_mode = parse_normal);
00184   bool get_keyval(std::string const &conf,
00185                   char const *key,
00186                   std::vector<cvm::rvector> &values,
00187                   std::vector<cvm::rvector> const &def_values = std::vector<cvm::rvector>(0, cvm::rvector()),
00188                   Parse_Mode const parse_mode = parse_normal);
00189   bool get_keyval(std::string const &conf,
00190                   char const *key,
00191                   std::vector<cvm::quaternion> &values,
00192                   std::vector<cvm::quaternion> const &def_values = std::vector<cvm::quaternion>(0, cvm::quaternion()),
00193                   Parse_Mode const parse_mode = parse_normal);
00194   bool get_keyval(std::string const &conf,
00195                   char const *key,
00196                   std::vector<colvarvalue> &values,
00197                   std::vector<colvarvalue> const &def_values = std::vector<colvarvalue>(0, colvarvalue(colvarvalue::type_notset)),
00198                   Parse_Mode const parse_mode = parse_normal);
00199 
00200 protected:
00201 
00203   bool get_key_string_value(std::string const &conf,
00204                             char const *key, std::string &data);
00205 
00207   bool get_key_string_multi_value(std::string const &conf,
00208                                   char const *key, std::vector<std::string>& data);
00209 
00211   template<typename TYPE>
00212   bool _get_keyval_scalar_(std::string const &conf,
00213                            char const *key,
00214                            TYPE &value,
00215                            TYPE const &def_value,
00216                            Parse_Mode const &parse_mode);
00217 
00219   template<typename TYPE>
00220   bool _get_keyval_vector_(std::string const &conf,
00221                            char const *key,
00222                            std::vector<TYPE> &values,
00223                            std::vector<TYPE> const &def_values,
00224                            Parse_Mode const &parse_mode);
00225 
00227   template<typename TYPE>
00228   int _get_keyval_scalar_value_(std::string const &key_str,
00229                                 std::string const &data,
00230                                 TYPE &value,
00231                                 TYPE const &def_value);
00232 
00234   template<typename TYPE>
00235   int _get_keyval_scalar_novalue_(std::string const &key_str,
00236                                   TYPE &value,
00237                                   Parse_Mode const &parse_mode);
00238 
00240   template<typename TYPE>
00241   void mark_key_set_user(std::string const &key_str,
00242                          TYPE const &value,
00243                          Parse_Mode const &parse_mode);
00244 
00246   template<typename TYPE>
00247   void mark_key_set_default(std::string const &key_str,
00248                             TYPE const &def_value,
00249                             Parse_Mode const &parse_mode);
00250 
00252   void error_key_required(std::string const &key_str,
00253                           Parse_Mode const &parse_mode);
00254 
00256   bool key_already_set(std::string const &key_str);
00257 
00258 public:
00259 
00261   static inline std::string to_lower_cppstr(std::string const &in)
00262   {
00263     std::string out = "";
00264     for (size_t i = 0; i < in.size(); i++) {
00265       out.append(1, (char) ::tolower(in[i]) );
00266     }
00267     return out;
00268   }
00269 
00277   class read_block {
00278 
00280     std::string const key;
00281 
00283     std::string * const data;
00284 
00285   public:
00286 
00287     read_block(std::string const &key_in, std::string *data_in = NULL);
00288 
00289     ~read_block();
00290 
00291     friend std::istream & operator >> (std::istream &is, read_block const &rb);
00292   };
00293 
00294 
00296   static const char * const white_space;
00297 
00305   bool key_lookup(std::string const &conf,
00306                   char const *key,
00307                   std::string *data = NULL,
00308                   size_t *save_pos = NULL);
00309 
00313   std::istream & read_config_line(std::istream &is, std::string &line);
00314 
00317   static std::istream & getline_nocomments(std::istream &is, std::string &s);
00318 
00322   static int check_braces(std::string const &conf, size_t const start_pos);
00323 
00326   static int check_ascii(std::string const &conf);
00327 
00332   static void split_string(const std::string& data, const std::string& delim, std::vector<std::string>& dest);
00333 
00334 protected:
00335 
00337   std::string const keyword_delimiters_left;
00338 
00340   std::string const keyword_delimiters_right;
00341 
00345   std::list<std::string> allowed_keywords;
00346 
00348   enum key_set_mode {
00349     key_not_set = 0,
00350     key_set_user = 1,
00351     key_set_default = 2
00352   };
00353 
00355   std::map<std::string, key_set_mode> key_set_modes;
00356 
00360   std::list<size_t>      data_begin_pos;
00361 
00365   std::list<size_t>      data_end_pos;
00366 
00368   void add_keyword(char const *key);
00369 
00371   void strip_values(std::string &conf);
00372 
00374   std::string config_string;
00375 
00376 };
00377 
00378 
00380 inline colvarparse::Parse_Mode operator | (colvarparse::Parse_Mode const &mode1,
00381                                            colvarparse::Parse_Mode const &mode2)
00382 {
00383   return static_cast<colvarparse::Parse_Mode>(static_cast<int>(mode1) |
00384                                               static_cast<int>(mode2));
00385 }
00386 
00387 #endif

Generated on Fri Oct 4 02:43:45 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002