minor updates to examples
authorYann Collet <cyan@fb.com>
Sat, 25 Nov 2017 01:18:46 +0000 (17:18 -0800)
committerYann Collet <cyan@fb.com>
Sat, 25 Nov 2017 01:18:46 +0000 (17:18 -0800)
see https://github.com/lz4/lz4/commit/810e2ca27b3561e0f6bfa7a88e0fde6faf807064#commitcomment-25810887

examples/HCStreaming_ringBuffer.c
examples/blockStreaming_doubleBuffer.c
examples/blockStreaming_lineByLine.c
examples/blockStreaming_ringBuffer.c
examples/dictionaryRandomAccess.c
examples/frameCompress.c
examples/printVersion.c
programs/platform.h

index d49b267..a878577 100644 (file)
@@ -1,12 +1,12 @@
 // LZ4 HC streaming API example : ring buffer
-// Based on previous work from Takayuki Matsuoka
+// Based on a previous example by Takayuki Matsuoka
 
 
 /**************************************
  * Compiler Options
  **************************************/
-#ifdef _MSC_VER    /* Visual Studio */
-#  define _CRT_SECURE_NO_WARNINGS   /* for MSVC */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800)  /* Visual Studio <= 2013 */
+#  define _CRT_SECURE_NO_WARNINGS
 #  define snprintf sprintf_s
 #endif
 
index d02f258..acb3455 100644 (file)
@@ -2,7 +2,7 @@
 // Copyright : Takayuki Matsuoka
 
 
-#ifdef _MSC_VER    /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800)  /* Visual Studio <= 2013 */
 #  define _CRT_SECURE_NO_WARNINGS
 #  define snprintf sprintf_s
 #endif
index f449aa3..677c426 100644 (file)
@@ -1,8 +1,8 @@
 // LZ4 streaming API example : line-by-line logfile compression
-// Copyright : Takayuki Matsuoka
+// by Takayuki Matsuoka
 
 
-#ifdef _MSC_VER    /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800)  /* Visual Studio <= 2013 */
 #  define _CRT_SECURE_NO_WARNINGS
 #  define snprintf sprintf_s
 #endif
index dec78af..0b6a3ce 100644 (file)
@@ -5,7 +5,7 @@
 /**************************************
  * Compiler Options
  **************************************/
-#ifdef _MSC_VER    /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800)  /* Visual Studio <= 2013 */
 #  define _CRT_SECURE_NO_WARNINGS
 #  define snprintf sprintf_s
 #endif
index 6acf99b..291fd08 100644 (file)
@@ -1,6 +1,6 @@
 // LZ4 API example : Dictionary Random Access
 
-#ifdef _MSC_VER    /* Visual Studio */
+#if defined(_MSC_VER) && (_MSC_VER <= 1800)  /* Visual Studio <= 2013 */
 #  define _CRT_SECURE_NO_WARNINGS
 #  define snprintf sprintf_s
 #endif
