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

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