From 677ed26aa70677380b2d27adc247f0534c1ab8d8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sun, 10 Jul 2016 14:23:30 +0200 Subject: [PATCH] Added examples/Makefile --- Makefile | 2 +- examples/.gitignore | 9 ++++++ examples/Makefile | 54 +++++++++++++++++++++++++++++++++ examples/simple_compression.c | 39 +++++++++++++++++++++--- examples/simple_decompression.c | 4 ++- 5 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 examples/.gitignore create mode 100644 examples/Makefile diff --git a/Makefile b/Makefile index 12a401207..b11fc5774 100644 --- a/Makefile +++ b/Makefile @@ -170,7 +170,7 @@ bmi32test: clean CFLAGS="-O3 -mbmi -m32 -Werror" $(MAKE) -C $(PRGDIR) test staticAnalyze: clean - CPPFLAGS=-g scan-build --status-bugs -v $(MAKE) all + CPPFLAGS=-g scan-build --status-bugs -v $(MAKE) all endif diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 000000000..5c9836d3a --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,9 @@ +#build +simple_compression +simple_decompression +dictionary_decompression + +#test artefact +tmp* +test* +*.zst diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 000000000..b20d14a74 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,54 @@ +# ########################################################################## +# ZSTD educational examples - Makefile +# Copyright (C) Yann Collet 2016 +# +# GPL v2 License +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# You can contact the author at : +# - zstd homepage : http://www.zstd.net/ +# ########################################################################## + +# This Makefile presumes libzstd is installed, using `sudo make install` + +LDFLAGS+= -lzstd + +.PHONY: default all clean test + +default: all + +all: simple_compression simple_decompression dictionary_decompression + +simple_compression : simple_compression.c + $(CC) $(CPPFLAGS) $(CFLAGS) $^ $(LDFLAGS) -o $@ + +simple_decompression : simple_decompression.c + $(CC) $(CPPFLAGS) $(CFLAGS) $^ $(LDFLAGS) -o $@ + +dictionary_decompression : dictionary_decompression.c + $(CC) $(CPPFLAGS) $(CFLAGS) $^ $(LDFLAGS) -o $@ + +clean: + @rm -f core *.o tmp* result* *.zst \ + simple_compression simple_decompression dictionary_decompression + @echo Cleaning completed + +test: all + cp README.md tmp + ./simple_compression tmp + @echo starting simple_decompression + ./simple_decompression tmp.zst + @echo tests completed diff --git a/examples/simple_compression.c b/examples/simple_compression.c index 1b6ef429e..08c6c9f51 100644 --- a/examples/simple_compression.c +++ b/examples/simple_compression.c @@ -74,7 +74,23 @@ static void* loadFile_X(const char* fileName, size_t* size) } -static void compress(const char* fname) +static void saveFile_X(const char* fileName, const void* buff, size_t buffSize) +{ + FILE* const oFile = fopen_X(fileName, "wb"); + size_t const wSize = fwrite(buff, 1, buffSize, oFile); + if (wSize != (size_t)buffSize) { + printf("fwrite: %s : %s \n", fileName, strerror(errno)); + exit(5); + } + size_t const closeError = fclose(oFile); + if (closeError) { + printf("fclose: %s : %s \n", fileName, strerror(errno)); + exit(6); + } +} + + +static void compress(const char* fname, const char* oname) { size_t fSize; void* const fBuff = loadFile_X(fname, &fSize); @@ -87,17 +103,31 @@ static void compress(const char* fname) exit(7); } + saveFile_X(oname, cBuff, cSize); + /* success */ - printf("%25s : %6u -> %7u \n", fname, (unsigned)fSize, (unsigned)cSize); + printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname); free(fBuff); free(cBuff); } +static const char* createOutFilename(const char* filename) +{ + size_t const inL = strlen(filename); + size_t const outL = inL + 5; + void* outSpace = malloc_X(outL); + memset(outSpace, 0, outL); + strcat(outSpace, filename); + strcat(outSpace, ".zst"); + return (const char*)outSpace; +} + int main(int argc, const char** argv) { const char* const exeName = argv[0]; + const char* const inFilename = argv[1]; if (argc!=2) { printf("wrong arguments\n"); @@ -106,7 +136,8 @@ int main(int argc, const char** argv) return 1; } - compress(argv[1]); + const char* const outFilename = createOutFilename(inFilename); + compress(inFilename, outFilename); - printf("%s compressed. \n", argv[1]); + return 0; } diff --git a/examples/simple_decompression.c b/examples/simple_decompression.c index 1b58e552d..b907afa19 100644 --- a/examples/simple_decompression.c +++ b/examples/simple_decompression.c @@ -113,5 +113,7 @@ int main(int argc, const char** argv) decompress(argv[1]); - printf("%s decoded. \n", argv[1]); + printf("%s correctly decoded (in memory). \n", argv[1]); + + return 0; }