-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
112 lines (94 loc) · 3.09 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Basic implementation of LZ78 compression algorithm
*
* Copyright (C) 2010 evilaliv3 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include "wrapper.h"
/* Usage program help */
void help(char* argv[]) {
fprintf(stderr,
"Usage: %s [Options]\n\n"
"Options:\n"
"-h show this help\n"
"-i input sets input source\n"
"-o output sets output destination\n"
"-d sets decompress mode\n"
"-t type sets compression algorithm\n"
"\n"
"Optional flags:\n"
"-b bsize sets size of I/O buffers\n"
"-a param sets additional parameter\n"
"",
argv[0]);
}
int main(int argc, char* argv[]) {
char* name_in = NULL;
char* name_out = NULL;
char* w_argv = NULL;
wrapper* w;
int bsize = B_SIZE_DEFAULT;
int opt, ret;
uint8_t w_mode = WRAPPER_MODE_COMPRESS;
uint8_t w_type = LZ78_ALGORITHM;
while ((opt = getopt(argc, argv, "i:o:dt:b:a:h")) != -1) {
switch (opt) {
case 'i': /* Input */
name_in = optarg;
break;
case 'o': /* Output */
name_out = optarg;
break;
case 'd': /* Decompress */
w_mode = WRAPPER_MODE_DECOMPRESS;
break;
case 't': /* Type */
if (!(w_type = get_algorithm(optarg))) {
fprintf(stderr, "Invalid algorithm type: %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'b': /* Buffer size */
bsize = byte_size(optarg);
break;
case 'a': /* Additional parameter */
w_argv = optarg;
break;
case 'h': /* Compressor help */
default:
help(argv);
exit(EXIT_FAILURE);
}
}
if (bsize <= 0) {
help(argv);
exit(EXIT_FAILURE);
}
/* Creates a wrapper instance */
w = wrapper_new(w_mode, w_type, w_argv);
if (w == NULL) {
fprintf(stderr, "Unable to create wrapper\n");
exit(EXIT_FAILURE);
}
/* Executes the wrapper function */
ret = wrapper_exec(w, name_in, name_out);
if (ret != WRAPPER_SUCCESS)
wrapper_perror();
/* Destroyes the wrapper instance */
wrapper_destroy(w);
return ret;
}