Commit 1cff9f2c authored by Mathis Rosenhauer's avatar Mathis Rosenhauer

getopt cmd line parsing in examples

parent ddf451e7
...@@ -9,10 +9,10 @@ OBJS = aee.o aed.o sz_compat.o ...@@ -9,10 +9,10 @@ OBJS = aee.o aed.o sz_compat.o
all: libae.a all: libae.a
test_encode: test_encode.o libae.a encode: encode.o libae.a
$(CC) $(CFLAGS) -o $@ $< -L. -lae $(CC) $(CFLAGS) -o $@ $< -L. -lae
test_decode: test_decode.o libae.a decode: decode.o libae.a
$(CC) $(CFLAGS) -o $@ $< -L. -lae $(CC) $(CFLAGS) -o $@ $< -L. -lae
test_szcomp: test_szcomp.o libae.a test_szcomp: test_szcomp.o libae.a
...@@ -32,20 +32,19 @@ install: libae.a ...@@ -32,20 +32,19 @@ install: libae.a
ln -sfT ../src/libae.a ../lib/libsz.a ln -sfT ../src/libae.a ../lib/libsz.a
clean: clean:
rm -f $(OBJS) test_encode.o test_decode.o \ rm -f $(OBJS) encode.o decode.o \
test_encode test_decode libae.a \ encode decode libae.a \
test_szcomp test_szcomp.o \ test_szcomp test_szcomp.o \
../data/test.ae ../data/test \ ../data/test.ae ../data/test \
*.gcno *.gcda *.gcov gmon.out *.gcno *.gcda *.gcov gmon.out
check: test_encode test_decode test_szcomp check: encode decode test_szcomp
./test_encode 1 1 < ../data/example_data > ../data/test.ae ./encode -cb1 ../data/example_data > ../data/test.aee
./test_decode 1 1 < ../data/test.ae > ../data/test ./decode -b1 ../data/test.aee
diff ../data/test ../data/example_data diff ../data/test ../data/example_data
./test_encode 99 99 < ../data/example_data > ../data/test.ae ./encode -cb1024 ../data/example_data > ../data/test.aee
./test_decode 101 101 < ../data/test.ae > ../data/test ./decode -b1024 ../data/test.aee
diff ../data/test ../data/example_data diff ../data/test ../data/example_data
./test_szcomp 65536 < ../data/example_data_16 > ../data/test ./test_szcomp 65536 ../data/example_data_16
diff ../data/test ../data/example_data_16
./test_szcomp 2097257 < ../data/zero_test > ../data/test
diff ../data/test ../data/zero_test
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <inttypes.h> #include <inttypes.h>
#include <string.h>
#include "libae.h" #include "libae.h"
#define CHUNK_OUT 1 #define CHUNK 1024
#define CHUNK_IN 1
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
ae_stream strm; ae_stream strm;
uint8_t *in; uint8_t *in;
uint8_t *out; uint8_t *out;
int chunk_in, chunk_out, i, c, total_out, status; int chunk, c, total_out, status;
int input_avail, output_avail; int input_avail, output_avail;
char *outfn, *infn, *ext;
FILE *infp, *outfp;
int cflag = 0;
if (argc == 3) 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)
{ {
chunk_in = atoi(argv[1]); infn = argv[optind];
chunk_out = atoi(argv[2]);
} }
else else
{ {
chunk_in = CHUNK_IN; fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
chunk_out = CHUNK_OUT; exit(-1);
} }
in = (uint8_t *)malloc(chunk_in); in = (uint8_t *)malloc(chunk);
out = (uint8_t *)malloc(chunk_out * sizeof(uint8_t)); out = (uint8_t *)malloc(chunk * sizeof(uint8_t));
if (in == NULL || out == NULL) if (in == NULL || out == NULL)
return 1; return 1;
strm.bit_per_sample = 8; 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.block_size = 8;
strm.segment_size = 2; strm.segment_size = 8;
strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS; strm.flags = AE_DATA_MSB | AE_DATA_PREPROCESS;
if (ae_decode_init(&strm) != AE_OK) if (ae_decode_init(&strm) != AE_OK)
return 1; return 1;
total_out = 0; total_out = 0;
strm.avail_in = 0; strm.avail_in = 0;
strm.avail_out = chunk_out; strm.avail_out = chunk;
strm.next_out = (uint8_t *)out; strm.next_out = (uint8_t *)out;
input_avail = 1; input_avail = 1;
...@@ -51,14 +103,11 @@ int main(int argc, char *argv[]) ...@@ -51,14 +103,11 @@ int main(int argc, char *argv[])
{ {
if (strm.avail_in == 0) if (strm.avail_in == 0)
{ {
i = 0; strm.avail_in = fread(in, 1, chunk, infp);
while(i < chunk_in && (c = getc(stdin)) != EOF) if (strm.avail_in != chunk)
in[i++] = c; input_avail = 0;
strm.avail_in = i;
strm.next_in = in; strm.next_in = in;
if (c == EOF)
input_avail = 0;
} }
if ((status = ae_decode(&strm, AE_NO_FLUSH)) != AE_OK) if ((status = ae_decode(&strm, AE_NO_FLUSH)) != AE_OK)
...@@ -69,17 +118,11 @@ int main(int argc, char *argv[]) ...@@ -69,17 +118,11 @@ int main(int argc, char *argv[])
if (strm.total_out - total_out > 0) if (strm.total_out - total_out > 0)
{ {
for (i=0; i < strm.total_out - total_out; i++) fwrite(out, strm.total_out - total_out, 1, outfp);
{
putc(out[i], stdout);
/* putc(out[i] >> 8, stdout); */
/* putc(out[i] >> 16, stdout); */
/* putc(out[i] >> 24, stdout); */
}
total_out = strm.total_out; total_out = strm.total_out;
output_avail = 1; output_avail = 1;
strm.next_out = (uint8_t *)out; strm.next_out = (uint8_t *)out;
strm.avail_out = chunk_out; strm.avail_out = chunk;
} }
else else
{ {
...@@ -89,7 +132,14 @@ int main(int argc, char *argv[]) ...@@ -89,7 +132,14 @@ int main(int argc, char *argv[])
} }
ae_decode_end(&strm); ae_decode_end(&strm);
fclose(infp);
fclose(outfp);
free(in); free(in);
free(out); free(out);
if (!cflag)
{
unlink(infn);
free(outfn);
}
return 0; return 0;
} }
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <inttypes.h> #include <inttypes.h>
#include <string.h>
#include "libae.h" #include "libae.h"
#define CHUNK_OUT 0x4000 #define CHUNK 1024
#define CHUNK_IN 1024
#define ALL_IN 9478
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
ae_stream strm; ae_stream strm;
uint8_t *in; uint8_t *in;
uint8_t *out; uint8_t *out;
int chunk_in, chunk_out, i, c, total_out, status; int chunk, total_out, status, c;
int input_avail, output_avail; 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 (argc == 3) if (optind < argc)
{ {
chunk_in = atoi(argv[1]); infn = argv[optind];
chunk_out = atoi(argv[2]);
} }
else else
{ {
chunk_in = CHUNK_IN; fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
chunk_out = CHUNK_OUT; exit(-1);
} }
out = (uint8_t *)malloc(chunk_out); out = (uint8_t *)malloc(chunk);
in = (uint8_t *)malloc(chunk_in * sizeof(uint8_t)); in = (uint8_t *)malloc(chunk);
if (in == NULL || out == NULL) if (in == NULL || out == NULL)
return 1; exit(-1);
strm.bit_per_sample = 8; strm.bit_per_sample = 16;
strm.block_size = 8; strm.block_size = 8;
strm.segment_size = 2; strm.segment_size = 8;
strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS; strm.flags = AE_DATA_MSB | AE_DATA_PREPROCESS;
if (ae_encode_init(&strm) != AE_OK) if (ae_encode_init(&strm) != AE_OK)
return 1; return 1;
total_out = 0; total_out = 0;
strm.avail_in = 0; strm.avail_in = 0;
strm.avail_out = chunk_out; strm.avail_out = chunk;
strm.next_out = out; strm.next_out = out;
input_avail = 1; input_avail = 1;
output_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) while(input_avail || output_avail)
{ {
if (strm.avail_in == 0) if (strm.avail_in == 0 && input_avail)
{ {
i = 0; strm.avail_in = fread(in, 1, chunk, infp);
while(i < chunk_in && (c = getc(stdin)) != EOF) if (strm.avail_in != chunk)
{
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; input_avail = 0;
strm.next_in = (uint8_t *)in;
} }
if ((status = ae_encode(&strm, AE_NO_FLUSH)) != AE_OK) if ((status = ae_encode(&strm, AE_NO_FLUSH)) != AE_OK)
...@@ -76,14 +115,11 @@ int main(int argc, char *argv[]) ...@@ -76,14 +115,11 @@ int main(int argc, char *argv[])
if (strm.total_out - total_out > 0) if (strm.total_out - total_out > 0)
{ {
for (i=0; i < strm.total_out - total_out; i++) fwrite(out, strm.total_out - total_out, 1, outfp);
{
putc(out[i], stdout);
}
total_out = strm.total_out; total_out = strm.total_out;
output_avail = 1; output_avail = 1;
strm.next_out = out; strm.next_out = out;
strm.avail_out = chunk_out; strm.avail_out = chunk;
} }
else else
{ {
...@@ -100,13 +136,18 @@ int main(int argc, char *argv[]) ...@@ -100,13 +136,18 @@ int main(int argc, char *argv[])
if (strm.total_out - total_out > 0) if (strm.total_out - total_out > 0)
{ {
for (i=0; i < strm.total_out - total_out; i++) fwrite(out, strm.total_out - total_out, 1, outfp);
{
putc(out[i], stdout);
}
} }
ae_encode_end(&strm); ae_encode_end(&strm);
fclose(infp);
fclose(outfp);
free(in); free(in);
free(out); free(out);
if (!cflag)
{
unlink(infn);
free(outfn);
}
return 0; return 0;
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <inttypes.h> #include <inttypes.h>
#include <string.h>
#include "szlib.h" #include "szlib.h"
#define OPTIONS_MASK (SZ_RAW_OPTION_MASK | SZ_MSB_OPTION_MASK | SZ_NN_OPTION_MASK) #define OPTIONS_MASK (SZ_RAW_OPTION_MASK | SZ_MSB_OPTION_MASK | SZ_NN_OPTION_MASK)
...@@ -10,15 +11,15 @@ ...@@ -10,15 +11,15 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int status, c; int status;
SZ_com_t sz_param; SZ_com_t sz_param;
unsigned char *dest; unsigned char *source, *dest, *dest1;
uint16_t *source; size_t destLen, dest1Len, sourceLen;
size_t destLen, sourceLen, n; 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; return 1;
} }
sz_param.options_mask = OPTIONS_MASK; sz_param.options_mask = OPTIONS_MASK;
...@@ -28,41 +29,35 @@ int main(int argc, char *argv[]) ...@@ -28,41 +29,35 @@ int main(int argc, char *argv[])
sourceLen = destLen = atoi(argv[1]); sourceLen = destLen = atoi(argv[1]);
source = (uint16_t *)malloc(sourceLen * sizeof(uint16_t)); source = (unsigned char *)malloc(sourceLen);
dest = (unsigned char *)malloc(destLen); dest = (unsigned char *)malloc(destLen);
dest1 = (unsigned char *)malloc(destLen);
if (source == NULL || dest == NULL) if (source == NULL || dest == NULL || dest1 == NULL)
return 1; return 1;
n = 0; if ((fp = fopen(argv[2], "r")) == NULL)
while((c = getc(stdin)) != EOF)
{ {
source[n] = c; fprintf(stderr, "Can't open %s\n", argv[2]);
source[n] |= getc(stdin) << 8; exit(-1);
n++;
} }
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); status = SZ_BufftoBuffCompress(dest, &destLen, source, sourceLen, &sz_param);
if (status != SZ_OK) if (status != SZ_OK)
return status; return status;
fprintf(stderr, "Compressed size is %li\n", destLen); dest1Len = sourceLen;
status = SZ_BufftoBuffDecompress(dest1, &dest1Len, dest, destLen, &sz_param);
status = SZ_BufftoBuffDecompress(source, &sourceLen, dest, destLen, &sz_param);
if (status != SZ_OK) if (status != SZ_OK)
return status; 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(source);
free(dest); free(dest);
free(dest1);
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment