]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
added some basic parsing for args
authorPaul Cruz <paulcruz74@fb.com>
Wed, 5 Jul 2017 19:20:16 +0000 (12:20 -0700)
committerPaul Cruz <paulcruz74@fb.com>
Wed, 5 Jul 2017 19:20:16 +0000 (12:20 -0700)
contrib/adaptive-compression/multi.c
contrib/adaptive-compression/run.sh

index 79904551d33defdb01c8ebab6ac06051c09473e8..4eb3d3b16d9eef88f63c7587eaeedb3ab013a8e5 100644 (file)
@@ -1,6 +1,8 @@
 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
 #define FILE_CHUNK_SIZE 4 << 20
 #define MAX_NUM_JOBS 30;
+#define stdinmark  "/*stdin*\\"
+#define stdoutmark "/*stdout*\\"
 typedef unsigned char BYTE;
 
 #include <stdio.h>      /* fprintf */
@@ -125,7 +127,8 @@ static adaptCCtx* createCCtx(unsigned numJobs, const char* const outFilename)
         return NULL;
     }
     {
-        FILE* dstFile = fopen(outFilename, "wb");
+        unsigned const stdoutUsed = !strcmp(outFilename, stdoutmark);
+        FILE* dstFile = stdoutUsed ? stdout : fopen(outFilename, "wb");
         if (dstFile == NULL) {
             DISPLAY("Error: could not open output file\n");
             freeCCtx(ctx);
@@ -267,7 +270,8 @@ static int createCompressionJob(adaptCCtx* ctx, BYTE* data, size_t srcSize)
 static int compressFilename(const char* const srcFilename, const char* const dstFilename)
 {
     BYTE* const src = malloc(FILE_CHUNK_SIZE);
-    FILE* const srcFile = fopen(srcFilename, "rb");
+    unsigned const stdinUsed = !strcmp(srcFilename, stdinmark);
+    FILE* const srcFile = stdinUsed ? stdin : fopen(srcFilename, "rb");
     size_t const numJobs = MAX_NUM_JOBS;
     int ret = 0;
     adaptCCtx* ctx = NULL;
@@ -341,9 +345,28 @@ cleanup:
 /* return 0 if successful, else return error */
 int main(int argCount, const char* argv[])
 {
-    if (argCount < 3) {
-        DISPLAY("Error: not enough arguments\n");
-        return 1;
+    const char* inFilename = stdinmark;
+    const char* outFilename = stdoutmark;
+    unsigned nextArgumentIsOutFilename = 0;
+    int argNum;
+    for (argNum=1; argNum<argCount; argNum++) {
+        const char* argument = argv[argNum];
+        if (argument[0]=='-') {
+            /* parse argument */
+            switch (argument[1]) {
+                case 'o':
+                    argument+=2;
+                    nextArgumentIsOutFilename = 1;
+                    break;
+            }
+        }
+
+        if (nextArgumentIsOutFilename) {
+            outFilename = argument;
+        }
+        else {
+            inFilename = argument;
+        }
     }
-    return compressFilename(argv[1], argv[2]);
+    return compressFilename(inFilename, outFilename);
 }
index 9d2e987061063e572fda918b657d57b517391e09..ec2df038742addf9f069135b3533682080a3a7ac 100755 (executable)
@@ -1,36 +1,36 @@
 make clean multi
 
-./multi tests/test2048.pdf tmp.zst
+./multi tests/test2048.pdf -otmp.zst
 zstd -d tmp.zst
 diff tmp tests/test2048.pdf
 echo "diff test complete"
 rm tmp*
 
-./multi tests/test512.pdf tmp.zst
+./multi tests/test512.pdf -otmp.zst
 zstd -d tmp.zst
 diff tmp tests/test512.pdf
 echo "diff test complete"
 rm tmp*
 
-./multi tests/test64.pdf tmp.zst
+./multi tests/test64.pdf -otmp.zst
 zstd -d tmp.zst
 diff tmp tests/test64.pdf
 echo "diff test complete"
 rm tmp*
 
-./multi tests/test16.pdf tmp.zst
+./multi tests/test16.pdf -otmp.zst
 zstd -d tmp.zst
 diff tmp tests/test16.pdf
 echo "diff test complete"
 rm tmp*
 
-./multi tests/test4.pdf tmp.zst
+./multi tests/test4.pdf -otmp.zst
 zstd -d tmp.zst
 diff tmp tests/test4.pdf
 echo "diff test complete"
 rm tmp*
 
-./multi tests/test.pdf tmp.zst
+./multi tests/test.pdf -otmp.zst
 zstd -d tmp.zst
 diff tmp tests/test.pdf
 echo "diff test complete"