index 80db90e..d66a8dc 100644 (file)
@@ -21,17 +21,15 @@ static const LZ4F_preferences_t lz4_preferences = {
 };
 
 static size_t compress_file(FILE *in, FILE *out, size_t *size_in, size_t *size_out) {
-    LZ4F_errorCode_t r;
+    size_t r=1;  /* function result; 1 == error, default (early exit) */
     LZ4F_compressionContext_t ctx;
     char *src, *buf = NULL;
-    size_t size, n, k, count_in = 0, count_out, offset = 0, frame_size;
+    size_t size, count_in = 0, count_out, offset = 0, frame_size;
 
-    r = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
-    if (LZ4F_isError(r)) {
+    if (LZ4F_isError( LZ4F_createCompressionContext(&ctx, LZ4F_VERSION) )) {
         printf("Failed to create context: error %zu\n", r);
         return 1;
     }
-    r = 1;  /* function result; 1 == error, by default (early exit) */
 
     src = malloc(BUF_SIZE);
     if (!src) {
@@ -40,41 +38,45 @@ static size_t compress_file(FILE *in, FILE *out, size_t *size_in, size_t *size_o
     }
 
     frame_size = LZ4F_compressBound(BUF_SIZE, &lz4_preferences);
-    size =  frame_size + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE;
+    size = frame_size + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE;
     buf = malloc(size);
     if (!buf) {
         printf("Not enough memory\n");
         goto cleanup;
     }
 
-    n = offset = count_out = LZ4F_compressBegin(ctx, buf, size, &lz4_preferences);
-    if (LZ4F_isError(n)) {
-        printf("Failed to start compression: error %zu\n", n);
-        goto cleanup;
+    {   size_t const headerSize = LZ4F_compressBegin(ctx, buf, size, &lz4_preferences);
+        if (LZ4F_isError(headerSize)) {
+            printf("Failed to start compression: error %zu\n", headerSize);
+            goto cleanup;
+        }
+        offset = count_out = headerSize;
+        printf("Buffer size is %zu bytes, header size %zu bytes\n", size, headerSize);
     }
 
-    printf("Buffer size is %zu bytes, header size %zu bytes\n", size, n);
 
     for (;;) {
-        k = fread(src, 1, BUF_SIZE, in);
-        if (k == 0)
+        size_t const readSize = fread(src, 1, BUF_SIZE, in);
+        if (readSize == 0)
             break;
-        count_in += k;
+        count_in += readSize;
 
-        n = LZ4F_compressUpdate(ctx, buf + offset, size - offset, src, k, NULL);
-        if (LZ4F_isError(n)) {
-            printf("Compression failed: error %zu\n", n);
-            goto cleanup;
+        {   size_t const compressedSize = LZ4F_compressUpdate(ctx, buf + offset, size - offset, src, readSize, NULL);
+            if (LZ4F_isError(compressedSize)) {
+                printf("Compression failed: error %zu\n", compressedSize);
+                goto cleanup;
+            }
+            offset += compressedSize;
+            count_out += compressedSize;
         }
 
-        offset += n;
-        count_out += n;
         if (size - offset < frame_size + LZ4_FOOTER_SIZE) {
+            size_t writtenSize;
             printf("Writing %zu bytes\n", offset);
 
-            k = fwrite(buf, 1, offset, out);
-            if (k < offset) {
-                if (ferror(out))
+            writtenSize = fwrite(buf, 1, offset, out);
+            if (writtenSize < offset) {
+                if (ferror(out))  /* note : ferror() must follow fwrite */
                     printf("Write failed\n");
                 else
                     printf("Short write\n");
@@ -85,31 +87,31 @@ static size_t compress_file(FILE *in, FILE *out, size_t *size_in, size_t *size_o
         }
     }
 
-    n = LZ4F_compressEnd(ctx, buf + offset, size - offset, NULL);
-    if (LZ4F_isError(n)) {
-        printf("Failed to end compression: error %zu\n", n);
-        goto cleanup;
+    {   size_t const compressedSize = LZ4F_compressEnd(ctx, buf + offset, size - offset, NULL);
+        if (LZ4F_isError(compressedSize)) {
+            printf("Failed to end compression: error %zu\n", compressedSize);
+            goto cleanup;
+        }
+        offset += compressedSize;
+        count_out += compressedSize;
     }
 
-    offset += n;
-    count_out += n;
     printf("Writing %zu bytes\n", offset);
-
-    k = fwrite(buf, 1, offset, out);
-    if (k < offset) {
-        if (ferror(out))
-            printf("Write failed\n");
-        else
-            printf("Short write\n");
-        goto cleanup;
-    }
+    {   size_t const writtenSize = fwrite(buf, 1, offset, out);
+        if (writtenSize < offset) {
+            if (ferror(out))
+                printf("Write failed\n");
+            else
+                printf("Short write\n");
+            goto cleanup;
+    }   }
 
     *size_in = count_in;
     *size_out = count_out;
-    r = 0;
+    r = 0;   /* success */
+
  cleanup:
-    if (ctx)
-        LZ4F_freeCompressionContext(ctx);
+    LZ4F_freeCompressionContext(ctx);   /* supports free on NULL */
     free(src);
     free(buf);
     return r;
@@ -128,28 +130,27 @@ static size_t get_block_size(const LZ4F_frameInfo_t* info) {
     }
 }
 
-static size_t decompress_file(FILE *in, FILE *out) {
+static size_t decompress_file(FILE* in, FILE* out) {
     void* const src = malloc(BUF_SIZE);
     void* dst = NULL;
     size_t dstCapacity = 0;
-    LZ4F_dctx *dctx = NULL;
-    size_t ret;
+    LZ4F_dctxdctx = NULL;
+    size_t ret = 1;
 
     /* Initialization */
     if (!src) { perror("decompress_file(src)"); goto cleanup; }
-    ret = LZ4F_createDecompressionContext(&dctx, 100);
-    if (LZ4F_isError(ret)) {
-        printf("LZ4F_dctx creation error: %s\n", LZ4F_getErrorName(ret));
-        goto cleanup;
-    }
+    {   size_t const dctxStatus = LZ4F_createDecompressionContext(&dctx, 100);
+        if (LZ4F_isError(dctxStatus)) {
+            printf("LZ4F_dctx creation error: %s\n", LZ4F_getErrorName(dctxStatus));
+            goto cleanup;
+    }   }
 
     /* Decompression */
-    ret = 1;
     while (ret != 0) {
         /* Load more input */
         size_t srcSize = fread(src, 1, BUF_SIZE, in);
-        void* srcPtr = src;
-        void* srcEnd = srcPtr + srcSize;
+        const void* srcPtr = src;
+        const void* const srcEnd = srcPtr + srcSize;
         if (srcSize == 0 || ferror(in)) {
             printf("Decompress: not enough input or error reading file\n");
             goto cleanup;
index 8607139..7af318a 100644 (file)
@@ -1,5 +1,5 @@
 // LZ4 trivial example : print Library version number
-// Copyright : Takayuki Matsuoka & Yann Collet
+// by Takayuki Matsuoka
 
 
 #include <stdio.h>
index 66491b6..db2efac 100644 (file)
@@ -30,10 +30,10 @@ extern "C" {
 *  Compiler Options
 ****************************************/
 #if defined(_MSC_VER)
-#  define _CRT_SECURE_NO_WARNINGS   /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
-#  define _CRT_SECURE_NO_DEPRECATE  /* VS2005 - must be declared before <io.h> and <windows.h> */ 
-#  if (_MSC_VER <= 1800)            /* (1800 = Visual Studio 2013) */
-#    define snprintf sprintf_s      /* snprintf unsupported by Visual <= 2013 */
+#  define _CRT_SECURE_NO_WARNINGS    /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
+#  if (_MSC_VER <= 1800)             /* (1800 = Visual Studio 2013) */
+#    define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */
+#    define snprintf sprintf_s       /* snprintf unsupported by Visual <= 2013 */
 #  endif
 #endif
 
@@ -60,7 +60,7 @@ extern "C" {
 *  Turn on Large Files support (>4GB) for 32-bit Linux/Unix
 ***********************************************************/
 #if !defined(__64BIT__) || defined(__MINGW32__)       /* No point defining Large file for 64 bit but MinGW-w64 requires it */
-#  if !defined(_FILE_OFFSET_BITS)   
+#  if !defined(_FILE_OFFSET_BITS)
 #    define _FILE_OFFSET_BITS 64                      /* turn off_t into a 64-bit type for ftello, fseeko */
 #  endif
 #  if !defined(_LARGEFILE_SOURCE)                     /* obsolete macro, replaced with _FILE_OFFSET_BITS */