From 4e7e8957828aab7857d24107c590f736f47bba1f Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Sun, 25 Aug 2019 17:48:02 +0200 Subject: [PATCH] slight cleanup in class ResourceFile --- ConvertDiskImage/ConvertDiskImage.cc | 4 ++-- Elf2Mac/Object.cc | 8 ++++---- LaunchAPPL/Client/Launcher.cc | 6 ++---- LaunchAPPL/Client/MakeExecutable.cc | 4 ++-- PEFTools/MakeImport.cc | 4 ++-- ResourceFiles/ResInfo.cc | 6 +++--- ResourceFiles/ResourceFile.cc | 20 +++++++++----------- ResourceFiles/ResourceFile.h | 25 ++++++++++++------------- Rez/Rez.cc | 17 ++++++++--------- 9 files changed, 44 insertions(+), 50 deletions(-) diff --git a/ConvertDiskImage/ConvertDiskImage.cc b/ConvertDiskImage/ConvertDiskImage.cc index 584a56af30..eee9e31469 100644 --- a/ConvertDiskImage/ConvertDiskImage.cc +++ b/ConvertDiskImage/ConvertDiskImage.cc @@ -51,8 +51,8 @@ int main(int argc, char* argv[]) return 1; } - ResourceFile file(argv[1]); - if(!file.read()) + ResourceFile file; + if(!file.read(argv[1])) { std::cerr << "Couldn't read input.\n"; return 1; diff --git a/Elf2Mac/Object.cc b/Elf2Mac/Object.cc index 3f48b65e42..f765133260 100644 --- a/Elf2Mac/Object.cc +++ b/Elf2Mac/Object.cc @@ -194,7 +194,7 @@ std::string fromhex(std::string hex) void Object::SingleSegmentApp(string output) { - ResourceFile file(output); + ResourceFile file; Resources& rsrc = file.resources; rsrc.addResource(Resource(ResType("CODE"), 0, @@ -218,7 +218,7 @@ void Object::SingleSegmentApp(string output) file.creator = ResType("????"); file.type = ResType("APPL"); - file.write(); + file.write(output); } @@ -227,7 +227,7 @@ void Object::MultiSegmentApp(string output, SegmentMap& segmentMap) { bool noisy = false; - ResourceFile file(output); + ResourceFile file; Resources& rsrc = file.resources; for(auto namedSec : sections) @@ -396,5 +396,5 @@ void Object::MultiSegmentApp(string output, SegmentMap& segmentMap) file.creator = ResType("????"); file.type = ResType("APPL"); - file.write(); + file.write(output); } diff --git a/LaunchAPPL/Client/Launcher.cc b/LaunchAPPL/Client/Launcher.cc index 16649b6976..963e484114 100644 --- a/LaunchAPPL/Client/Launcher.cc +++ b/LaunchAPPL/Client/Launcher.cc @@ -22,8 +22,7 @@ Launcher::Launcher(boost::program_options::variables_map &options) } else { - app.assign(fn); - if(!app.read()) + if(!app.read(fn)) throw std::runtime_error("Could not load application file."); } @@ -39,8 +38,7 @@ Launcher::Launcher(boost::program_options::variables_map &options) Launcher::Launcher(boost::program_options::variables_map &options, ResourceFile::Format f) : Launcher(options) { - app.assign(appPath.string(), f); - app.write(); + app.write(appPath.string(), f); } void Launcher::DumpOutput() diff --git a/LaunchAPPL/Client/MakeExecutable.cc b/LaunchAPPL/Client/MakeExecutable.cc index 2707881f56..56ac95671d 100644 --- a/LaunchAPPL/Client/MakeExecutable.cc +++ b/LaunchAPPL/Client/MakeExecutable.cc @@ -11,8 +11,8 @@ namespace fs = boost::filesystem; void MakeExecutable(string fn) { - ResourceFile rsrcFile(fn); - if(!rsrcFile.read()) + ResourceFile rsrcFile; + if(!rsrcFile.read(fn)) { std::cerr << "Cannot read application file: " << fn << std::endl; exit(1); diff --git a/PEFTools/MakeImport.cc b/PEFTools/MakeImport.cc index 79085469b9..d3125f5991 100755 --- a/PEFTools/MakeImport.cc +++ b/PEFTools/MakeImport.cc @@ -214,8 +214,8 @@ void MakeImportLibrary(char *pefptr, size_t pefsize, fs::path dest, fs::path tmp bool MakeImportLibraryMulti(fs::path path, fs::path libname) { - ResourceFile resFile(path.string()); - assert(resFile.read()); + ResourceFile resFile; + assert(resFile.read(path.string())); std::vector data(resFile.data.begin(), resFile.data.end()); diff --git a/ResourceFiles/ResInfo.cc b/ResourceFiles/ResInfo.cc index 963d511e13..db7e91ca64 100644 --- a/ResourceFiles/ResInfo.cc +++ b/ResourceFiles/ResInfo.cc @@ -106,9 +106,9 @@ int main(int argc, char *argv[]) if(options.count("input")) for(std::string fn : options["input"].as>()) { - ResourceFile rsrcFile(fn, format); + ResourceFile rsrcFile; - if(!rsrcFile.read()) + if(!rsrcFile.read(fn, format)) { std::cerr << "Can't read file.\n"; return 1; @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) if(showCreator) out << " " << rsrcFile.creator; if(showFormat) - out << " " << reverseFormats[rsrcFile.format]; + out << " " << reverseFormats[rsrcFile.getFormat()]; if(showCount) out << " " << rsrcFile.resources.resources.size(); if(showSize) diff --git a/ResourceFiles/ResourceFile.cc b/ResourceFiles/ResourceFile.cc index 678ca55811..e77ab573f6 100644 --- a/ResourceFiles/ResourceFile.cc +++ b/ResourceFiles/ResourceFile.cc @@ -107,20 +107,18 @@ static void writeMacBinary(std::ostream& out, std::string filename, byte(out,0); } - - -ResourceFile::ResourceFile() +bool ResourceFile::read(std::string path, Format f) { + if(!assign(path, f)) + return false; + return read(); } -ResourceFile::ResourceFile(std::string path, ResourceFile::Format f) +bool ResourceFile::write(std::string path, Format f) { - assign(path, f); -} - -ResourceFile::~ResourceFile() -{ - + if(!assign(path, f)) + return false; + return write(); } static bool CheckAppleDouble(fs::path path, std::string prefix) @@ -157,7 +155,7 @@ bool ResourceFile::assign(std::string pathstring, ResourceFile::Format f) format = Format::macbin; else if(path.extension() == ".as") format = Format::applesingle; - else if(path.extension() == ".dsk" || path.extension() == ".img") + else if(path.extension() == ".dsk") format = Format::diskimage; else if(path.filename().string().substr(0,2) == "._") { diff --git a/ResourceFiles/ResourceFile.h b/ResourceFiles/ResourceFile.h index ab3ccccb85..730ca77776 100644 --- a/ResourceFiles/ResourceFile.h +++ b/ResourceFiles/ResourceFile.h @@ -25,30 +25,29 @@ public: percent_appledouble }; - ResourceFile(); - ResourceFile(std::string path, Format f = Format::autodetect); - ~ResourceFile(); - - bool assign(std::string path, Format f = Format::autodetect); - + bool read(std::string path, Format f = Format::autodetect); + bool write(std::string path, Format f = Format::autodetect); bool read(std::istream& in, Format f); bool write(std::ostream& in, Format f); - - bool read(); - bool write(); - static bool hasPlainDataFork(Format f); bool hasPlainDataFork(); + Format getFormat() { return format; } static bool isSingleFork(Format f); - std::string pathstring; - std::string filename; - Format format; ResType type; ResType creator; Resources resources; std::string data; + +private: + bool assign(std::string path, Format f = Format::autodetect); + bool read(); + bool write(); + + std::string pathstring; + std::string filename; + Format format = Format::autodetect; }; #endif // RESOURCEFILE_H diff --git a/Rez/Rez.cc b/Rez/Rez.cc index d54d7775ed..5b24e4554f 100644 --- a/Rez/Rez.cc +++ b/Rez/Rez.cc @@ -25,8 +25,8 @@ static void usage() static void CopyBinaryResources(RezWorld& world, const std::string& fn) { - ResourceFile copyRsrc(fn); - if(!copyRsrc.read()) + ResourceFile copyRsrc; + if(!copyRsrc.read(fn)) { world.problem(Diagnostic(Diagnostic::error, "Could not read binary resource file " + fn, yy::location())); } @@ -93,11 +93,11 @@ int main(int argc, const char *argv[]) world.verboseFlag = true; std::string outfile = options["output"].as(); - ResourceFile rsrcFile(outfile); + ResourceFile rsrcFile; if(options.count("append")) { - rsrcFile.read(); + rsrcFile.read(outfile); world.getResources().addResources(rsrcFile.resources); } @@ -105,8 +105,8 @@ int main(int argc, const char *argv[]) if(options.count("data")) { std::string fn = options["data"].as(); - ResourceFile dataFile(fn); - if(!dataFile.read()) + ResourceFile dataFile; + if(!dataFile.read(fn)) world.problem(Diagnostic(Diagnostic::error, "Could not read dataresource file " + fn, yy::location())); rsrcFile.data = dataFile.data; } @@ -162,13 +162,12 @@ int main(int argc, const char *argv[]) { std::cerr << "Writing " << rsrcFile.resources.countResources() << " resources.\n"; } - rsrcFile.write(); + rsrcFile.write(outfile); if(options.count("cc")) for(std::string ccFile : options["cc"].as>()) { - rsrcFile.assign(ccFile); - rsrcFile.write(); + rsrcFile.write(ccFile); } return 0;