1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
|
// -*- c++ -*-
// This file is part of the Collective Variables module (Colvars).
// The original version of Colvars and its updates are located at:
// https://github.com/colvars/colvars
// Please update all Colvars source files before making any changes.
// If you wish to distribute your changes, please submit them to the
// Colvars repository at GitHub.
#include <sstream>
#include <iostream>
#include "colvarmodule.h"
#include "colvarvalue.h"
#include "colvarparse.h"
// space & tab
char const * const colvarparse::white_space = " \t";
// definition of single-value keyword parsers
template<typename TYPE> bool colvarparse::_get_keyval_scalar_(std::string const &conf,
char const *key,
TYPE &value,
TYPE const &def_value,
Parse_Mode const parse_mode)
{
std::string data;
bool b_found = false, b_found_any = false;
size_t save_pos = 0, found_count = 0;
do {
std::string data_this = "";
b_found = key_lookup(conf, key, &data_this, &save_pos);
if (b_found) {
if (!b_found_any)
b_found_any = true;
found_count++;
data = data_this;
}
} while (b_found);
if (found_count > 1) {
cvm::error("Error: found more than one instance of \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
if (data.size()) {
std::istringstream is(data);
TYPE x(def_value);
if (is >> x) {
value = x;
} else {
cvm::error("Error: in parsing \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = "+
cvm::to_str(value)+"\n");
}
} else {
if (b_found_any) {
cvm::error("Error: improper or missing value "
"for \""+std::string(key)+"\".\n", INPUT_ERROR);
}
value = def_value;
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = "+
cvm::to_str(def_value)+" [default]\n");
}
}
return b_found_any;
}
bool colvarparse::_get_keyval_scalar_string_(std::string const &conf,
char const *key,
std::string &value,
std::string const &def_value,
Parse_Mode const parse_mode)
{
std::string data;
bool b_found = false, b_found_any = false;
size_t save_pos = 0, found_count = 0;
do {
std::string data_this = "";
b_found = key_lookup(conf, key, &data_this, &save_pos);
if (b_found) {
if (!b_found_any)
b_found_any = true;
found_count++;
data = data_this;
}
} while (b_found);
if (found_count > 1) {
cvm::error("Error: found more than one instance of \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
if (data.size()) {
std::istringstream is(data);
size_t data_count = 0;
std::string x(def_value);
while (is >> x) {
value = x;
data_count++;
}
if (data_count == 0)
cvm::error("Error: in parsing \""+
std::string(key)+"\".\n", INPUT_ERROR);
if (data_count > 1) {
cvm::error("Error: multiple values "
"are not allowed for keyword \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = \""+
cvm::to_str(value)+"\"\n");
}
} else {
if (b_found_any) {
cvm::error("Error: improper or missing value "
"for \""+std::string(key)+"\".\n", INPUT_ERROR);
}
value = def_value;
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = \""+
cvm::to_str(def_value)+"\" [default]\n");
}
}
return b_found_any;
}
// multiple-value keyword parsers
template<typename TYPE> bool colvarparse::_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)
{
std::string data;
bool b_found = false, b_found_any = false;
size_t save_pos = 0, found_count = 0;
do {
std::string data_this = "";
b_found = key_lookup(conf, key, &data_this, &save_pos);
if (b_found) {
if (!b_found_any)
b_found_any = true;
found_count++;
data = data_this;
}
} while (b_found);
if (found_count > 1) {
cvm::error("Error: found more than one instance of \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
if (data.size()) {
std::istringstream is(data);
if (values.size() == 0) {
std::vector<TYPE> x;
if (def_values.size())
x = def_values;
else
x.assign(1, TYPE());
for (size_t i = 0;
( is >> x[ ((i<x.size()) ? i : x.size()-1) ] );
i++) {
values.push_back(x[ ((i<x.size()) ? i : x.size()-1) ]);
}
} else {
size_t i = 0;
for ( ; i < values.size(); i++) {
TYPE x(values[i]);
if (is >> x) {
values[i] = x;
} else {
cvm::error("Error: in parsing \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
}
}
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = "+
cvm::to_str(values)+"\n");
}
} else {
if (b_found_any) {
cvm::error("Error: improper or missing values for \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
for (size_t i = 0; i < values.size(); i++)
values[i] = def_values[ (i > def_values.size()) ? 0 : i ];
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = "+
cvm::to_str(def_values)+" [default]\n");
}
}
return b_found_any;
}
// single-value keyword parsers
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
int &value,
int const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_<int>(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
size_t &value,
size_t const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_<size_t>(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
long &value,
long const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_<long>(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::string &value,
std::string const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_string_(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
cvm::real &value,
cvm::real const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_<cvm::real>(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
cvm::rvector &value,
cvm::rvector const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_<cvm::rvector>(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
cvm::quaternion &value,
cvm::quaternion const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_<cvm::quaternion>(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
colvarvalue &value,
colvarvalue const &def_value,
Parse_Mode const parse_mode)
{
return _get_keyval_scalar_<colvarvalue>(conf, key, value, def_value, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
bool &value,
bool const &def_value,
Parse_Mode const parse_mode)
{
std::string data;
bool b_found = false, b_found_any = false;
size_t save_pos = 0, found_count = 0;
do {
std::string data_this = "";
b_found = key_lookup(conf, key, &data_this, &save_pos);
if (b_found) {
if (!b_found_any)
b_found_any = true;
found_count++;
data = data_this;
}
} while (b_found);
if (found_count > 1) {
cvm::error("Error: found more than one instance of \""+
std::string(key)+"\".\n", INPUT_ERROR);
}
if (data.size()) {
if ( (data == std::string("on")) ||
(data == std::string("yes")) ||
(data == std::string("true")) ) {
value = true;
} else if ( (data == std::string("off")) ||
(data == std::string("no")) ||
(data == std::string("false")) ) {
value = false;
} else
cvm::error("Error: boolean values only are allowed "
"for \""+std::string(key)+"\".\n", INPUT_ERROR);
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = "+
(value ? "on" : "off")+"\n");
}
} else {
if (b_found_any) {
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = on\n");
}
value = true;
} else {
value = def_value;
if (parse_mode != parse_silent) {
cvm::log("# "+std::string(key)+" = "+
(def_value ? "on" : "off")+" [default]\n");
}
}
}
return b_found_any;
}
// multiple-value keyword parsers
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<int> &values,
std::vector<int> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<int>(conf, key, values, def_values, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<size_t> &values,
std::vector<size_t> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<size_t>(conf, key, values, def_values, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<long> &values,
std::vector<long> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<long>(conf, key, values, def_values, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<std::string> &values,
std::vector<std::string> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<std::string>(conf, key, values, def_values, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<cvm::real> &values,
std::vector<cvm::real> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<cvm::real>(conf, key, values, def_values, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<cvm::rvector> &values,
std::vector<cvm::rvector> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<cvm::rvector>(conf, key, values, def_values, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<cvm::quaternion> &values,
std::vector<cvm::quaternion> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<cvm::quaternion>(conf, key, values, def_values, parse_mode);
}
bool colvarparse::get_keyval(std::string const &conf,
char const *key,
std::vector<colvarvalue> &values,
std::vector<colvarvalue> const &def_values,
Parse_Mode const parse_mode)
{
return _get_keyval_vector_<colvarvalue>(conf, key, values, def_values, parse_mode);
}
void colvarparse::add_keyword(char const *key)
{
for (std::list<std::string>::iterator ki = allowed_keywords.begin();
ki != allowed_keywords.end(); ki++) {
if (to_lower_cppstr(std::string(key)) == *ki)
return;
}
// not found in the list
// if (cvm::debug())
// cvm::log("Registering a new keyword, \""+std::string (key)+"\".\n");
allowed_keywords.push_back(to_lower_cppstr(std::string(key)));
}
void colvarparse::strip_values(std::string &conf)
{
size_t offset = 0;
data_begin_pos.sort();
data_end_pos.sort();
std::list<size_t>::iterator data_begin = data_begin_pos.begin();
std::list<size_t>::iterator data_end = data_end_pos.begin();
for ( ; (data_begin != data_begin_pos.end()) &&
(data_end != data_end_pos.end()) ;
data_begin++, data_end++) {
// std::cerr << "data_begin, data_end "
// << *data_begin << ", " << *data_end
// << "\n";
size_t const nchars = *data_end-*data_begin;
// std::cerr << "conf[data_begin:data_end] = \""
// << std::string (conf, *data_begin - offset, nchars)
// << "\"\n";
conf.erase(*data_begin - offset, nchars);
offset += nchars;
// std::cerr << ("Stripped config = \"\n"+conf+"\"\n");
}
}
void colvarparse::clear_keyword_registry()
{
allowed_keywords.clear();
data_begin_pos.clear();
data_end_pos.clear();
}
int colvarparse::check_keywords(std::string &conf, char const *key)
{
if (cvm::debug())
cvm::log("Configuration string for \""+std::string(key)+
"\": \"\n"+conf+"\".\n");
strip_values(conf);
// after stripping, the config string has either empty lines, or
// lines beginning with a keyword
std::string line;
std::istringstream is(conf);
while (cvm::getline(is, line)) {
if (line.size() == 0)
continue;
if (line.find_first_not_of(white_space) ==
std::string::npos)
continue;
std::string uk;
std::istringstream line_is(line);
line_is >> uk;
// if (cvm::debug())
// cvm::log ("Checking the validity of \""+uk+"\" from line:\n" + line);
uk = to_lower_cppstr(uk);
bool found_keyword = false;
for (std::list<std::string>::iterator ki = allowed_keywords.begin();
ki != allowed_keywords.end(); ki++) {
if (uk == *ki) {
found_keyword = true;
break;
}
}
if (!found_keyword) {
cvm::error("Error: keyword \""+uk+"\" is not supported, "
"or not recognized in this context.\n", INPUT_ERROR);
return INPUT_ERROR;
}
}
clear_keyword_registry();
return COLVARS_OK;
}
std::istream & colvarparse::read_config_line(std::istream &is,
std::string &line)
{
cvm::getline(is, line);
config_string += line+'\n';
size_t const comment = line.find('#');
if (comment != std::string::npos) {
line.erase(comment);
}
return is;
}
std::istream & colvarparse::getline_nocomments(std::istream &is,
std::string &line)
{
cvm::getline(is, line);
size_t const comment = line.find('#');
if (comment != std::string::npos) {
line.erase(comment);
}
return is;
}
bool colvarparse::key_lookup(std::string const &conf,
char const *key_in,
std::string *data,
size_t *save_pos)
{
if (cvm::debug()) {
cvm::log("Looking for the keyword \""+std::string(key_in)+
"\" and its value.\n");
}
// add this keyword to the register (in its camelCase version)
add_keyword(key_in);
// use the lowercase version from now on
std::string const key(to_lower_cppstr(key_in));
// "conf_lower" is only used to lookup the keyword, but its value
// will be read from "conf", in order not to mess up file names
std::string const conf_lower(to_lower_cppstr(conf));
// by default, there is no value, unless we found one
if (data != NULL) {
data->clear();
}
// start from the first occurrence of key
size_t pos = conf_lower.find(key, (save_pos != NULL) ? *save_pos : 0);
// iterate over all instances of the substring until it finds it as isolated keyword
while (true) {
if (pos == std::string::npos) {
// no valid instance of the keyword has been found
if (cvm::debug()) {
cvm::log("Keyword \""+std::string(key_in)+"\" not found.\n");
}
return false;
}
bool b_isolated_left = true, b_isolated_right = true;
if (pos > 0) {
if ( std::string("\n"+std::string(white_space)+
"}").find(conf[pos-1]) ==
std::string::npos ) {
// none of the valid delimiting characters is on the left of key
b_isolated_left = false;
}
}
if (pos < conf.size()-key.size()-1) {
if ( std::string("\n"+std::string(white_space)+
"{").find(conf[pos+key.size()]) ==
std::string::npos ) {
// none of the valid delimiting characters is on the right of key
b_isolated_right = false;
}
}
// check that there are matching braces between here and the end of conf
bool const b_not_within_block = (check_braces(conf, pos) == COLVARS_OK);
bool const b_isolated = (b_isolated_left && b_isolated_right &&
b_not_within_block);
if (b_isolated) {
// found it
break;
} else {
// try the next occurrence of key
pos = conf_lower.find(key, pos+key.size());
}
}
if (save_pos != NULL) {
// save the pointer for a future call (when iterating over multiple
// valid instances of the same keyword)
*save_pos = pos + key.size();
}
// get the remainder of the line
size_t pl = conf.rfind("\n", pos);
size_t line_begin = (pl == std::string::npos) ? 0 : pos;
size_t nl = conf.find("\n", pos);
size_t line_end = (nl == std::string::npos) ? conf.size() : nl;
std::string line(conf, line_begin, (line_end-line_begin));
size_t data_begin = (to_lower_cppstr(line)).find(key) + key.size();
data_begin = line.find_first_not_of(white_space, data_begin+1);
if (data_begin != std::string::npos) {
size_t data_end = line.find_last_not_of(white_space) + 1;
data_end = (data_end == std::string::npos) ? line.size() : data_end;
size_t brace = line.find('{', data_begin); // look for an opening brace
size_t brace_last = brace;
if (brace != std::string::npos) {
// find the matching closing brace
// if (cvm::debug()) {
// cvm::log("Multi-line value, config is now \""+line+"\".\n");
// }
int brace_count = 1;
while (brace_count > 0) {
brace = line.find_first_of("{}", brace_last+1);
// find all braces within this line
while (brace < std::string::npos) {
brace_last = brace;
if (line[brace] == '{') brace_count++;
if (line[brace] == '}') brace_count--;
if (brace_count == 0) {
data_end = brace+1;
break;
}
brace = line.find_first_of("{}", brace+1);
}
if (brace_count == 0) {
data_end = brace+1;
break;
}
if (brace == std::string::npos) {
// add a new line
if (line_end >= conf.size()) {
cvm::error("Parse error: reached the end while "
"looking for closing brace; until now "
"the following was parsed: \"\n"+
line+"\".\n", INPUT_ERROR);
return false;
}
line_begin = line_end;
nl = conf.find('\n', line_begin+1);
if (nl == std::string::npos)
line_end = conf.size();
else
line_end = nl;
line.append(conf, line_begin, (line_end-line_begin));
// if (cvm::debug()) {
// cvm::log("Added a new line, config is now \""+line+"\".\n");
// }
}
if (brace_count < 0) {
cvm::error("Error: found closing brace without opening brace.\n", INPUT_ERROR);
}
}
// strip the leading and trailing braces
data_begin = line.find_first_of('{') + 1;
data_begin = line.find_first_not_of(white_space,
data_begin);
data_end = line.find_last_of('}', line.size()) - 1;
data_end = line.find_last_not_of(white_space,
data_end) + 1;
}
if (data != NULL) {
data->append(line, data_begin, (data_end-data_begin));
if (cvm::debug()) {
cvm::log("Keyword value = \""+*data+"\".\n");
}
if (data->size()) {
data_begin_pos.push_back(conf.find(*data, pos+key.size()));
data_end_pos.push_back(data_begin_pos.back()+data->size());
}
}
}
if (save_pos != NULL) *save_pos = line_end;
return true;
}
std::istream & operator>> (std::istream &is, colvarparse::read_block const &rb)
{
size_t start_pos = is.tellg();
std::string read_key, next;
if ( !(is >> read_key) || !(read_key == rb.key) ||
!(is >> next) ) {
// the requested keyword has not been found, or it is not possible
// to read data after it
is.clear();
is.seekg(start_pos, std::ios::beg);
is.setstate(std::ios::failbit);
return is;
}
if (next != "{") {
(*rb.data) = next;
return is;
}
size_t brace_count = 1;
std::string line;
while (colvarparse::getline_nocomments(is, line)) {
size_t br = 0, br_old = 0;
while ( (br = line.find_first_of("{}", br)) != std::string::npos) {
if (line[br] == '{') brace_count++;
if (line[br] == '}') brace_count--;
br_old = br;
br++;
}
if (brace_count) (*rb.data).append(line + "\n");
else {
(*rb.data).append(line, 0, br_old);
break;
}
}
if (brace_count) {
// end-of-file reached
// restore initial position
is.clear();
is.seekg(start_pos, std::ios::beg);
is.setstate(std::ios::failbit);
}
return is;
}
int colvarparse::check_braces(std::string const &conf,
size_t const start_pos)
{
int brace_count = 0;
size_t brace = start_pos;
while ((brace = conf.find_first_of("{}", brace)) != std::string::npos) {
if (conf[brace] == '{') brace_count++;
if (conf[brace] == '}') brace_count--;
brace++;
}
return (brace_count != 0) ? INPUT_ERROR : COLVARS_OK;
}
|