getopt cmd line parsing in examples
authorMathis Rosenhauer <rosenhauer@dkrz.de>
Mon, 13 Aug 2012 15:19:38 +0000 (17:19 +0200)
committerMathis Rosenhauer <rosenhauer@dkrz.de>
Mon, 13 Aug 2012 15:19:38 +0000 (17:19 +0200)
data/example_data_16 [new file with mode: 0644]
src/Makefile
src/decode.c [new file with mode: 0644]
src/encode.c [new file with mode: 0644]
src/test_decode.c [deleted file]
src/test_encode.c [deleted file]
src/test_szcomp.c

diff --git a/data/example_data_16 b/data/example_data_16
new file mode 100644 (file)
index 0000000..b9ca914
Binary files /dev/null and b/data/example_data_16 differ
index 9e66e12..7ba4853 100644 (file)
@@ -9,10 +9,10 @@ OBJS = aee.o aed.o sz_compat.o
 
 all: libae.a
 
-test_encode: test_encode.o libae.a
+encode: encode.o libae.a
        $(CC) $(CFLAGS) -o $@ $< -L. -lae
 
-test_decode: test_decode.o libae.a
+decode: decode.o libae.a
        $(CC) $(CFLAGS) -o $@ $< -L. -lae
 
 test_szcomp: test_szcomp.o libae.a
@@ -32,20 +32,19 @@ install: libae.a
        ln -sfT ../src/libae.a ../lib/libsz.a
 
 clean:
-       rm -f $(OBJS) test_encode.o test_decode.o \
-       test_encode test_decode libae.a \
+       rm -f $(OBJS) encode.o decode.o \
+       encode decode libae.a \
        test_szcomp test_szcomp.o \
        ../data/test.ae ../data/test \
        *.gcno *.gcda *.gcov gmon.out
 
