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
jocadbz
BodgeMaster 2024-06-09 02:51:32 +02:00 committed by Jocadbz
parent ef0a2707dd
commit 61cec73c49
1 changed files with 23 additions and 11 deletions

View File

@ -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<uint8_t> bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC
std::vector<char> differentBytes = std::vector<char>(bytes.begin(), bytes.end());
std::vector<uint8_t> bytes = file->read(file->size.value).value;
std::vector<char> compressed = decompressData(differentBytes.data(), file->size.value);
std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end());
ErrorOr<std::vector<uint8_t>> 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<uint8_t> bytes = file->read(file->size.value).value; // this is what you get from lib/file IIRC
std::vector<char> differentBytes = std::vector<char>(bytes.begin(), bytes.end());
std::vector<uint8_t> bytes = file->read(file->size.value).value;
std::vector<char> compressed = compressData(differentBytes.data(), file->size.value);
std::vector<unsigned char> unsigneddata = std::vector<unsigned char>(compressed.begin(), compressed.end());
ErrorOr<std::vector<uint8_t>> 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;
}