Skip to content

Commit

Permalink
Merge pull request #133 from ctabin/debug-handling-marco
Browse files Browse the repository at this point in the history
Allows custom error handling
  • Loading branch information
ctabin authored May 22, 2022
2 parents efea55b + 3dc74f2 commit d60c6e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
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

0 comments on commit d60c6e6

Please sign in to comment.