diff --git a/src/fix.cpp b/src/fix.cpp index 8cf13f6f1d..b9d9fb3969 100644 --- a/src/fix.cpp +++ b/src/fix.cpp @@ -42,21 +42,15 @@ Fix::Fix(LAMMPS *lmp, int /*narg*/, char **arg) : // fix ID, group, and style // ID must be all alphanumeric chars or underscores - int n = strlen(arg[0]) + 1; - id = new char[n]; - strcpy(id,arg[0]); - - for (int i = 0; i < n-1; i++) - if (!isalnum(id[i]) && id[i] != '_') - error->all(FLERR,"Fix ID must be alphanumeric or underscore characters"); + id = utils::strdup(arg[0]); + if (!utils::is_id(id)) + error->all(FLERR,"Fix ID must be alphanumeric or underscore characters"); igroup = group->find(arg[1]); if (igroup == -1) error->all(FLERR,"Could not find fix group ID"); groupbit = group->bitmask[igroup]; - n = strlen(arg[2]) + 1; - style = new char[n]; - strcpy(style,arg[2]); + style = utils::strdup(arg[2]); restart_global = restart_peratom = restart_file = 0; force_reneighbor = 0; diff --git a/src/fix_ave_chunk.cpp b/src/fix_ave_chunk.cpp index 3b612aeb73..d3220dd9df 100644 --- a/src/fix_ave_chunk.cpp +++ b/src/fix_ave_chunk.cpp @@ -57,9 +57,7 @@ FixAveChunk::FixAveChunk(LAMMPS *lmp, int narg, char **arg) : nrepeat = utils::inumeric(FLERR,arg[4],false,lmp); nfreq = utils::inumeric(FLERR,arg[5],false,lmp); - int n = strlen(arg[6]) + 1; - idchunk = new char[n]; - strcpy(idchunk,arg[6]); + idchunk = utils::strdup(arg[6]); global_freq = nfreq; no_change_box = 1; @@ -190,9 +188,7 @@ FixAveChunk::FixAveChunk(LAMMPS *lmp, int narg, char **arg) : if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/chunk command"); biasflag = 1; - int n = strlen(arg[iarg+1]) + 1; - id_bias = new char[n]; - strcpy(id_bias,arg[iarg+1]); + id_bias = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"adof") == 0) { if (iarg+2 > narg) @@ -220,31 +216,23 @@ FixAveChunk::FixAveChunk(LAMMPS *lmp, int narg, char **arg) : } else if (strcmp(arg[iarg],"format") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/chunk command"); delete [] format_user; - int n = strlen(arg[iarg+1]) + 2; - format_user = new char[n]; - sprintf(format_user," %s",arg[iarg+1]); + format_user = utils::strdup(arg[iarg+1]); format = format_user; iarg += 2; } else if (strcmp(arg[iarg],"title1") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/chunk command"); delete [] title1; - int n = strlen(arg[iarg+1]) + 1; - title1 = new char[n]; - strcpy(title1,arg[iarg+1]); + 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/chunk command"); delete [] title2; - int n = strlen(arg[iarg+1]) + 1; - title2 = new char[n]; - strcpy(title2,arg[iarg+1]); + title2 = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"title3") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/chunk command"); delete [] title3; - int n = strlen(arg[iarg+1]) + 1; - title3 = new char[n]; - strcpy(title3,arg[iarg+1]); + title3 = utils::strdup(arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal fix ave/chunk command"); } diff --git a/src/fix_ave_correlate.cpp b/src/fix_ave_correlate.cpp index efd06a182a..81ca94cdc8 100644 --- a/src/fix_ave_correlate.cpp +++ b/src/fix_ave_correlate.cpp @@ -139,23 +139,17 @@ FixAveCorrelate::FixAveCorrelate(LAMMPS * lmp, int narg, char **arg): } else if (strcmp(arg[iarg],"title1") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/correlate command"); delete [] title1; - int n = strlen(arg[iarg+1]) + 1; - title1 = new char[n]; - strcpy(title1,arg[iarg+1]); + 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 command"); delete [] title2; - int n = strlen(arg[iarg+1]) + 1; - title2 = new char[n]; - strcpy(title2,arg[iarg+1]); + title2 = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"title3") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/correlate command"); delete [] title3; - int n = strlen(arg[iarg+1]) + 1; - title3 = new char[n]; - strcpy(title3,arg[iarg+1]); + title3 = utils::strdup(arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal fix ave/correlate command"); } diff --git a/src/fix_ave_histo.cpp b/src/fix_ave_histo.cpp index 0d82fd6b04..0f6943ac31 100644 --- a/src/fix_ave_histo.cpp +++ b/src/fix_ave_histo.cpp @@ -993,23 +993,17 @@ void FixAveHisto::options(int iarg, int narg, char **arg) } else if (strcmp(arg[iarg],"title1") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/histo command"); delete [] title1; - int n = strlen(arg[iarg+1]) + 1; - title1 = new char[n]; - strcpy(title1,arg[iarg+1]); + 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/histo command"); delete [] title2; - int n = strlen(arg[iarg+1]) + 1; - title2 = new char[n]; - strcpy(title2,arg[iarg+1]); + title2 = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"title3") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/histo command"); delete [] title3; - int n = strlen(arg[iarg+1]) + 1; - title3 = new char[n]; - strcpy(title3,arg[iarg+1]); + title3 = utils::strdup(arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal fix ave/histo command"); } diff --git a/src/fix_ave_time.cpp b/src/fix_ave_time.cpp index e51c86a5e1..7ecb88ddab 100644 --- a/src/fix_ave_time.cpp +++ b/src/fix_ave_time.cpp @@ -1067,31 +1067,23 @@ void FixAveTime::options(int iarg, int narg, char **arg) } else if (strcmp(arg[iarg],"format") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/time command"); delete [] format_user; - int n = strlen(arg[iarg+1]) + 2; - format_user = new char[n]; - sprintf(format_user," %s",arg[iarg+1]); + format_user = utils::strdup(arg[iarg+1]); format = format_user; iarg += 2; } else if (strcmp(arg[iarg],"title1") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/spatial command"); delete [] title1; - int n = strlen(arg[iarg+1]) + 1; - title1 = new char[n]; - strcpy(title1,arg[iarg+1]); + 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/spatial command"); delete [] title2; - int n = strlen(arg[iarg+1]) + 1; - title2 = new char[n]; - strcpy(title2,arg[iarg+1]); + title2 = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"title3") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/spatial command"); delete [] title3; - int n = strlen(arg[iarg+1]) + 1; - title3 = new char[n]; - strcpy(title3,arg[iarg+1]); + title3 = utils::strdup(arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal fix ave/time command"); } diff --git a/src/fix_box_relax.cpp b/src/fix_box_relax.cpp index ef3032fe0c..ef576e96f6 100644 --- a/src/fix_box_relax.cpp +++ b/src/fix_box_relax.cpp @@ -761,9 +761,7 @@ int FixBoxRelax::modify_param(int narg, char **arg) tflag = 0; } delete [] id_temp; - int n = strlen(arg[1]) + 1; - id_temp = new char[n]; - strcpy(id_temp,arg[1]); + id_temp = utils::strdup(arg[1]); int icompute = modify->find_compute(arg[1]); if (icompute < 0) @@ -792,9 +790,7 @@ int FixBoxRelax::modify_param(int narg, char **arg) pflag = 0; } delete [] id_press; - int n = strlen(arg[1]) + 1; - id_press = new char[n]; - strcpy(id_press,arg[1]); + id_press = utils::strdup(arg[1]); int icompute = modify->find_compute(arg[1]); if (icompute < 0) error->all(FLERR,"Could not find fix_modify pressure ID"); diff --git a/src/fix_controller.cpp b/src/fix_controller.cpp index 0e2530de41..21d1ccf751 100644 --- a/src/fix_controller.cpp +++ b/src/fix_controller.cpp @@ -67,9 +67,7 @@ FixController::FixController(LAMMPS *lmp, int narg, char **arg) : // control variable arg - int n = strlen(arg[iarg]) + 1; - cvID = new char[n]; - strcpy(cvID,arg[iarg]); + cvID = utils::strdup(arg[iarg]); // error check diff --git a/src/fix_deform.cpp b/src/fix_deform.cpp index 4aec94fc32..b3e5722ee1 100644 --- a/src/fix_deform.cpp +++ b/src/fix_deform.cpp @@ -126,12 +126,8 @@ rfix(nullptr), irregular(nullptr), set(nullptr) error->all(FLERR,"Illegal fix deform command"); delete [] set[index].hstr; delete [] set[index].hratestr; - int n = strlen(&arg[iarg+2][2]) + 1; - set[index].hstr = new char[n]; - strcpy(set[index].hstr,&arg[iarg+2][2]); - n = strlen(&arg[iarg+3][2]) + 1; - set[index].hratestr = new char[n]; - strcpy(set[index].hratestr,&arg[iarg+3][2]); + set[index].hstr = utils::strdup(&arg[iarg+2][2]); + set[index].hratestr = utils::strdup(&arg[iarg+3][2]); iarg += 4; } else error->all(FLERR,"Illegal fix deform command"); @@ -188,12 +184,8 @@ rfix(nullptr), irregular(nullptr), set(nullptr) error->all(FLERR,"Illegal fix deform command"); delete [] set[index].hstr; delete [] set[index].hratestr; - int n = strlen(&arg[iarg+2][2]) + 1; - set[index].hstr = new char[n]; - strcpy(set[index].hstr,&arg[iarg+2][2]); - n = strlen(&arg[iarg+3][2]) + 1; - set[index].hratestr = new char[n]; - strcpy(set[index].hratestr,&arg[iarg+3][2]); + set[index].hstr = utils::strdup(&arg[iarg+2][2]); + set[index].hratestr = utils::strdup(&arg[iarg+3][2]); iarg += 4; } else error->all(FLERR,"Illegal fix deform command"); diff --git a/src/fix_group.cpp b/src/fix_group.cpp index 903eb1b23b..ebd729c737 100644 --- a/src/fix_group.cpp +++ b/src/fix_group.cpp @@ -39,12 +39,9 @@ idregion(nullptr), idvar(nullptr), idprop(nullptr) // dgroupbit = bitmask of dynamic group // group ID is last part of fix ID - int n = strlen(id) - strlen("GROUP_") + 1; - char *dgroup = new char[n]; - strcpy(dgroup,&id[strlen("GROUP_")]); - gbit = group->bitmask[group->find(dgroup)]; - gbitinverse = group->inversemask[group->find(dgroup)]; - delete [] dgroup; + auto dgroupid = std::string(id).substr(strlen("GROUP_")); + gbit = group->bitmask[group->find(dgroupid)]; + gbitinverse = group->inversemask[group->find(dgroupid)]; // process optional args @@ -61,9 +58,7 @@ idregion(nullptr), idvar(nullptr), idprop(nullptr) error->all(FLERR,"Region ID for group dynamic does not exist"); regionflag = 1; delete [] idregion; - int n = strlen(arg[iarg+1]) + 1; - idregion = new char[n]; - strcpy(idregion,arg[iarg+1]); + idregion = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"var") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal group command"); @@ -71,9 +66,7 @@ idregion(nullptr), idvar(nullptr), idprop(nullptr) error->all(FLERR,"Variable name for group dynamic does not exist"); varflag = 1; delete [] idvar; - int n = strlen(arg[iarg+1]) + 1; - idvar = new char[n]; - strcpy(idvar,arg[iarg+1]); + idvar = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"property") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal group command"); @@ -81,9 +74,7 @@ idregion(nullptr), idvar(nullptr), idprop(nullptr) error->all(FLERR,"Per atom property for group dynamic does not exist"); propflag = 1; delete [] idprop; - int n = strlen(arg[iarg+1]) + 1; - idprop = new char[n]; - strcpy(idprop,arg[iarg+1]); + idprop = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"every") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal group command"); @@ -123,7 +114,7 @@ void FixGroup::init() if (group->dynamic[igroup]) error->all(FLERR,"Group dynamic parent group cannot be dynamic"); - if (strstr(update->integrate_style,"respa")) + if (utils::strmatch(update->integrate_style,"^respa")) nlevels_respa = ((Respa *) update->integrate)->nlevels; // set current indices for region and variable diff --git a/src/fix_nh.cpp b/src/fix_nh.cpp index 49a469d296..120aeeb7de 100644 --- a/src/fix_nh.cpp +++ b/src/fix_nh.cpp @@ -275,9 +275,7 @@ FixNH::FixNH(LAMMPS *lmp, int narg, char **arg) : else { allremap = 0; delete [] id_dilate; - int n = strlen(arg[iarg+1]) + 1; - id_dilate = new char[n]; - strcpy(id_dilate,arg[iarg+1]); + id_dilate = utils::strdup(arg[iarg+1]); int idilate = group->find(id_dilate); if (idilate == -1) error->all(FLERR,"Fix nvt/npt/nph dilate group ID does not exist"); @@ -1414,9 +1412,7 @@ int FixNH::modify_param(int narg, char **arg) tcomputeflag = 0; } delete [] id_temp; - int n = strlen(arg[1]) + 1; - id_temp = new char[n]; - strcpy(id_temp,arg[1]); + id_temp = utils::strdup(arg[1]); int icompute = modify->find_compute(arg[1]); if (icompute < 0) @@ -1448,9 +1444,7 @@ int FixNH::modify_param(int narg, char **arg) pcomputeflag = 0; } delete [] id_press; - int n = strlen(arg[1]) + 1; - id_press = new char[n]; - strcpy(id_press,arg[1]); + id_press = utils::strdup(arg[1]); int icompute = modify->find_compute(arg[1]); if (icompute < 0) error->all(FLERR,"Could not find fix_modify pressure ID"); diff --git a/src/fix_press_berendsen.cpp b/src/fix_press_berendsen.cpp index d83262e9dd..d929ca6f28 100644 --- a/src/fix_press_berendsen.cpp +++ b/src/fix_press_berendsen.cpp @@ -467,9 +467,7 @@ int FixPressBerendsen::modify_param(int narg, char **arg) tflag = 0; } delete [] id_temp; - int n = strlen(arg[1]) + 1; - id_temp = new char[n]; - strcpy(id_temp,arg[1]); + id_temp = utils::strdup(arg[1]); int icompute = modify->find_compute(arg[1]); if (icompute < 0) error->all(FLERR,"Could not find fix_modify temperature ID"); @@ -496,9 +494,7 @@ int FixPressBerendsen::modify_param(int narg, char **arg) pflag = 0; } delete [] id_press; - int n = strlen(arg[1]) + 1; - id_press = new char[n]; - strcpy(id_press,arg[1]); + id_press = utils::strdup(arg[1]); int icompute = modify->find_compute(arg[1]); if (icompute < 0) error->all(FLERR,"Could not find fix_modify pressure ID"); diff --git a/src/fix_spring.cpp b/src/fix_spring.cpp index 4bbaf594bd..89d569fc8b 100644 --- a/src/fix_spring.cpp +++ b/src/fix_spring.cpp @@ -70,9 +70,7 @@ FixSpring::FixSpring(LAMMPS *lmp, int narg, char **arg) : if (narg != 10) error->all(FLERR,"Illegal fix spring command"); styleflag = COUPLE; - int n = strlen(arg[4]) + 1; - group2 = new char[n]; - strcpy(group2,arg[4]); + group2 = utils::strdup(arg[4]); igroup2 = group->find(arg[4]); if (igroup2 == -1) error->all(FLERR,"Fix spring couple group ID does not exist"); diff --git a/src/fix_spring_chunk.cpp b/src/fix_spring_chunk.cpp index 9ae4ee4632..8eb35e74aa 100644 --- a/src/fix_spring_chunk.cpp +++ b/src/fix_spring_chunk.cpp @@ -49,13 +49,8 @@ FixSpringChunk::FixSpringChunk(LAMMPS *lmp, int narg, char **arg) : k_spring = utils::numeric(FLERR,arg[3],false,lmp); - int n = strlen(arg[4]) + 1; - idchunk = new char[n]; - strcpy(idchunk,arg[4]); - - n = strlen(arg[5]) + 1; - idcom = new char[n]; - strcpy(idcom,arg[5]); + idchunk = utils::strdup(arg[4]); + idcom = utils::strdup(arg[5]); esprings = 0.0; nchunk = 0; diff --git a/src/fix_temp_berendsen.cpp b/src/fix_temp_berendsen.cpp index f951975937..39b8a5fefd 100644 --- a/src/fix_temp_berendsen.cpp +++ b/src/fix_temp_berendsen.cpp @@ -205,9 +205,7 @@ int FixTempBerendsen::modify_param(int narg, char **arg) tflag = 0; } delete [] id_temp; - int n = strlen(arg[1]) + 1; - id_temp = new char[n]; - strcpy(id_temp,arg[1]); + id_temp = utils::strdup(arg[1]); int icompute = modify->find_compute(id_temp); if (icompute < 0) diff --git a/src/fix_temp_csld.cpp b/src/fix_temp_csld.cpp index 2ee2044639..787b4dd34e 100644 --- a/src/fix_temp_csld.cpp +++ b/src/fix_temp_csld.cpp @@ -261,9 +261,7 @@ int FixTempCSLD::modify_param(int narg, char **arg) tflag = 0; } delete [] id_temp; - int n = strlen(arg[1]) + 1; - id_temp = new char[n]; - strcpy(id_temp,arg[1]); + id_temp = utils::strdup(arg[1]); int icompute = modify->find_compute(id_temp); if (icompute < 0) diff --git a/src/fix_temp_csvr.cpp b/src/fix_temp_csvr.cpp index cdce9175d6..525cf5b74a 100644 --- a/src/fix_temp_csvr.cpp +++ b/src/fix_temp_csvr.cpp @@ -216,9 +216,7 @@ int FixTempCSVR::modify_param(int narg, char **arg) tflag = 0; } delete [] id_temp; - int n = strlen(arg[1]) + 1; - id_temp = new char[n]; - strcpy(id_temp,arg[1]); + id_temp = utils::strdup(arg[1]); int icompute = modify->find_compute(id_temp); if (icompute < 0) diff --git a/src/fix_temp_rescale.cpp b/src/fix_temp_rescale.cpp index fa4c87a256..b57f6c238c 100644 --- a/src/fix_temp_rescale.cpp +++ b/src/fix_temp_rescale.cpp @@ -202,9 +202,7 @@ int FixTempRescale::modify_param(int narg, char **arg) tflag = 0; } delete [] id_temp; - int n = strlen(arg[1]) + 1; - id_temp = new char[n]; - strcpy(id_temp,arg[1]); + id_temp = utils::strdup(arg[1]); int icompute = modify->find_compute(id_temp); if (icompute < 0) diff --git a/src/fix_wall_region.cpp b/src/fix_wall_region.cpp index 4d976373a5..7e30fe2361 100644 --- a/src/fix_wall_region.cpp +++ b/src/fix_wall_region.cpp @@ -54,9 +54,7 @@ FixWallRegion::FixWallRegion(LAMMPS *lmp, int narg, char **arg) : iregion = domain->find_region(arg[3]); if (iregion == -1) error->all(FLERR,"Region ID for fix wall/region does not exist"); - int n = strlen(arg[3]) + 1; - idregion = new char[n]; - strcpy(idregion,arg[3]); + idregion = utils::strdup(arg[3]); if (strcmp(arg[4],"lj93") == 0) style = LJ93; else if (strcmp(arg[4],"lj126") == 0) style = LJ126;