-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffkub.c
166 lines (158 loc) · 4.2 KB
/
huffkub.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Todo: how to cast pointer to struct and access its element:
// eg. if p is pointer to node, then I am using:
// ((leaf *) p)->ch, but there must be a more beautiful way?
// oneletter_ failing
#include <stdio.h>
#include "struct.h"
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <error.h>
#include <unistd.h>
#include "huffkub.h"
#include "compress.h"
#include "extract.h"
void parse_args(int argc, char **argv) {
opts = 0;
int opt;
while ((opt = getopt(argc, argv, "cxiof:d:hv")) != -1) {
switch (opt) {
case 'c':
if (opts & UNPACK)
error(2, 0, "-c and -x are exclusive");
opts |= PACK;
break;
case 'x':
if (opts & PACK)
error(2, 0, "-c and -x are exclusive");
opts |= UNPACK;
break;
case 'i':
if (opts & FILEIN)
error(2, 0, "-f and -i are exclusive");
opts |= STDIN;
break;
case 'o':
if (opts & FILEOUT)
error(2, 0, "-d and -o are exclusive");
opts |= STDOUT;
break;
case 'f':
if (opts & STDIN)
error(2, 0, "-f and -i are exclusive");
opts |= FILEIN;
strncpy(inputfile, optarg, 128);
break;
case 'd':
if (opts & STDOUT)
error(2, 0, "-d and -o are exclusive");
opts |= FILEOUT;
strncpy(outputfile, optarg, 128);
break;
case 'v':
opts |= VERBOSE;
break;
case 'h':
default:
printf("Simple file packer using Huffman algorithm.\n\n"
"Usage:\thuffkub [-c] [-x] [-i] [-o] [-f inputfile] [-d outputfile]\n\n"
"Argument usage:\n\t -c\t\tPack (exclusive with -x)\n"
"\t -x\t\tUnpack (exclusive with -c)\n"
"\t -i\t\tRead input from stdin (exclusive with -f)\n"
"\t -o\t\tWrite output to stdout (exclusive with -d)\n"
"\t -f FILE\tRead input from FILE (exclusive with -i)\n"
"\t -d FILE\tWrite output to FILE (exclusive with -o)\n"
"\t -v\t\tVerbose output (only when -o is not set)\n"
"\t -h\t\tPrint this help message\n");
exit(0);
}
}
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Use -h for usage\n");
exit(0);
}
blksize = 1024;
parse_args(argc, argv);
if (opts & PACK) {
compress();
}
if (opts & UNPACK) {
extract();
}
if (opts & VERBOSE) {
print_tree1((node* ) tip);
verbose_print();
}
return 0;
}
void verbose_print() {
int i = 0;
fprintf(stderr, "/*\nCHAR\tFREQ\tCODE\n");
for (i = 0; i < CHAR; i++) {
if (chararray[i] != NULL) {
fprintf(stderr, "%c\t%d\t", chararray[i]->ch, chararray[i]->freq);
print_code(calc_code(chararray[i]));
}
}
fprintf(stderr, "\n*/");
}
void print_tree(node * rr) {
leaf *l,*r;
int printmode;
if (rr->type == 2) {
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%p|%d\"]\n ", rr, rr, rr->freq);
if ( (((hub *) rr)->left)->type == 1){
l = ( (leaf *) ((hub *) rr)->left);
if ((printmode = sanitize(l->ch)) == 1)
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", l, l->ch, l->freq);
else if (printmode ==2)
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", l, (int) l->ch, l->freq);
else
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", l, l->ch, l->freq);
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, l);
} else {
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, ((hub *) rr)->left);
}
print_tree(((hub *) rr)->left);
if ( (((hub *) rr)->right)->type == 1){
r = ( (leaf *) ((hub *) rr)->right);
if ((printmode = sanitize(r->ch)) == 1)
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", r, r->ch, r->freq);
else if (printmode == 2)
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", r, (int) r->ch, r->freq);
else
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", r, r->ch, r->freq);
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, r);
} else {
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, ((hub *) rr)->right);
}
print_tree(((hub *) rr)->right);
}
}
void print_tree1(node * rr) {
fprintf(stderr, "digraph bt {\n"
"\trandkir = LR;\n");
print_tree(rr);
fprintf(stderr, "}\n");
}
int sanitize(unsigned char c) {
if ((signed char) c < 32 || (signed char) c > 126)
return 2;
switch ((signed char) c){
case 10:
case '"':
case '|':
case ' ':
case '<':
case '>':
case '{':
case '}':
case 0:
return 1;
break;
default:
return 0;
}
}