-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
67 lines (60 loc) · 1.51 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <string.h>
#include "huffman.h"
#include "simple_file_buffer.h"
int file_encode(const char *input_file, const char *output_file) {
struct buffer_ops *in, *out;
in = create_file_buffer_ops(input_file, "rb");
if (!in) {
LOGE("Create input file ops failed\n");
return 1;
}
out = create_file_buffer_ops(output_file, "wb");
if (!out) {
LOGE("Create input file ops failed\n");
goto OUT;
}
encode(in, out);
desotry_file_buffer_ops(out);
OUT:
desotry_file_buffer_ops(in);
return 0;
}
int file_decode(const char *input_file, const char *output_file) {
struct buffer_ops *in, *out;
in = create_file_buffer_ops(input_file, "rb");
if (!in) {
LOGE("Create input file ops failed\n");
return 1;
}
out = create_file_buffer_ops(output_file, "wb");
if (!out) {
LOGE("Create input file ops failed\n");
goto OUT;
}
decode(in, out);
desotry_file_buffer_ops(out);
OUT:
desotry_file_buffer_ops(in);
return 0;
}
int main(int argc, char *argv[]) {
char *input, *output;
if (argc < 3) {
LOGE("Miss args.\n");
LOGE("Usage: -d/-e input [output]\n");
return 1;
}
input = argv[2];
output = NULL;
if (argc > 3) {
output = argv[3];
}
if (!strcmp(argv[1], "-e")) {
file_encode(input, output);
} else if (!strcmp(argv[1], "-d")) {
file_decode(input, output);
} else {
LOGE("Unknown action.\n");
}
return 0;
}