Collective Variables Module - Developer Documentation
colvarparse.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 #ifndef COLVARPARSE_H
4 #define COLVARPARSE_H
5 
6 #include <cstring>
7 #include <string>
8 
9 #include "colvarmodule.h"
10 #include "colvarvalue.h"
11 
12 
14 
15 
18 class colvarparse {
19 
20 private:
21 
25  std::list<std::string> allowed_keywords;
26 
30  std::list<size_t> data_begin_pos;
31 
35  std::list<size_t> data_end_pos;
36 
38  void add_keyword(char const *key);
39 
41  void strip_values(std::string &conf);
42 
44  std::string config_string;
45 
46 public:
47 
48 
49  inline colvarparse()
50  {
51  init();
52  }
53 
55  inline colvarparse(const std::string& conf)
56  {
57  init(conf);
58  }
59 
61  inline void init()
62  {
63  config_string.clear();
65  }
66 
68  inline void init(const std::string& conf)
69  {
70  if (! config_string.size()) {
71  init();
72  config_string = conf;
73  }
74  }
75 
76  inline const std::string& get_config()
77  {
78  return config_string;
79  }
80 
82  enum Parse_Mode {
91  };
92 
96  int check_keywords(std::string &conf, char const *key);
97 
100 
125 
126  bool get_keyval(std::string const &conf,
127  char const *key,
128  int &value,
129  int const &def_value = (int)0,
130  Parse_Mode const parse_mode = parse_normal);
131  bool get_keyval(std::string const &conf,
132  char const *key,
133  size_t &value,
134  size_t const &def_value = (size_t)0,
135  Parse_Mode const parse_mode = parse_normal);
136  bool get_keyval(std::string const &conf,
137  char const *key,
138  long &value,
139  long const &def_value = 0,
140  Parse_Mode const parse_mode = parse_normal);
141  bool get_keyval(std::string const &conf,
142  char const *key,
143  std::string &value,
144  std::string const &def_value = std::string(""),
145  Parse_Mode const parse_mode = parse_normal);
146  bool get_keyval(std::string const &conf,
147  char const *key,
148  cvm::real &value,
149  cvm::real const &def_value = (cvm::real)0.0,
150  Parse_Mode const parse_mode = parse_normal);
151  bool get_keyval(std::string const &conf,
152  char const *key,
153  cvm::rvector &value,
154  cvm::rvector const &def_value = cvm::rvector(),
155  Parse_Mode const parse_mode = parse_normal);
156  bool get_keyval(std::string const &conf,
157  char const *key,
158  cvm::quaternion &value,
159  cvm::quaternion const &def_value = cvm::quaternion(),
160  Parse_Mode const parse_mode = parse_normal);
161  bool get_keyval(std::string const &conf,
162  char const *key,
163  colvarvalue &value,
165  Parse_Mode const parse_mode = parse_normal);
166  bool get_keyval(std::string const &conf,
167  char const *key,
168  bool &value,
169  bool const &def_value = false,
170  Parse_Mode const parse_mode = parse_normal);
171  bool get_keyval(std::string const &conf,
172  char const *key,
173  std::vector<int> &values,
174  std::vector<int> const &def_values = std::vector<int>(0, (int)0),
175  Parse_Mode const parse_mode = parse_normal);
176  bool get_keyval(std::string const &conf,
177  char const *key,
178  std::vector<size_t> &values,
179  std::vector<size_t> const &def_values = std::vector<size_t>(0, (size_t)0),
180  Parse_Mode const parse_mode = parse_normal);
181  bool get_keyval(std::string const &conf,
182  char const *key,
183  std::vector<long> &values,
184  std::vector<long> const &def_values = std::vector<long>(0, (long)0),
185  Parse_Mode const parse_mode = parse_normal);
186  bool get_keyval(std::string const &conf,
187  char const *key,
188  std::vector<std::string> &values,
189  std::vector<std::string> const &def_values = std::vector<std::string>(0, std::string("")),
190  Parse_Mode const parse_mode = parse_normal);
191  bool get_keyval(std::string const &conf,
192  char const *key,
193  std::vector<cvm::real> &values,
194  std::vector<cvm::real> const &def_values = std::vector<cvm::real>(0, (cvm::real)0.0),
195  Parse_Mode const parse_mode = parse_normal);
196  bool get_keyval(std::string const &conf,
197  char const *key,
198  std::vector<cvm::rvector> &values,
199  std::vector<cvm::rvector> const &def_values = std::vector<cvm::rvector>(0, cvm::rvector()),
200  Parse_Mode const parse_mode = parse_normal);
201  bool get_keyval(std::string const &conf,
202  char const *key,
203  std::vector<cvm::quaternion> &values,
204  std::vector<cvm::quaternion> const &def_values = std::vector<cvm::quaternion>(0, cvm::quaternion()),
205  Parse_Mode const parse_mode = parse_normal);
206  bool get_keyval(std::string const &conf,
207  char const *key,
208  std::vector<colvarvalue> &values,
209  std::vector<colvarvalue> const &def_values = std::vector<colvarvalue>(0, colvarvalue(colvarvalue::type_notset)),
210  Parse_Mode const parse_mode = parse_normal);
211 
212 protected:
213 
214  // Templates
215  template<typename TYPE> bool _get_keyval_scalar_(std::string const &conf,
216  char const *key,
217  TYPE &value,
218  TYPE const &def_value,
219  Parse_Mode const parse_mode);
220  bool _get_keyval_scalar_string_(std::string const &conf,
221  char const *key,
222  std::string &value,
223  std::string const &def_value,
224  Parse_Mode const parse_mode);
225 
226  template<typename TYPE> bool _get_keyval_vector_(std::string const &conf,
227  char const *key,
228  std::vector<TYPE> &values,
229  std::vector<TYPE> const &def_values,
230  Parse_Mode const parse_mode);
231 
232 public:
233 
235  static inline std::string to_lower_cppstr(std::string const &in)
236  {
237  std::string out = "";
238  for (size_t i = 0; i < in.size(); i++) {
239  out.append(1, (char) ::tolower(in[i]) );
240  }
241  return out;
242  }
243 
251  class read_block {
252 
253  std::string const key;
254  std::string * const data;
255 
256  public:
257  inline read_block(std::string const &key_in, std::string &data_in)
258  : key(key_in), data(&data_in)
259  {}
260  inline ~read_block() {}
261  friend std::istream & operator >> (std::istream &is, read_block const &rb);
262  };
263 
264 
266  static const char * const white_space;
267 
275  bool key_lookup(std::string const &conf,
276  char const *key,
277  std::string *data = NULL,
278  size_t *save_pos = NULL);
279 
282  static std::istream & getline_nocomments(std::istream &is,
283  std::string &s);
284 
286  bool brace_check(std::string const &conf,
287  size_t const start_pos = 0);
288 
289 };
290 
291 
292 #endif
Like parse_normal, but don&#39;t send any message to the log (useful e.g. in restart files when such mess...
Definition: colvarparse.h:90
1-dimensional vector of real numbers with four components and a quaternion algebra ...
Definition: colvartypes.h:1020
Undefined type.
Definition: colvarvalue.h:45
void clear_keyword_registry()
Use this after parsing a config string (note that check_keywords() calls it already) ...
Definition: colvarparse.cpp:476
vector of real numbers with three components
Definition: colvartypes.h:714
bool key_lookup(std::string const &conf, char const *key, std::string *data=NULL, size_t *save_pos=NULL)
Low-level function for parsing configuration strings; automatically adds the requested keyword to the...
Definition: colvarparse.cpp:543
Collective variables main module.
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:76
void init(const std::string &conf)
Set a new config string for this object.
Definition: colvarparse.h:68
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:34
Helper class to read a block of the type "key { ... }" from a stream and store it in a string...
Definition: colvarparse.h:251
int check_keywords(std::string &conf, char const *key)
Check that all the keywords within "conf" are in the list of allowed keywords; this will invoke strip...
Definition: colvarparse.cpp:484
(default) Read the first instance of a keyword (if any), report its value, and print a warning when t...
Definition: colvarparse.h:86
bool brace_check(std::string const &conf, size_t const start_pos=0)
Check if the content of the file has matching braces.
Definition: colvarparse.cpp:776
colvarparse(const std::string &conf)
Constructor that stores the object&#39;s config string.
Definition: colvarparse.h:55
static const char *const white_space
Accepted white space delimiters, used in key_lookup()
Definition: colvarparse.h:266
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:82
static std::string to_lower_cppstr(std::string const &in)
Return a lowercased copy of the string.
Definition: colvarparse.h:235
Base class containing parsing functions; all objects which need to parse input inherit from this...
Definition: colvarparse.h:18
void init()
Set the object ready to parse a new configuration string.
Definition: colvarparse.h:61
static std::istream & getline_nocomments(std::istream &is, std::string &s)
Works as std::getline() but also removes everything between a comment character and the following new...
Definition: colvarparse.cpp:531