-check: test_encode test_decode test_szcomp
-       ./test_encode 1 1 < ../data/example_data > ../data/test.ae
-       ./test_decode 1 1 < ../data/test.ae > ../data/test
-       diff ../data/test ../data/example_data
-       ./test_encode 99 99 < ../data/example_data > ../data/test.ae
-       ./test_decode 101 101 < ../data/test.ae > ../data/test
-       diff ../data/test ../data/example_data
-        ./test_szcomp 65536 < ../data/example_data_16 > ../data/test
-       diff ../data/test ../data/example_data_16
-       ./test_szcomp 2097257 < ../data/zero_test > ../data/test
-       diff ../data/test ../data/zero_test
+check: encode decode test_szcomp
+        ./encode -cb1 ../data/example_data > ../data/test.aee 
+        ./decode -b1 ../data/test.aee
+        diff ../data/test ../data/example_data
+        ./encode -cb1024 ../data/example_data > ../data/test.aee 
+        ./decode -b1024 ../data/test.aee
+        diff ../data/test ../data/example_data
+        ./test_szcomp 65536 ../data/example_data_16
+
+
diff --git a/src/decode.c b/src/decode.c
new file mode 100644 (file)
index 0000000..bde19b4
--- /dev/null
@@ -0,0 +1,145 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include "libae.h"
+
+#define CHUNK 1024
+
+int main(int argc, char *argv[])
+{
+    ae_stream strm;
+    uint8_t *in;
+    uint8_t *out;
+    int chunk, c, total_out, status;
+    int input_avail, output_avail;
+    char *outfn, *infn, *ext;
+    FILE *infp, *outfp;
+    int cflag = 0;
+
+    chunk = CHUNK;
+    opterr = 0;
+
+    while ((c = getopt (argc, argv, "cb:")) != -1)
+        switch (c)
+        {
+        case 'b':
+            chunk = 2 * atoi(optarg);
+            break;
+        case 'c':
+            cflag = 1;
+            break;
+        case '?':
+            if (optopt == 'b')
+                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
+            else if (isprint (optopt))
+                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
+            else
+                fprintf (stderr,
+                         "Unknown option character `\\x%x'.\n",
+                         optopt);
+            return 1;
+        default:
+            abort ();
+        }
+
+    if (optind < argc)
+    {
+        infn = argv[optind];
+    }
+    else
+    {
+        fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
+        exit(-1);
+    }
+
+    in = (uint8_t *)malloc(chunk);
+    out = (uint8_t *)malloc(chunk * sizeof(uint8_t));
+    if (in == NULL || out == NULL)
+        return 1;
+
+    if (cflag)
+    {
+        outfp = stdout;
+    }
+    else
+    {
+        outfn = malloc(strlen(infn));
+        if (outfn == NULL)
+            exit(-1);
+
+        if ((ext = strstr(infn, ".aee")) == NULL)
+        {
+            fprintf(stderr, "Error: input file needs to end with .aee\n");
+            exit(-1);
+        }
+        strncpy(outfn, infn, ext - infn);
+
+        if ((outfp = fopen(outfn, "w")) == NULL)
+            exit(-1);
+    }
+    if ((infp = fopen(infn, "r")) == NULL)
+        exit(-1);
+
+    strm.bit_per_sample = 16;
+    strm.block_size = 8;
+    strm.segment_size = 8;
+    strm.flags = AE_DATA_MSB | AE_DATA_PREPROCESS;
+
+    if (ae_decode_init(&strm) != AE_OK)
+        return 1;
+
+    total_out = 0;
+    strm.avail_in = 0;
+    strm.avail_out = chunk;
+    strm.next_out = (uint8_t *)out;
+
+    input_avail = 1;
+    output_avail = 1;
+
+    while(input_avail || output_avail)
+    {
+        if (strm.avail_in == 0)
+        {
+            strm.avail_in = fread(in, 1, chunk, infp);
+            if (strm.avail_in != chunk)
+                input_avail = 0;
+
+            strm.next_in = in;
+        }
+
+        if ((status = ae_decode(&strm, AE_NO_FLUSH)) != AE_OK)
+        {
+            fprintf(stderr, "error is %i\n", status);
+            return 1;
+        }
+
+        if (strm.total_out - total_out > 0)
+        {
+            fwrite(out, strm.total_out - total_out, 1, outfp);
+            total_out = strm.total_out;
+            output_avail = 1;
+            strm.next_out = (uint8_t *)out;
+            strm.avail_out = chunk;
+        }
+        else
+        {
+            output_avail = 0;
+        }
+
+    }
+
+    ae_decode_end(&strm);
+    fclose(infp);
+    fclose(outfp);
+    free(in);
+    free(out);
+    if (!cflag)
+    {
+        unlink(infn);
+        free(outfn);
+    }
+    return 0;
+}
diff --git a/src/encode.c b/src/encode.c
new file mode 100644 (file)
index 0000000..03cdf26
--- /dev/null
@@ -0,0 +1,153 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include "libae.h"
+
+#define CHUNK 1024
+
+int main(int argc, char *argv[])
+{
+    ae_stream strm;
+    uint8_t *in;
+    uint8_t *out;
+    int chunk, total_out, status, c;
+    int input_avail, output_avail;
+    char *outfn, *infn;
+    FILE *infp, *outfp;
+    int cflag = 0;
+
+    chunk = CHUNK;
+    opterr = 0;
+
+    while ((c = getopt (argc, argv, "cb:")) != -1)
+        switch (c)
+        {
+        case 'b':
+            chunk = 2 * atoi(optarg);
+            break;
+        case 'c':
+            cflag = 1;
+            break;
+        case '?':
+            if (optopt == 'b')
+                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
+            else if (isprint (optopt))
+                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
+            else
+                fprintf (stderr,
+                         "Unknown option character `\\x%x'.\n",
+                         optopt);
+            return 1;
+        default:
+            abort ();
+        }
+
+    if (optind < argc)
+    {
+        infn = argv[optind];
+    }
+    else
+    {
+        fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
+        exit(-1);
+    }
+
+    out = (uint8_t *)malloc(chunk);
+    in = (uint8_t *)malloc(chunk);
+
+
+    if (in == NULL || out == NULL)
+        exit(-1);
+
+    strm.bit_per_sample = 16;
+    strm.block_size = 8;
+    strm.segment_size = 8;
+    strm.flags = AE_DATA_MSB | AE_DATA_PREPROCESS;
+
+    if (ae_encode_init(&strm) != AE_OK)
+        return 1;
+
+    total_out = 0;
+    strm.avail_in = 0;
+    strm.avail_out = chunk;
+    strm.next_out = out;
+
+    input_avail = 1;
+    output_avail = 1;
+
+    if ((infp = fopen(infn, "r")) == NULL)
+        exit(-1);
+
+    if (cflag)
+    {
+        outfp = stdout;
+    }
+    else
+    {
+        outfn = malloc(strlen(infn) + 4);
+        if (outfn == NULL)
+            exit(-1);
+
+        sprintf(outfn, "%s.aee", infn);
+
+        if ((outfp = fopen(outfn, "w")) == NULL)
+            exit(-1);
+    }
+
+    while(input_avail || output_avail)
+    {
+        if (strm.avail_in == 0 && input_avail)
+        {
+            strm.avail_in = fread(in, 1, chunk, infp);
+            if (strm.avail_in != chunk)
+                input_avail = 0;
+            strm.next_in = (uint8_t *)in;
+        }
+
+        if ((status = ae_encode(&strm, AE_NO_FLUSH)) != AE_OK)
+        {
+            fprintf(stderr, "error is %i\n", status);
+            return 1;
+        }
+
+        if (strm.total_out - total_out > 0)
+        {
+            fwrite(out, strm.total_out - total_out, 1, outfp);
+            total_out = strm.total_out;
+            output_avail = 1;
+            strm.next_out = out;
+            strm.avail_out = chunk;
+        }
+        else
+        {
+            output_avail = 0;
+        }
+
+    }
+
+    if ((status = ae_encode(&strm, AE_FLUSH)) != AE_OK)
+    {
+        fprintf(stderr, "error is %i\n", status);
+        return 1;
+    }
+
+    if (strm.total_out - total_out > 0)
+    {
+        fwrite(out, strm.total_out - total_out, 1, outfp);
+    }
+
+    ae_encode_end(&strm);
+    fclose(infp);
+    fclose(outfp);
+    free(in);
+    free(out);
+    if (!cflag)
+    {
+        unlink(infn);
+        free(outfn);
+    }
+    return 0;
+}
diff --git a/src/test_decode.c b/src/test_decode.c
deleted file mode 100644 (file)
index 65a67f6..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <inttypes.h>
-#include "libae.h"
-
-#define CHUNK_OUT 1
-#define CHUNK_IN 1
-
-int main(int argc, char *argv[])
-{
-    ae_stream strm;
-    uint8_t *in;
-    uint8_t *out;
-    int chunk_in, chunk_out, i, c, total_out, status;
-    int input_avail, output_avail;
-
-    if (argc == 3)
-    {
-        chunk_in = atoi(argv[1]);
-        chunk_out = atoi(argv[2]);
-    }
-    else
-    {
-        chunk_in = CHUNK_IN;
-        chunk_out = CHUNK_OUT;
-    }
-
-    in = (uint8_t *)malloc(chunk_in);
-    out = (uint8_t *)malloc(chunk_out * sizeof(uint8_t));
-    if (in == NULL || out == NULL)
-        return 1;
-
-    strm.bit_per_sample = 8;
-    strm.block_size = 8;
-    strm.segment_size = 2;
-    strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS;
-
-    if (ae_decode_init(&strm) != AE_OK)
-        return 1;
-
-    total_out = 0;
-    strm.avail_in = 0;
-    strm.avail_out = chunk_out;
-    strm.next_out = (uint8_t *)out;
-
-    input_avail = 1;
-    output_avail = 1;
-
-    while(input_avail || output_avail)
-    {
-        if (strm.avail_in == 0)
-        {
-            i = 0;
-            while(i < chunk_in && (c = getc(stdin)) != EOF)
-                in[i++] = c;
-            strm.avail_in = i;
-
-            strm.next_in = in;
-            if (c == EOF)
-                input_avail = 0;
-        }
-
-        if ((status = ae_decode(&strm, AE_NO_FLUSH)) != AE_OK)
-        {
-            fprintf(stderr, "error is %i\n", status);
-            return 1;
-        }
-
-        if (strm.total_out - total_out > 0)
-        {
-            for (i=0; i < strm.total_out - total_out; i++)
-            {
-                putc(out[i], stdout);
-                /* putc(out[i] >> 8, stdout); */
-                /* putc(out[i] >> 16, stdout); */
-                /* putc(out[i] >> 24, stdout); */
-            }
-            total_out = strm.total_out;
-            output_avail = 1;
-            strm.next_out = (uint8_t *)out;
-            strm.avail_out = chunk_out;
-        }
-        else
-        {
-            output_avail = 0;
-        }
-
-    }
-
-    ae_decode_end(&strm);
-    free(in);
-    free(out);
-    return 0;
-}
diff --git a/src/test_encode.c b/src/test_encode.c
deleted file mode 100644 (file)
index 41c01b9..0000000
+++ /dev/null
@@ -1,112 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <inttypes.h>
-#include "libae.h"
-
-#define CHUNK_OUT 0x4000
-#define CHUNK_IN 1024
-#define ALL_IN 9478
-
-int main(int argc, char *argv[])
-{
-    ae_stream strm;
-    uint8_t *in;
-    uint8_t *out;
-    int chunk_in, chunk_out, i, c, total_out, status;
-    int input_avail, output_avail;
-
-    if (argc == 3)
-    {
-        chunk_in = atoi(argv[1]);
-        chunk_out = atoi(argv[2]);
-    }
-    else
-    {
-        chunk_in = CHUNK_IN;
-        chunk_out = CHUNK_OUT;
-    }
-
-    out = (uint8_t *)malloc(chunk_out);
-    in = (uint8_t *)malloc(chunk_in * sizeof(uint8_t));
-    if (in == NULL || out == NULL)
-        return 1;
-
-    strm.bit_per_sample = 8;
-    strm.block_size = 8;
-    strm.segment_size = 2;
-    strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS;
-
-    if (ae_encode_init(&strm) != AE_OK)
-        return 1;
-
-    total_out = 0;
-    strm.avail_in = 0;
-    strm.avail_out = chunk_out;
-    strm.next_out = out;
-
-    input_avail = 1;
-    output_avail = 1;
-
-    while(input_avail || output_avail)
-    {
-        if (strm.avail_in == 0)
-        {
-            i = 0;
-            while(i < chunk_in && (c = getc(stdin)) != EOF)
-            {
-                in[i] = c;
-                /* in[i] |= getc(stdin) << 8; */
-                /* in[i] |= getc(stdin) << 16; */
-                /* in[i] |= getc(stdin) << 24; */
-                i++;
-            }
-            strm.avail_in = i;
-
-            strm.next_in = (uint8_t *)in;
-            if (c == EOF)
-                input_avail = 0;
-        }
-
-        if ((status = ae_encode(&strm, AE_NO_FLUSH)) != AE_OK)
-        {
-            fprintf(stderr, "error is %i\n", status);
-            return 1;
-        }
-
-        if (strm.total_out - total_out > 0)
-        {
-            for (i=0; i < strm.total_out - total_out; i++)
-            {
-                putc(out[i], stdout);
-            }
-            total_out = strm.total_out;
-            output_avail = 1;
-            strm.next_out = out;
-            strm.avail_out = chunk_out;
-        }
-        else
-        {
-            output_avail = 0;
-        }
-
-    }
-
-    if ((status = ae_encode(&strm, AE_FLUSH)) != AE_OK)
-    {
-        fprintf(stderr, "error is %i\n", status);
-        return 1;
-    }
-
-    if (strm.total_out - total_out > 0)
-    {
-        for (i=0; i < strm.total_out - total_out; i++)
-        {
-            putc(out[i], stdout);
-        }
-    }
-    ae_encode_end(&strm);
-    free(in);
-    free(out);
-    return 0;
-}
index 0f57246..7d16cfb 100644 (file)
@@ -2,6 +2,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <inttypes.h>
+#include <string.h>
 #include "szlib.h"
 
 #define OPTIONS_MASK        (SZ_RAW_OPTION_MASK | SZ_MSB_OPTION_MASK | SZ_NN_OPTION_MASK)
 
 int main(int argc, char *argv[])
 {
-    int status, c;
+    int status;
     SZ_com_t sz_param;
-    unsigned char *dest;
-    uint16_t *source;
-    size_t destLen, sourceLen, n;
+    unsigned char *source, *dest, *dest1;
+    size_t destLen, dest1Len, sourceLen;
+    FILE *fp;
 
-    if (argc < 2)
+    if (argc < 3)
     {
-        fprintf(stderr, "Input size missing!\n");
+        fprintf(stderr, "Usage: %s buffer_size file\n", argv[0]);
         return 1;
     }
     sz_param.options_mask = OPTIONS_MASK;
@@ -28,41 +29,35 @@ int main(int argc, char *argv[])
 
     sourceLen = destLen = atoi(argv[1]);
 
-    source = (uint16_t *)malloc(sourceLen * sizeof(uint16_t));
+    source = (unsigned char *)malloc(sourceLen);
     dest = (unsigned char *)malloc(destLen);
+    dest1 = (unsigned char *)malloc(destLen);
 
-    if (source == NULL || dest == NULL)
+    if (source == NULL || dest == NULL || dest1 == NULL)
         return 1;
 
-    n = 0;
-    while((c = getc(stdin)) != EOF)
+    if ((fp = fopen(argv[2], "r")) == NULL)
     {
-        source[n] = c;
-        source[n] |= getc(stdin) << 8;
-        n++;
+        fprintf(stderr, "Can't open %s\n", argv[2]);
+        exit(-1);
     }
-    sourceLen = n * sizeof(uint16_t);
 
-    fprintf(stderr, "Uncompressed size is %li\n", sourceLen);
+    sourceLen = fread(source, 1, sourceLen, fp);
 
     status = SZ_BufftoBuffCompress(dest, &destLen, source, sourceLen, &sz_param);
     if (status != SZ_OK)
         return status;
 
-    fprintf(stderr, "Compressed size is %li\n", destLen);
-
-    status = SZ_BufftoBuffDecompress(source, &sourceLen, dest, destLen, &sz_param);
+    dest1Len = sourceLen;
+    status = SZ_BufftoBuffDecompress(dest1, &dest1Len, dest, destLen, &sz_param);
     if (status != SZ_OK)
         return status;
 
-    fprintf(stderr, "Uncompressed size is %li again\n", sourceLen);
+    if (memcmp(source, dest1, sourceLen) != 0)
+        fprintf(stderr, "File %s Buffers differ\n", argv[2]);
 
-    for(c = 0; c < sourceLen / sizeof(uint16_t); c++)
-    {
-        putc(source[c], stdout);
-        putc(source[c] >> 8, stdout);
-    }
     free(source);
     free(dest);
+    free(dest1);
     return 0;
 }