slight cleanup in class ResourceFile

This commit is contained in:
Wolfgang Thaller 2019-08-25 17:48:02 +02:00
parent 57d99dfbed
commit 4e7e895782
9 changed files with 44 additions and 50 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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<char> data(resFile.data.begin(), resFile.data.end());

View File

@ -106,9 +106,9 @@ int main(int argc, char *argv[])
if(options.count("input"))
for(std::string fn : options["input"].as<std::vector<std::string>>())
{
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)

View File

@ -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) == "._")
{

View File

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

View File

@ -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<std::string>();
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<std::string>();
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<std::vector<std::string>>())
{
rsrcFile.assign(ccFile);
rsrcFile.write();
rsrcFile.write(ccFile);
}
return 0;