From 1930a8fd00d736f9fca676b8fe1a025e54a0cb18 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 27 May 2020 05:29:42 -0400 Subject: [PATCH] make some more use of {fmt} in lammps.cpp --- src/input.cpp | 14 ++++---- src/input.h | 2 +- src/lammps.cpp | 97 +++++++++++++++++++++++--------------------------- 3 files changed, 52 insertions(+), 61 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 7024814896..cc7f036042 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -50,6 +50,7 @@ #include "error.h" #include "memory.h" #include "utils.h" +#include "fmt/format.h" #ifdef _WIN32 #include @@ -275,11 +276,11 @@ void Input::file(const char *filename) return command name to caller ------------------------------------------------------------------------- */ -char *Input::one(const char *single) +char *Input::one(const std::string &single) { - int n = strlen(single) + 1; + int n = single.size() + 1; if (n > maxline) reallocate(line,maxline,n); - strcpy(line,single); + strcpy(line,single.c_str()); // echo the command unless scanning for label @@ -300,11 +301,8 @@ char *Input::one(const char *single) // execute the command and return its name - 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).c_str()); return command; } diff --git a/src/input.h b/src/input.h index b4df0f0160..c7b63131ee 100644 --- a/src/input.h +++ b/src/input.h @@ -34,7 +34,7 @@ class Input : protected Pointers { ~Input(); void file(); // process all input void file(const char *); // process an input script - char *one(const char *); // process a single command + char *one(const std::string&); // process a single command void substitute(char *&, char *&, int &, int &, int); // substitute for variables in a string int expand_args(int, char **, int, char **&); // expand args due to wildcard diff --git a/src/lammps.cpp b/src/lammps.cpp index dc8116f42d..2768d9bfb6 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -456,62 +456,55 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : MPI_Comm_split(universe->uworld,universe->iworld,0,&world); MPI_Comm_rank(world,&me); - if (me == 0) - if (partscreenflag == 0) - if (screenflag == 0) { - char str[32]; - sprintf(str,"screen.%d",universe->iworld); - screen = fopen(str,"w"); - if (screen == NULL) error->one(FLERR,"Cannot open screen file"); - } else if (strcmp(arg[screenflag],"none") == 0) - screen = NULL; - else { - char str[128]; - snprintf(str,128,"%s.%d",arg[screenflag],universe->iworld); - screen = fopen(str,"w"); - if (screen == NULL) error->one(FLERR,"Cannot open screen file"); - } - else if (strcmp(arg[partscreenflag],"none") == 0) - screen = NULL; - else { - char str[128]; - snprintf(str,128,"%s.%d",arg[partscreenflag],universe->iworld); - screen = fopen(str,"w"); - if (screen == NULL) error->one(FLERR,"Cannot open screen file"); - } else screen = NULL; - - if (me == 0) - if (partlogflag == 0) - if (logflag == 0) { - char str[32]; - sprintf(str,"log.lammps.%d",universe->iworld); - logfile = fopen(str,"w"); - if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); - } else if (strcmp(arg[logflag],"none") == 0) - logfile = NULL; - else { - char str[128]; - snprintf(str,128,"%s.%d",arg[logflag],universe->iworld); - logfile = fopen(str,"w"); - if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); - } - else if (strcmp(arg[partlogflag],"none") == 0) - logfile = NULL; - else { - char str[128]; - snprintf(str,128,"%s.%d",arg[partlogflag],universe->iworld); - logfile = fopen(str,"w"); - if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); - } else logfile = NULL; - + screen = logfile = infile = NULL; if (me == 0) { + std::string str; + if (partscreenflag == 0) { + if (screenflag == 0) { + str = fmt::format("screen.{}",universe->iworld); + screen = fopen(str.c_str(),"w"); + if (screen == NULL) error->one(FLERR,"Cannot open screen file"); + } else if (strcmp(arg[screenflag],"none") == 0) { + screen = NULL; + } else { + str = fmt::format("{}.{}",arg[screenflag],universe->iworld); + screen = fopen(str.c_str(),"w"); + if (screen == NULL) error->one(FLERR,"Cannot open screen file"); + } + } else if (strcmp(arg[partscreenflag],"none") == 0) { + screen = NULL; + } else { + str = fmt::format("{}.{}",arg[partscreenflag],universe->iworld); + screen = fopen(str.c_str(),"w"); + if (screen == NULL) error->one(FLERR,"Cannot open screen file"); + } + + if (partlogflag == 0) { + if (logflag == 0) { + str = fmt::format("log.lammps.{}",universe->iworld); + logfile = fopen(str.c_str(),"w"); + if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); + } else if (strcmp(arg[logflag],"none") == 0) { + logfile = NULL; + } else { + str = fmt::format("{}.{}",arg[logflag],universe->iworld); + logfile = fopen(str.c_str(),"w"); + if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); + } + } else if (strcmp(arg[partlogflag],"none") == 0) { + logfile = NULL; + } else { + str = fmt::format("{}.{}",arg[partlogflag],universe->iworld); + logfile = fopen(str.c_str(),"w"); + if (logfile == NULL) error->one(FLERR,"Cannot open logfile"); + } + infile = fopen(arg[inflag],"r"); if (infile == NULL) { - char str[128]; - snprintf(str,128,"Cannot open input script %s",arg[inflag]); - error->one(FLERR,str); + str = fmt::format("Cannot open input script {}",arg[inflag]); + error->one(FLERR,str.c_str()); } - } else infile = NULL; + } // screen and logfile messages for universe and world