-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.c
74 lines (64 loc) · 1.33 KB
/
decode.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "huffman-init.h"
int main()
{
NODE* huffmantree_root = getHuffmanTreeRoot();
int fd_encoded = open("etext", O_RDONLY);
int fd_decoded = open("ptext", O_WRONLY | O_CREAT);
int n, x;
char rbuf[1];
char wbuf[1];
NODE* temp = huffmantree_root;
do
{
n = read(fd_encoded, rbuf, 1);
if(n == 1)
{
if(rbuf[0] == '\n')
{
wbuf[0] = '\n';
write(fd_decoded, wbuf, 1);
break;
}
if(temp->left != NULL && temp->right != NULL)
{
if(rbuf[0] == '0')
{
temp = temp->left;
continue;
}
else if(rbuf[0] == '1')
{
temp = temp->right;
continue;
}
else if(rbuf[0] == ' ')
{
wbuf[0] = ' ';
write(fd_decoded, wbuf, 1);
continue;
}
}
wbuf[0] = temp->c;
write(fd_decoded, wbuf, 1);
temp = huffmantree_root;
if(rbuf[0] == '.')
{
continue;
}
}
}
while(rbuf[0] != '\n');
// Delete the Huffman tree and the sorted frequency list from memory
clearList(&htree_root);
clearList(&sortedlist_head);
close(fd_encoded);
close(fd_decoded);
printf("Written to ptext\n");
return 0;
}