From 70925cbde4dcd1bf7dd22169ee0eae9095d37d86 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 24 Jan 2020 22:29:35 -0600 Subject: [PATCH] [nostalgia/pack] Cleanup error handling and file I/O --- src/nostalgia/tools/pack.cpp | 39 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/nostalgia/tools/pack.cpp b/src/nostalgia/tools/pack.cpp index 7c111a8e..d8b37e2e 100644 --- a/src/nostalgia/tools/pack.cpp +++ b/src/nostalgia/tools/pack.cpp @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -19,46 +20,46 @@ using namespace std; using namespace ox; -[[nodiscard]] static ox::Error writeFileBuff(const std::string &path, const std::vector &buff) { - auto file = fopen(path.c_str(), "wb"); - if (!file) { - return OxError(1); +static void writeFileBuff(const std::string &path, std::vector &buff) { + try { + std::ofstream f(path, std::ios::binary); + f.write(reinterpret_cast(buff.data()), buff.size()); + } catch (const std::fstream::failure&) { + throw OxError(2); } - oxReturnError(OxError(fwrite(buff.data(), buff.size(), 1, file) != 1)); - oxReturnError(OxError(fclose(file))); - return OxError(0); } -[[nodiscard]] ox::Error run(ClArgs args) { +void run(ClArgs args) { std::string argSrc = args.getString("src").c_str(); std::string argDst = args.getString("dst").c_str(); if (argSrc == "") { std::cerr << "error: must specify a source directory\n"; - return OxError(1); + throw OxError(1); } if (argDst == "") { std::cerr << "error: must specify a destination ROM file\n"; - return OxError(1); + throw OxError(1); } std::vector buff(32 * ox::units::MB); - oxReturnError(ox::FileSystem32::format(buff.data(), buff.size())); + oxThrowError(ox::FileSystem32::format(buff.data(), buff.size())); ox::PassThroughFS src(argSrc.c_str()); ox::FileSystem32 dst(ox::FileStore32(buff.data(), buff.size())); - oxReturnError(nostalgia::pack(&src, &dst)); + oxThrowError(nostalgia::pack(&src, &dst)); - oxReturnError(dst.resize()); + oxThrowError(dst.resize()); std::cout << "new size: " << dst.size() << '\n'; buff.resize(dst.size()); - oxReturnError(writeFileBuff(argDst, buff)); - return OxError(0); + writeFileBuff(argDst, buff); } int main(int argc, const char **args) { - auto err = run(ClArgs(argc, args)); - oxAssert(err, "pack failed"); - if (err) { + try { + run(ClArgs(argc, args)); + } catch (const ox::Error &err) { + oxAssert(err, "pack failed"); std::cerr << "pack failed...\n"; + return static_cast(err); } - return static_cast(err); + return 0; }