From 61cec73c49780894cb18790d563c666719b8711f Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sun, 9 Jun 2024 02:51:32 +0200 Subject: [PATCH] lib/zlibutil, tools/zlibutil: moving things around This includes the following changes: - Move the data type wrangling into the lib to make it easier to use - Put the functions into their own `zlib` namespace - Use ErrorOr instead of exceptions - Error codes for compression/decompression --- src/tools/zlibutil.cpp | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/tools/zlibutil.cpp b/src/tools/zlibutil.cpp index f9e3bdb..184f05b 100644 --- a/src/tools/zlibutil.cpp +++ b/src/tools/zlibutil.cpp @@ -28,6 +28,7 @@ #include "../lib/cli.hpp" #include "../lib/file.hpp" #include "../lib/zlibutil.hpp" +#include "../lib/error.hpp" #define EXIT_SUCCESS 0 #define EXIT_RUNTIME 1 @@ -112,14 +113,18 @@ int main(int argc, char* argv[]) { return EXIT_RUNTIME; } File* file = filePointer.value; - File *writeFile; + File* writeFile; if (cliParser.getFlag("decompress").value) { - std::vector bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC - std::vector differentBytes = std::vector(bytes.begin(), bytes.end()); + std::vector bytes = file->read(file->size.value).value; - std::vector compressed = decompressData(differentBytes.data(), file->size.value); - std::vector unsigneddata = std::vector(compressed.begin(), compressed.end()); + ErrorOr> decompressed = zlib::decompressData(bytes); + if (decompressed.isError) { + std::cout << "Error: Failed to decompress: " << filename << std::endl; + delete file; + // not cleaning up writeFile here bc it hasn't been created + return EXIT_RUNTIME; + } std::string outFilename; if (filename.length() > 3 && filename.rfind(".zz") == filename.length()-3) { @@ -128,16 +133,23 @@ int main(int argc, char* argv[]) { outFilename = filename + ".uncompressed"; } writeFile = File::open(outFilename, 'w').value; - writeFile->write(unsigneddata); + writeFile->write(decompressed.value); writeFile->close(); } else { - std::vector bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC - std::vector differentBytes = std::vector(bytes.begin(), bytes.end()); + std::vector bytes = file->read(file->size.value).value; - std::vector compressed = compressData(differentBytes.data(), file->size.value); - std::vector unsigneddata = std::vector(compressed.begin(), compressed.end()); + ErrorOr> compressed = zlib::compressData(bytes); + if (compressed.isError) { + std::cout << "Error: Failed to compress: " << filename << std::endl; + delete file; + // not cleaning up writeFile here bc it hasn't been created + return EXIT_RUNTIME; + } writeFile = File::open(filename + ".zz", 'w').value; - writeFile->write(unsigneddata); + writeFile->write(compressed.value); writeFile->close(); } + + delete file; + delete writeFile; }