simplify. use utils::strdup() more.

This commit is contained in:
Axel Kohlmeyer
2021-09-17 22:51:59 -04:00
parent 385220fd4b
commit a71b77c06e
11 changed files with 37 additions and 88 deletions

View File

@ -156,18 +156,14 @@ FixAveCorrelateLong::FixAveCorrelateLong(LAMMPS * lmp, int narg, char **arg):
} else if (strcmp(arg[iarg],"title1") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal fix ave/correlate/long command");
delete [] title1;
int n = strlen(arg[iarg+1]) + 1;
title1 = new char[n];
strcpy(title1,arg[iarg+1]);
delete[] title1;
title1 = utils::strdup(arg[iarg+1]);
iarg += 2;
} else if (strcmp(arg[iarg],"title2") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal fix ave/correlate/long command");
delete [] title2;
int n = strlen(arg[iarg+1]) + 1;
title2 = new char[n];
strcpy(title2,arg[iarg+1]);
delete[] title2;
title2 = utils::strdup(arg[iarg+1]);
iarg += 2;
} else error->all(FLERR,"Illegal fix ave/correlate/long command");
}

View File

@ -57,8 +57,7 @@ FixNumDiff::FixNumDiff(LAMMPS *lmp, int narg, char **arg) :
error->all(FLERR,"Illegal fix numdiff command");
std::string cmd = id + std::string("_pe");
id_pe = new char[cmd.size()+1];
strcpy(id_pe,cmd.c_str());
id_pe = utils::strdup(cmd);
cmd += " all pe";
modify->add_compute(cmd);

View File

@ -394,12 +394,8 @@ void PairEAMAlloyGPU::read_file(char *filename)
error->one(FLERR,"Incorrect element names in EAM potential file");
file->elements = new char*[file->nelements];
for (int i = 0; i < file->nelements; i++) {
const std::string word = values.next_string();
const int n = word.length() + 1;
file->elements[i] = new char[n];
strcpy(file->elements[i], word.c_str());
}
for (int i = 0; i < file->nelements; i++)
file->elements[i] = utils::strdup(values.next_string());
//

View File

@ -395,9 +395,7 @@ void PairEAMFSGPU::read_file(char *filename)
file->elements = new char*[file->nelements];
for (int i = 0; i < file->nelements; i++) {
const std::string word = values.next_string();
const int n = word.length() + 1;
file->elements[i] = new char[n];
strcpy(file->elements[i], word.c_str());
file->elements[i] = utils::strdup(word);
}
//
@ -408,6 +406,7 @@ void PairEAMFSGPU::read_file(char *filename)
file->nr = values.next_int();
file->dr = values.next_double();
file->cut = values.next_double();
rhomax = 0.0;
if ((file->nrho <= 0) || (file->nr <= 0) || (file->dr <= 0.0))
error->one(FLERR,"Invalid EAM potential file");

View File

