From a48c4dcd331c5fab05c4ee5285295899e8f8d54c Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sun, 24 Mar 2024 19:48:15 +0100 Subject: [PATCH] tools/zlibutil: Change CLI to act more like other compression utilities Compression is now the implied default action, decompress with the -d flag. --- src/tools/zlibutil.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/tools/zlibutil.cpp b/src/tools/zlibutil.cpp index 4336a05..f9e3bdb 100644 --- a/src/tools/zlibutil.cpp +++ b/src/tools/zlibutil.cpp @@ -36,7 +36,7 @@ #define CHUNK_SIZE 16384 // Chunk size /* -Finnaly, the main file +Finally, the main file ⠀⠀⠀⠀⠀⠀⠀⢠⣤⣄⣀⣠⣤⣶⣿⣯⣿⣽⣾⣷⣶⣶⡦⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⢀⣿⣟⣯⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣟⣦⣄⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣾⡷⣄⠀⠀⠀⠀⠀ @@ -66,8 +66,7 @@ Finnaly, the main file int main(int argc, char* argv[]) { std::vector flags; - flags.push_back(CLI::Flag('c', "compress", "compress a file")); - flags.push_back(CLI::Flag('d', "decompress", "descompress a file")); + flags.push_back(CLI::Flag('d', "decompress", "descompress a file")); std::vector options; std::vector arguments; arguments.push_back(CLI::Argument("FILE", "path of the file to compress/decompress")); @@ -105,32 +104,39 @@ int main(int argc, char* argv[]) { return EXIT_USAGE; } + std::string filename = cliParser.getArgument(0).value; - ErrorOr filePointer = File::open(cliParser.getArgument(0).value, 'r'); + ErrorOr filePointer = File::open(filename, 'r'); if (filePointer.isError) { - std::cout << "Failed to open file: " << cliParser.getArgument(0).value << std::endl; + std::cout << "Failed to open file: " << filename << std::endl; return EXIT_RUNTIME; } File* file = filePointer.value; File *writeFile; - if (cliParser.getFlag("compress").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 compressed = compressData(differentBytes.data(), file->size.value); - std::vector unsigneddata = std::vector(compressed.begin(), compressed.end()); - writeFile = File::open(cliParser.getArgument(0).value + ".compressed", 'w').value; - writeFile->write(unsigneddata); - writeFile->close(); - } else if (cliParser.getFlag("decompress").value) { + 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 compressed = decompressData(differentBytes.data(), file->size.value); std::vector unsigneddata = std::vector(compressed.begin(), compressed.end()); - writeFile = File::open(cliParser.getArgument(0).value + ".uncompressed", 'w').value; + std::string outFilename; + if (filename.length() > 3 && filename.rfind(".zz") == filename.length()-3) { + outFilename = filename.substr(0, filename.length()-3); + } else { + outFilename = filename + ".uncompressed"; + } + writeFile = File::open(outFilename, 'w').value; + writeFile->write(unsigneddata); + 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 compressed = compressData(differentBytes.data(), file->size.value); + std::vector unsigneddata = std::vector(compressed.begin(), compressed.end()); + writeFile = File::open(filename + ".zz", 'w').value; writeFile->write(unsigneddata); writeFile->close(); }