1
0
mirror of https://github.com/facebook/zstd.git synced 2025-03-06 16:56:49 +02:00

Added examples/Makefile

This commit is contained in:
Yann Collet 2016-07-10 14:23:30 +02:00
parent 25c506601c
commit 677ed26aa7
5 changed files with 102 additions and 6 deletions

View File

@ -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

9
examples/.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
#build
simple_compression
simple_decompression
dictionary_decompression
#test artefact
tmp*
test*
*.zst

54
examples/Makefile Normal file
View File

@ -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

View File

@ -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;
}

View File

@ -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;
}