Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows custom error handling #133

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions src/libzippp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/libzippp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down