-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress.c
More file actions
245 lines (176 loc) · 6.06 KB
/
compress.c
File metadata and controls
245 lines (176 loc) · 6.06 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include "tree.h"
#include "priority_queue.h"
#include "frequency_table.h"
#include "file.h"
#include "util.h"
#include "codeword.h"
void UseTreeProduceCodeWordFunction(CodeWord cw, TreeNode trn);
void PrintCompressionTreeFunction(FILE* fp, int* buffer_p, int* buffer_len_p, TreeNode trn);
// add .huff suffix
char* CreateCompressedFileName(char* filename){
assert(filename != NULL);
char* filename_out = (char*) malloc((strlen(filename)+5+1) * sizeof(char));
assert(filename_out != NULL);
strcpy(filename_out, filename);
strcat(filename_out, ".huff");
return filename_out;
}
void compression_status(char* name_in, char* name_out, FILE* fp_in, FILE* fp_out){
assert(fp_in != NULL && fp_out != NULL);
assert(name_in != NULL && name_out != NULL);
fseek(fp_in, 0, SEEK_END);
fseek(fp_out, 0, SEEK_END);
long size_in = ftell(fp_in);
long size_out = ftell(fp_out);
printf("Input file: %s\nSize: %.3f KB\n", name_in, (float) size_in / 1024);
printf("Output file: %s\nSize: %.3f KB\n", name_out, (float) size_out / 1024);
printf("Space saving: %.2f%%\n", ((float) 1 - (float)size_out / (float) size_in) * 100);
return;
}
FreqTable ReadFileCountFrequency(FILE* fp){
assert(fp != NULL);
fseek(fp, 0, SEEK_SET);
FreqTable fqtable = FreqTableCreate(ASCII_SIZE);
// read file
int c;
while ((c = getc(fp)) != EOF){
FreqTableInsert(fqtable, c);
}
return fqtable;
}
PriorityQueue UseFreqTableProducePriorityQueue(FreqTable fqtable){
assert(fqtable != NULL && fqtable->table != NULL);
assert(fqtable->size > 0);
PriorityQueue pq = PriorityQueueCreate();
for (int i = 0; i < fqtable->size; i++){
if (fqtable->table[i] > 0){
// create a tree node and insert into the table
TreeNode trn = TreeNodeCreate(i, fqtable->table[i]);
PriorityQueueInsertTreeNode(pq, trn);
}
}
return pq;
}
Tree UsePriorityQueueProduceTree(PriorityQueue pq){
assert(IsPriorityQueueValid(pq));
// pick two trn nodes from the queue
// combine them, smaller occ be the right child
// and insert back to the queue
// until only one trn left
TreeNode trn1, trn2, trn_parent;
int trn_parent_occ;
while (GetCount(pq) != 1){
trn1 = PriorityQueueGetTreeNode(pq);
trn2 = PriorityQueueGetTreeNode(pq);
trn_parent_occ = SumTwoOcc(trn1, trn2);
trn_parent = TreeNodeCreate(INTERNAL_NODE_C, trn_parent_occ);
ConnectAsLeftChild(trn2, trn_parent);
ConnectAsRightChild(trn1, trn_parent);
PriorityQueueInsertTreeNode(pq, trn_parent);
}
TreeNode root = PriorityQueueGetTreeNode(pq);
ResetInternalNodeToRootNode(root);
Tree tr = TreeCreate(root);
return tr;
}
CodeWord UseTreeProduceCodeWord(Tree tr){
assert(tr != NULL);
CodeWord cw = CodeWordCreate(ASCII_SIZE);
// traversal the tree
// insert each leaf into the codeword
UseTreeProduceCodeWordFunction(cw, tr->root);
return cw;
}
void UseTreeProduceCodeWordFunction(CodeWord cw, TreeNode trn){
if (trn != NULL){
if (IsLeafNode(trn)){
CodeWordNode cwn = CodeWordNodeCreate(trn);
CodeWordInsertNode(cw, cwn);
}
else{
UseTreeProduceCodeWordFunction(cw, trn->left);
UseTreeProduceCodeWordFunction(cw, trn->right);
}
}
return;
}
int ReadFilePrintCompression(FILE* fp_in, FILE* fp_out, CodeWord cw){
assert(fp_in != NULL && fp_out != NULL);
assert(cw != NULL);
assert(cw->size > 0 && cw->list != NULL);
// move the file descriptor to the start of the file
// read again
fseek(fp_in, 0, SEEK_SET);
int c;
int buffer = 0;
int buffer_len = 0;
CodeWordNode cwn;
while ((c = getc(fp_in)) != EOF){
cwn = CodeWordGetNode(cw, c);
PrintCodeWordToFile(fp_out, &buffer, &buffer_len, cwn);
}
// after finish, pad the last byte if necessary
int pad_num = PadByte(fp_out, &buffer, &buffer_len);
return pad_num;
}
void PrintCompressionTree(FILE* fp, Tree tr){
assert(fp != NULL);
assert(tr != NULL && tr->root != NULL);
// output post order traversal of the tree
// during decompression, use stack to rebuild the tree
int buffer = 0;
int buffer_len = 0;
PrintCompressionTreeFunction(fp, &buffer, &buffer_len, tr->root);
PadByte(fp, &buffer, &buffer_len);
return;
}
void PrintCompressionTreeFunction(FILE* fp, int* buffer_p, int* buffer_len_p, TreeNode trn){
assert(fp != NULL);
assert(buffer_p != NULL && buffer_len_p != NULL);
if (trn != NULL){
// post order traversal: left, right, middle
// check this node first
if (IsLeafNode(trn)){
// for leaf node: print 1 + byte
PrintOneBit(fp, buffer_p, buffer_len_p, 1);
PrintOneByte(fp, buffer_p, buffer_len_p, GetC(trn));
}
else{
// internal node
// go down left and right first
PrintCompressionTreeFunction(fp, buffer_p, buffer_len_p, trn->left);
PrintCompressionTreeFunction(fp, buffer_p, buffer_len_p, trn->right);
// then print the bit 0
PrintOneBit(fp, buffer_p, buffer_len_p, 0);
}
}
return;
}
void PrintFirstByteEmpty(FILE* fp){
assert(fp != NULL);
fseek(fp, 0, SEEK_SET);
putc(0, fp);
return;
}
void RePrintFirstByteWithPadNumber(FILE* fp, int pad_num){
assert(fp != NULL);
assert(pad_num >= 0 && pad_num <= 7);
fseek(fp, 0, SEEK_SET);
putc(pad_num, fp);
return;
}
void PrintSecondByteCharCount(FILE* fp, FreqTable fqtable){
assert(fp != NULL);
assert(IsFreqTableValid(fqtable));
fseek(fp, 1, SEEK_SET);
int char_count = FreqTableGetCharCount(fqtable);
// for now, only deal with the ASCII set, char count >= 0 and <= 256
// so here -1, in order to print in one byte, and for compression + 1
putc(char_count - 1, fp);
return;
}