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, // Takes a successfully opened ifstream. If it's gzipped, returns an istream. Otherwise,
// returns the original ifstream, rewound to the start. // returns the original ifstream, rewound to the start.
std::istream* uncompressed(std::ifstream* f, const std::string filename) { std::istream& uncompressed(const std::string filename, std::ios_base::openmode mode) {
if (!f) std::ifstream& f = *new std::ifstream();
return nullptr; f.open(filename, mode);
if (f.fail())
return f;
// Check for gzip magic // Check for gzip magic
unsigned char magic[3]; unsigned char magic[3];
int n = 0; int n = 0;
while (n < 3) while (n < 3)
{ {
int c = f->get(); int c = f.get();
if (c != EOF) { if (c != EOF) {
magic[n] = (unsigned char) c; magic[n] = (unsigned char) c;
} }
@ -116,15 +118,16 @@ std::istream* uncompressed(std::ifstream* f, const std::string filename) {
if (magic[2] != 8) if (magic[2] != 8)
log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n", log_cmd_error("gzip file `%s' uses unsupported compression type %02x\n",
filename.c_str(), unsigned(magic[2])); filename.c_str(), unsigned(magic[2]));
delete f; gzip_istream& s = *new gzip_istream();
gzip_istream* s = new gzip_istream(); delete &f;
return s->open(filename.c_str()) ? s : nullptr; s.open(filename.c_str());
return s;
#else #else
log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", filename.c_str()); log_cmd_error("File `%s' is a gzip file, but Yosys is compiled without zlib.\n", filename.c_str());
#endif // YOSYS_ENABLE_ZLIB #endif // YOSYS_ENABLE_ZLIB
} else { } else {
f->clear(); f.clear();
f->seekg(0, std::ios::beg); f.seekg(0, std::ios::beg);
return f; return f;
} }
} }

View File

@ -72,7 +72,7 @@ private:
#endif // YOSYS_ENABLE_ZLIB #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 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(), args.begin(), args.begin()+argidx);
next_args.insert(next_args.end(), filenames.begin()+1, filenames.end()); 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); yosys_input_files.insert(filename);
if (ff->fail()) { f = &uncompressed(filename, bin_input ? std::ifstream::binary : std::ifstream::in);
delete ff;
ff = nullptr;
}
f = uncompressed(ff, filename);
} }
if (f == NULL) if (f == NULL)
log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno)); 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; LibertyMergedCells merged;
for (auto path : liberty_files) { for (auto path : liberty_files) {
std::ifstream f; std::istream& f = uncompressed(path);
f.open(path.c_str()); if (f.fail())
std::istream* ff = uncompressed(&f, path);
if (ff->fail())
log_cmd_error("Can't open liberty file `%s': %s\n", path.c_str(), strerror(errno)); 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); merged.merge(p);
delete ff; delete &f;
} }
find_cell(merged.cells, ID($_DFF_N_), false, false, false, false, false, false, dont_use_cells); find_cell(merged.cells, ID($_DFF_N_), false, false, false, false, false, false, dont_use_cells);