From 3dc74f2825436535f915deec229bd385b96badb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Tabin?= Date: Sun, 22 May 2022 11:33:11 +0200 Subject: [PATCH] Allows custom error handling --- README.md | 10 ++++++++++ src/libzippp.cpp | 18 +++++++++++------- src/libzippp.h | 3 +++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cd0a107..42b4062 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,16 @@ int main(int argc, char** argv) { } ``` +### Error handling + +Actually the error handling is pretty basic and the errors details are dumped to `stderr`. +It is possible to override the macro `LIBZIPPP_ERROR_DEBUG` in order to handle the errors in +some custom way. + +```C++ +#define LIBZIPPP_ERROR_DEBUG(str, errormsg) fprintf(stderr, str "\n", errormsg); +``` + ## Known issues ### LINUX diff --git a/src/libzippp.cpp b/src/libzippp.cpp index c1b395e..738efff 100644 --- a/src/libzippp.cpp +++ b/src/libzippp.cpp @@ -144,10 +144,11 @@ bool ZipArchive::openBuffer(void* data, libzippp_uint32 size, OpenMode om, bool /* create source from buffer */ zip_source* localZipSource = zip_source_buffer_create(data, size, 0, &error); if (localZipSource == nullptr) { - fprintf(stderr, "can't create zip source: %s\n", zip_error_strerror(&error)); - zip_error_fini(&error); zip_source_free(localZipSource); localZipSource = nullptr; + + LIBZIPPP_ERROR_DEBUG("can't create zip source: %s\n", zip_error_strerror(&error)); + zip_error_fini(&error); return false; } @@ -178,7 +179,7 @@ bool ZipArchive::openSource(zip_source* source, OpenMode om, bool checkConsisten /* open zip archive from source */ zipHandle = zip_open_from_source(source, zipFlag, &error); if (zipHandle == nullptr) { - fprintf(stderr, "can't open zip from source: %s\n", zip_error_strerror(&error)); + LIBZIPPP_ERROR_DEBUG("can't open zip from source: %s", zip_error_strerror(&error)) zip_error_fini(&error); return false; } @@ -218,12 +219,15 @@ bool ZipArchive::open(OpenMode om, bool checkConsistency) { //error during opening of the file if (errorFlag!=ZIP_ER_OK) { - /*char* errorStr = new char[256]; + zipHandle = nullptr; + + char* errorStr = new char[256]; zip_error_to_str(errorStr, 255, errorFlag, errno); errorStr[255] = '\0'; - cout << "Error: " << errorStr << endl;*/ + LIBZIPPP_ERROR_DEBUG("Unable to open archive", errorStr) + delete errorStr; + errorStr = nullptr; - zipHandle = nullptr; return false; } @@ -282,7 +286,7 @@ int ZipArchive::close(void) { bufferLength = newLength; } else { - fprintf(stderr, "can't read back from source: %d\n", srcOpen); + LIBZIPPP_ERROR_DEBUG("can't read back from source", "changes were not pushed by in the buffer") return srcOpen; } } diff --git a/src/libzippp.h b/src/libzippp.h index 12fd33c..27e776f 100644 --- a/src/libzippp.h +++ b/src/libzippp.h @@ -50,6 +50,9 @@ struct zip_source; #define LIBZIPPP_DEFAULT_CHUNK_SIZE 524288 #define LIBZIPPP_DEFAULT_PROGRESSION_PRECISION 0.5 +// allow custom debug handling when errors occurs in libzippp +#define LIBZIPPP_ERROR_DEBUG(str, errormsg) fprintf(stderr, str "\n", errormsg); + //libzip documentation //- http://www.nih.at/libzip/libzip.html //- http://slash.developpez.com/tutoriels/c/utilisation-libzip/