get rid of local buffers and snprinf()

This commit is contained in:
Axel Kohlmeyer
2020-06-04 16:25:09 -04:00
parent 4b58e33b1e
commit 62ee8d41f3
8 changed files with 76 additions and 124 deletions

View File

@ -251,11 +251,9 @@ void Input::file(const char *filename)
error->one(FLERR,"Too many nested levels of input scripts"); error->one(FLERR,"Too many nested levels of input scripts");
infile = fopen(filename,"r"); infile = fopen(filename,"r");
if (infile == NULL) { if (infile == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open input script {}",filename));
snprintf(str,128,"Cannot open input script %s",filename);
error->one(FLERR,str);
}
infiles[nfile++] = infile; infiles[nfile++] = infile;
} }
@ -545,11 +543,10 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag)
value = variable->retrieve(var); value = variable->retrieve(var);
} }
if (value == NULL) { if (value == NULL)
char str[128]; error->one(FLERR,fmt::format("Substitution for illegal "
snprintf(str,128,"Substitution for illegal variable %s",var); "variable {}",var));
error->one(FLERR,str);
}
// check if storage in str2 needs to be expanded // check if storage in str2 needs to be expanded
// re-initialize ptr and ptr2 to the point beyond the variable. // re-initialize ptr and ptr2 to the point beyond the variable.
@ -1038,11 +1035,9 @@ void Input::include()
error->one(FLERR,"Too many nested levels of input scripts"); error->one(FLERR,"Too many nested levels of input scripts");
infile = fopen(arg[0],"r"); infile = fopen(arg[0],"r");
if (infile == NULL) { if (infile == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open input script {}",arg[0]));
snprintf(str,128,"Cannot open input script %s",arg[0]);
error->one(FLERR,str);
}
infiles[nfile++] = infile; infiles[nfile++] = infile;
} }
@ -1073,11 +1068,9 @@ void Input::jump()
else { else {
if (infile && infile != stdin) fclose(infile); if (infile && infile != stdin) fclose(infile);
infile = fopen(arg[0],"r"); infile = fopen(arg[0],"r");
if (infile == NULL) { if (infile == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open input script {}",arg[0]));
snprintf(str,128,"Cannot open input script %s",arg[0]);
error->one(FLERR,str);
}
infiles[nfile-1] = infile; infiles[nfile-1] = infile;
} }
} }
@ -1118,11 +1111,9 @@ void Input::log()
if (appendflag) logfile = fopen(arg[0],"a"); if (appendflag) logfile = fopen(arg[0],"a");
else logfile = fopen(arg[0],"w"); else logfile = fopen(arg[0],"w");
if (logfile == NULL) { if (logfile == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open logfile {}",arg[0]));
snprintf(str,128,"Cannot open logfile %s",arg[0]);
error->one(FLERR,str);
}
} }
if (universe->nworlds == 1) universe->ulogfile = logfile; if (universe->nworlds == 1) universe->ulogfile = logfile;
} }
@ -1197,11 +1188,9 @@ void Input::print()
if (fp != NULL) fclose(fp); if (fp != NULL) fclose(fp);
if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w"); if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w");
else fp = fopen(arg[iarg+1],"a"); else fp = fopen(arg[iarg+1],"a");
if (fp == NULL) { if (fp == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open print file {}",
snprintf(str,128,"Cannot open print file %s",arg[iarg+1]); arg[iarg+1]));
error->one(FLERR,str);
}
} }
iarg += 2; iarg += 2;
} else if (strcmp(arg[iarg],"screen") == 0) { } else if (strcmp(arg[iarg],"screen") == 0) {
@ -1810,18 +1799,15 @@ void Input::pair_style()
{ {
if (narg < 1) error->all(FLERR,"Illegal pair_style command"); if (narg < 1) error->all(FLERR,"Illegal pair_style command");
if (force->pair) { if (force->pair) {
std::string style = arg[0];
int match = 0; int match = 0;
if (strcmp(arg[0],force->pair_style) == 0) match = 1; if (style == force->pair_style) match = 1;
if (!match && lmp->suffix_enable) { if (!match && lmp->suffix_enable) {
char estyle[256]; if (lmp->suffix)
if (lmp->suffix) { if (style + "/" + lmp->suffix == force->pair_style) match = 1;
snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix);
if (strcmp(estyle,force->pair_style) == 0) match = 1; if (lmp->suffix2)
} if (style + "/" + lmp->suffix2 == force->pair_style) match = 1;
if (lmp->suffix2) {
snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix2);
if (strcmp(estyle,force->pair_style) == 0) match = 1;
}
} }
if (match) { if (match) {
force->pair->settings(narg-1,&arg[1]); force->pair->settings(narg-1,&arg[1]);

View File

@ -436,11 +436,9 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
if (universe->me == 0) { if (universe->me == 0) {
if (inflag == 0) infile = stdin; if (inflag == 0) infile = stdin;
else infile = fopen(arg[inflag],"r"); else infile = fopen(arg[inflag],"r");
if (infile == NULL) { if (infile == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open input script {}",
snprintf(str,128,"Cannot open input script %s",arg[inflag]); arg[inflag]));
error->one(FLERR,str);
}
} }
if ((universe->me == 0) && !helpflag) if ((universe->me == 0) && !helpflag)

View File

@ -41,6 +41,7 @@
#include "neighbor.h" #include "neighbor.h"
#include "neigh_list.h" #include "neigh_list.h"
#include "neigh_request.h" #include "neigh_request.h"
#include "fmt/format.h"
#if defined(LAMMPS_EXCEPTIONS) #if defined(LAMMPS_EXCEPTIONS)
#include "exceptions.h" #include "exceptions.h"
@ -1582,14 +1583,11 @@ void lammps_create_atoms(void *ptr, int n, tagint *id, int *type,
// warn if new natoms is not correct // warn if new natoms is not correct
if (lmp->atom->natoms != natoms_prev + n) { if ((lmp->atom->natoms != natoms_prev + n) && (lmp->comm->me == 0))
char str[128]; lmp->error->warning(FLERR,fmt::format("Library warning in "
snprintf(str, 128, "Library warning in lammps_create_atoms, " "lammps_create_atoms: "
"invalid total atoms " BIGINT_FORMAT " " BIGINT_FORMAT, "invalid total atoms {} vs. {}",
lmp->atom->natoms,natoms_prev+n); lmp->atom->natoms,natoms_prev+n));
if (lmp->comm->me == 0)
lmp->error->warning(FLERR,str);
}
} }
END_CAPTURE END_CAPTURE
} }
@ -1607,19 +1605,13 @@ void lammps_set_fix_external_callback(void *ptr, char *id, FixExternalFnPtr call
BEGIN_CAPTURE BEGIN_CAPTURE
{ {
int ifix = lmp->modify->find_fix(id); int ifix = lmp->modify->find_fix(id);
if (ifix < 0) { if (ifix < 0)
char str[128]; lmp->error->all(FLERR,fmt::format("Can not find fix with ID '{}'!", id));
snprintf(str, 128, "Can not find fix with ID '%s'!", id);
lmp->error->all(FLERR,str);
}
Fix *fix = lmp->modify->fix[ifix]; Fix *fix = lmp->modify->fix[ifix];
if (strcmp("external",fix->style) != 0){ if (strcmp("external",fix->style) != 0)
char str[128]; lmp->error->all(FLERR,fmt::format("Fix '{}' is not of style external!", id));
snprintf(str, 128, "Fix '%s' is not of style external!", id);
lmp->error->all(FLERR,str);
}
FixExternal * fext = (FixExternal*) fix; FixExternal * fext = (FixExternal*) fix;
fext->set_callback(callback, caller); fext->set_callback(callback, caller);

View File

@ -14,6 +14,7 @@
#include "memory.h" #include "memory.h"
#include <cstdlib> #include <cstdlib>
#include "error.h" #include "error.h"
#include "fmt/format.h"
#if defined(LMP_USER_INTEL) && defined(__INTEL_COMPILER) #if defined(LMP_USER_INTEL) && defined(__INTEL_COMPILER)
#ifndef LMP_INTEL_NO_TBB #ifndef LMP_INTEL_NO_TBB
@ -56,12 +57,9 @@ void *Memory::smalloc(bigint nbytes, const char *name)
#else #else
void *ptr = malloc(nbytes); void *ptr = malloc(nbytes);
#endif #endif
if (ptr == NULL) { if (ptr == NULL)
char str[128]; error->one(FLERR,fmt::format("Failed to allocate {} bytes for array {}",
sprintf(str,"Failed to allocate " BIGINT_FORMAT " bytes for array %s", nbytes,name));
nbytes,name);
error->one(FLERR,str);
}
return ptr; return ptr;
} }
@ -92,12 +90,9 @@ void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
#else #else
ptr = realloc(ptr,nbytes); ptr = realloc(ptr,nbytes);
#endif #endif
if (ptr == NULL) { if (ptr == NULL)
char str[128]; error->one(FLERR,fmt::format("Failed to reallocate {} bytes for array {}",
sprintf(str,"Failed to reallocate " BIGINT_FORMAT " bytes for array %s", nbytes,name));
nbytes,name);
error->one(FLERR,str);
}
return ptr; return ptr;
} }
@ -121,8 +116,6 @@ void Memory::sfree(void *ptr)
void Memory::fail(const char *name) void Memory::fail(const char *name)
{ {
char str[128]; error->one(FLERR,fmt::format("Cannot create/grow a vector/array of "
snprintf(str,128, "pointers for {}",name));
"Cannot create/grow a vector/array of pointers for %s",name);
error->one(FLERR,str);
} }

