simplify more code with std::string and fmtlib

This commit is contained in:
Axel Kohlmeyer 2020-06-25 23:13:28 -04:00
parent e4a3a518f7
commit 84ec2cc39b
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
3 changed files with 22 additions and 35 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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