#include <colvarparse.h>
Inheritance diagram for colvarparse:

Public Types | |
| enum | Parse_Mode { parse_normal, parse_silent } |
| How a keyword is parsed in a string. More... | |
Public Member Functions | |
| colvarparse () | |
| _get_keyval_scalar_proto_ (int,(int) 0) | |
| _get_keyval_scalar_proto_ (size_t,(size_t) 0) | |
| _get_keyval_scalar_proto_ (std::string, std::string("")) | |
| _get_keyval_scalar_proto_ (cvm::real,(cvm::real) 0.0) | |
| _get_keyval_scalar_proto_ (cvm::rvector, cvm::rvector()) | |
| _get_keyval_scalar_proto_ (cvm::quaternion, cvm::quaternion()) | |
| _get_keyval_scalar_proto_ (colvarvalue, colvarvalue(colvarvalue::type_notset)) | |
| _get_keyval_scalar_proto_ (bool, false) | |
| _get_keyval_vector_proto_ (int, 0) | |
| _get_keyval_vector_proto_ (size_t, 0) | |
| _get_keyval_vector_proto_ (std::string, std::string("")) | |
| _get_keyval_vector_proto_ (cvm::real, 0.0) | |
| _get_keyval_vector_proto_ (cvm::rvector, cvm::rvector()) | |
| _get_keyval_vector_proto_ (cvm::quaternion, cvm::quaternion()) | |
| _get_keyval_vector_proto_ (colvarvalue, colvarvalue(colvarvalue::type_notset)) | |
| void | 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_values() first and then loop over all words. | |
| 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 keywords to the list of valid ones. | |
| bool | brace_check (std::string const &conf, size_t const start_pos=0) |
| Check if the content of the file has matching braces. | |
Static Public Member Functions | |
| std::string | to_lower_cppstr (std::string const &in) |
| Return a lowercased copy of the string. | |
| std::istream & | getline_nocomments (std::istream &is, std::string &s, char const delim= '\n') |
| Works as std::getline() but also removes everything between a comment character and the following newline. | |
Static Public Attributes | |
| std::string const | white_space = " \t" |
| Accepted white space delimiters, used in key_lookup(). | |
| std::string | dummy_string = "" |
| Used as a default argument by key_lookup. | |
| size_t | dummy_pos = 0 |
| Used as a default argument by key_lookup. | |
Protected Member Functions | |
| void | add_keyword (char const *key) |
| Add a new valid keyword to the list. | |
| void | strip_values (std::string &conf) |
| Remove all the values from the config string. | |
Protected Attributes | |
| std::list< std::string > | allowed_keywords |
| List of legal keywords for this object: this is updated by each call to colvarparse::get_keyval() or colvarparse::key_lookup(). | |
| std::list< size_t > | data_begin_pos |
| List of delimiters for the values of each keyword in the configuration string; all keywords will be stripped of their values before the keyword check is performed. | |
| std::list< size_t > | data_end_pos |
| List of delimiters for the values of each keyword in the configuration string; all keywords will be stripped of their values before the keyword check is performed. | |
| bool | save_delimiters |
| Whether or not to accumulate data_begin_pos and data_end_pos in key_lookup(); it may be useful to disable this after the constructor is called, because other files may be read (e.g. restart) that would mess up with the registry; in any case, nothing serious happens until check_keywords() is invoked (which should happen only right after construction). | |
|
|
How a keyword is parsed in a string.
Definition at line 56 of file colvarparse.h. 00056 {
00060 parse_normal,
00064 parse_silent
00065 };
|
|
|
Definition at line 51 of file colvarparse.h. 00052 : save_delimiters (true) 00053 {}
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
|
Add a new valid keyword to the list.
Definition at line 304 of file colvarparse.C. References allowed_keywords, and to_lower_cppstr(). Referenced by key_lookup(). 00305 {
00306 for (std::list<std::string>::iterator ki = allowed_keywords.begin();
00307 ki != allowed_keywords.end(); ki++) {
00308 if (to_lower_cppstr (std::string (key)) == *ki)
00309 return;
00310 }
00311 // not found in the list
00312 // if (cvm::debug())
00313 // cvm::log ("Registering a new keyword, \""+std::string (key)+"\".\n");
00314 allowed_keywords.push_back (to_lower_cppstr (std::string (key)));
00315 }
|
|
||||||||||||
|
Check if the content of the file has matching braces.
Definition at line 620 of file colvarparse.C. Referenced by key_lookup(). 00622 {
00623 size_t brace_count = 0;
00624 size_t brace = start_pos;
00625 while ( (brace = conf.find_first_of ("{}", brace)) != std::string::npos) {
00626 if (conf[brace] == '{') brace_count++;
00627 if (conf[brace] == '}') brace_count--;
00628 brace++;
00629 }
00630
00631 if (brace_count != 0)
00632 return false;
00633 else
00634 return true;
00635 }
|
|
||||||||||||
|
Check that all the keywords within "conf" are in the list of allowed keywords; this will invoke strip_values() first and then loop over all words.
Definition at line 350 of file colvarparse.C. References allowed_keywords, colvarmodule::debug(), colvarmodule::fatal_error(), colvarmodule::log(), strip_values(), to_lower_cppstr(), and white_space. Referenced by colvarmodule::colvarmodule(), colvarmodule::init_biases(), colvarmodule::init_colvars(), and colvarmodule::atom_group::parse(). 00351 {
00352 // if (cvm::debug())
00353 // cvm::log ("Configuration string for \""+std::string (key)+
00354 // ": \"\n"+conf+"\".\n");
00355
00356 strip_values (conf);
00357 // after stripping, the config string has either empty lines, or
00358 // lines beginning with a keyword
00359
00360 std::string line;
00361 std::istringstream is (conf);
00362 while (std::getline (is, line)) {
00363 if (line.size() == 0)
00364 continue;
00365 if (line.find_first_not_of (white_space) ==
00366 std::string::npos)
00367 continue;
00368
00369 std::string uk;
00370 std::istringstream line_is (line);
00371 line_is >> uk;
00372 if (cvm::debug())
00373 cvm::log ("Checking the validity of \""+uk+"\".\n");
00374 uk = to_lower_cppstr (uk);
00375
00376 bool found_keyword = false;
00377 for (std::list<std::string>::iterator ki = allowed_keywords.begin();
00378 ki != allowed_keywords.end(); ki++) {
00379 if (uk == *ki) {
00380 found_keyword = true;
00381 break;
00382 }
00383 }
00384 if (!found_keyword)
00385 cvm::fatal_error ("Error: keyword \""+uk+"\" is not supported, "
00386 "or not recognized in this context.\n");
00387 }
00388 }
|
|
||||||||||||||||
|
Works as std::getline() but also removes everything between a comment character and the following newline.
Definition at line 391 of file colvarparse.C. Referenced by colvarmodule::colvarmodule(), operator>>(), and colvarmodule::read_traj(). 00394 {
00395 std::getline (is, line, delim);
00396 size_t const comment = line.find ('#');
00397 if (comment != std::string::npos) {
00398 line.erase (comment);
00399 }
00400 return is;
00401 }
|
|
||||||||||||||||||||
|
Low-level function for parsing configuration strings; automatically adds the requested keywords to the list of valid ones.
Definition at line 404 of file colvarparse.C. References add_keyword(), brace_check(), data_begin_pos, data_end_pos, dummy_pos, colvarmodule::fatal_error(), to_lower_cppstr(), and white_space. Referenced by colvar::alpha_angles::alpha_angles(), colvar::distance_z::distance_z(), colvarmodule::init_biases(), colvarmodule::init_colvars(), colvarmodule::atom_group::parse(), colvar::cvc::parse_group(), and colvarbias_meta::read_hill(). 00408 {
00409 // add this keyword to the register (in its camelCase version)
00410 add_keyword (key_in);
00411
00412 // use the lowercase version from now on
00413 std::string const key (to_lower_cppstr (key_in));
00414
00415 // "conf_lower" is only used to lookup the keyword, but its value
00416 // will be read from "conf", in order not to mess up file names
00417 std::string const conf_lower (to_lower_cppstr (conf));
00418
00419 // by default, there is no value, unless we found one
00420 data = "";
00421
00422 // when the function is invoked without save_pos, ensure that we
00423 // start from zero
00424 colvarparse::dummy_pos = 0;
00425
00426 // start from the first occurrence of key
00427 size_t pos = conf_lower.find (key, save_pos);
00428
00429 // iterate over all instances until it finds the isolated keyword
00430 while (true) {
00431
00432 if (pos == std::string::npos) {
00433 // no valid instance of the keyword has been found
00434 return false;
00435 }
00436
00437 bool b_isolated_left = true, b_isolated_right = true;
00438
00439 if (pos > 0) {
00440 if ( std::string ("\n"+white_space+
00441 "}").find (conf[pos-1]) ==
00442 std::string::npos ) {
00443 // none of the valid delimiting characters is on the left of key
00444 b_isolated_left = false;
00445 }
00446 }
00447
00448 if (pos < conf.size()-key.size()-1) {
00449 if ( std::string ("\n"+white_space+
00450 "{").find (conf[pos+key.size()]) ==
00451 std::string::npos ) {
00452 // none of the valid delimiting characters is on the right of key
00453 b_isolated_right = false;
00454 }
00455 }
00456
00457 // check that there are matching braces between here and the end of conf
00458 bool const b_not_within_block = brace_check (conf, pos);
00459
00460 bool const b_isolated = (b_isolated_left && b_isolated_right &&
00461 b_not_within_block);
00462
00463 if (b_isolated) {
00464 // found it
00465 break;
00466 } else {
00467 // try the next occurrence of key
00468 pos = conf_lower.find (key, pos+key.size());
00469 }
00470 }
00471
00472 // check it is not between quotes
00473 // if ( (conf.find_last_of ("\"",
00474 // conf.find_last_of (white_space, pos)) !=
00475 // std::string::npos) &&
00476 // (conf.find_first_of ("\"",
00477 // conf.find_first_of (white_space, pos)) !=
00478 // std::string::npos) )
00479 // return false;
00480
00481
00482 // save the pointer for a future call (when iterating over multiple
00483 // valid instances of the same keyword)
00484 save_pos = pos + key.size();
00485
00486 // get the remainder of the line
00487 size_t pl = conf.rfind ("\n", pos);
00488 size_t line_begin = (pl == std::string::npos) ? 0 : pos;
00489 size_t nl = conf.find ("\n", pos);
00490 size_t line_end = (nl == std::string::npos) ? conf.size() : nl;
00491 std::string line (conf, line_begin, (line_end-line_begin));
00492
00493 size_t data_begin = (to_lower_cppstr (line)).find (key) + key.size();
00494 data_begin = line.find_first_not_of (white_space, data_begin+1);
00495
00496 // size_t data_begin_absolute = data_begin + line_begin;
00497 // size_t data_end_absolute = data_begin;
00498
00499 if (data_begin != std::string::npos) {
00500
00501 size_t data_end = line.find_last_not_of (white_space) + 1;
00502 data_end = (data_end == std::string::npos) ? line.size() : data_end;
00503 // data_end_absolute = data_end + line_begin;
00504
00505 if (line.find ('{', data_begin) != std::string::npos) {
00506
00507 size_t brace_count = 1;
00508 size_t brace = line.find ('{', data_begin); // start from the first opening brace
00509
00510 while (brace_count > 0) {
00511
00512 // find the matching closing brace
00513 brace = line.find_first_of ("{}", brace+1);
00514 while (brace == std::string::npos) {
00515 // add a new line
00516 if (line_end >= conf.size()) {
00517 cvm::fatal_error ("Parse error: reached the end while "
00518 "looking for closing brace; until now "
00519 "the following was parsed: \"\n"+
00520 line+"\".\n");
00521 return false;
00522 }
00523 size_t const old_end = line.size();
00524 // data_end_absolute += old_end+1;
00525
00526 line_begin = line_end;
00527 nl = conf.find ('\n', line_begin+1);
00528 if (nl == std::string::npos)
00529 line_end = conf.size();
00530 else
00531 line_end = nl;
00532 line.append (conf, line_begin, (line_end-line_begin));
00533
00534 brace = line.find_first_of ("{}", old_end);
00535 }
00536
00537 if (line[brace] == '{') brace_count++;
00538 if (line[brace] == '}') brace_count--;
00539 }
00540
00541 // set data_begin afterthe opening brace
00542 data_begin = line.find_first_of ('{', data_begin) + 1;
00543 data_begin = line.find_first_not_of (white_space,
00544 data_begin);
00545 // set data_end before the closing brace
00546 data_end = brace;
00547 data_end = line.find_last_not_of (white_space+"}",
00548 data_end) + 1;
00549 // data_end_absolute = line_end;
00550
00551 if (data_end > line.size())
00552 data_end = line.size();
00553 }
00554
00555 data.append (line, data_begin, (data_end-data_begin));
00556
00557 if (data.size() && save_delimiters) {
00558 data_begin_pos.push_back (conf.find (data, pos+key.size()));
00559 data_end_pos.push_back (data_begin_pos.back()+data.size());
00560 // std::cerr << "key = " << key << ", data = \""
00561 // << data << "\", data_begin, data_end = "
00562 // << data_begin_pos.back() << ", " << data_end_pos.back()
00563 // << "\n";
00564 }
00565 }
00566
00567 save_pos = line_end;
00568
00569 return true;
00570 }
|
|
|
Remove all the values from the config string.
Definition at line 318 of file colvarparse.C. References data_begin_pos, and data_end_pos. Referenced by check_keywords(). 00319 {
00320 size_t offset = 0;
00321 data_begin_pos.sort();
00322 data_end_pos.sort();
00323
00324 std::list<size_t>::iterator data_begin = data_begin_pos.begin();
00325 std::list<size_t>::iterator data_end = data_end_pos.begin();
00326
00327 for ( ; (data_begin != data_begin_pos.end()) &&
00328 (data_end != data_end_pos.end()) ;
00329 data_begin++, data_end++) {
00330
00331 // std::cerr << "data_begin, data_end "
00332 // << *data_begin << ", " << *data_end
00333 // << "\n";
00334
00335 size_t const nchars = *data_end-*data_begin;
00336
00337 // std::cerr << "conf[data_begin:data_end] = \""
00338 // << std::string (conf, *data_begin - offset, nchars)
00339 // << "\"\n";
00340
00341 conf.erase (*data_begin - offset, nchars);
00342 offset += nchars;
00343
00344 // std::cerr << ("Stripped config = \"\n"+conf+"\"\n");
00345
00346 }
00347 }
|
|
|
Return a lowercased copy of the string.
Definition at line 132 of file colvarparse.h. Referenced by add_keyword(), check_keywords(), colvarbias::colvarbias(), key_lookup(), operator>>(), colvar::parse_analysis(), and pdb_field_str2enum(). 00133 {
00134 std::string out = "";
00135 for (size_t i = 0; i < in.size(); i++) {
00136 out.append (1, (char) ::tolower (in[i]) );
00137 }
00138 return out;
00139 }
|
|
|
List of legal keywords for this object: this is updated by each call to colvarparse::get_keyval() or colvarparse::key_lookup().
Definition at line 23 of file colvarparse.h. Referenced by add_keyword(), and check_keywords(). |
|
|
List of delimiters for the values of each keyword in the configuration string; all keywords will be stripped of their values before the keyword check is performed.
Definition at line 28 of file colvarparse.h. Referenced by key_lookup(), and strip_values(). |
|
|
List of delimiters for the values of each keyword in the configuration string; all keywords will be stripped of their values before the keyword check is performed.
Definition at line 33 of file colvarparse.h. Referenced by key_lookup(), and strip_values(). |
|
|
Used as a default argument by key_lookup.
Definition at line 16 of file colvarparse.C. Referenced by key_lookup(). |
|
|
Used as a default argument by key_lookup.
Definition at line 15 of file colvarparse.C. |
|
|
Whether or not to accumulate data_begin_pos and data_end_pos in key_lookup(); it may be useful to disable this after the constructor is called, because other files may be read (e.g. restart) that would mess up with the registry; in any case, nothing serious happens until check_keywords() is invoked (which should happen only right after construction).
Definition at line 41 of file colvarparse.h. |
|
|
Accepted white space delimiters, used in key_lookup().
Definition at line 13 of file colvarparse.C. Referenced by check_keywords(), and key_lookup(). |
1.3.9.1