update parser for label2type() function with better argument checking and handling of ',' in labels
This commit is contained in:
@ -3953,6 +3953,8 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t
|
|||||||
// parse contents for comma-separated args
|
// parse contents for comma-separated args
|
||||||
// narg = number of args, args = strings between commas
|
// narg = number of args, args = strings between commas
|
||||||
|
|
||||||
|
std::string contents_copy(contents); // for label2type
|
||||||
|
|
||||||
char *args[MAXFUNCARG];
|
char *args[MAXFUNCARG];
|
||||||
int narg = parse_args(contents,args);
|
int narg = parse_args(contents,args);
|
||||||
|
|
||||||
@ -4377,15 +4379,17 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t
|
|||||||
} else argstack[nargstack++] = value;
|
} else argstack[nargstack++] = value;
|
||||||
|
|
||||||
} else if (strcmp(word,"label2type") == 0) {
|
} else if (strcmp(word,"label2type") == 0) {
|
||||||
if (narg != 2) print_var_error(FLERR,"Invalid label2type() function syntax in variable formula",ivar);
|
|
||||||
|
|
||||||
if (!atom->labelmapflag)
|
if (!atom->labelmapflag)
|
||||||
print_var_error(FLERR,"Cannot use label2type() function without a labelmap",ivar);
|
print_var_error(FLERR,"Cannot use label2type() function without a labelmap",ivar);
|
||||||
|
|
||||||
int value;
|
auto pos = contents_copy.find_first_of(',');
|
||||||
std::string kind = args[0];
|
if (pos == std::string::npos)
|
||||||
std::string typestr = utils::trim(utils::utf8_subst(args[1]));
|
print_var_error(FLERR, fmt::format("Invalid label2type({}) function in variable formula",
|
||||||
|
contents_copy), ivar);
|
||||||
|
std::string typestr = contents_copy.substr(pos+1);
|
||||||
|
std::string kind = contents_copy.substr(0, pos);
|
||||||
|
|
||||||
|
int value = -1;
|
||||||
if (kind == "atom") {
|
if (kind == "atom") {
|
||||||
value = atom->lmap->find(typestr,Atom::ATOM);
|
value = atom->lmap->find(typestr,Atom::ATOM);
|
||||||
} else if (kind == "bond") {
|
} else if (kind == "bond") {
|
||||||
@ -4396,6 +4400,8 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t
|
|||||||
value = atom->lmap->find(typestr,Atom::DIHEDRAL);
|
value = atom->lmap->find(typestr,Atom::DIHEDRAL);
|
||||||
} else if (kind == "improper") {
|
} else if (kind == "improper") {
|
||||||
value = atom->lmap->find(typestr,Atom::IMPROPER);
|
value = atom->lmap->find(typestr,Atom::IMPROPER);
|
||||||
|
} else {
|
||||||
|
print_var_error(FLERR, fmt::format("Invalid type kind {} in variable formula",kind), ivar);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value == -1)
|
if (value == -1)
|
||||||
|
|||||||
Reference in New Issue
Block a user