add utils::logical() function to complement the *numeric() functions

This commit is contained in:
Axel Kohlmeyer
2021-09-16 17:52:51 -04:00
parent 0dd35bdb66
commit cef100991f
5 changed files with 187 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include "tokenizer.h"
#include "update.h"
#include <algorithm>
#include <cctype>
#include <cerrno>
#include <cstring>
@ -345,6 +346,48 @@ std::string utils::check_packages_for_style(const std::string &style, const std:
return errmsg;
}
/* ----------------------------------------------------------------------
read a boolean value from a string
transform to lower case before checking
generate an error if is not a legitimate boolean
called by various commands to check validity of their arguments
------------------------------------------------------------------------- */
bool utils::logical(const char *file, int line, const char *str, bool do_abort, LAMMPS *lmp)
{
int n = 0;
if (str) n = strlen(str);
if (n == 0) {
const char msg[] = "Expected boolean parameter instead of NULL or empty string "
"in input script or data file";
if (do_abort)
lmp->error->one(file, line, msg);
else
lmp->error->all(file, line, msg);
}
// convert to ascii and lowercase
std::string buf(str);
if (has_utf8(buf)) buf = utf8_subst(buf);
std::transform(buf.begin(), buf.end(), buf.begin(), tolower);
bool rv = false;
if ((buf == "yes") || (buf == "on") || (buf == "true") || (buf == "1")) {
rv = true;
} else if ((buf == "no") || (buf == "off") || (buf == "false") || (buf == "0")) {
rv = false;
} else {
std::string msg("Expected boolean parameter instead of '");
msg += buf + "' in input script or data file";
if (do_abort)
lmp->error->one(file, line, msg);
else
lmp->error->all(file, line, msg);
}
return rv;
}
/* ----------------------------------------------------------------------
read a floating point value from a string
generate an error if not a legitimate floating point value