From a2664649df3a7c450ee8f6299df29713e1081b21 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 9 Sep 2016 19:33:56 +0200 Subject: [PATCH] better error handling --- Makefile | 4 ++-- examples/Makefile | 5 +++-- examples/dictionary_compression.c | 10 ++++++---- examples/dictionary_decompression.c | 4 ++-- examples/simple_compression.c | 4 ++-- examples/simple_decompression.c | 4 ++-- examples/streaming_compression.c | 5 +++-- examples/streaming_decompression.c | 2 +- 8 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index c9f1fe41..7860ce1d 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ clean: @$(MAKE) -C $(PRGDIR) $@ > $(VOID) @$(MAKE) -C $(TESTDIR) $@ > $(VOID) @$(MAKE) -C $(ZWRAPDIR) $@ > $(VOID) - @rm -f zstd + @$(RM) zstd @echo Cleaning completed @@ -121,7 +121,7 @@ endif ifneq (,$(filter $(HOST_OS),MSYS POSIX)) cmaketest: cmake --version - rm -rf projects/cmake/build + $(RM) -r projects/cmake/build mkdir projects/cmake/build cd projects/cmake/build ; cmake -DPREFIX:STRING=~/install_test_dir $(CMAKE_PARAMS) .. ; $(MAKE) install ; $(MAKE) uninstall diff --git a/examples/Makefile b/examples/Makefile index f568bc00..54602dfe 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -61,6 +61,7 @@ clean: test: all cp README.md tmp + cp Makefile tmp2 @echo starting simple compression ./simple_compression tmp ./simple_decompression tmp.zst @@ -69,6 +70,6 @@ test: all ./streaming_compression tmp ./streaming_decompression tmp.zst > /dev/null @echo starting dictionary compression - ./dictionary_compression tmp README.md - ./dictionary_decompression tmp.zst README.md + ./dictionary_compression tmp2 tmp README.md + ./dictionary_decompression tmp2.zst tmp.zst README.md @echo tests completed diff --git a/examples/dictionary_compression.c b/examples/dictionary_compression.c index 08d639c0..adcc3b4d 100644 --- a/examples/dictionary_compression.c +++ b/examples/dictionary_compression.c @@ -73,12 +73,12 @@ static void saveFile_orDie(const char* fileName, const void* buff, size_t buffSi /* createDict() : `dictFileName` is supposed to have been created using `zstd --train` */ -static ZSTD_CDict* createCDict_orDie(const char* dictFileName) +static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel) { size_t dictSize; printf("loading dictionary %s \n", dictFileName); void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize); - ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 3); + ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel); if (!cdict) { fprintf(stderr, "ZSTD_createCDict error \n"); exit(7); @@ -96,6 +96,7 @@ static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdi void* const cBuff = malloc_orDie(cBuffSize); ZSTD_CCtx* const cctx = ZSTD_createCCtx(); + if (cctx==NULL) { fprintf(stderr, "ZSTD_createCCtx() error \n"); exit(10); } size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict); if (ZSTD_isError(cSize)) { fprintf(stderr, "error compressing %s : %s \n", fname, ZSTD_getErrorName(cSize)); @@ -107,7 +108,7 @@ static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdi /* success */ printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname); - ZSTD_freeCCtx(cctx); + ZSTD_freeCCtx(cctx); /* never fails */ free(fBuff); free(cBuff); } @@ -127,6 +128,7 @@ static char* createOutFilename_orDie(const char* filename) int main(int argc, const char** argv) { const char* const exeName = argv[0]; + int const cLevel = 3; if (argc<3) { fprintf(stderr, "wrong arguments\n"); @@ -137,7 +139,7 @@ int main(int argc, const char** argv) /* load dictionary only once */ const char* const dictName = argv[argc-1]; - ZSTD_CDict* const dictPtr = createCDict_orDie(dictName); + ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel); int u; for (u=1; u