View File

@ -28,6 +28,7 @@
#include "memory.h" #include "memory.h"
#include "error.h" #include "error.h"
#include "utils.h" #include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using namespace FixConst; using namespace FixConst;
@ -245,21 +246,15 @@ void Modify::init()
// error if any fix or compute is using a dynamic group when not allowed // error if any fix or compute is using a dynamic group when not allowed
for (i = 0; i < nfix; i++) for (i = 0; i < nfix; i++)
if (!fix[i]->dynamic_group_allow && group->dynamic[fix[i]->igroup]) { if (!fix[i]->dynamic_group_allow && group->dynamic[fix[i]->igroup])
char str[128]; error->all(FLERR,fmt::format("Fix {} does not allow use with a "
snprintf(str,128, "dynamic group",fix[i]->id));
"Fix %s does not allow use of dynamic group",fix[i]->id);
error->all(FLERR,str);
}
for (i = 0; i < ncompute; i++) for (i = 0; i < ncompute; i++)
if (!compute[i]->dynamic_group_allow && if (!compute[i]->dynamic_group_allow &&
group->dynamic[compute[i]->igroup]) { group->dynamic[compute[i]->igroup])
char str[128]; error->all(FLERR,fmt::format("Compute {} does not allow use with a "
snprintf(str,128,"Compute %s does not allow use of dynamic group", "dynamic group",compute[i]->id));
fix[i]->id);
error->all(FLERR,str);
}
// warn if any particle is time integrated more than once // warn if any particle is time integrated more than once