@ -37,7 +37,7 @@ enum { CN, BR, FN, FT };
/* ---------------------------------------------------------------------- */
ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg), tensor_style(NULL)
Compute(lmp, narg, arg), tensor_style(nullptr)
{
if (narg < 3) error->all(FLERR, "Illegal compute fabric command");
@ -80,9 +80,8 @@ ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) :
if (iarg + 1 >= narg) error->all(FLERR, "Invalid keyword in compute fabric command");
int ntypes = atom->ntypes;
int i, j, itype, jtype, in, jn, infield, jnfield;
int i, j, itype, jtype;
int inlo, inhi, jnlo, jnhi;
char *istr, *jstr;
if (!type_filter) {
memory->create(type_filter, ntypes + 1, ntypes + 1, "compute/fabric:type_filter");
@ -91,24 +90,12 @@ ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) :
}
}
in = strlen(arg[iarg + 1]) + 1;
istr = new char[in];
strcpy(istr, arg[iarg + 1]);
std::vector<std::string> iwords = Tokenizer(istr, ",").as_vector();
infield = iwords.size();
jn = strlen(arg[iarg + 2]) + 1;
jstr = new char[jn];
strcpy(jstr, arg[iarg + 2]);
std::vector<std::string> jwords = Tokenizer(jstr, ",").as_vector();
jnfield = jwords.size();
for (i = 0; i < infield; i++) {
const char *ifield = iwords[i].c_str();
std::vector<std::string> iwords = Tokenizer(arg[iarg+1], ",").as_vector();
std::vector<std::string> jwords = Tokenizer(arg[iarg+2], ",").as_vector();
for (const auto &ifield : iwords) {
utils::bounds(FLERR, ifield, 1, ntypes, inlo, inhi, error);
for (j = 0; j < jnfield; j++) {
const char *jfield = jwords[j].c_str();
for (const auto &jfield : jwords) {
utils::bounds(FLERR, jfield, 1, ntypes, jnlo, jnhi, error);
for (itype = inlo; itype <= inhi; itype++) {
@ -119,10 +106,6 @@ ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) :
}
}
}
delete[] istr;
delete[] jstr;
iarg += 2;
} else
error->all(FLERR, "Illegal compute fabric command");
@ -152,7 +135,7 @@ ComputeFabric::~ComputeFabric()
void ComputeFabric::init()
{
if (force->pair == NULL) error->all(FLERR, "No pair style is defined for compute fabric");
if (force->pair == nullptr) error->all(FLERR, "No pair style is defined for compute fabric");
if (force->pair->single_enable == 0 && (fn_flag || ft_flag))
error->all(FLERR, "Pair style does not support compute fabric normal or tangential force");

View File

@ -46,8 +46,7 @@ FixWallGranRegion::FixWallGranRegion(LAMMPS *lmp, int narg, char **arg) :
if (iregion == -1)
error->all(FLERR,"Region ID for fix wall/gran/region does not exist");
region = domain->regions[iregion];
region_style = new char[strlen(region->style)+1];
strcpy(region_style,region->style);
region_style = utils::strdup(region->style);
nregion = region->nregion;
tmax = domain->regions[iregion]->tmax;

View File

@ -54,8 +54,7 @@ FixHalt::FixHalt(LAMMPS *lmp, int narg, char **arg) :
attribute = TLIMIT;
} else if (strcmp(arg[iarg],"diskfree") == 0) {
attribute = DISKFREE;
dlimit_path = new char[2];
strcpy(dlimit_path,".");
dlimit_path = utils::strdup(".");
} else if (strcmp(arg[iarg],"bondmax") == 0) {
attribute = BONDMAX;
} else {

View File

@ -737,9 +737,7 @@ void Force::store_style(char *&str, const std::string &style, int sflag)
if (sflag == 1) estyle += std::string("/") + lmp->suffix;
else if (sflag == 2) estyle += std::string("/") + lmp->suffix2;
else if (sflag == 3) estyle += std::string("/") + lmp->suffixp;
str = new char[estyle.size()+1];
strcpy(str,estyle.c_str());
str = utils::strdup(estyle);
}
/* ----------------------------------------------------------------------

View File

@ -412,17 +412,11 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
if (strcmp(arg[iarg+1],"hybrid") == 0) {
if (iarg+4 > narg)
error->universe_all(FLERR,"Invalid command-line argument");
int n = strlen(arg[iarg+2]) + 1;
suffix = new char[n];
strcpy(suffix,arg[iarg+2]);
n = strlen(arg[iarg+3]) + 1;
suffix2 = new char[n];
strcpy(suffix2,arg[iarg+3]);
suffix = utils::strdup(arg[iarg+2]);
suffix2 = utils::strdup(arg[iarg+3]);
iarg += 4;
} else {
int n = strlen(arg[iarg+1]) + 1;
suffix = new char[n];
strcpy(suffix,arg[iarg+1]);
suffix = utils::strdup(arg[iarg+1]);
iarg += 2;
}
@ -870,14 +864,12 @@ void LAMMPS::post_create()
// invoke any command-line package commands
if (num_package) {
char str[256];
std::string str;
for (int i = 0; i < num_package; i++) {
strcpy(str,"package");
str = "package";
for (char **ptr = packargs[i]; *ptr != nullptr; ++ptr) {
if (strlen(str) + strlen(*ptr) + 2 > 256)
error->all(FLERR,"Too many -pk arguments in command line");
strcat(str," ");
strcat(str,*ptr);
str += " ";
str += *ptr;
}
input->one(str);
}

View File

@ -1887,11 +1887,12 @@ int Neighbor::choose_pair(NeighRequest *rq)
// convert newton request to newtflag = on or off
int newtflag;
if (rq->newton == 0 && newton_pair) newtflag = 1;
else if (rq->newton == 0 && !newton_pair) newtflag = 0;
else if (rq->newton == 1) newtflag = 1;
else if (rq->newton == 2) newtflag = 0;
bool newtflag;
if (rq->newton == 0 && newton_pair) newtflag = true;
else if (rq->newton == 0 && !newton_pair) newtflag = false;
else if (rq->newton == 1) newtflag = true;
else if (rq->newton == 2) newtflag = false;
else error->all(FLERR,"Illegal 'newton' flag in neighbor list request");
int molecular = atom->molecular;
@ -2565,27 +2566,16 @@ void Neighbor::modify_params(int narg, char **arg)
type2collection[i] = -1;
// For each custom range, define mapping for types in interval
int nfield;
char *str;
for (i = 0; i < ncollections; i++){
n = strlen(arg[iarg+2+i]) + 1;
str = new char[n];
strcpy(str,arg[iarg+2+i]);
std::vector<std::string> words = Tokenizer(str, ",").as_vector();
nfield = words.size();
for (j = 0; j < nfield; j++) {
const char * field = words[j].c_str();
utils::bounds(FLERR,field,1,ntypes,nlo,nhi,error);
std::vector<std::string> words = Tokenizer(arg[iarg+2+i], ",").as_vector();
for (const auto &word : words) {
utils::bounds(FLERR,word,1,ntypes,nlo,nhi,error);
for (k = nlo; k <= nhi; k++) {
if (type2collection[k] != -1)
error->all(FLERR,"Type specified more than once in collection/type commnd");
type2collection[k] = i;
}
}
delete [] str;
}
// Check for undefined atom type

View File

@ -335,8 +335,7 @@ void Update::create_integrate(int narg, char **arg, int trysuffix)
if (sflag == 1) estyle += lmp->suffix;
else estyle += lmp->suffix2;
}
integrate_style = new char[estyle.size()+1];
strcpy(integrate_style,estyle.c_str());
integrate_style = utils::strdup(estyle);
}
/* ----------------------------------------------------------------------
@ -406,8 +405,7 @@ void Update::create_minimize(int narg, char **arg, int trysuffix)
if (sflag == 1) estyle += lmp->suffix;
else estyle += lmp->suffix2;
}
minimize_style = new char[estyle.size()+1];
strcpy(minimize_style,estyle.c_str());
minimize_style = utils::strdup(estyle);
}
/* ----------------------------------------------------------------------