Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarparse.h
Go to the documentation of this file.
1// -*- c++ -*-
2
3// This file is part of the Collective Variables module (Colvars).
4// The original version of Colvars and its updates are located at:
5// https://github.com/Colvars/colvars
6// Please update all Colvars source files before making any changes.
7// If you wish to distribute your changes, please submit them to the
8// Colvars repository at GitHub.
9
10#ifndef COLVARPARSE_H
11#define COLVARPARSE_H
12
13#include <cstring>
14#include <list>
15#include <string>
16
17#include "colvarmodule.h"
18#include "colvarvalue.h"
19#include "colvarparams.h"
20
21
23
24
27class colvarparse : public colvarparams {
28
29public:
30
33
35 colvarparse(const std::string& conf);
36
38 void clear();
39
41 void set_string(std::string const &conf);
42
44 ~colvarparse() override;
45
47 inline std::string const & get_config() const
48 {
49 return config_string;
50 }
51
57 parse_echo = (1<<1),
65 parse_required = (1<<16),
68 parse_override = (1<<17),
70 parse_restart = (1<<18),
72 parse_normal = (1<<1) | (1<<2) | (1<<17),
74 parse_deprecated = (1<<1) | (1<<3) | (1<<17)
75 };
76
80 int check_keywords(std::string &conf, char const *key);
81
84
109
110 bool get_keyval(std::string const &conf,
111 char const *key,
112 int &value,
113 int const &def_value = 0,
114 Parse_Mode const parse_mode = parse_normal);
115 bool get_keyval(std::string const &conf,
116 char const *key,
117 size_t &value,
118 size_t const &def_value = 0,
119 Parse_Mode const parse_mode = parse_normal);
120 bool get_keyval(std::string const &conf,
121 char const *key,
122 long &value,
123 long const &def_value = 0,
124 Parse_Mode const parse_mode = parse_normal);
125 bool get_keyval(std::string const &conf,
126 char const *key,
127 cvm::step_number &value,
128 cvm::step_number const &def_value = 0,
129 Parse_Mode const parse_mode = parse_normal);
130 bool get_keyval(std::string const &conf,
131 char const *key,
132 std::string &value,
133 std::string const &def_value = std::string(""),
134 Parse_Mode const parse_mode = parse_normal);
135 bool get_keyval(std::string const &conf,
136 char const *key,
137 cvm::real &value,
138 cvm::real const &def_value = 0.0,
139 Parse_Mode const parse_mode = parse_normal);
140 bool get_keyval(std::string const &conf,
141 char const *key,
142 cvm::rvector &value,
143 cvm::rvector const &def_value = cvm::rvector(),
144 Parse_Mode const parse_mode = parse_normal);
145 bool get_keyval(std::string const &conf,
146 char const *key,
147 cvm::quaternion &value,
148 cvm::quaternion const &def_value = cvm::quaternion(),
149 Parse_Mode const parse_mode = parse_normal);
150 bool get_keyval(std::string const &conf,
151 char const *key,
152 colvarvalue &value,
154 Parse_Mode const parse_mode = parse_normal);
155 bool get_keyval(std::string const &conf,
156 char const *key,
157 bool &value,
158 bool const &def_value = false,
159 Parse_Mode const parse_mode = parse_normal);
160 bool get_keyval(std::string const &conf,
161 char const *key,
162 std::vector<int> &values,
163 std::vector<int> const &def_values = std::vector<int>(0, 0),
164 Parse_Mode const parse_mode = parse_normal);
165 bool get_keyval(std::string const &conf,
166 char const *key,
167 std::vector<size_t> &values,
168 std::vector<size_t> const &def_values = std::vector<size_t>(0, 0),
169 Parse_Mode const parse_mode = parse_normal);
170 bool get_keyval(std::string const &conf,
171 char const *key,
172 std::vector<long> &values,
173 std::vector<long> const &def_values = std::vector<long>(0, 0),
174 Parse_Mode const parse_mode = parse_normal);
175 bool get_keyval(std::string const &conf,
176 char const *key,
177 std::vector<std::string> &values,
178 std::vector<std::string> const &def_values = std::vector<std::string>(0, std::string("")),
179 Parse_Mode const parse_mode = parse_normal);
180 bool get_keyval(std::string const &conf,
181 char const *key,
182 std::vector<cvm::real> &values,
183 std::vector<cvm::real> const &def_values = std::vector<cvm::real>(0, 0.0),
184 Parse_Mode const parse_mode = parse_normal);
185 bool get_keyval(std::string const &conf,
186 char const *key,
187 std::vector<cvm::rvector> &values,
188 std::vector<cvm::rvector> const &def_values = std::vector<cvm::rvector>(0, cvm::rvector()),
189 Parse_Mode const parse_mode = parse_normal);
190 bool get_keyval(std::string const &conf,
191 char const *key,
192 std::vector<cvm::quaternion> &values,
193 std::vector<cvm::quaternion> const &def_values = std::vector<cvm::quaternion>(0, cvm::quaternion()),
194 Parse_Mode const parse_mode = parse_normal);
195 bool get_keyval(std::string const &conf,
196 char const *key,
197 std::vector<colvarvalue> &values,
198 std::vector<colvarvalue> const &def_values = std::vector<colvarvalue>(0, colvarvalue(colvarvalue::type_notset)),
199 Parse_Mode const parse_mode = parse_normal);
200
201protected:
202
204 bool get_key_string_value(std::string const &conf,
205 char const *key, std::string &data);
206
208 bool get_key_string_multi_value(std::string const &conf,
209 char const *key, std::vector<std::string>& data);
210
212 template<typename TYPE>
213 bool _get_keyval_scalar_(std::string const &conf,
214 char const *key,
215 TYPE &value,
216 TYPE const &def_value,
217 Parse_Mode const &parse_mode);
218
220 template<typename TYPE>
221 bool _get_keyval_vector_(std::string const &conf,
222 char const *key,
223 std::vector<TYPE> &values,
224 std::vector<TYPE> const &def_values,
225 Parse_Mode const &parse_mode);
226
228 template<typename TYPE>
229 int _get_keyval_scalar_value_(std::string const &key_str,
230 std::string const &data,
231 TYPE &value,
232 TYPE const &def_value);
233
235 template<typename TYPE>
236 int _get_keyval_scalar_novalue_(std::string const &key_str,
237 TYPE &value,
238 Parse_Mode const &parse_mode);
239
241 template<typename TYPE>
242 void mark_key_set_user(std::string const &key_str,
243 TYPE const &value,
244 Parse_Mode const &parse_mode);
245
247 template<typename TYPE>
248 void mark_key_set_default(std::string const &key_str,
249 TYPE const &def_value,
250 Parse_Mode const &parse_mode);
251
253 void error_key_required(std::string const &key_str,
254 Parse_Mode const &parse_mode);
255
257 bool key_already_set(std::string const &key_str);
258
259public:
260
262 static inline std::string to_lower_cppstr(std::string const &in)
263 {
264 std::string out = "";
265 for (size_t i = 0; i < in.size(); i++) {
266 out.append(1, static_cast<char>( ::tolower(in[i])) );
267 }
268 return out;
269 }
270
277 public:
278
279 read_block(std::string const &key, std::string *data = nullptr);
280
281 ~read_block();
282
284 friend std::istream & operator >> (std::istream &is, read_block const &rb);
285
288
289 private:
290
292 std::string const key;
293
295 std::string * const data;
296
300 std::istream & read_block_contents(std::istream &is, bool block_only = false) const;
301 };
302
303
305 static const char * const white_space;
306
314 bool key_lookup(std::string const &conf,
315 char const *key,
316 std::string *data = nullptr,
317 size_t *save_pos = nullptr);
318
322 std::istream & read_config_line(std::istream &is, std::string &line);
323
326 static std::istream & getline_nocomments(std::istream &is, std::string &s);
327
331 static int check_braces(std::string const &conf, size_t const start_pos);
332
335 static int check_ascii(std::string const &conf);
336
341 static void split_string(const std::string& data, const std::string& delim, std::vector<std::string>& dest);
342
343protected:
344
346 std::string const keyword_delimiters_left;
347
349 std::string const keyword_delimiters_right;
350
354 std::list<std::string> allowed_keywords;
355
358 key_not_set = 0,
359 key_set_user = 1,
360 key_set_default = 2
361 };
362
364 std::map<std::string, key_set_mode> key_set_modes;
365
369 std::list<size_t> data_begin_pos;
370
374 std::list<size_t> data_end_pos;
375
377 void add_keyword(char const *key);
378
380 void strip_values(std::string &conf);
381
383 std::string config_string;
384
385};
386
387
390 colvarparse::Parse_Mode const &mode2)
391{
392 return static_cast<colvarparse::Parse_Mode>(static_cast<int>(mode1) |
393 static_cast<int>(mode2));
394}
395
396#endif
1-dimensional vector of real numbers with four components and a quaternion algebra
Definition: colvartypes.h:958
vector of real numbers with three components
Definition: colvartypes.h:727
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:95
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:92
Definition: colvarparams.h:18
Definition: colvarparse.h:276
std::string const key
Keyword that identifies the block.
Definition: colvarparse.h:292
friend std::istream & operator>>(std::istream &is, read_block const &rb)
Read block from stream, first check that key matches, then call read_contents()
Definition: colvarparse.cpp:867
std::string *const data
Where to keep the data.
Definition: colvarparse.h:295
std::istream & read_block_contents(std::istream &is, bool block_only=false) const
Definition: colvarparse.cpp:900
Base class containing parsing functions; all objects which need to parse input inherit from this.
Definition: colvarparse.h:27
bool key_already_set(std::string const &key_str)
True if the keyword has been set already.
Definition: colvarparse.cpp:547
bool key_lookup(std::string const &conf, char const *key, std::string *data=nullptr, size_t *save_pos=nullptr)
Low-level function for parsing configuration strings; automatically adds the requested keyword to the...
Definition: colvarparse.cpp:663
static void split_string(const std::string &data, const std::string &delim, std::vector< std::string > &dest)
Split a string with a specified delimiter into a vector.
Definition: colvarparse.cpp:1007
static int check_ascii(std::string const &conf)
Check that a config string contains non-ASCII characters.
Definition: colvarparse.cpp:988
~colvarparse() override
Default destructor.
Definition: colvarparse.cpp:67
std::string const keyword_delimiters_right
Characters allowed immediately to the right of a kewyord.
Definition: colvarparse.h:349
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:369
void set_string(std::string const &conf)
Set a new config string for this object.
Definition: colvarparse.cpp:58
std::string const keyword_delimiters_left
Characters allowed immediately to the left of a kewyord.
Definition: colvarparse.h:346
std::string config_string
Configuration string of the object (includes comments)
Definition: colvarparse.h:383
key_set_mode
How a keyword has been set.
Definition: colvarparse.h:357
bool _get_keyval_scalar_(std::string const &conf, char const *key, TYPE &value, TYPE const &def_value, Parse_Mode const &parse_mode)
Template for single-value keyword parsers.
Definition: colvarparse.cpp:240
void add_keyword(char const *key)
Add a new valid keyword to the list.
Definition: colvarparse.cpp:533
static int check_braces(std::string const &conf, size_t const start_pos)
Check if the content of a config string has matching braces.
Definition: colvarparse.cpp:974
bool get_key_string_multi_value(std::string const &conf, char const *key, std::vector< std::string > &data)
Get multiple strings from repeated instances of a same keyword.
Definition: colvarparse.cpp:99
void mark_key_set_user(std::string const &key_str, TYPE const &value, Parse_Mode const &parse_mode)
Record that the keyword has just been user-defined.
Definition: colvarparse.cpp:122
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:591
int _get_keyval_scalar_novalue_(std::string const &key_str, TYPE &value, Parse_Mode const &parse_mode)
Handle the case where the user provides a keyword without value.
Definition: colvarparse.cpp:220
static std::string to_lower_cppstr(std::string const &in)
Return a lowercased copy of the string.
Definition: colvarparse.h:262
std::istream & read_config_line(std::istream &is, std::string &line)
Reads a configuration line, adds it to config_string, and returns the stream.
Definition: colvarparse.cpp:638
static const char *const white_space
Accepted white space delimiters, used in key_lookup()
Definition: colvarparse.h:305
bool _get_keyval_vector_(std::string const &conf, char const *key, std::vector< TYPE > &values, std::vector< TYPE > const &def_values, Parse_Mode const &parse_mode)
Template for multiple-value keyword parsers.
Definition: colvarparse.cpp:286
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:374
int _get_keyval_scalar_value_(std::string const &key_str, std::string const &data, TYPE &value, TYPE const &def_value)
Extract the value of a variable from a string.
Definition: colvarparse.cpp:168
void clear()
Set the object ready to parse a new configuration string.
Definition: colvarparse.cpp:43
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:354
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:651
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:53
@ parse_deprecation_warning
Print a deprecation warning if the keyword is given.
Definition: colvarparse.h:61
@ parse_echo_default
Print the default value of a keyword, if it is NOT given.
Definition: colvarparse.h:59
@ parse_echo
Print the value of a keyword if it is given.
Definition: colvarparse.h:57
@ parse_normal
Alias for old default behavior (should be phased out)
Definition: colvarparse.h:72
@ parse_deprecated
Settings for a deprecated keyword.
Definition: colvarparse.h:74
@ parse_restart
The call is being executed from a read_restart() function.
Definition: colvarparse.h:70
@ parse_null
Zero for all flags.
Definition: colvarparse.h:55
@ parse_override
Definition: colvarparse.h:68
@ parse_silent
Do not print the keyword.
Definition: colvarparse.h:63
@ parse_required
Raise error if the keyword is not provided.
Definition: colvarparse.h:65
void error_key_required(std::string const &key_str, Parse_Mode const &parse_mode)
Raise error condition due to the keyword being required!
Definition: colvarparse.cpp:151
void clear_keyword_registry()
Use this after parsing a config string (note that check_keywords() calls it already)
Definition: colvarparse.cpp:582
void strip_values(std::string &conf)
Remove all the values from the config string.
Definition: colvarparse.cpp:559
bool get_key_string_value(std::string const &conf, char const *key, std::string &data)
Get the string value of a keyword, and save it for later parsing.
Definition: colvarparse.cpp:74
colvarparse()
Default constructor.
Definition: colvarparse.cpp:35
std::map< std::string, key_set_mode > key_set_modes
Track which keywords have been already set, and how.
Definition: colvarparse.h:364
void mark_key_set_default(std::string const &key_str, TYPE const &def_value, Parse_Mode const &parse_mode)
Record that the keyword has just been set to its default value.
Definition: colvarparse.cpp:139
std::string const & get_config() const
Get the configuration string (includes comments)
Definition: colvarparse.h:47
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
@ type_notset
Undefined type.
Definition: colvarvalue.h:54
Definition: colvars_memstream.h:30
Collective variables main module.
Functions to handle scalar parameters used in objects.
colvarparse::Parse_Mode operator|(colvarparse::Parse_Mode const &mode1, colvarparse::Parse_Mode const &mode2)
Bitwise OR between two Parse_mode flags.
Definition: colvarparse.h:389