Collective Variables Module - Developer Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 protected:
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 
44 
46  void add_keyword(char const *key);
47 
49  void strip_values(std::string &conf);
50 
52  std::string config_string;
53 
54 public:
55 
56 
57  inline colvarparse()
58  : save_delimiters(true)
59  {
60  init();
61  }
62 
64  inline colvarparse(const std::string& conf)
65  : save_delimiters(true)
66  {
67  init(conf);
68  }
69 
71  inline void init()
72  {
73  config_string.clear();
75  }
76 
78  inline void init(const std::string& conf)
79  {
80  if (! config_string.size()) {
81  init();
82  config_string = conf;
83  }
84  }
85 
86  inline const std::string& get_config()
87  {
88  return config_string;
89  }
90 
92  enum Parse_Mode {
101  };
102 
106  int check_keywords(std::string &conf, char const *key);
107 
109  void clear_keyword_registry();
110 
111 public:
112 
137 
138  bool get_keyval(std::string const &conf,
139  char const *key,
140  int &value,
141  int const &def_value = (int)0,
142  Parse_Mode const parse_mode = parse_normal);
143  bool get_keyval(std::string const &conf,
144  char const *key,
145  size_t &value,
146  size_t const &def_value = (size_t)0,
147  Parse_Mode const parse_mode = parse_normal);
148  bool get_keyval(std::string const &conf,
149  char const *key,
150  long &value,
151  long const &def_value = 0,
152  Parse_Mode const parse_mode = parse_normal);
153  bool get_keyval(std::string const &conf,
154  char const *key,
155  std::string &value,
156  std::string const &def_value = std::string(""),
157  Parse_Mode const parse_mode = parse_normal);
158  bool get_keyval(std::string const &conf,
159  char const *key,
160  cvm::real &value,
161  cvm::real const &def_value = (cvm::real)0.0,
162  Parse_Mode const parse_mode = parse_normal);
163  bool get_keyval(std::string const &conf,
164  char const *key,
165  cvm::rvector &value,
166  cvm::rvector const &def_value = cvm::rvector(),
167  Parse_Mode const parse_mode = parse_normal);
168  bool get_keyval(std::string const &conf,
169  char const *key,
170  cvm::quaternion &value,
171  cvm::quaternion const &def_value = cvm::quaternion(),
172  Parse_Mode const parse_mode = parse_normal);
173  bool get_keyval(std::string const &conf,
174  char const *key,
175  colvarvalue &value,
177  Parse_Mode const parse_mode = parse_normal);
178  bool get_keyval(std::string const &conf,
179  char const *key,
180  bool &value,
181  bool const &def_value = false,
182  Parse_Mode const parse_mode = parse_normal);
183  bool get_keyval(std::string const &conf,
184  char const *key,
185  std::vector<int> &values,
186  std::vector<int> const &def_values = std::vector<int>(0, (int)0),
187  Parse_Mode const parse_mode = parse_normal);
188  bool get_keyval(std::string const &conf,
189  char const *key,
190  std::vector<size_t> &values,
191  std::vector<size_t> const &def_values = std::vector<size_t>(0, (size_t)0),
192  Parse_Mode const parse_mode = parse_normal);
193  bool get_keyval(std::string const &conf,
194  char const *key,
195  std::vector<long> &values,
196  std::vector<long> const &def_values = std::vector<long>(0, (long)0),
197  Parse_Mode const parse_mode = parse_normal);
198  bool get_keyval(std::string const &conf,
199  char const *key,
200  std::vector<std::string> &values,
201  std::vector<std::string> const &def_values = std::vector<std::string>(0, std::string("")),
202  Parse_Mode const parse_mode = parse_normal);
203  bool get_keyval(std::string const &conf,
204  char const *key,
205  std::vector<cvm::real> &values,
206  std::vector<cvm::real> const &def_values = std::vector<cvm::real>(0, (cvm::real)0.0),
207  Parse_Mode const parse_mode = parse_normal);
208  bool get_keyval(std::string const &conf,
209  char const *key,
210  std::vector<cvm::rvector> &values,
211  std::vector<cvm::rvector> const &def_values = std::vector<cvm::rvector>(0, cvm::rvector()),
212  Parse_Mode const parse_mode = parse_normal);
213  bool get_keyval(std::string const &conf,
214  char const *key,
215  std::vector<cvm::quaternion> &values,
216  std::vector<cvm::quaternion> const &def_values = std::vector<cvm::quaternion>(0, cvm::quaternion()),
217  Parse_Mode const parse_mode = parse_normal);
218  bool get_keyval(std::string const &conf,
219  char const *key,
220  std::vector<colvarvalue> &values,
221  std::vector<colvarvalue> const &def_values = std::vector<colvarvalue>(0, colvarvalue(colvarvalue::type_notset)),
222  Parse_Mode const parse_mode = parse_normal);
223 
224 protected:
225 
226  // Templates
227  template<typename TYPE> bool _get_keyval_scalar_(std::string const &conf,
228  char const *key,
229  TYPE &value,
230  TYPE const &def_value,
231  Parse_Mode const parse_mode);
232  bool _get_keyval_scalar_string_(std::string const &conf,
233  char const *key,
234  std::string &value,
235  std::string const &def_value,
236  Parse_Mode const parse_mode);
237 
238  template<typename TYPE> bool _get_keyval_vector_(std::string const &conf,
239  char const *key,
240  std::vector<TYPE> &values,
241  std::vector<TYPE> const &def_values,
242  Parse_Mode const parse_mode);
243 
244 public:
245 
247  static inline std::string to_lower_cppstr(std::string const &in)
248  {
249  std::string out = "";
250  for (size_t i = 0; i < in.size(); i++) {
251  out.append(1, (char) ::tolower(in[i]) );
252  }
253  return out;
254  }
255 
263  class read_block {
264 
265  std::string const key;
266  std::string * const data;
267 
268  public:
269  inline read_block(std::string const &key_in, std::string &data_in)
270  : key(key_in), data(&data_in)
271  {}
272  inline ~read_block() {}
273  friend std::istream & operator >> (std::istream &is, read_block const &rb);
274  };
275 
276 
278  static std::string const white_space;
279 
287  bool key_lookup(std::string const &conf,
288  char const *key,
289  std::string &data = dummy_string,
290  size_t &save_pos = dummy_pos);
291 
293  static std::string dummy_string;
295  static size_t dummy_pos;
296 
299  static std::istream & getline_nocomments(std::istream &is,
300  std::string &s);
301 
303  bool brace_check(std::string const &conf,
304  size_t const start_pos = 0);
305 
306 };
307 
308 
309 #endif
static std::string const white_space
Accepted white space delimiters, used in key_lookup()
Definition: colvarparse.h:278
Like parse_normal, but don't send any message to the log (useful e.g. in restart files when such mess...
Definition: colvarparse.h:100
1-dimensional vector of real numbers with four components and a quaternion algebra ...
Definition: colvartypes.h:1025
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:479
std::list< size_t > data_end_pos
List of delimiters for the values of each keyword in the configuration string; all keywords will be s...
Definition: colvarparse.h:35
void strip_values(std::string &conf)
Remove all the values from the config string.
Definition: colvarparse.cpp:447
vector of real numbers with three components
Definition: colvartypes.h:709
Collective variables main module.
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:85
static size_t dummy_pos
Used as a default argument by key_lookup.
Definition: colvarparse.h:295
bool save_delimiters
Whether or not to accumulate data_begin_pos and data_end_pos in key_lookup(); it may be useful to dis...
Definition: colvarparse.h:43
void init(const std::string &conf)
Set a new config string for this object.
Definition: colvarparse.h:78
static std::string dummy_string
Used as a default argument by key_lookup.
Definition: colvarparse.h:293
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:34
std::string config_string
Configuration string of the object.
Definition: colvarparse.h:52
void add_keyword(char const *key)
Add a new valid keyword to the list.
Definition: colvarparse.cpp:433
std::list< std::string > allowed_keywords
List of legal keywords for this object: this is updated by each call to colvarparse::get_keyval() or ...
Definition: colvarparse.h:25
Helper class to read a block of the type "key { ... }" from a stream and store it in a string...
Definition: colvarparse.h:263
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:487
std::list< size_t > data_begin_pos
List of delimiters for the values of each keyword in the configuration string; all keywords will be s...
Definition: colvarparse.h:30
(default) Read the first instance of a keyword (if any), report its value, and print a warning when t...
Definition: colvarparse.h:96
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:777
colvarparse(const std::string &conf)
Constructor that stores the object's config string.
Definition: colvarparse.h:64
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:92
static std::string to_lower_cppstr(std::string const &in)
Return a lowercased copy of the string.
Definition: colvarparse.h:247
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:71
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:534
bool key_lookup(std::string const &conf, char const *key, std::string &data=dummy_string, size_t &save_pos=dummy_pos)
Low-level function for parsing configuration strings; automatically adds the requested keyword to the...
Definition: colvarparse.cpp:546