gzip: simplify uncompressed interface

This commit is contained in:
Emil J. Tywoniak 2025-01-03 18:40:40 +01:00
parent 094ab8c017
commit 0b96aa1923
4 changed files with 18 additions and 23 deletions

View File

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

View File

@ -72,7 +72,7 @@ private:
#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

View File

@ -469,14 +469,8 @@ void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector<s
next_args.insert(next_args.end(), args.begin(), args.begin()+argidx);
next_args.insert(next_args.end(), filenames.begin()+1, filenames.end());
}
std::ifstream *ff = new std::ifstream;
ff->open(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));

View File

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