From e4a3a518f77d2bf89d8efcae648b0cc924a8b888 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 25 Jun 2020 21:32:30 -0400 Subject: [PATCH] more use of fmtlib and std::string --- src/info.cpp | 12 +++----- src/input.cpp | 9 ++---- src/kspace.cpp | 16 +++++------ src/lammps.cpp | 7 ++--- src/modify.cpp | 52 ++++++++++++++++------------------ src/ntopo_improper_partial.cpp | 9 ++---- src/pair_table.cpp | 33 ++++++++------------- src/read_data.cpp | 7 ++--- src/read_dump.cpp | 40 ++++++++------------------ src/read_restart.cpp | 42 +++++++++------------------ src/reset_ids.cpp | 10 +++---- src/thermo.cpp | 19 +++++-------- 12 files changed, 94 insertions(+), 162 deletions(-) diff --git a/src/info.cpp b/src/info.cpp index 76e8d9bd09..8cb87e9fea 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -848,16 +848,12 @@ bool Info::is_active(const char *category, const char *name) if (!match && lmp->suffix_enable) { if (lmp->suffix) { - char *name_w_suffix = new char [len + 2 + strlen(lmp->suffix)]; - sprintf(name_w_suffix,"%s/%s",name,lmp->suffix); - if (strcmp(style,name_w_suffix) == 0) match = 1; - delete[] name_w_suffix; + std::string name_w_suffix = name + std::string("/") + lmp->suffix; + if (name_w_suffix == style) match = 1; } if (!match && lmp->suffix2) { - char *name_w_suffix = new char [len + 2 + strlen(lmp->suffix2)]; - sprintf(name_w_suffix,"%s/%s",name,lmp->suffix2); - if (strcmp(style,name_w_suffix) == 0) match = 1; - delete[] name_w_suffix; + std::string name_w_suffix = name + std::string("/") + lmp->suffix2; + if (name_w_suffix == style) match = 1; } } return match ? true : false; diff --git a/src/input.cpp b/src/input.cpp index 412084133e..7708a79174 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -227,11 +227,8 @@ void Input::file() // execute the command - if (execute_command()) { - char *str = new char[maxline+32]; - sprintf(str,"Unknown command: %s",line); - error->all(FLERR,str); - } + if (execute_command()) + error->all(FLERR,fmt::format("Unknown command: {}",line)); } } @@ -1249,7 +1246,7 @@ char *shell_failed_message(const char* cmd, int errnum) const char *errmsg = strerror(errnum); int len = strlen(cmd)+strlen(errmsg)+64; char *msg = new char[len]; - sprintf(msg,"Shell command '%s' failed with error '%s'", cmd, errmsg); + snprintf(msg, len, "Shell command '%s' failed with error '%s'", cmd, errmsg); return msg; } diff --git a/src/kspace.cpp b/src/kspace.cpp index a9356a39f1..8769409331 100644 --- a/src/kspace.cpp +++ b/src/kspace.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "atom.h" #include "comm.h" #include "force.h" @@ -25,6 +26,7 @@ #include "error.h" #include "suffix.h" #include "domain.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -307,10 +309,10 @@ void KSpace::qsum_qsq(int warning_flag) // so issue warning or error if (fabs(qsum) > SMALL) { - char str[128]; - sprintf(str,"System is not charge neutral, net charge = %g",qsum); - if (!warn_nonneutral) error->all(FLERR,str); - if (warn_nonneutral == 1 && comm->me == 0) error->warning(FLERR,str); + std::string message = fmt::format("System is not charge neutral, net " + "charge = {}",qsum); + if (!warn_nonneutral) error->all(FLERR,message); + if (warn_nonneutral == 1 && comm->me == 0) error->warning(FLERR,message); warn_nonneutral = 2; } } @@ -324,12 +326,10 @@ double KSpace::estimate_table_accuracy(double q2_over_sqrt, double spr) double table_accuracy = 0.0; int nctb = force->pair->ncoultablebits; if (comm->me == 0) { - char str[128]; if (nctb) - sprintf(str," using %d-bit tables for long-range coulomb",nctb); + error->message(FLERR,fmt::format(" using {}-bit tables for long-range coulomb",nctb)); else - sprintf(str," using polynomial approximation for long-range coulomb"); - error->message(FLERR,str); + error->message(FLERR," using polynomial approximation for long-range coulomb"); } if (nctb) { diff --git a/src/lammps.cpp b/src/lammps.cpp index e0cca74e85..3b768d1862 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -667,15 +667,12 @@ LAMMPS::~LAMMPS() double totalclock = MPI_Wtime() - initclock; if ((me == 0) && (screen || logfile)) { - char outtime[128]; int seconds = fmod(totalclock,60.0); totalclock = (totalclock - seconds) / 60.0; int minutes = fmod(totalclock,60.0); int hours = (totalclock - minutes) / 60.0; - sprintf(outtime,"Total wall time: " - "%d:%02d:%02d\n", hours, minutes, seconds); - if (screen) fputs(outtime,screen); - if (logfile) fputs(outtime,logfile); + utils::logmesg(this,fmt::format("Total wall time: {}:{:02d}:{:02d}\n", + hours, minutes, seconds)); } if (universe->nworlds == 1) { diff --git a/src/modify.cpp b/src/modify.cpp index f7fd18d55c..b2bb6e5e8e 100644 --- a/src/modify.cpp +++ b/src/modify.cpp @@ -13,6 +13,7 @@ #include "modify.h" #include +#include #include "style_compute.h" #include "style_fix.h" #include "atom.h" @@ -836,18 +837,17 @@ void Modify::add_fix(int narg, char **arg, int trysuffix) int match = 0; if (strcmp(arg[2],fix[ifix]->style) == 0) match = 1; if (!match && trysuffix && lmp->suffix_enable) { - char estyle[256]; if (lmp->suffix) { - sprintf(estyle,"%s/%s",arg[2],lmp->suffix); - if (strcmp(estyle,fix[ifix]->style) == 0) match = 1; + std::string estyle = arg[2] + std::string("/") + lmp->suffix; + if (estyle == fix[ifix]->style) match = 1; } if (lmp->suffix2) { - sprintf(estyle,"%s/%s",arg[2],lmp->suffix2); - if (strcmp(estyle,fix[ifix]->style) == 0) match = 1; + std::string estyle = arg[2] + std::string("/") + lmp->suffix2; + if (estyle == fix[ifix]->style) match = 1; } } - if (!match) error->all(FLERR, - "Replacing a fix, but new style != old style"); + if (!match) + error->all(FLERR,"Replacing a fix, but new style != old style"); if (fix[ifix]->igroup != igroup && comm->me == 0) error->warning(FLERR,"Replacing a fix, but new group != old group"); @@ -870,26 +870,24 @@ void Modify::add_fix(int narg, char **arg, int trysuffix) if (trysuffix && lmp->suffix_enable) { if (lmp->suffix) { - int n = strlen(arg[2])+strlen(lmp->suffix)+2; - char *estyle = new char[n]; - sprintf(estyle,"%s/%s",arg[2],lmp->suffix); + std::string estyle = arg[2] + std::string("/") + lmp->suffix; if (fix_map->find(estyle) != fix_map->end()) { FixCreator fix_creator = (*fix_map)[estyle]; fix[ifix] = fix_creator(lmp,narg,arg); delete[] fix[ifix]->style; - fix[ifix]->style = estyle; - } else delete[] estyle; + fix[ifix]->style = new char[estyle.size()+1]; + strcpy(fix[ifix]->style,estyle.c_str()); + } } if (fix[ifix] == NULL && lmp->suffix2) { - int n = strlen(arg[2])+strlen(lmp->suffix2)+2; - char *estyle = new char[n]; - sprintf(estyle,"%s/%s",arg[2],lmp->suffix2); + std::string estyle = arg[2] + std::string("/") + lmp->suffix2; if (fix_map->find(estyle) != fix_map->end()) { FixCreator fix_creator = (*fix_map)[estyle]; fix[ifix] = fix_creator(lmp,narg,arg); delete[] fix[ifix]->style; - fix[ifix]->style = estyle; - } else delete[] estyle; + fix[ifix]->style = new char[estyle.size()+1]; + strcpy(fix[ifix]->style,estyle.c_str()); + } } } @@ -1164,7 +1162,7 @@ int Modify::check_rigid_list_overlap(int *select) int n = 0; for (int ifix = 0; ifix < nfix; ifix++) { - if (strncmp("rigid",fix[ifix]->style,5) == 0) { + if (utils::strmatch(fix[ifix]->style,"^rigid")) { const int * const body = (const int *)fix[ifix]->extract("body",dim); if ((body == NULL) || (dim != 1)) break; @@ -1209,26 +1207,24 @@ void Modify::add_compute(int narg, char **arg, int trysuffix) if (trysuffix && lmp->suffix_enable) { if (lmp->suffix) { - int n = strlen(arg[2])+strlen(lmp->suffix)+2; - char *estyle = new char[n]; - sprintf(estyle,"%s/%s",arg[2],lmp->suffix); + std::string estyle = arg[2] + std::string("/") + lmp->suffix; if (compute_map->find(estyle) != compute_map->end()) { ComputeCreator compute_creator = (*compute_map)[estyle]; compute[ncompute] = compute_creator(lmp,narg,arg); delete[] compute[ncompute]->style; - compute[ncompute]->style = estyle; - } else delete[] estyle; + compute[ncompute]->style = new char[estyle.size()+1]; + strcpy(compute[ncompute]->style,estyle.c_str()); + } } if (compute[ncompute] == NULL && lmp->suffix2) { - int n = strlen(arg[2])+strlen(lmp->suffix2)+2; - char *estyle = new char[n]; - sprintf(estyle,"%s/%s",arg[2],lmp->suffix2); + std::string estyle = arg[2] + std::string("/") + lmp->suffix2; if (compute_map->find(estyle) != compute_map->end()) { ComputeCreator compute_creator = (*compute_map)[estyle]; compute[ncompute] = compute_creator(lmp,narg,arg); delete[] compute[ncompute]->style; - compute[ncompute]->style = estyle; - } else delete[] estyle; + compute[ncompute]->style = new char[estyle.size()+1]; + strcpy(compute[ncompute]->style,estyle.c_str()); + } } } diff --git a/src/ntopo_improper_partial.cpp b/src/ntopo_improper_partial.cpp index f2afc68387..c4c6ec3177 100644 --- a/src/ntopo_improper_partial.cpp +++ b/src/ntopo_improper_partial.cpp @@ -97,10 +97,7 @@ void NTopoImproperPartial::build() int all; MPI_Allreduce(&nmissing,&all,1,MPI_INT,MPI_SUM,world); - if (all) { - char str[128]; - sprintf(str, - "Improper atoms missing at step " BIGINT_FORMAT,update->ntimestep); - if (me == 0) error->warning(FLERR,str); - } + if (all && me == 0) + error->warning(FLERR,fmt::format("Improper atoms missing at step {}", + update->ntimestep)); } diff --git a/src/pair_table.cpp b/src/pair_table.cpp index 7130a39b26..73b916f966 100644 --- a/src/pair_table.cpp +++ b/src/pair_table.cpp @@ -116,37 +116,28 @@ void PairTable::compute(int eflag, int vflag) if (rsq < cutsq[itype][jtype]) { tb = &tables[tabindex[itype][jtype]]; - if (rsq < tb->innersq) { - sprintf(estr,"Pair distance < table inner cutoff: " - "ijtype %d %d dist %g",itype,jtype,sqrt(rsq)); - error->one(FLERR,estr); - } - + if (rsq < tb->innersq) + error->one(FLERR,fmt::format("Pair distance < table inner cutoff: " + "ijtype {} {} dist {}",itype,jtype,sqrt(rsq))); if (tabstyle == LOOKUP) { itable = static_cast ((rsq - tb->innersq) * tb->invdelta); - if (itable >= tlm1) { - sprintf(estr,"Pair distance > table outer cutoff: " - "ijtype %d %d dist %g",itype,jtype,sqrt(rsq)); - error->one(FLERR,estr); - } + if (itable >= tlm1) + error->one(FLERR,fmt::format("Pair distance > table outer cutoff: " + "ijtype {} {} dist {}",itype,jtype,sqrt(rsq))); fpair = factor_lj * tb->f[itable]; } else if (tabstyle == LINEAR) { itable = static_cast ((rsq - tb->innersq) * tb->invdelta); - if (itable >= tlm1) { - sprintf(estr,"Pair distance > table outer cutoff: " - "ijtype %d %d dist %g",itype,jtype,sqrt(rsq)); - error->one(FLERR,estr); - } + if (itable >= tlm1) + error->one(FLERR,fmt::format("Pair distance > table outer cutoff: " + "ijtype {} {} dist {}",itype,jtype,sqrt(rsq))); fraction = (rsq - tb->rsq[itable]) * tb->invdelta; value = tb->f[itable] + fraction*tb->df[itable]; fpair = factor_lj * value; } else if (tabstyle == SPLINE) { itable = static_cast ((rsq - tb->innersq) * tb->invdelta); - if (itable >= tlm1) { - sprintf(estr,"Pair distance > table outer cutoff: " - "ijtype %d %d dist %g",itype,jtype,sqrt(rsq)); - error->one(FLERR,estr); - } + if (itable >= tlm1) + error->one(FLERR,fmt::format("Pair distance > table outer cutoff: " + "ijtype {} {} dist {}",itype,jtype,sqrt(rsq))); b = (rsq - tb->rsq[itable]) * tb->invdelta; a = 1.0 - b; value = a * tb->f[itable] + b * tb->f[itable+1] + diff --git a/src/read_data.cpp b/src/read_data.cpp index 1ccbccb835..5ce873f43b 100644 --- a/src/read_data.cpp +++ b/src/read_data.cpp @@ -1157,11 +1157,8 @@ void ReadData::header(int firstpass) parse_keyword(1); for (n = 0; n < NSECTIONS; n++) if (strcmp(keyword,section_keywords[n]) == 0) break; - if (n == NSECTIONS) { - char str[128]; - sprintf(str,"Unknown identifier in data file: %s",keyword); - error->all(FLERR,str); - } + if (n == NSECTIONS) + error->all(FLERR,fmt::format("Unknown identifier in data file: {}",keyword)); // error checks on header values // must be consistent with atom style and other header values diff --git a/src/read_dump.cpp b/src/read_dump.cpp index 99e9486679..0426323a21 100644 --- a/src/read_dump.cpp +++ b/src/read_dump.cpp @@ -289,13 +289,9 @@ bigint ReadDump::seek(bigint nrequest, int exact) for (ifile = 0; ifile < nfile; ifile++) { ntimestep = -1; if (multiproc) { - char *ptr = strchr(files[ifile],'%'); - char *multiname = new char[strlen(files[ifile]) + 16]; - *ptr = '\0'; - sprintf(multiname,"%s%d%s",files[ifile],0,ptr+1); - *ptr = '%'; - readers[0]->open_file(multiname); - delete [] multiname; + std::string multiname = files[ifile]; + multiname.replace(multiname.find("%"),1,"0"); + readers[0]->open_file(multiname.c_str()); } else readers[0]->open_file(files[ifile]); while (1) { @@ -337,13 +333,9 @@ bigint ReadDump::seek(bigint nrequest, int exact) if (multiproc && filereader) { for (int i = 0; i < nreader; i++) { if (me == 0 && i == 0) continue; // proc 0, reader 0 already found it - char *ptr = strchr(files[currentfile],'%'); - char *multiname = new char[strlen(files[currentfile]) + 16]; - *ptr = '\0'; - sprintf(multiname,"%s%d%s",files[currentfile],firstfile+i,ptr+1); - *ptr = '%'; - readers[i]->open_file(multiname); - delete [] multiname; + std::string multiname = files[currentfile]; + multiname.replace(multiname.find("%"),1,fmt::format("{}",firstfile+i)); + readers[i]->open_file(multiname.c_str()); bigint step; while (1) { @@ -389,13 +381,9 @@ bigint ReadDump::next(bigint ncurrent, bigint nlast, int nevery, int nskip) ntimestep = -1; if (ifile != currentfile) { if (multiproc) { - char *ptr = strchr(files[ifile],'%'); - char *multiname = new char[strlen(files[ifile]) + 16]; - *ptr = '\0'; - sprintf(multiname,"%s%d%s",files[ifile],0,ptr+1); - *ptr = '%'; - readers[0]->open_file(multiname); - delete [] multiname; + std::string multiname = files[ifile]; + multiname.replace(multiname.find("%"),1,"0"); + readers[0]->open_file(multiname.c_str()); } else readers[0]->open_file(files[ifile]); } @@ -447,13 +435,9 @@ bigint ReadDump::next(bigint ncurrent, bigint nlast, int nevery, int nskip) if (multiproc && filereader) { for (int i = 0; i < nreader; i++) { if (me == 0 && i == 0) continue; - char *ptr = strchr(files[currentfile],'%'); - char *multiname = new char[strlen(files[currentfile]) + 16]; - *ptr = '\0'; - sprintf(multiname,"%s%d%s",files[currentfile],firstfile+i,ptr+1); - *ptr = '%'; - readers[i]->open_file(multiname); - delete [] multiname; + std::string multiname = files[currentfile]; + multiname.replace(multiname.find("%"),1,fmt::format("{}",firstfile+i)); + readers[i]->open_file(multiname.c_str()); bigint step; while (1) { diff --git a/src/read_restart.cpp b/src/read_restart.cpp index ade751fdef..0f3c05e290 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -104,19 +104,14 @@ void ReadRestart::command(int narg, char **arg) if (me == 0) { if (screen) fprintf(screen,"Reading restart file ...\n"); - char *hfile; + std::string hfile = file; if (multiproc) { - hfile = new char[strlen(file) + 16]; - char *ptr = strchr(file,'%'); - *ptr = '\0'; - sprintf(hfile,"%s%s%s",file,"base",ptr+1); - *ptr = '%'; - } else hfile = file; - fp = fopen(hfile,"rb"); + hfile.replace(hfile.find("%"),1,"base"); + } + fp = fopen(hfile.c_str(),"rb"); if (fp == NULL) error->one(FLERR,fmt::format("Cannot open restart file {}: {}", hfile, utils::getsyserror())); - if (multiproc) delete [] hfile; } // read magic string, endian flag, format revision @@ -272,14 +267,10 @@ void ReadRestart::command(int narg, char **arg) else if (nprocs <= multiproc_file) { - char *procfile = new char[strlen(file) + 16]; - char *ptr = strchr(file,'%'); - for (int iproc = me; iproc < multiproc_file; iproc += nprocs) { - *ptr = '\0'; - sprintf(procfile,"%s%d%s",file,iproc,ptr+1); - *ptr = '%'; - fp = fopen(procfile,"rb"); + std::string procfile = file; + procfile.replace(procfile.find("%"),1,fmt::format("{}",iproc)); + fp = fopen(procfile.c_str(),"rb"); if (fp == NULL) error->one(FLERR,fmt::format("Cannot open restart file {}: {}", procfile, utils::getsyserror())); @@ -309,8 +300,6 @@ void ReadRestart::command(int narg, char **arg) fclose(fp); fp = NULL; } - - delete [] procfile; } // input of multiple native files with procs > files @@ -343,16 +332,12 @@ void ReadRestart::command(int narg, char **arg) MPI_Comm_split(world,icluster,0,&clustercomm); if (filereader) { - char *procfile = new char[strlen(file) + 16]; - char *ptr = strchr(file,'%'); - *ptr = '\0'; - sprintf(procfile,"%s%d%s",file,icluster,ptr+1); - *ptr = '%'; - fp = fopen(procfile,"rb"); + std::string procfile = file; + procfile.replace(procfile.find("%"),1,fmt::format("{}",icluster)); + fp = fopen(procfile.c_str(),"rb"); if (fp == NULL) error->one(FLERR,fmt::format("Cannot open restart file {}: {}", procfile, utils::getsyserror())); - delete [] procfile; } int flag,procsperfile; @@ -618,10 +603,9 @@ void ReadRestart::file_search(char *inpfile, char *outfile) // create outfile with maxint substituted for "*" // use original inpfile, not pattern, since need to retain "%" in filename - ptr = strchr(inpfile,'*'); - *ptr = '\0'; - sprintf(outfile,"%s" BIGINT_FORMAT "%s",inpfile,maxnum,ptr+1); - *ptr = '*'; + std::string newoutfile = inpfile; + newoutfile.replace(newoutfile.find("*"),1,fmt::format("{}",maxnum)); + strcpy(outfile,newoutfile.c_str()); // clean up diff --git a/src/reset_ids.cpp b/src/reset_ids.cpp index b8255cd273..671ba7940f 100644 --- a/src/reset_ids.cpp +++ b/src/reset_ids.cpp @@ -24,6 +24,7 @@ #include "memory.h" #include "error.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -252,12 +253,9 @@ void ResetIDs::command(int narg, char **arg) int all; MPI_Allreduce(&badcount,&all,1,MPI_INT,MPI_SUM,world); - if (all) { - char str[128]; - sprintf(str,"Reset_ids missing %d bond topology atom IDs - " - "use comm_modify cutoff",all); - error->all(FLERR,str); - } + if (all) + error->all(FLERR,fmt::format("Reset_ids missing {} bond topology atom IDs - " + "use comm_modify cutoff",all)); // reset IDs and atom map for owned atoms diff --git a/src/thermo.cpp b/src/thermo.cpp index b2a9b12c86..52b1813152 100644 --- a/src/thermo.cpp +++ b/src/thermo.cpp @@ -45,6 +45,7 @@ #include "error.h" #include "math_const.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace MathConst; @@ -423,21 +424,15 @@ bigint Thermo::lost_check() // error message - if (lostflag == Thermo::ERROR) { - char str[64]; - sprintf(str, - "Lost atoms: original " BIGINT_FORMAT " current " BIGINT_FORMAT, - atom->natoms,ntotal); - error->all(FLERR,str); - } + if (lostflag == Thermo::ERROR) + error->all(FLERR,fmt::format("Lost atoms: original {} current {}", + atom->natoms,ntotal)); // warning message - char str[64]; - sprintf(str, - "Lost atoms: original " BIGINT_FORMAT " current " BIGINT_FORMAT, - atom->natoms,ntotal); - if (me == 0) error->warning(FLERR,str,0); + if (me == 0) + error->warning(FLERR,fmt::format("Lost atoms: original {} current {}", + atom->natoms,ntotal),0); // reset total atom count