LZ4Demo now supports Pipe mode (inspired by Huan Truong mod)
authoryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Tue, 30 Aug 2011 22:35:09 +0000 (22:35 +0000)
committeryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Tue, 30 Aug 2011 22:35:09 +0000 (22:35 +0000)
git-svn-id: https://lz4.googlecode.com/svn/trunk@13 650e7d94-2a16-8b24-b05c-7c0b3f6821cd

lz4.c
main.c

diff --git a/lz4.c b/lz4.c
index 14caea5..de928f3 100644 (file)
--- a/lz4.c
+++ b/lz4.c
@@ -41,7 +41,7 @@
 // Lowering this value reduce memory usage\r
 // It may also improve speed, especially if you reach L1 cache size (32KB for Intel, 64KB for AMD)\r
 // Expanding memory usage typically improves compression ratio\r
-// Memory usage formula : N->2^(N+2) Bytes (examples : 17 -> 512KB ; 12 -> 16KB)\r
+// Memory usage formula for 32 bits systems : N->2^(N+2) Bytes (examples : 17 -> 512KB ; 12 -> 16KB)\r
 #define HASH_LOG 12\r
 \r
 \r
diff --git a/main.c b/main.c
index 1d8e6ec..b8c80e0 100644 (file)
--- a/main.c
+++ b/main.c
@@ -26,8 +26,9 @@
 //****************************\r
 // Includes\r
 //****************************\r
-#include <stdio.h>     // printf, fopen, fread\r
+#include <stdio.h>     // fprintf, fopen, fread\r
 #include <stdlib.h>    // malloc\r
+#include <string.h>    // strcmp\r
 #include "lz4.h"\r
 \r
 \r
 //****************************\r
 int usage()\r
 {\r
-       printf("Usage :\n");\r
-       printf("      %s [arg] input output\n",BINARY_NAME);\r
-       printf("Arguments :\n");\r
-       printf(" -c : force compression (default)\n");\r
-       printf(" -d : force decompression \n");\r
-       printf(" -h : help (this text)\n");     \r
+       fprintf(stderr, "Usage :\n");\r
+       fprintf(stderr, "      %s [arg] input output\n",BINARY_NAME);\r
+       fprintf(stderr, "Arguments :\n");\r
+       fprintf(stderr, " -c : force compression (default)\n");\r
+       fprintf(stderr, " -d : force decompression \n");\r
+       fprintf(stderr, " -h : help (this text)\n");    \r
+       fprintf(stderr, "input  : can be 'stdin' (pipe)  or a filename\n");\r
+       fprintf(stderr, "output : can be 'stdout' (pipe) or a filename\n");\r
        return 0;\r
 }\r
 \r
 \r
 int badusage()\r
 {\r
-       printf("Wrong parameters\n");\r
+       fprintf(stderr, "Wrong parameters\n");\r
        usage();\r
        return 0;\r
 }\r
@@ -98,11 +101,27 @@ int compress_file(char* input_filename, char* output_filename)
        U64 compressedfilesize = ARCHIVE_MAGICNUMBER_SIZE;\r
        char* in_buff;\r
        char* out_buff;\r
-       FILE* finput = fopen( input_filename, "rb" ); \r
-       FILE* foutput = fopen( output_filename, "wb" ); \r
+       FILE* finput;\r
+       FILE* foutput;\r
+       char stdinmark[] = "stdin";\r
+       char stdoutmark[] = "stdout";\r
+\r
+       if (!strcmp (input_filename, stdinmark)) {\r
+               fprintf(stderr, "Using stdin for input\n");\r
+               finput = stdin;\r
+       } else {\r
+               finput = fopen( input_filename, "rb" );\r
+       }\r
+\r
+       if (!strcmp (output_filename, stdoutmark)) {\r
+               fprintf(stderr, "Using stdout for output\n");\r
+               foutput = stdout;\r
+       } else {\r
+               foutput = fopen( output_filename, "wb" );\r
+       }\r
        \r
