simplify by using utils::strdup()

This commit is contained in:
Axel Kohlmeyer
2021-03-16 08:35:06 -04:00
parent 2f8770971c
commit 5d5fc4a04d
6 changed files with 19 additions and 57 deletions

View File

@ -278,14 +278,8 @@ void ReadData::command(int narg, char **arg)
if (fix_index[nfix] < 0)
error->all(FLERR,"Fix ID for read_data does not exist");
if (strcmp(arg[iarg+2],"NULL") == 0) fix_header[nfix] = nullptr;
else {
int n = strlen(arg[iarg+2]) + 1;
fix_header[nfix] = new char[n];
strcpy(fix_header[nfix],arg[iarg+2]);
}
int n = strlen(arg[iarg+3]) + 1;
fix_section[nfix] = new char[n];
strcpy(fix_section[nfix],arg[iarg+3]);
else fix_header[nfix] = utils::strdup(arg[iarg+2]);
fix_section[nfix] = utils::strdup(arg[iarg+3]);
nfix++;
iarg += 4;

View File

@ -64,9 +64,7 @@ ReadDump::ReadDump(LAMMPS *lmp) : Pointers(lmp)
fields = nullptr;
buf = nullptr;
int n = strlen("native") + 1;
readerstyle = new char[n];
strcpy(readerstyle,"native");
readerstyle = utils::strdup("native");
nreader = 0;
readers = nullptr;
@ -182,9 +180,7 @@ void ReadDump::store_files(int nstr, char **str)
// either all or none of files must have '%' wild-card
for (int i = 0; i < nfile; i++) {
int n = strlen(str[i]) + 1;
files[i] = new char[n];
strcpy(files[i],str[i]);
files[i] = utils::strdup(str[i]);
if (i == 0) {
if (strchr(files[i],'%')) multiproc = 1;
@ -1258,9 +1254,7 @@ int ReadDump::fields_and_keywords(int narg, char **arg)
for (i = 0; i < nfield; i++)
if (type == fieldtype[i]) break;
if (i == nfield) error->all(FLERR,"Illegal read_dump command");
int n = strlen(arg[iarg+2]) + 1;
fieldlabel[i] = new char[n];
strcpy(fieldlabel[i],arg[iarg+2]);
fieldlabel[i] = utils::strdup(arg[iarg+2]);
iarg += 3;
} else if (strcmp(arg[iarg],"scaled") == 0) {
if (iarg+2 > narg) error->all(FLERR,"Illegal read_dump command");
@ -1277,9 +1271,7 @@ int ReadDump::fields_and_keywords(int narg, char **arg)
} else if (strcmp(arg[iarg],"format") == 0) {
if (iarg+2 > narg) error->all(FLERR,"Illegal read_dump command");
delete [] readerstyle;
int n = strlen(arg[iarg+1]) + 1;
readerstyle = new char[n];
strcpy(readerstyle,arg[iarg+1]);
readerstyle = utils::strdup(arg[iarg+1]);
iarg += 2;
break;
} else error->all(FLERR,"Illegal read_dump command");

View File

@ -33,13 +33,8 @@ Region::Region(LAMMPS *lmp, int /*narg*/, char **arg) :
id(nullptr), style(nullptr), contact(nullptr), list(nullptr),
xstr(nullptr), ystr(nullptr), zstr(nullptr), tstr(nullptr)
{
int n = strlen(arg[0]) + 1;
id = new char[n];
strcpy(id,arg[0]);
n = strlen(arg[1]) + 1;
style = new char[n];
strcpy(style,arg[1]);
id = utils::strdup(arg[0]);
style = utils::strdup(arg[1]);
varshape = 0;
xstr = ystr = zstr = tstr = nullptr;
@ -338,23 +333,17 @@ void Region::options(int narg, char **arg)
if (strcmp(arg[iarg+1],"NULL") != 0) {
if (strstr(arg[iarg+1],"v_") != arg[iarg+1])
error->all(FLERR,"Illegal region command");
int n = strlen(&arg[iarg+1][2]) + 1;
xstr = new char[n];
strcpy(xstr,&arg[iarg+1][2]);
xstr = utils::strdup(&arg[iarg+1][2]);
}
if (strcmp(arg[iarg+2],"NULL") != 0) {
if (strstr(arg[iarg+2],"v_") != arg[iarg+2])
error->all(FLERR,"Illegal region command");
int n = strlen(&arg[iarg+2][2]) + 1;
ystr = new char[n];
strcpy(ystr,&arg[iarg+2][2]);
ystr = utils::strdup(&arg[iarg+2][2]);
}
if (strcmp(arg[iarg+3],"NULL") != 0) {
if (strstr(arg[iarg+3],"v_") != arg[iarg+3])
error->all(FLERR,"Illegal region command");
int n = strlen(&arg[iarg+3][2]) + 1;
zstr = new char[n];
strcpy(zstr,&arg[iarg+3][2]);
zstr = utils::strdup(&arg[iarg+3][2]);
}
moveflag = 1;
iarg += 4;
@ -363,9 +352,7 @@ void Region::options(int narg, char **arg)
if (iarg+8 > narg) error->all(FLERR,"Illegal region command");
if (strstr(arg[iarg+1],"v_") != arg[iarg+1])
error->all(FLERR,"Illegal region command");
int n = strlen(&arg[iarg+1][2]) + 1;
tstr = new char[n];
strcpy(tstr,&arg[iarg+1][2]);
tstr = utils::strdup(&arg[iarg+1][2]);
point[0] = utils::numeric(FLERR,arg[iarg+2],false,lmp);
point[1] = utils::numeric(FLERR,arg[iarg+3],false,lmp);
point[2] = utils::numeric(FLERR,arg[iarg+4],false,lmp);

View File

@ -39,12 +39,9 @@ RegIntersect::RegIntersect(LAMMPS *lmp, int narg, char **arg) :
list = new int[n];
nregion = 0;
int m,iregion;
for (int iarg = 0; iarg < n; iarg++) {
m = strlen(arg[iarg+3]) + 1;
idsub[nregion] = new char[m];
strcpy(idsub[nregion],arg[iarg+3]);
iregion = domain->find_region(idsub[nregion]);
idsub[nregion] = utils::strdup(arg[iarg+3]);
int iregion = domain->find_region(idsub[nregion]);
if (iregion == -1)
error->all(FLERR,"Region intersect region ID does not exist");
list[nregion++] = iregion;
@ -123,9 +120,8 @@ void RegIntersect::init()
// re-build list of sub-regions in case other regions were deleted
// error if a sub-region was deleted
int iregion;
for (int ilist = 0; ilist < nregion; ilist++) {
iregion = domain->find_region(idsub[ilist]);
int iregion = domain->find_region(idsub[ilist]);
if (iregion == -1)
error->all(FLERR,"Region union region ID does not exist");
list[ilist] = iregion;

View File

@ -40,12 +40,9 @@ RegUnion::RegUnion(LAMMPS *lmp, int narg, char **arg) : Region(lmp, narg, arg),
list = new int[n];
nregion = 0;
int m,iregion;
for (int iarg = 0; iarg < n; iarg++) {
m = strlen(arg[iarg+3]) + 1;
idsub[nregion] = new char[m];
strcpy(idsub[nregion],arg[iarg+3]);
iregion = domain->find_region(idsub[nregion]);
idsub[nregion] = utils::strdup(arg[iarg+3]);
int iregion = domain->find_region(idsub[nregion]);
if (iregion == -1)
error->all(FLERR,"Region union region ID does not exist");
list[nregion++] = iregion;
@ -117,9 +114,8 @@ void RegUnion::init()
// re-build list of sub-regions in case other regions were deleted
// error if a sub-region was deleted
int iregion;
for (int ilist = 0; ilist < nregion; ilist++) {
iregion = domain->find_region(idsub[ilist]);
int iregion = domain->find_region(idsub[ilist]);
if (iregion == -1)
error->all(FLERR,"Region union region ID does not exist");
list[ilist] = iregion;

View File

@ -96,10 +96,7 @@ char *TextFileReader::next_line(int nparams) {
if (ignore_comments && (ptr = strchr(line, '#'))) *ptr = '\0';
nwords = utils::count_words(line);
if (nwords > 0) {
n = strlen(line);
}
if (nwords > 0) n = strlen(line);
while (nwords == 0 || nwords < nparams) {
char *ptr = fgets(&line[n], MAXLINE - n, fp);