From 84ec2cc39b2c1ee24e05b5ed8107f51b411243c5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 25 Jun 2020 23:13:28 -0400 Subject: [PATCH] simplify more code with std::string and fmtlib --- src/input.cpp | 8 ++++---- src/update.cpp | 44 ++++++++++++++++---------------------------- src/utils.cpp | 5 ++--- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 7708a79174..e5f8034e72 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1243,10 +1243,10 @@ void Input::quit() 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]; - snprintf(msg, len, "Shell command '%s' failed with error '%s'", cmd, errmsg); + std::string errmsg = fmt::format("Shell command '{}' failed with error '{}'", + cmd, strerror(errnum)); + char *msg = new char[errmsg.size()+1]; + strcpy(msg, errmsg.c_str()); return msg; } diff --git a/src/update.cpp b/src/update.cpp index c82e2bcf96..65242b8639 100644 --- a/src/update.cpp +++ b/src/update.cpp @@ -309,18 +309,14 @@ void Update::create_integrate(int narg, char **arg, int trysuffix) int sflag; new_integrate(arg[0],narg-1,&arg[1],trysuffix,sflag); + std::string estyle = arg[0]; if (sflag) { - char estyle[256]; - if (sflag == 1) snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix); - else snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix2); - int n = strlen(estyle) + 1; - integrate_style = new char[n]; - strcpy(integrate_style,estyle); - } else { - int n = strlen(arg[0]) + 1; - integrate_style = new char[n]; - strcpy(integrate_style,arg[0]); + estyle += "/"; + if (sflag == 1) estyle += lmp->suffix; + else estyle += lmp->suffix2; } + integrate_style = new char[estyle.size()+1]; + strcpy(integrate_style,estyle.c_str()); } /* ---------------------------------------------------------------------- @@ -333,8 +329,7 @@ void Update::new_integrate(char *style, int narg, char **arg, 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 + std::string("/") + lmp->suffix; if (integrate_map->find(estyle) != integrate_map->end()) { IntegrateCreator integrate_creator = (*integrate_map)[estyle]; integrate = integrate_creator(lmp, narg, arg); @@ -344,8 +339,7 @@ void Update::new_integrate(char *style, int narg, char **arg, if (lmp->suffix2) { sflag = 2; - char estyle[256]; - snprintf(estyle,256,"%s/%s",style,lmp->suffix2); + std::string estyle = style + std::string("/") + lmp->suffix2; if (integrate_map->find(estyle) != integrate_map->end()) { IntegrateCreator integrate_creator = (*integrate_map)[estyle]; integrate = integrate_creator(lmp, narg, arg); @@ -386,18 +380,14 @@ void Update::create_minimize(int narg, char **arg, int trysuffix) int sflag; new_minimize(arg[0],narg-1,&arg[1],trysuffix,sflag); + std::string estyle = arg[0]; if (sflag) { - char estyle[256]; - if (sflag == 1) snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix); - else snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix2); - int n = strlen(estyle) + 1; - minimize_style = new char[n]; - strcpy(minimize_style,estyle); - } else { - int n = strlen(arg[0]) + 1; - minimize_style = new char[n]; - strcpy(minimize_style,arg[0]); + estyle += "/"; + if (sflag == 1) estyle += lmp->suffix; + else estyle += lmp->suffix2; } + minimize_style = new char[estyle.size()+1]; + strcpy(minimize_style,estyle.c_str()); } /* ---------------------------------------------------------------------- @@ -410,8 +400,7 @@ void Update::new_minimize(char *style, int /* narg */, char ** /* arg */, 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 + std::string("/") + lmp->suffix; if (minimize_map->find(estyle) != minimize_map->end()) { MinimizeCreator minimize_creator = (*minimize_map)[estyle]; minimize = minimize_creator(lmp); @@ -421,8 +410,7 @@ void Update::new_minimize(char *style, int /* narg */, char ** /* arg */, if (lmp->suffix2) { sflag = 2; - char estyle[256]; - snprintf(estyle,256,"%s/%s",style,lmp->suffix2); + std::string estyle = style + std::string("/") + lmp->suffix2; if (minimize_map->find(estyle) != minimize_map->end()) { MinimizeCreator minimize_creator = (*minimize_map)[estyle]; minimize = minimize_creator(lmp); diff --git a/src/utils.cpp b/src/utils.cpp index df858dbb36..a4fea83abc 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -121,11 +121,10 @@ static const char *guesspath(char *buf, int len, FILE *fp) memset(buf,0,len); #if defined(__linux__) - char procpath[32]; int fd = fileno(fp); - snprintf(procpath,32,"/proc/self/fd/%d",fd); // get pathname from /proc or copy (unknown) - if (readlink(procpath,buf,len-1) <= 0) strcpy(buf,"(unknown)"); + if (readlink(fmt::format("/proc/self/fd/{}",fd).c_str(),buf,len-1) <= 0) + strcpy(buf,"(unknown)"); #else strcpy(buf,"(unknown)"); #endif