-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
133 lines (100 loc) · 2.86 KB
/
file.c
File metadata and controls
133 lines (100 loc) · 2.86 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "tree.h"
#include "list.h"
FILE* open_the_file(char* filename, char* mode){
assert(filename != NULL && mode != NULL);
FILE* fp = fopen(filename, mode);
assert(fp != NULL);
return fp;
}
FILE* close_the_file(FILE* fp){
assert(fp != NULL);
fclose(fp);
fp = NULL;
return fp;
}
// single digit
void FilePrintDigit(int* buffer_p, int* buffer_len_p, FILE* fp, int new_digit){
assert(buffer_p != NULL && buffer_len_p != NULL);
assert(fp != NULL);
assert(new_digit == 0 || new_digit == 1);
(*buffer_p) <<= 1;
(*buffer_p) |= (new_digit & 1);
(*buffer_len_p) += 1;
// print when 8
if (*buffer_len_p == 8){
fputc(*buffer_p, fp);
// reset
(*buffer_p) = 0;
(*buffer_len_p) = 0;
}
return;
}
// for new node, print the byte
void FilePrintByte(int* buffer_p, int* buffer_len_p, FILE* fp, int new_byte){
assert(buffer_p != NULL && buffer_len_p != NULL);
assert(fp != NULL);
assert(new_byte >= 0);
if (*buffer_len_p == 0){
fputc(new_byte, fp);
}
else{
int this_digit;
int mask;
// input into FilePrintDigit one by one
for (int i = 7; i >= 0; i--){
mask = 1 << i;
this_digit = ((new_byte & mask) >> i) & 1;
FilePrintDigit(buffer_p, buffer_len_p, fp, this_digit);
}
}
return;
}
// print the node path from the root node
// left = 0, right = 1
void FilePrintNodePath(int* buffer_p, int* buffer_len_p, FILE* fp, TreeNode trn){
assert(buffer_p != NULL && buffer_len_p != NULL);
assert(fp != NULL);
assert(trn != NULL);
// recursion, go upwards from the tree first
if (trn->parent->c != ROOT_C){
FilePrintNodePath(buffer_p, buffer_len_p, fp, trn->parent);
}
// now trn's parent is the root node
if (trn == trn->parent->left){
FilePrintDigit(buffer_p, buffer_len_p, fp, 0);
}
else{
FilePrintDigit(buffer_p, buffer_len_p, fp, 1);
}
return;
}
void FilePrintEmptyByte(FILE* fp){
assert(fp != NULL);
// put 0 for now, will change at the end of compression
fputc(0, fp);
return;
}
void FileRePrintFirstByte(int num, FILE* fp){
assert(num >= 0 && num <= 7);
assert(fp != NULL);
fseek(fp, 0, SEEK_SET);
fputc(num, fp);
return;
}
// for the last byte, need to pad and print to the front
// return the number of bits pad, should be 0 to 7
int FilePrintPad(int* buffer_p, int* buffer_len_p, FILE* fp){
assert(buffer_p != NULL && buffer_len_p != NULL);
assert((*buffer_len_p) >= 0 && (*buffer_len_p) <= 7);
assert(fp != NULL);
int pad_num = 0;
if ((*buffer_len_p) != 0){
pad_num = 8 - (*buffer_len_p);
(*buffer_p) <<= pad_num;
fputc(*buffer_p, fp);
}
return pad_num;
}