move Input::expand_args() to utils::expand_args()

This commit is contained in:
Axel Kohlmeyer 2020-08-29 18:32:10 -04:00
parent fcd0b9f78f
commit 741a1d1fc9
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
14 changed files with 19 additions and 149 deletions

View File

@ -54,7 +54,7 @@ ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) :
int iarg = 4;
int expand = 0;
char **earg;
int nargnew = input->expand_args(narg-iarg,&arg[iarg],1,earg);
int nargnew = utils::expand_args(FLERR,narg-iarg,&arg[iarg],1,earg,lmp);
if (earg != &arg[iarg]) expand = 1;
arg = earg;

View File

@ -22,6 +22,7 @@
#include "variable.h"
#include "memory.h"
#include "error.h"
#include "utils.h"
using namespace LAMMPS_NS;
@ -80,7 +81,7 @@ ComputeGlobalAtom::ComputeGlobalAtom(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
int nargnew = input->expand_args(narg-iarg,&arg[iarg],1,earg);
int nargnew = utils::expand_args(FLERR,narg-iarg,&arg[iarg],1,earg,lmp);
if (earg != &arg[iarg]) expand = 1;
arg = earg;

View File

@ -25,6 +25,7 @@
#include "variable.h"
#include "memory.h"
#include "error.h"
#include "utils.h"
using namespace LAMMPS_NS;
@ -78,7 +79,7 @@ ComputeReduce::ComputeReduce(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
int nargnew = input->expand_args(narg-iarg,&arg[iarg],1,earg);
int nargnew = utils::expand_args(FLERR,narg-iarg,&arg[iarg],1,earg,lmp);
if (earg != &arg[iarg]) expand = 1;
arg = earg;

View File

@ -25,6 +25,7 @@
#include "variable.h"
#include "memory.h"
#include "error.h"
#include "utils.h"
using namespace LAMMPS_NS;
@ -64,7 +65,7 @@ ComputeReduceChunk::ComputeReduceChunk(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
int nargnew = input->expand_args(narg-iarg,&arg[iarg],1,earg);
int nargnew = utils::expand_args(FLERR,narg-iarg,&arg[iarg],1,earg,lmp);
if (earg != &arg[iarg]) expand = 1;
arg = earg;

View File

@ -29,6 +29,7 @@
#include "error.h"
#include "update.h"
#include "variable.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -76,7 +77,7 @@ DumpCustom::DumpCustom(LAMMPS *lmp, int narg, char **arg) :
// nfield may be shrunk below if extra optional args exist
expand = 0;
nfield = nargnew = input->expand_args(narg-5,&arg[5],1,earg);
nfield = nargnew = utils::expand_args(FLERR,narg-5,&arg[5],1,earg,lmp);
if (earg != &arg[5]) expand = 1;
// allocate field vectors

View File

@ -24,6 +24,7 @@
#include "memory.h"
#include "error.h"
#include "force.h"
#include "utils.h"
using namespace LAMMPS_NS;
@ -57,7 +58,7 @@ DumpLocal::DumpLocal(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
nfield = input->expand_args(nfield,&arg[5],1,earg);
nfield = utils::expand_args(FLERR,nfield,&arg[5],1,earg,lmp);
if (earg != &arg[5]) expand = 1;

View File

@ -23,6 +23,7 @@
#include "memory.h"
#include "error.h"
#include "force.h"
#include "utils.h"
using namespace LAMMPS_NS;
using namespace FixConst;
@ -51,7 +52,7 @@ FixAveAtom::FixAveAtom(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
nvalues = input->expand_args(nvalues,&arg[6],1,earg);
nvalues = utils::expand_args(FLERR,nvalues,&arg[6],1,earg,lmp);
if (earg != &arg[6]) expand = 1;
arg = earg;

View File

@ -72,7 +72,7 @@ FixAveChunk::FixAveChunk(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
int nargnew = input->expand_args(narg-7,&arg[7],1,earg);
int nargnew = utils::expand_args(FLERR,narg-7,&arg[7],1,earg,lmp);
if (earg != &arg[7]) expand = 1;
arg = earg;

View File

@ -65,7 +65,7 @@ FixAveCorrelate::FixAveCorrelate(LAMMPS * lmp, int narg, char **arg):
int expand = 0;
char **earg;
int nargnew = input->expand_args(narg-6,&arg[6],0,earg);
int nargnew = utils::expand_args(FLERR,narg-6,&arg[6],0,earg,lmp);
if (earg != &arg[6]) expand = 1;
arg = earg;

View File

@ -107,7 +107,7 @@ FixAveHisto::FixAveHisto(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
nvalues = input->expand_args(nvalues,&arg[9],mode,earg);
nvalues = utils::expand_args(FLERR,nvalues,&arg[9],mode,earg,lmp);
if (earg != &arg[9]) expand = 1;
arg = earg;

View File

@ -88,7 +88,7 @@ FixAveTime::FixAveTime(LAMMPS *lmp, int narg, char **arg) :
int expand = 0;
char **earg;
nvalues = input->expand_args(nvalues,&arg[6],mode,earg);
nvalues = utils::expand_args(FLERR,nvalues,&arg[6],mode,earg,lmp);
if (earg != &arg[6]) expand = 1;
arg = earg;

View File

@ -599,141 +599,6 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag)
strcpy(str,str2);
}
/* ----------------------------------------------------------------------
expand arg to earg, for arguments with syntax c_ID[*] or f_ID[*]
fields to consider in input arg range from iarg to narg
return new expanded # of values, and copy them w/out "*" into earg
if any expansion occurs, earg is new allocation, must be freed by caller
if no expansion occurs, earg just points to arg, caller need not free
------------------------------------------------------------------------- */
int Input::expand_args(int narg, char **arg, int mode, char **&earg)
{
int n,iarg,index,nlo,nhi,nmax,expandflag,icompute,ifix;
char *ptr1,*ptr2,*str;
ptr1 = NULL;
for (iarg = 0; iarg < narg; iarg++) {
ptr1 = strchr(arg[iarg],'*');
if (ptr1) break;
}
if (!ptr1) {
earg = arg;
return narg;
}
// maxarg should always end up equal to newarg, so caller can free earg
int maxarg = narg-iarg;
earg = (char **) memory->smalloc(maxarg*sizeof(char *),"input:earg");
int newarg = 0;
for (iarg = 0; iarg < narg; iarg++) {
expandflag = 0;
if (strncmp(arg[iarg],"c_",2) == 0 ||
strncmp(arg[iarg],"f_",2) == 0) {
ptr1 = strchr(&arg[iarg][2],'[');
if (ptr1) {
ptr2 = strchr(ptr1,']');
if (ptr2) {
*ptr2 = '\0';
if (strchr(ptr1,'*')) {
if (arg[iarg][0] == 'c') {
*ptr1 = '\0';
icompute = modify->find_compute(&arg[iarg][2]);
*ptr1 = '[';
// check for global vector/array, peratom array, local array
if (icompute >= 0) {
if (mode == 0 && modify->compute[icompute]->vector_flag) {
nmax = modify->compute[icompute]->size_vector;
expandflag = 1;
} else if (mode == 1 && modify->compute[icompute]->array_flag) {
nmax = modify->compute[icompute]->size_array_cols;
expandflag = 1;
} else if (modify->compute[icompute]->peratom_flag &&
modify->compute[icompute]->size_peratom_cols) {
nmax = modify->compute[icompute]->size_peratom_cols;
expandflag = 1;
} else if (modify->compute[icompute]->local_flag &&
modify->compute[icompute]->size_local_cols) {
nmax = modify->compute[icompute]->size_local_cols;
expandflag = 1;
}
}
} else if (arg[iarg][0] == 'f') {
*ptr1 = '\0';
ifix = modify->find_fix(&arg[iarg][2]);
*ptr1 = '[';
// check for global vector/array, peratom array, local array
if (ifix >= 0) {
if (mode == 0 && modify->fix[ifix]->vector_flag) {
nmax = modify->fix[ifix]->size_vector;
expandflag = 1;
} else if (mode == 1 && modify->fix[ifix]->array_flag) {
nmax = modify->fix[ifix]->size_array_cols;
expandflag = 1;
} else if (modify->fix[ifix]->peratom_flag &&
modify->fix[ifix]->size_peratom_cols) {
nmax = modify->fix[ifix]->size_peratom_cols;
expandflag = 1;
} else if (modify->fix[ifix]->local_flag &&
modify->fix[ifix]->size_local_cols) {
nmax = modify->fix[ifix]->size_local_cols;
expandflag = 1;
}
}
}
}
*ptr2 = ']';
}
}
}
if (expandflag) {
*ptr2 = '\0';
force->bounds(FLERR,ptr1+1,nmax,nlo,nhi);
*ptr2 = ']';
if (newarg+nhi-nlo+1 > maxarg) {
maxarg += nhi-nlo+1;
earg = (char **)
memory->srealloc(earg,maxarg*sizeof(char *),"input:earg");
}
for (index = nlo; index <= nhi; index++) {
n = strlen(arg[iarg]) + 16; // 16 = space for large inserted integer
str = earg[newarg] = new char[n];
strncpy(str,arg[iarg],ptr1+1-arg[iarg]);
sprintf(&str[ptr1+1-arg[iarg]],"%d",index);
strcat(str,ptr2);
newarg++;
}
} else {
if (newarg == maxarg) {
maxarg++;
earg = (char **)
memory->srealloc(earg,maxarg*sizeof(char *),"input:earg");
}
n = strlen(arg[iarg]) + 1;
earg[newarg] = new char[n];
strcpy(earg[newarg],arg[iarg]);
newarg++;
}
}
//printf("NEWARG %d\n",newarg);
//for (int i = 0; i < newarg; i++)
// printf(" arg %d: %s\n",i,earg[i]);
return newarg;
}
/* ----------------------------------------------------------------------
return number of triple quotes in line
------------------------------------------------------------------------- */
@ -1139,7 +1004,7 @@ void Input::partition()
else error->all(FLERR,"Illegal partition command");
int ilo,ihi;
force->bounds(FLERR,arg[1],universe->nworlds,ilo,ihi);
utils::bounds(FLERR,arg[1],1,universe->nworlds,ilo,ihi,error);
// copy original line to copy, since will use strtok() on it
// ptr = start of 4th word

View File

@ -38,7 +38,6 @@ class Input : protected Pointers {
char *one(const std::string&); // process a single command
void substitute(char *&, char *&, int &, int &, int);
// substitute for variables in a string
int expand_args(int, char **, int, char **&); // expand args due to wildcard
void write_echo(const std::string &); // send text to active echo file pointers
protected:

View File

@ -126,7 +126,7 @@ Thermo::Thermo(LAMMPS *lmp, int narg, char **arg) : Pointers(lmp)
int expand = 0;
char **earg;
int nvalues = input->expand_args(narg-1,&arg[1],0,earg);
int nvalues = utils::expand_args(FLERR,narg-1,&arg[1],0,earg,lmp);
if (earg != &arg[1]) expand = 1;
line = new char[256+nvalues*64];