-       if ( finput==0 ) { printf("Pb opening %s\n", input_filename);  return 2; }\r
-       if ( foutput==0) { printf("Pb opening %s\n", output_filename); return 3; }\r
+       if ( finput==0 ) { fprintf(stderr, "Pb opening %s\n", input_filename);  return 2; }\r
+       if ( foutput==0) { fprintf(stderr, "Pb opening %s\n", output_filename); return 3; }\r
 \r
        // Allocate Memory\r
        in_buff = malloc(CHUNKSIZE);\r
@@ -131,7 +150,7 @@ int compress_file(char* input_filename, char* output_filename)
        }\r
 \r
        // Status\r
-       printf("Compressed %llu bytes into %llu bytes ==> %.2f%%\n", \r
+       fprintf(stderr, "Compressed %llu bytes into %llu bytes ==> %.2f%%\n", \r
                (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100);\r
 \r
        fclose(finput);\r
@@ -146,14 +165,27 @@ int decode_file(char* input_filename, char* output_filename)
        U64 filesize = 0;\r
        char* in_buff;\r
        char* out_buff;\r
-       FILE* finput = fopen( input_filename, "rb" ); \r
-       FILE* foutput = fopen( output_filename, "wb" ); \r
        size_t uselessRet;\r
        int sinkint;\r
        U32 nextSize;\r
-       \r
-       if (finput==0 ) { printf("Pb opening %s\n", input_filename);  return 4; }\r
-       if (foutput==0) { printf("Pb opening %s\n", output_filename); return 5; }\r
+       FILE* finput;\r
+       FILE* foutput;\r
+       char stdinmark[] = "stdin";\r
+       char stdoutmark[] = "stdout";\r
+\r
+       if (!strcmp (input_filename, stdinmark)) {\r
+               fprintf(stderr, "Using stdin for input\n");\r
+               finput = stdin;\r
+       } else {\r
+               finput = fopen( input_filename, "rb" );\r
+       }\r
+\r
+       if (!strcmp (output_filename, stdoutmark)) {\r
+               fprintf(stderr, "Using stdout for output\n");\r
+               foutput = stdout;\r
+       } else {\r
+               foutput = fopen( output_filename, "wb" );\r
+       }\r
 \r
        // Allocate Memory\r
        in_buff = malloc(OUT_CHUNKSIZE);\r
@@ -161,7 +193,7 @@ int decode_file(char* input_filename, char* output_filename)
        \r
        // Check Archive Header\r
        uselessRet = fread(out_buff, 1, ARCHIVE_MAGICNUMBER_SIZE, finput);\r
-       if (*(U32*)out_buff != ARCHIVE_MAGICNUMBER) { printf("Wrong file : cannot be decoded\n"); return 6; }\r
+       if (*(U32*)out_buff != ARCHIVE_MAGICNUMBER) { fprintf(stderr,"Wrong file : cannot be decoded\n"); return 6; }\r
        uselessRet = fread(in_buff, 1, 4, finput);\r
        nextSize = *(U32*)in_buff;\r
 \r
@@ -190,7 +222,7 @@ int decode_file(char* input_filename, char* output_filename)
        fwrite(out_buff, 1, sinkint, foutput);\r
 \r
        // Status\r
-       printf("Successfully decoded %llu bytes \n", (unsigned long long)filesize);\r
+       fprintf(stderr, "Successfully decoded %llu bytes \n", (unsigned long long)filesize);\r
 \r
        fclose(finput);\r
        fclose(foutput);\r
@@ -208,7 +240,7 @@ int main(int argc, char** argv)
           *output_filename=0;\r
 \r
   // Welcome message\r
-  printf(WELCOME_MESSAGE);\r
+  fprintf(stderr, WELCOME_MESSAGE);\r
 \r
   if (argc<2) { badusage(); return 1; }\r
 \r