-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
132 lines (93 loc) · 1.92 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "huff.h"
#include "llist.h"
#include "tlist.h"
#include "tree.h"
struct lnode* mklist(int fd)
{
int nr, i;
char buf[BUFSIZE];
struct lnode *root = 0;
struct lnode *result = 0;
while ((nr = read(fd, buf, BUFSIZE)) != -1 && nr != 0) {
/* count characters */
printf("Read %d bytes.\n", nr);
for(i = 0; i < nr; i++) {
if (i == 0)
root = mklnode(buf[i], 1);
else if ((result = findlnode(root, buf[i])))
incfreq(result);
else
applnode(buf[i], 1, root);
}
}
return root;
}
struct tlist* unite(struct tlist *list)
{
struct tlist *tmp = 0;
if (list == 0)
return list;
while (list->next != 0) {
tmp = list;
list->tree = insnode(0, list->tree, list->next->tree);
list->next = unite(list->next->next);
}
return list;
}
struct kpnode* mktree(struct lnode *list)
{
struct kpnode *root = 0;
struct tlist *tlp = mktlist(0);
struct tlist *tlroot = tlp;
/* make leaves */
for (; list != 0; list = list->next) {
tlp->tree = mknode(list->ch);
tlp->freq = list->freq;
if (list->next != 0) {
tlp->next = mktlist(0);
tlp = tlp->next;
}
}
tlroot = sorttlist(tlroot);
disptlist(tlroot);
/* connect leaves to form whole tree */
tlroot = unite(tlroot);
disptlist(tlroot);
root = tlroot->tree;
return root;
}
int encode(void)
{
return 0;
}
int main(int argc, char **argv)
{
int fd1, fd2;
struct lnode *clist;
struct kpnode *tree;
if (argc < 1) {
fprintf(stderr, "Error: not enough arguments.\n");
return 1;
}
if ((fd1 = open(argv[1], O_RDONLY)) < 0) {
fprintf(stderr, "Error: cannot open file.\n");
return 1;
}
if ((clist = mklist(fd1)) == 0) {
fprintf(stderr, "Error while compiling frequencies.\n");
return 1;
}
displist(clist);
tree = mktree(clist);
disptree(tree);
encode();
/*save(fd2);*/
remlnode(&clist);
/*remnode(&tree);*/
close(fd1);
return 0;
}