Skip to content
Snippets Groups Projects
Commit 1cff9f2c authored by Mathis Rosenhauer's avatar Mathis Rosenhauer
Browse files

getopt cmd line parsing in examples

parent ddf451e7
No related branches found
No related tags found
No related merge requests found
File added
......@@ -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
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "libae.h"
#define CHUNK_OUT 1
#define CHUNK_IN 1
#define CHUNK 1024
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 chunk, c, total_out, status;
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]);
chunk_out = atoi(argv[2]);
infn = argv[optind];
}
else
{
chunk_in = CHUNK_IN;
chunk_out = CHUNK_OUT;
fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
exit(-1);
}
in = (uint8_t *)malloc(chunk_in);
out = (uint8_t *)malloc(chunk_out * sizeof(uint8_t));
in = (uint8_t *)malloc(chunk);
out = (uint8_t *)malloc(chunk * sizeof(uint8_t));
if (in == NULL || out == NULL)
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.segment_size = 2;
strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS;
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_out;
strm.avail_out = chunk;
strm.next_out = (uint8_t *)out;
input_avail = 1;
......@@ -51,14 +103,11 @@ int main(int argc, char *argv[])
{
if (strm.avail_in == 0)
{
i = 0;
while(i < chunk_in && (c = getc(stdin)) != EOF)
in[i++] = c;
strm.avail_in = i;
strm.avail_in = fread(in, 1, chunk, infp);
if (strm.avail_in != chunk)
input_avail = 0;
strm.next_in = in;
if (c == EOF)
input_avail = 0;
}
if ((status = ae_decode(&strm, AE_NO_FLUSH)) != AE_OK)
......@@ -69,17 +118,11 @@ int main(int argc, char *argv[])
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); */
}
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_out;
strm.avail_out = chunk;
}
else
{
......@@ -89,7 +132,14 @@ int main(int argc, char *argv[])
}
ae_decode_end(&strm);
fclose(infp);
fclose(outfp);
free(in);
free(out);
if (!cflag)
{
unlink(infn);
free(outfn);
}
return 0;
}
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <string.h>
#include "libae.h"
#define CHUNK_OUT 0x4000
#define CHUNK_IN 1024
#define ALL_IN 9478
#define CHUNK 1024
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 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 (argc == 3)
if (optind < argc)
{
chunk_in = atoi(argv[1]);
chunk_out = atoi(argv[2]);
infn = argv[optind];
}
else
{
chunk_in = CHUNK_IN;
chunk_out = CHUNK_OUT;
fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
exit(-1);
}
out = (uint8_t *)malloc(chunk_out);
in = (uint8_t *)malloc(chunk_in * sizeof(uint8_t));
out = (uint8_t *)malloc(chunk);
in = (uint8_t *)malloc(chunk);
if (in == NULL || out == NULL)
return 1;
exit(-1);
strm.bit_per_sample = 8;
strm.bit_per_sample = 16;
strm.block_size = 8;
strm.segment_size = 2;
strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS;
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_out;
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)
if (strm.avail_in == 0 && input_avail)
{
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)
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)
......@@ -76,14 +115,11 @@ int main(int argc, char *argv[])
if (strm.total_out - total_out > 0)
{
for (i=0; i < strm.total_out - total_out; i++)
{
putc(out[i], stdout);
}
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_out;
strm.avail_out = chunk;
}
else
{
......@@ -100,13 +136,18 @@ int main(int argc, char *argv[])
if (strm.total_out - total_out > 0)
{
for (i=0; i < strm.total_out - total_out; i++)
{
putc(out[i], stdout);
}
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;
}
......@@ -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)
......@@ -10,15 +11,15 @@
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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment