-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.c
40 lines (37 loc) · 1.11 KB
/
image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "compress.h"
// TODO: Note from Professor Daniels
// Changes: compress411->compress, compress40->compress, decompress40->decompress
// TODO: Throw an error if too many images are given as input
static void (*compress_or_decompress)(FILE *input) = compress;
int main(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
compress_or_decompress = compress;
} else if (!strcmp(argv[i], "-d")) {
compress_or_decompress = decompress;
} else if (*argv[i] == '-') {
fprintf(stderr, "%s: unknown option '%s'\n", argv[0], argv[i]);
exit(1);
} else if (argc - i > 2) {
fprintf(stderr, "Usage: %s -d [filename]\n"
" %s -c [filename]\n", argv[0], argv[0]);
exit(1);
} else {
break;
}
}
assert(argc - i <= 1); // at most one file on command line
if (i < argc) {
FILE* fp = fopen(argv[i], "r");
assert(fp);
compress_or_decompress(fp);
fclose(fp);
} else {
compress_or_decompress(stdin);
}
}