View File

@ -26,6 +26,7 @@
#include "memory.h" #include "memory.h"
#include "error.h" #include "error.h"
#include "utils.h" #include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
@ -1721,11 +1722,8 @@ void Molecule::deallocate()
void Molecule::open(char *file) void Molecule::open(char *file)
{ {
fp = fopen(file,"r"); fp = fopen(file,"r");
if (fp == NULL) { if (fp == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open molecule file {}",file));
snprintf(str,128,"Cannot open molecule file %s",file);
error->one(FLERR,str);
}
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------

View File

@ -31,6 +31,7 @@
#include "memory.h" #include "memory.h"
#include "error.h" #include "error.h"
#include "utils.h" #include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using namespace MathConst; using namespace MathConst;
@ -249,11 +250,9 @@ void PairCoulStreitz::read_file(char *file)
FILE *fp; FILE *fp;
if (comm->me == 0) { if (comm->me == 0) {
fp = fopen(file,"r"); fp = fopen(file,"r");
if (fp == NULL) { if (fp == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open coul/streitz potential "
snprintf(str,128,"Cannot open coul/streitz potential file %s",file); "file {}",file));
error->one(FLERR,str);
}
} }
// read each line out of file, skipping blank lines or leading '#' // read each line out of file, skipping blank lines or leading '#'

View File

@ -22,6 +22,7 @@
#include "potential_file_reader.h" #include "potential_file_reader.h"
#include "utils.h" #include "utils.h"
#include "tokenizer.h" #include "tokenizer.h"
#include "fmt/format.h"
#include <cstring> #include <cstring>
@ -36,11 +37,9 @@ PotentialFileReader::PotentialFileReader(LAMMPS *lmp,
fp = force->open_potential(filename.c_str()); fp = force->open_potential(filename.c_str());
if (fp == NULL) { if (fp == NULL)
char str[128]; error->one(FLERR,fmt::format("Cannot open {} potential file {}",
snprintf(str, 128, "cannot open %s potential file %s", potential_name.c_str(), filename.c_str()); potential_name, filename));
error->one(FLERR, str);
}
} }
PotentialFileReader::~PotentialFileReader() { PotentialFileReader::~PotentialFileReader() {
@ -49,12 +48,9 @@ PotentialFileReader::~PotentialFileReader() {
void PotentialFileReader::skip_line() { void PotentialFileReader::skip_line() {
char *ptr = fgets(line, MAXLINE, fp); char *ptr = fgets(line, MAXLINE, fp);
if (ptr == nullptr) { if (ptr == nullptr)
// EOF error->one(FLERR,fmt::format("Missing line in {} potential file!",
char str[128]; potential_name));
snprintf(str, 128, "Missing line in %s potential file!", potential_name.c_str());
error->one(FLERR, str);
}
} }
char *PotentialFileReader::next_line(int nparams) { char *PotentialFileReader::next_line(int nparams) {
@ -83,16 +79,13 @@ char *PotentialFileReader::next_line(int nparams) {
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF
if (nwords > 0 && nwords < nparams) { if (nwords > 0 && nwords < nparams)
char str[128]; error->one(FLERR,fmt::format("Incorrect format in {} potential "
snprintf(str, 128, "Incorrect format in %s potential file! %d/%d parameters", potential_name.c_str(), nwords, nparams); "file! {}/{} parameters",
error->one(FLERR, str); potential_name, nwords, nparams));
}
return nullptr; return nullptr;
} }
// strip comment // strip comment
if ((ptr = strchr(line, '#'))) *ptr = '\0'; if ((ptr = strchr(line, '#'))) *ptr = '\0';
@ -114,11 +107,9 @@ void PotentialFileReader::next_dvector(int n, double * list) {
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF
if (i < n) { if (i < n)
char str[128]; error->one(FLERR,fmt::format("Incorrect format in {} potential file! "
snprintf(str, 128, "Incorrect format in %s potential file! %d/%d values", potential_name.c_str(), i, n); "{}/{} values", potential_name, i, n));
error->one(FLERR, str);
}
} }
ValueTokenizer values(line); ValueTokenizer values(line);