simplify and avoid temporary buffers when piping to/from gzip

This commit is contained in:
Axel Kohlmeyer
2021-04-07 23:26:21 -04:00
parent a84ac392a3
commit 1ca38db9df
5 changed files with 60 additions and 68 deletions

View File

@ -14,16 +14,16 @@
#include "dump.h" #include "dump.h"
#include "atom.h" #include "atom.h"
#include "irregular.h"
#include "update.h"
#include "domain.h"
#include "group.h"
#include "output.h"
#include "modify.h"
#include "fix.h"
#include "compute.h" #include "compute.h"
#include "memory.h" #include "domain.h"
#include "error.h" #include "error.h"
#include "fix.h"
#include "group.h"
#include "irregular.h"
#include "memory.h"
#include "modify.h"
#include "output.h"
#include "update.h"
#include <cstring> #include <cstring>
@ -141,12 +141,9 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp)
if (strchr(filename,'*')) multifile = 1; if (strchr(filename,'*')) multifile = 1;
char *suffix = filename + strlen(filename) - strlen(".bin"); if (utils::strmatch(filename, "\\.bin$")) binary = 1;
if (suffix > filename && strcmp(suffix,".bin") == 0) binary = 1; if (utils::strmatch(filename, "\\.gz$")
suffix = filename + strlen(filename) - strlen(".gz"); || utils::strmatch(filename, "\\.zst$")) compressed = 1;
if (suffix > filename && strcmp(suffix,".gz") == 0) compressed = 1;
suffix = filename + strlen(filename) - strlen(".zst");
if (suffix > filename && strcmp(suffix,".zst") == 0) compressed = 1;
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
@ -582,12 +579,11 @@ void Dump::openfile()
if (filewriter) { if (filewriter) {
if (compressed) { if (compressed) {
#ifdef LAMMPS_GZIP #ifdef LAMMPS_GZIP
char gzip[128]; auto gzip = fmt::format("gzip -6 > {}",filecurrent);
sprintf(gzip,"gzip -6 > %s",filecurrent);
#ifdef _WIN32 #ifdef _WIN32
fp = _popen(gzip,"wb"); fp = _popen(gzip.c_str(),"wb");
#else #else
fp = popen(gzip,"w"); fp = popen(gzip.c_str(),"w");
#endif #endif
#else #else
error->one(FLERR,"Cannot open gzipped file"); error->one(FLERR,"Cannot open gzipped file");

View File

@ -18,19 +18,18 @@
#include "fix_tmd.h" #include "fix_tmd.h"
#include "atom.h"
#include "domain.h"
#include "error.h"
#include "force.h"
#include "group.h"
#include "memory.h"
#include "modify.h"
#include "respa.h"
#include "update.h"
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
#include "atom.h"
#include "update.h"
#include "modify.h"
#include "domain.h"
#include "group.h"
#include "respa.h"
#include "force.h"
#include "memory.h"
#include "error.h"
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using namespace FixConst; using namespace FixConst;
@ -520,31 +519,29 @@ void FixTMD::readfile(char *file)
void FixTMD::open(char *file) void FixTMD::open(char *file)
{ {
compressed = 0; if (utils::strmatch(file,"\\.gz$")) {
char *suffix = file + strlen(file) - 3; compressed = 1;
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
if (!compressed) fp = fopen(file,"r");
else {
#ifdef LAMMPS_GZIP #ifdef LAMMPS_GZIP
char gunzip[128]; auto gunzip = fmt::format("gzip -c -d {}",file);
snprintf(gunzip,128,"gzip -c -d %s",file);
#ifdef _WIN32 #ifdef _WIN32
fp = _popen(gunzip,"rb"); fp = _popen(gunzip.c_str(),"rb");
#else #else
fp = popen(gunzip,"r"); fp = popen(gunzip.c_str(),"r");
#endif #endif
#else #else
error->one(FLERR,"Cannot open gzipped file"); error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif #endif
} else {
compressed = 0;
fp = fopen(file,"r");
} }
if (fp == nullptr) { if (fp == nullptr)
char str[128]; error->one(FLERR,fmt::format("Cannot open file {}: {}",
snprintf(str,128,"Cannot open file %s",file); file, utils::getsyserror()));
error->one(FLERR,str);
}
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */

View File

@ -4734,19 +4734,14 @@ void lammps_set_fix_external_callback(void *handle, char *id, FixExternalFnPtr c
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("Cannot 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 "
snprintf(str, 128, "Fix '%s' is not of style external!", id); "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

@ -1954,13 +1954,12 @@ int ReadData::reallocate(int **pcount, int cmax, int amax)
void ReadData::open(char *file) void ReadData::open(char *file)
{ {
compressed = 0; if (utils::strmatch(file,"\\.gz$")) {
char *suffix = file + strlen(file) - 3; compressed = 1;
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
if (!compressed) fp = fopen(file,"r");
else {
#ifdef LAMMPS_GZIP #ifdef LAMMPS_GZIP
std::string gunzip = fmt::format("gzip -c -d {}",file); auto gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32 #ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb"); fp = _popen(gunzip.c_str(),"rb");
#else #else
@ -1968,8 +1967,11 @@ void ReadData::open(char *file)
#endif #endif
#else #else
error->one(FLERR,"Cannot open gzipped file: " + utils::getsyserror()); error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif #endif
} else {
compressed = 0;
fp = fopen(file,"r");
} }
if (fp == nullptr) if (fp == nullptr)

View File

@ -37,13 +37,12 @@ void Reader::open_file(const char *file)
{ {
if (fp != nullptr) close_file(); if (fp != nullptr) close_file();
compressed = 0; if (utils::strmatch(file,"\\.gz$")) {
const char *suffix = file + strlen(file) - 3; compressed = 1;
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
if (!compressed) fp = fopen(file,"r");
else {
#ifdef LAMMPS_GZIP #ifdef LAMMPS_GZIP
std::string gunzip = fmt::format("gzip -c -d {}",file); auto gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32 #ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb"); fp = _popen(gunzip.c_str(),"rb");
#else #else
@ -51,8 +50,11 @@ void Reader::open_file(const char *file)
#endif #endif
#else #else
error->one(FLERR,"Cannot open gzipped file: " + utils::getsyserror()); error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif #endif
} else {
compressed = 0;
fp = fopen(file,"r");
} }
if (fp == nullptr) if (fp == nullptr)