From 0b96aa192317fd9d6a2d0090c1579525f3859d57 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Fri, 3 Jan 2025 18:40:40 +0100 Subject: [PATCH] gzip: simplify uncompressed interface --- kernel/gzip.cc | 21 ++++++++++++--------- kernel/gzip.h | 2 +- kernel/register.cc | 8 +------- passes/techmap/dfflibmap.cc | 10 ++++------ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/kernel/gzip.cc b/kernel/gzip.cc index 900e33bf49d..0da53ff68c6 100644 --- a/kernel/gzip.cc +++ b/kernel/gzip.cc @@ -96,15 +96,17 @@ gzip_istream::ibuf::~ibuf() { // Takes a successfully opened ifstream. If it's gzipped, returns an istream. Otherwise, // returns the original ifstream, rewound to the start. -std::istream* uncompressed(std::ifstream* f, const std::string filename) { - if (!f) - return nullptr; +std::istream& uncompressed(const std::string filename, std::ios_base::openmode mode) { + std::ifstream& f = *new std::ifstream(); + f.open(filename, mode); + if (f.fail()) + return f; // Check for gzip magic unsigned char magic[3]; int n = 0; while (n < 3) { - int c = f->get(); + int c = f.get(); if (c != EOF) { magic[n] = (unsigned char) c; } @@ -116,15 +118,16 @@ std::istream* uncompressed(std::ifstream* f, const std::string filename) { if (magic[2] != 8) log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n", filename.c_str(), unsigned(magic[2])); - delete f; - gzip_istream* s = new gzip_istream(); - return s->open(filename.c_str()) ? s : nullptr; + gzip_istream& s = *new gzip_istream(); + delete &f; + s.open(filename.c_str()); + return s; #else log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", filename.c_str()); #endif // YOSYS_ENABLE_ZLIB } else { - f->clear(); - f->seekg(0, std::ios::beg); + f.clear(); + f.seekg(0, std::ios::beg); return f; } } diff --git a/kernel/gzip.h b/kernel/gzip.h index 22f5093a0bf..0201d2340bf 100644 --- a/kernel/gzip.h +++ b/kernel/gzip.h @@ -72,7 +72,7 @@ class gzip_istream final : public std::istream { #endif // YOSYS_ENABLE_ZLIB -std::istream* uncompressed(std::ifstream* f, const std::string filename); +std::istream& uncompressed(const std::string filename, std::ios_base::openmode mode = std::ios_base::in); YOSYS_NAMESPACE_END diff --git a/kernel/register.cc b/kernel/register.cc index a2e7cb5772a..040cc62076d 100644 --- a/kernel/register.cc +++ b/kernel/register.cc @@ -469,14 +469,8 @@ void Frontend::extra_args(std::istream *&f, std::string &filename, std::vectoropen(filename.c_str(), bin_input ? std::ifstream::binary : std::ifstream::in); yosys_input_files.insert(filename); - if (ff->fail()) { - delete ff; - ff = nullptr; - } - f = uncompressed(ff, filename); + f = &uncompressed(filename, bin_input ? std::ifstream::binary : std::ifstream::in); } if (f == NULL) log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno)); diff --git a/passes/techmap/dfflibmap.cc b/passes/techmap/dfflibmap.cc index fedcba1e1ce..df2bfa88f74 100644 --- a/passes/techmap/dfflibmap.cc +++ b/passes/techmap/dfflibmap.cc @@ -631,14 +631,12 @@ struct DfflibmapPass : public Pass { LibertyMergedCells merged; for (auto path : liberty_files) { - std::ifstream f; - f.open(path.c_str()); - std::istream* ff = uncompressed(&f, path); - if (ff->fail()) + std::istream& f = uncompressed(path); + if (f.fail()) log_cmd_error("Can't open liberty file `%s': %s\n", path.c_str(), strerror(errno)); - LibertyParser p(*ff); + LibertyParser p(f); merged.merge(p); - delete ff; + delete &f; } find_cell(merged.cells, ID($_DFF_N_), false, false, false, false, false, false, dont_use_cells);