simplify parse_vector() method by using Tokenizer class

This commit is contained in:
Axel Kohlmeyer
2023-05-04 21:14:25 -04:00
parent 366b64571d
commit 1b7f9439ad
2 changed files with 8 additions and 62 deletions

View File

@ -4770,43 +4770,21 @@ void Variable::atom_vector(char *word, Tree **tree, Tree **treestack, int &ntree
void Variable::parse_vector(int ivar, char *str) void Variable::parse_vector(int ivar, char *str)
{ {
// unlimited allows for any vector length // check for square brackets, remove them, and split into vector
int nstr = strlen(str)-1;
char **args; if ((str[0] != '[') || (str[nstr] != ']'))
int nvec = parse_args_unlimited(str,args); error->all(FLERR,"Vector variable formula lacks opening or closing brace: {}", str);
std::vector<std::string> args = Tokenizer(std::string(str+1, str+nstr), ",").as_vector();
if (args[nvec-1][strlen(args[nvec-1])-1] != ']')
error->all(FLERR,"Vector variable formula lacks closing brace: {}",str);
int nvec = args.size();
vecs[ivar].n = nvec; vecs[ivar].n = nvec;
vecs[ivar].nmax = nvec; vecs[ivar].nmax = nvec;
vecs[ivar].currentstep = -1; vecs[ivar].currentstep = -1;
memory->destroy(vecs[ivar].values); memory->destroy(vecs[ivar].values);
memory->create(vecs[ivar].values,vecs[ivar].nmax,"variable:values"); memory->create(vecs[ivar].values,vecs[ivar].nmax,"variable:values");
char *onearg,*copy; for (int i = 0; i < nvec; i++)
for (int i = 0; i < nvec; i++) { vecs[ivar].values[i] = utils::numeric(FLERR, args[i], false, lmp);
onearg = utils::strdup(args[i]);
if (onearg[0] == '[') {
copy = utils::strdup(utils::trim(&onearg[1]));
delete[] onearg;
onearg = copy;
}
if (onearg[strlen(onearg)-1] == ']') {
onearg[strlen(onearg)-1] = '\0';
copy = utils::strdup(utils::trim(onearg));
delete[] onearg;
onearg = copy;
}
vecs[ivar].values[i] = utils::numeric(FLERR, onearg, false, lmp);
delete[] onearg;
}
// delete stored args
for (int i = 0; i < nvec; i++) delete[] args[i];
memory->sfree(args);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -4834,37 +4812,6 @@ int Variable::parse_args(char *str, char **args)
return narg; return narg;
} }
/* ----------------------------------------------------------------------
parse string for comma-separated args
store copy of each arg in args array
grow args array as large as needed
------------------------------------------------------------------------- */
int Variable::parse_args_unlimited(char *str, char **&args)
{
char *ptrnext;
int narg = 0;
char *ptr = str;
int maxarg = 0;
args = nullptr;
while (ptr) {
ptrnext = find_next_comma(ptr);
if (ptrnext) *ptrnext = '\0';
if (narg == maxarg) {
maxarg += CHUNK;
args = (char **) memory->srealloc(args,maxarg*sizeof(char *),"variable:args");
}
args[narg] = utils::strdup(utils::trim(ptr));
narg++;
ptr = ptrnext;
if (ptr) ptr++;
}
return narg;
}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
find next comma in str find next comma in str
skip commas inside one or more nested parenthesis skip commas inside one or more nested parenthesis

View File

@ -147,7 +147,6 @@ class Variable : protected Pointers {
int is_atom_vector(char *); int is_atom_vector(char *);
void atom_vector(char *, Tree **, Tree **, int &); void atom_vector(char *, Tree **, Tree **, int &);
int parse_args(char *, char **); int parse_args(char *, char **);
int parse_args_unlimited(char *, char **&);
void parse_vector(int, char *); void parse_vector(int, char *);
char *find_next_comma(char *); char *find_next_comma(char *);
void print_var_error(const std::string &, int, const std::string &, int, int global = 1); void print_var_error(const std::string &, int, const std::string &, int, int global = 1);