no local buffers and snprintf() in a bunch of style creation functions

This commit is contained in:
Axel Kohlmeyer
2020-06-04 14:36:56 -04:00
parent 481d3cb219
commit b27ef02bc2
3 changed files with 79 additions and 99 deletions

View File

@ -234,7 +234,7 @@ void Force::setup()
create a pair style, called from input script or restart file
------------------------------------------------------------------------- */
void Force::create_pair(const char *style, int trysuffix)
void Force::create_pair(const std::string &style, int trysuffix)
{
delete [] pair_style;
if (pair) delete pair;
@ -254,13 +254,12 @@ void Force::create_pair(const char *style, int trysuffix)
return sflag = 0 for no suffix added, 1 or 2 for suffix1/2 added
------------------------------------------------------------------------- */
Pair *Force::new_pair(const char *style, int trysuffix, int &sflag)
Pair *Force::new_pair(const std::string &style, int trysuffix, int &sflag)
{
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
sflag = 1;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix);
std::string estyle = style + "/" + lmp->suffix;
if (pair_map->find(estyle) != pair_map->end()) {
PairCreator pair_creator = (*pair_map)[estyle];
return pair_creator(lmp);
@ -268,8 +267,7 @@ Pair *Force::new_pair(const char *style, int trysuffix, int &sflag)
}
if (lmp->suffix2) {
sflag = 2;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
std::string estyle = style + "/" + lmp->suffix2;
if (pair_map->find(estyle) != pair_map->end()) {
PairCreator pair_creator = (*pair_map)[estyle];
return pair_creator(lmp);
@ -278,7 +276,7 @@ Pair *Force::new_pair(const char *style, int trysuffix, int &sflag)
}
sflag = 0;
if (strcmp(style,"none") == 0) return NULL;
if (style == "none") return NULL;
if (pair_map->find(style) != pair_map->end()) {
PairCreator pair_creator = (*pair_map)[style];
return pair_creator(lmp);
@ -307,17 +305,17 @@ Pair *Force::pair_creator(LAMMPS *lmp)
return NULL if no match or if nsub=0 and multiple sub-styles match
------------------------------------------------------------------------- */
Pair *Force::pair_match(const char *word, int exact, int nsub)
Pair *Force::pair_match(const std::string &word, int exact, int nsub)
{
int iwhich,count;
if (exact && strcmp(pair_style,word) == 0) return pair;
if (exact && (word == pair_style)) return pair;
else if (!exact && utils::strmatch(pair_style,word)) return pair;
else if (utils::strmatch(pair_style,"^hybrid")) {
PairHybrid *hybrid = (PairHybrid *) pair;
count = 0;
for (int i = 0; i < hybrid->nstyles; i++)
if ((exact && strcmp(hybrid->keywords[i],word) == 0) ||
if ((exact && (word == hybrid->keywords[i])) ||
(!exact && utils::strmatch(hybrid->keywords[i],word))) {
iwhich = i;
count++;
@ -352,7 +350,7 @@ char *Force::pair_match_ptr(Pair *ptr)
create a bond style, called from input script or restart file
------------------------------------------------------------------------- */
void Force::create_bond(const char *style, int trysuffix)
void Force::create_bond(const std::string &style, int trysuffix)
{
delete [] bond_style;
if (bond) delete bond;
@ -366,13 +364,12 @@ void Force::create_bond(const char *style, int trysuffix)
generate a bond class, fist with suffix appended
------------------------------------------------------------------------- */
Bond *Force::new_bond(const char *style, int trysuffix, int &sflag)
Bond *Force::new_bond(const std::string &style, int trysuffix, int &sflag)
{
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
sflag = 1;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix);
std::string estyle = style + "/" + lmp->suffix;
if (bond_map->find(estyle) != bond_map->end()) {
BondCreator bond_creator = (*bond_map)[estyle];
return bond_creator(lmp);
@ -381,8 +378,7 @@ Bond *Force::new_bond(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) {
sflag = 2;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
std::string estyle = style + "/" + lmp->suffix2;
if (bond_map->find(estyle) != bond_map->end()) {
BondCreator bond_creator = (*bond_map)[estyle];
return bond_creator(lmp);
@ -391,7 +387,7 @@ Bond *Force::new_bond(const char *style, int trysuffix, int &sflag)
}
sflag = 0;
if (strcmp(style,"none") == 0) return NULL;
if (style == "none") return NULL;
if (bond_map->find(style) != bond_map->end()) {
BondCreator bond_creator = (*bond_map)[style];
return bond_creator(lmp);
@ -416,13 +412,13 @@ Bond *Force::bond_creator(LAMMPS *lmp)
return ptr to current bond class or hybrid sub-class if matches style
------------------------------------------------------------------------- */
Bond *Force::bond_match(const char *style)
Bond *Force::bond_match(const std::string &style)
{
if (strcmp(bond_style,style) == 0) return bond;
if (style == bond_style) return bond;
else if (strcmp(bond_style,"hybrid") == 0) {
BondHybrid *hybrid = (BondHybrid *) bond;
for (int i = 0; i < hybrid->nstyles; i++)
if (strcmp(hybrid->keywords[i],style) == 0) return hybrid->styles[i];
if (style == hybrid->keywords[i]) return hybrid->styles[i];
}
return NULL;
}
@ -431,7 +427,7 @@ Bond *Force::bond_match(const char *style)
create an angle style, called from input script or restart file
------------------------------------------------------------------------- */
void Force::create_angle(const char *style, int trysuffix)
void Force::create_angle(const std::string &style, int trysuffix)
{
delete [] angle_style;
if (angle) delete angle;
@ -445,13 +441,12 @@ void Force::create_angle(const char *style, int trysuffix)
generate an angle class
------------------------------------------------------------------------- */
Angle *Force::new_angle(const char *style, int trysuffix, int &sflag)
Angle *Force::new_angle(const std::string &style, int trysuffix, int &sflag)
{
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
sflag = 1;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix);
std::string estyle = style + "/" + lmp->suffix;
if (angle_map->find(estyle) != angle_map->end()) {
AngleCreator angle_creator = (*angle_map)[estyle];
return angle_creator(lmp);
@ -460,8 +455,7 @@ Angle *Force::new_angle(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) {
sflag = 2;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix);
std::string estyle = style + "/" + lmp->suffix;
if (angle_map->find(estyle) != angle_map->end()) {
AngleCreator angle_creator = (*angle_map)[estyle];
return angle_creator(lmp);
@ -470,7 +464,7 @@ Angle *Force::new_angle(const char *style, int trysuffix, int &sflag)
}
sflag = 0;
if (strcmp(style,"none") == 0) return NULL;
if (style == "none") return NULL;
if (angle_map->find(style) != angle_map->end()) {
AngleCreator angle_creator = (*angle_map)[style];
return angle_creator(lmp);
@ -491,18 +485,17 @@ Angle *Force::angle_creator(LAMMPS *lmp)
return new T(lmp);
}
/* ----------------------------------------------------------------------
return ptr to current angle class or hybrid sub-class if matches style
------------------------------------------------------------------------- */
Angle *Force::angle_match(const char *style)
Angle *Force::angle_match(const std::string &style)
{
if (strcmp(angle_style,style) == 0) return angle;
else if (strcmp(angle_style,"hybrid") == 0) {
if (style == angle_style) return angle;
else if (utils::strmatch(angle_style,"^hybrid")) {
AngleHybrid *hybrid = (AngleHybrid *) angle;
for (int i = 0; i < hybrid->nstyles; i++)
if (strcmp(hybrid->keywords[i],style) == 0) return hybrid->styles[i];
if (style == hybrid->keywords[i]) return hybrid->styles[i];
}
return NULL;
}
@ -511,7 +504,7 @@ Angle *Force::angle_match(const char *style)
create a dihedral style, called from input script or restart file
------------------------------------------------------------------------- */
void Force::create_dihedral(const char *style, int trysuffix)
void Force::create_dihedral(const std::string &style, int trysuffix)
{
delete [] dihedral_style;
if (dihedral) delete dihedral;
@ -525,13 +518,12 @@ void Force::create_dihedral(const char *style, int trysuffix)
generate a dihedral class
------------------------------------------------------------------------- */
Dihedral *Force::new_dihedral(const char *style, int trysuffix, int &sflag)
Dihedral *Force::new_dihedral(const std::string &style, int trysuffix, int &sflag)
{
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
sflag = 1;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix);
std::string estyle = style + "/" + lmp->suffix;
if (dihedral_map->find(estyle) != dihedral_map->end()) {
DihedralCreator dihedral_creator = (*dihedral_map)[estyle];
return dihedral_creator(lmp);
@ -540,8 +532,7 @@ Dihedral *Force::new_dihedral(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) {
sflag = 2;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
std::string estyle = style + "/" + lmp->suffix2;
if (dihedral_map->find(estyle) != dihedral_map->end()) {
DihedralCreator dihedral_creator = (*dihedral_map)[estyle];
return dihedral_creator(lmp);
@ -550,7 +541,7 @@ Dihedral *Force::new_dihedral(const char *style, int trysuffix, int &sflag)
}
sflag = 0;
if (strcmp(style,"none") == 0) return NULL;
if (style == "none") return NULL;
if (dihedral_map->find(style) != dihedral_map->end()) {
DihedralCreator dihedral_creator = (*dihedral_map)[style];
return dihedral_creator(lmp);
@ -575,13 +566,13 @@ Dihedral *Force::dihedral_creator(LAMMPS *lmp)
return ptr to current angle class or hybrid sub-class if matches style
------------------------------------------------------------------------- */
Dihedral *Force::dihedral_match(const char *style)
Dihedral *Force::dihedral_match(const std::string &style)
{
if (strcmp(dihedral_style,style) == 0) return dihedral;
else if (strcmp(dihedral_style,"hybrid") == 0) {
if (style == dihedral_style) return dihedral;
else if (utils::strmatch(dihedral_style,"^hybrid")) {
DihedralHybrid *hybrid = (DihedralHybrid *) dihedral;
for (int i = 0; i < hybrid->nstyles; i++)
if (strcmp(hybrid->keywords[i],style) == 0) return hybrid->styles[i];
if (style == hybrid->keywords[i]) return hybrid->styles[i];
}
return NULL;
}
@ -590,7 +581,7 @@ Dihedral *Force::dihedral_match(const char *style)
create an improper style, called from input script or restart file
------------------------------------------------------------------------- */
void Force::create_improper(const char *style, int trysuffix)
void Force::create_improper(const std::string &style, int trysuffix)
{
delete [] improper_style;
if (improper) delete improper;
@ -604,13 +595,12 @@ void Force::create_improper(const char *style, int trysuffix)
generate a improper class
------------------------------------------------------------------------- */
Improper *Force::new_improper(const char *style, int trysuffix, int &sflag)
Improper *Force::new_improper(const std::string &style, int trysuffix, int &sflag)
{
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
sflag = 1;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix);
std::string estyle = style + "/" + lmp->suffix;
if (improper_map->find(estyle) != improper_map->end()) {
ImproperCreator improper_creator = (*improper_map)[estyle];
return improper_creator(lmp);
@ -619,8 +609,7 @@ Improper *Force::new_improper(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) {
sflag = 2;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
std::string estyle = style + "/" + lmp->suffix2;
if (improper_map->find(estyle) != improper_map->end()) {
ImproperCreator improper_creator = (*improper_map)[estyle];
return improper_creator(lmp);
@ -629,7 +618,7 @@ Improper *Force::new_improper(const char *style, int trysuffix, int &sflag)
}
sflag = 0;
if (strcmp(style,"none") == 0) return NULL;
if (style == "none") return NULL;
if (improper_map->find(style) != improper_map->end()) {
ImproperCreator improper_creator = (*improper_map)[style];
return improper_creator(lmp);
@ -654,13 +643,13 @@ Improper *Force::improper_creator(LAMMPS *lmp)
return ptr to current improper class or hybrid sub-class if matches style
------------------------------------------------------------------------- */
Improper *Force::improper_match(const char *style)
Improper *Force::improper_match(const std::string &style)
{
if (strcmp(improper_style,style) == 0) return improper;
else if (strcmp(improper_style,"hybrid") == 0) {
if (style == improper_style) return improper;
else if (utils::strmatch(improper_style,"^hybrid")) {
ImproperHybrid *hybrid = (ImproperHybrid *) improper;
for (int i = 0; i < hybrid->nstyles; i++)
if (strcmp(hybrid->keywords[i],style) == 0) return hybrid->styles[i];
if (style == hybrid->keywords[i]) return hybrid->styles[i];
}
return NULL;
}
@ -669,7 +658,7 @@ Improper *Force::improper_match(const char *style)
new kspace style
------------------------------------------------------------------------- */
void Force::create_kspace(const char *style, int trysuffix)
void Force::create_kspace(const std::string &style, int trysuffix)
{
delete [] kspace_style;
if (kspace) delete kspace;
@ -687,13 +676,12 @@ void Force::create_kspace(const char *style, int trysuffix)
generate a kspace class
------------------------------------------------------------------------- */
KSpace *Force::new_kspace(const char *style, int trysuffix, int &sflag)
KSpace *Force::new_kspace(const std::string &style, int trysuffix, int &sflag)
{
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
sflag = 1;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix);
std::string estyle = style + "/" + lmp->suffix;
if (kspace_map->find(estyle) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[estyle];
return kspace_creator(lmp);
@ -702,8 +690,7 @@ KSpace *Force::new_kspace(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) {
sflag = 1;
char estyle[256];
snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
std::string estyle = style + "/" + lmp->suffix2;
if (kspace_map->find(estyle) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[estyle];
return kspace_creator(lmp);
@ -712,7 +699,7 @@ KSpace *Force::new_kspace(const char *style, int trysuffix, int &sflag)
}
sflag = 0;
if (strcmp(style,"none") == 0) return NULL;
if (style == "none") return NULL;
if (kspace_map->find(style) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[style];
return kspace_creator(lmp);
@ -740,9 +727,9 @@ KSpace *Force::kspace_creator(LAMMPS *lmp)
return NULL if no match
------------------------------------------------------------------------- */
KSpace *Force::kspace_match(const char *word, int exact)
KSpace *Force::kspace_match(const std::string &word, int exact)
{
if (exact && strcmp(kspace_style,word) == 0) return kspace;
if (exact && (word == kspace_style)) return kspace;
else if (!exact && utils::strmatch(kspace_style,word)) return kspace;
return NULL;
}
@ -753,20 +740,15 @@ KSpace *Force::kspace_match(const char *word, int exact)
if sflag = 1/2, append suffix or suffix2 to style
------------------------------------------------------------------------- */
void Force::store_style(char *&str, const char *style, int sflag)
void Force::store_style(char *&str, const std::string &style, int sflag)
{
if (sflag) {
char estyle[256];
if (sflag == 1) snprintf(estyle,256,"%s/%s",style,lmp->suffix);
else snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
int n = strlen(estyle) + 1;
str = new char[n];
strcpy(str,estyle);
} else {
int n = strlen(style) + 1;
str = new char[n];
strcpy(str,style);
}
std::string estyle = style;
if (sflag == 1) estyle += std::string("/") + lmp->suffix;
else if (sflag == 2) estyle += std::string("/") + lmp->suffix2;
str = new char[estyle.size()+1];
strcpy(str,estyle.c_str());
}
/* ----------------------------------------------------------------------