]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Added examples/Makefile
authorYann Collet <yann.collet.73@gmail.com>
Sun, 10 Jul 2016 12:23:30 +0000 (14:23 +0200)
committerYann Collet <yann.collet.73@gmail.com>
Sun, 10 Jul 2016 12:25:38 +0000 (14:25 +0200)
Makefile
examples/.gitignore [new file with mode: 0644]
examples/Makefile [new file with mode: 0644]
examples/simple_compression.c
examples/simple_decompression.c

index 12a401207b67f685774d5f4c75490881361f35e9..b11fc5774cc846ab96b81ae2333bf69c4b0f6145 100644 (file)
--- 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 (file)
index 0000000..5c9836d
--- /dev/null
@@ -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 (file)
index 0000000..b20d14a
--- /dev/null
@@ -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
index 1b6ef429e23561dcf76082e759fb511f7585d66e..08c6c9f51eedf85ce483c222a768cece454db95f 100644 (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;
 }
index 1b58e552de33210e69bcaf7e2e240b0e668295af..b907afa19e451d927cc1c3704cf11b7df9d1b5d9 100644 (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;
 }