-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar_info_node.c
179 lines (148 loc) · 7.03 KB
/
char_info_node.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
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "char_info_node.h"
CharInfoNode* createCharInfoNodeDictionary() {
CharInfoNode* pCharInfoNodeDictionary = (CharInfoNode*)calloc(MAX_CHARACTERS, sizeof(CharInfoNode));
if (pCharInfoNodeDictionary == NULL) {
fprintf(stderr, "ERROR: Failed to Create Character Information Dictionary!\nCAUSE: Memory Allocation for pCharInfoNodeDictionary Failed!\n");
return NULL;
}
for (int decimalCharValue = 0; decimalCharValue < MAX_CHARACTERS; decimalCharValue++) {
pCharInfoNodeDictionary[decimalCharValue].character = (char)decimalCharValue;
pCharInfoNodeDictionary[decimalCharValue].frequency = 0;
pCharInfoNodeDictionary[decimalCharValue].huffmanCode = NULL;
}
return pCharInfoNodeDictionary;
}
void calculateCharFreqs(const char text[], CharInfoNode* pCharInfoNodeDictionary) {
if (pCharInfoNodeDictionary == NULL) {
fprintf(stderr, "ERROR: Failed to Calculate Character Frequencies!\nCAUSE: Pointer to pCharInfoNodeDictionary is NULL!\n");
return;
}
if (text == NULL) {
fprintf(stderr, "ERROR: Failed to Encode Text!\nCAUSE: text is NULL!\n");
return;
}
for (int textIndex = 0; text[textIndex] != '\0'; textIndex++) {
unsigned char castedTextCharacter = (unsigned char)text[textIndex];
if (isprint(castedTextCharacter)) {
pCharInfoNodeDictionary[castedTextCharacter].frequency++;
}
}
}
int compareCharFreqs(const void* pVoidFirstCharInfoNode, const void* pVoidSecondCharInfoNode) {
CharInfoNode* pFirstCharInfoNode = (CharInfoNode*)pVoidFirstCharInfoNode;
CharInfoNode* pSecondCharInfoNode = (CharInfoNode*)pVoidSecondCharInfoNode;
return (pFirstCharInfoNode->frequency - pSecondCharInfoNode->frequency);
}
void sortCharFreqs(CharInfoNode* pCharInfoNodeDictionary) {
if (pCharInfoNodeDictionary == NULL) {
fprintf(stderr, "ERROR: Failed to Sort Character Frequencies!\nCAUSE: Pointer to pCharInfoNodeDictionary is NULL!\n");
return;
}
qsort(pCharInfoNodeDictionary, MAX_CHARACTERS, sizeof(CharInfoNode), compareCharFreqs);
}
void assignHuffmanCodes(HCTNode* pHCTNode, CharInfoNode* pCharInfoNodeDictionary, char* huffmanCodeBuffer, int huffmanCodeLength) {
if (pHCTNode == NULL) return;
if (pCharInfoNodeDictionary == NULL) {
fprintf(stderr, "ERROR: Failed to Assign Huffman Codes!\nCAUSE: Pointer to pCharInfoNodeDictionary is NULL!\n");
return;
}
if (huffmanCodeBuffer == NULL) {
fprintf(stderr, "ERROR: Failed to Assign Huffman Codes!\nCAUSE: Pointer to huffmanCodeBuffer is NULL!\n");
return;
}
if (huffmanCodeLength < 0) {
fprintf(stderr, "ERROR: Failed to Assign Huffman Codes!\nCAUSE: huffmanCodeLength is Negative!\n");
return;
}
// Check if leaf node and assign Huffman code
if (pHCTNode->pLeftNode == NULL && pHCTNode->pRightNode == NULL) {
unsigned char charIndex = (unsigned char)pHCTNode->character;
for (int decimalCharIndex = 0; decimalCharIndex < MAX_CHARACTERS; ++decimalCharIndex) {
if (pCharInfoNodeDictionary[decimalCharIndex].character == charIndex) {
pCharInfoNodeDictionary[decimalCharIndex].huffmanCode = (char*)calloc(huffmanCodeLength + 1, sizeof(char));
strncpy(pCharInfoNodeDictionary[decimalCharIndex].huffmanCode, huffmanCodeBuffer, huffmanCodeLength);
pCharInfoNodeDictionary[decimalCharIndex].huffmanCode[huffmanCodeLength] = '\0';
return;
}
}
return;
}
// Recurse on left child with '0' appended
if (pHCTNode->pLeftNode != NULL) {
huffmanCodeBuffer[huffmanCodeLength] = '0';
assignHuffmanCodes(pHCTNode->pLeftNode, pCharInfoNodeDictionary, huffmanCodeBuffer, huffmanCodeLength + 1);
}
// Recurse on right child with '1' appended
if (pHCTNode->pRightNode != NULL) {
huffmanCodeBuffer[huffmanCodeLength] = '1';
assignHuffmanCodes(pHCTNode->pRightNode, pCharInfoNodeDictionary, huffmanCodeBuffer, huffmanCodeLength + 1);
}
}
void encodeText(const char text[], CharInfoNode* pCharInfoNodeDictionary) {
if (text == NULL) {
fprintf(stderr, "ERROR: Failed to Encode Text!\nCAUSE: text is NULL!\n");
return;
}
if (pCharInfoNodeDictionary == NULL) {
fprintf(stderr, "ERROR: Failed to Encode Text!\nCAUSE: Pointer to pCharInfoNodeDictionary is NULL!\n");
return;
}
int textLength = (int)strlen(text);
char* encodedText = (char*)calloc(textLength * 8 + 1, sizeof(char)); // 8 bits per character
if (encodedText == NULL) {
fprintf(stderr, "ERROR: Failed to Encode Text!\nCAUSE: Memory Allocation for Encoded Text Failed!\n");
return;
}
int encodedTextIndex = 0;
for (int textIndex = 0; textIndex < textLength - 1; textIndex++) {
unsigned char castedTextCharacter = (unsigned char)text[textIndex];
for (int decimalCharIndex = 0; decimalCharIndex < MAX_CHARACTERS; decimalCharIndex++) {
if (pCharInfoNodeDictionary[decimalCharIndex].character == castedTextCharacter) {
int huffmanCodeLength = (int)strlen(pCharInfoNodeDictionary[decimalCharIndex].huffmanCode);
strncpy(&encodedText[encodedTextIndex], pCharInfoNodeDictionary[decimalCharIndex].huffmanCode, huffmanCodeLength);
encodedTextIndex += huffmanCodeLength;
break;
}
}
}
encodedText[encodedTextIndex] = '\0';
fprintf(stdout, "\nHere's your Encoded Text:\n%s\n", encodedText);
fflush(stdout);
free(encodedText);
}
void destroyCharInfoDictionary(CharInfoNode* pCharInfoNodeDictionary)
{
if (pCharInfoNodeDictionary == NULL)
{
fprintf(stderr, "ERROR: Failed to Destroy Character Information Dictionary!\nCAUSE: Pointer to pCharInfoNodeDictionary is NULL!\n");
return;
}
for (int decimalCharIndex = 0; decimalCharIndex < MAX_CHARACTERS; decimalCharIndex++)
{
if (pCharInfoNodeDictionary[decimalCharIndex].huffmanCode != NULL)
{
free(pCharInfoNodeDictionary[decimalCharIndex].huffmanCode);
}
}
free(pCharInfoNodeDictionary);
}
void printCharInfoDictionary(CharInfoNode* pCharInfoNodeDictionary) {
if (pCharInfoNodeDictionary == NULL) {
fprintf(stderr, "ERROR: Failed to Print Character Information Dictionary!\nCAUSE: Pointer to pCharInfoNodeDictionary is NULL!\n");
return;
}
fprintf(stdout, "| %s | %s | %s |\n",
"Characters",
"Frequencies",
"Huffman Code");
fflush(stdout);
for (int decimalCharIndex = 0; decimalCharIndex < MAX_CHARACTERS; decimalCharIndex++) {
if (pCharInfoNodeDictionary[decimalCharIndex].frequency > 0) {
fprintf(stdout, "| %-10c | %04d | %-12s |\n",
pCharInfoNodeDictionary[decimalCharIndex].character,
pCharInfoNodeDictionary[decimalCharIndex].frequency,
pCharInfoNodeDictionary[decimalCharIndex].huffmanCode);
fflush(stdout);
}
}
}