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

View File

@ -18,19 +18,18 @@
#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 <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 FixConst;
@ -520,31 +519,29 @@ void FixTMD::readfile(char *file)
void FixTMD::open(char *file)
{
compressed = 0;
char *suffix = file + strlen(file) - 3;
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
if (!compressed) fp = fopen(file,"r");
else {
if (utils::strmatch(file,"\\.gz$")) {
compressed = 1;
#ifdef LAMMPS_GZIP
char gunzip[128];
snprintf(gunzip,128,"gzip -c -d %s",file);
auto gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32
fp = _popen(gunzip,"rb");
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip,"r");
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif
} else {
compressed = 0;
fp = fopen(file,"r");
}
if (fp == nullptr) {
char str[128];
snprintf(str,128,"Cannot open file %s",file);
error->one(FLERR,str);
}
if (fp == nullptr)
error->one(FLERR,fmt::format("Cannot open file {}: {}",
file, utils::getsyserror()));
}
/* ---------------------------------------------------------------------- */

View File

@ -4734,19 +4734,14 @@ void lammps_set_fix_external_callback(void *handle, char *id, FixExternalFnPtr c
BEGIN_CAPTURE
{
int ifix = lmp->modify->find_fix(id);
if (ifix < 0) {
char str[128];
snprintf(str, 128, "Can not find fix with ID '%s'!", id);
lmp->error->all(FLERR,str);
}
if (ifix < 0)
lmp->error->all(FLERR,fmt::format("Cannot find fix with ID '{}'!", id));
Fix *fix = lmp->modify->fix[ifix];
if (strcmp("external",fix->style) != 0) {
char str[128];
snprintf(str, 128, "Fix '%s' is not of style external!", id);
lmp->error->all(FLERR,str);
}
if (strcmp("external",fix->style) != 0)
lmp->error->all(FLERR,fmt::format("Fix '{}' is not of style "
"external!", id));
FixExternal * fext = (FixExternal*) fix;
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)
{
compressed = 0;
char *suffix = file + strlen(file) - 3;
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
if (!compressed) fp = fopen(file,"r");
else {
if (utils::strmatch(file,"\\.gz$")) {
compressed = 1;
#ifdef LAMMPS_GZIP
std::string gunzip = fmt::format("gzip -c -d {}",file);
auto gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb");
#else
@ -1968,8 +1967,11 @@ void ReadData::open(char *file)
#endif
#else
error->one(FLERR,"Cannot open gzipped file: " + utils::getsyserror());
error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif
} else {
compressed = 0;
fp = fopen(file,"r");
}
if (fp == nullptr)

View File

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