-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.c
676 lines (610 loc) · 17.4 KB
/
Source.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//preproccessor directives
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_DEPTH 12
//Type definitions
//huffman tree node
typedef struct tree
{
char character;
int frequency;
struct tree* left;
struct tree* right;
}tree;
//queue
typedef struct queue
{
struct tree* node;
struct queue* next;
}queue;
//priority queue
typedef struct PriorityQueue
{
struct queue* Queue;
int size;
}PriorityQueue;
//Huffman codes list node
typedef struct HuffmanCodes
{
char character;
int* code;
}HuffmanCodes;
//Huffman codes linked list
typedef struct HuffmanCodesList
{
struct HuffmanCodes* data;
struct HuffmanCodesList* next;
}HuffmanCodesList;
//Function prototypes
//priority queue functions
PriorityQueue* InitiateQueue();
PriorityQueue* enqueue(PriorityQueue* head, tree* data);
PriorityQueue* dequeue(PriorityQueue* head);
tree* peek(PriorityQueue* head);
PriorityQueue* TerminateQueue(PriorityQueue* head);
//huffman tree functions
tree* BuildHuffmanTree(PriorityQueue* Frequencies); //takse frequencies priority queue and creates huffman tree
void TerminateTree(tree* root);
//Huffman codes linked list functions
HuffmanCodesList* InitaiteList();
HuffmanCodes* InitiateNode();
HuffmanCodes* TerminateNode(HuffmanCodes* head);
HuffmanCodesList* add(HuffmanCodesList* head, HuffmanCodes* data);
HuffmanCodes* searchwithCharacter(HuffmanCodesList* head, char character);
HuffmanCodes* searchwithcode(HuffmanCodesList* head, int* code);
HuffmanCodesList* Terminatelist(HuffmanCodesList* head);
//Huffman encoding functions
PriorityQueue* readfile(PriorityQueue* head, FILE *ptr); //opens .txt file and stores the characters frequencies in a priority queue
HuffmanCodesList* savecodes(tree* haffmantree, HuffmanCodesList** list); //takes huffman tree and stores the huffman codes in a linked list
int getcodes(tree* root, int arr[MAX_DEPTH], int top, HuffmanCodesList** head); //wrapper function for savecodes()
int printcodes(HuffmanCodesList* codes); //prints the huffman codes on the screen
int HuffmanCodesToFile(FILE* write, HuffmanCodesList* codes, int bits); //saves the huffman codes in a .cod file
int* PrinfHuffmanString(FILE* read, HuffmanCodesList* codes,int* NumberOfBits); //prints huffman string on the screen and returns the huffman string
int HuffmanStringToFile(int* HuffmanString, FILE* write, int NumberOfBits); //saves huffman string in a .com file
//huffman decoding functions
HuffmanCodesList* FileToHuffmanCodes(FILE* read, HuffmanCodesList** codes,int* bits , int* overflow); //reads .cod file and returns huffman codes
int* FileToHuffmanString(FILE* read, int overflow, int NumberOfBits); //reads .com file and returns huffman string
int DecodeHuffmanString(FILE* write, int* HuffmanString, HuffmanCodesList* codes, int overflow, int NumberOfBits); //decodes the huffman string using the huffman codes and restores the original .txt file
//utility functions
int power(int x, int y);
int BinaryToDecimal(int binary[8]);
int DecimalToBinary(int Decimal, int binary[8]);
int main()
{
int selection;
char path_read[100], path_write_codes[100], path_write_Huffman_String[100], path_write_text[100],stopper;
int* HuffmanString;
int length;
int overflow = 0;
int NumberOfBits = 0;
FILE* read = NULL;
FILE* write_codes = NULL;
FILE* write_Huffman_String = NULL;
FILE* write_txt = NULL;
PriorityQueue* Nodes = InitiateQueue();
tree* HuffmanTree = NULL;
HuffmanCodesList* codes = InitaiteList();
while (1)
{
printf("\nplease select an operation :-\n1) compress file.\n2) decompress file\n3) exit\nenter selection >> ");
scanf("%d", &selection);
if (selection == 1)
{
stopper = getchar();
printf("\nenter the relative .txt file path >> ");
gets_s(path_read, 100);
strcpy(path_write_codes, path_read);
strcpy(path_write_Huffman_String, path_read);
length = strlen(path_read);
for (int i = 0; i < length; i++)
if (path_read[i] == '.')
{
path_write_codes[i + 1] = 'c';
path_write_codes[i + 2] = 'o';
path_write_codes[i + 3] = 'd';
path_write_Huffman_String[i + 1] = 'c';
path_write_Huffman_String[i + 2] = 'o';
path_write_Huffman_String[i + 3] = 'm';
break;
}
read = fopen(path_read, "r");
write_codes = fopen(path_write_codes, "w");
write_Huffman_String = fopen(path_write_Huffman_String, "w");
Nodes = readfile(Nodes, read);
HuffmanTree = BuildHuffmanTree(Nodes);
codes = savecodes(HuffmanTree, &codes);
TerminateTree(HuffmanTree);
printcodes(codes);
HuffmanString = PrinfHuffmanString(read, codes, &NumberOfBits);
HuffmanCodesToFile(write_codes, codes, NumberOfBits);
HuffmanStringToFile(HuffmanString, write_Huffman_String, NumberOfBits);
free(HuffmanString);
fclose(read);
fclose(write_codes);
fclose(write_Huffman_String);
printf("\nCompression sucessfull !!!\n .com file name : %s\n.cod file name : %s",path_write_Huffman_String,path_write_codes);
}
else if (selection == 2)
{
stopper = getchar();
printf("\nenter the relative path if the .cod file that contains the huffman codes >> ");
gets_s(path_read, 100);
read = fopen(path_read, "r");
length = strlen(path_read);
strcpy(path_write_text, path_read);
for (int i = 0; i < length; i++)
if (path_read[i] == '.')
{
path_write_text[i + 1] = 't';
path_write_text[i + 2] = 'x';
path_write_text[i + 3] = 't';
break;
}
codes = FileToHuffmanCodes(read, &codes,&NumberOfBits,&overflow);
fclose(read);
printcodes(codes);
printf("\nenter the relative path if the .com file that contains the path to the huffman string >> ");
gets_s(path_read, 100);
read = fopen(path_read, "r");
HuffmanString = FileToHuffmanString(read, overflow, NumberOfBits);
fclose(read);
write_txt = fopen(path_write_text, "w");
DecodeHuffmanString(write_txt, HuffmanString, codes, overflow, NumberOfBits);
free(HuffmanString);
codes = Terminatelist(codes);
fclose(write_txt);
printf("\nDecompression sucessfull !!!\n .txt file name : %s", path_write_text);
}
else if (selection == 3)
return 0;
else
{
printf("\nPlease enter a valid input");
do
{
stopper = getchar();
} while (stopper);
}
}
return 0;
}
PriorityQueue* InitiateQueue()
{
PriorityQueue* Nodes = (PriorityQueue*)malloc(sizeof(PriorityQueue));
Nodes->Queue = NULL;
Nodes->size = 0;
return Nodes;
}
PriorityQueue* enqueue(PriorityQueue* head, tree* data)
{
queue* itr = head->Queue;
queue* node = (queue*)malloc(sizeof(queue));
node->node = data;
node->next = NULL;
if (itr == NULL)
{
head->size = 1;
head->Queue = node;
return head;
}
if(itr->next == NULL)
if (data->frequency < itr->node->frequency)
{
node->next = itr;
head->Queue = node;
head->size = 2;
return head;
}
else
{
itr->next = node;
head->size = 2;
return head;
}
while (itr->next->next != NULL)
if (itr->node->frequency > data->frequency)
{
node->next = itr->next;
itr->next = node;
head->size++;
return head;
}
else
itr = itr->next;
node->next = NULL;
itr->next->next = node;
head->size++;
return head;
}
PriorityQueue* dequeue(PriorityQueue* head)
{
if (head->Queue == NULL)
{
head->size = 0;
return NULL;
}
head->Queue = head->Queue->next;
head->size--;
return head;
}
tree* peek(PriorityQueue* head)
{
return head->Queue->node;
}
PriorityQueue* TerminateQueue(PriorityQueue* head)
{
if (head->Queue == NULL)
{
head->size = 0;
return head;
}
queue* tmp = head->Queue;
head->Queue = head->Queue->next;
head->size--;
free(tmp);
return TerminateQueue(head);
}
HuffmanCodesList* InitaiteList()
{
HuffmanCodesList* codes = (HuffmanCodesList*)malloc(sizeof(HuffmanCodesList));
codes->data = NULL;
codes->next = NULL;
return codes;
}
HuffmanCodes* InitiateNode()
{
HuffmanCodes* node = (HuffmanCodes*)malloc(sizeof(HuffmanCodes));
node->code = (int*)malloc(sizeof(int)* MAX_DEPTH);
return node;
}
HuffmanCodes* TerminateNode(HuffmanCodes* head)
{
free(head->code);
free(head);
return NULL;
}
HuffmanCodesList* add(HuffmanCodesList* head, HuffmanCodes* data)
{
if (head->data == NULL)
{
head->data = data;
return head;
}
HuffmanCodesList* tmp = (HuffmanCodesList*)malloc(sizeof(HuffmanCodesList));
tmp->data = data;
tmp->next = NULL;
HuffmanCodesList* itr = head;
while (itr->next != NULL)
itr = itr->next;
itr->next = tmp;
return head;
}
HuffmanCodes* searchwithCharacter(HuffmanCodesList* head,char character)
{
HuffmanCodesList* itr = head;
HuffmanCodes* out = InitiateNode();
while (itr != NULL)
if (itr->data->character == character)
{
out->character = character;
for (int i = 0; i < MAX_DEPTH; i++)
out->code[i] = itr->data->code[i];
return out;
}
else
itr = itr->next;
return NULL;
}
HuffmanCodes* searchwithcode(HuffmanCodesList* head, int* code)
{
HuffmanCodesList* itr = head;
int flag = 1;
HuffmanCodes* out = InitiateNode();
while (itr != NULL)
{
for (int i = 0; i < MAX_DEPTH; i++)
if (itr->data->code[i] != code[i])
flag = 0;
if (flag == 1)
{
out->character = itr->data->character;
for (int i = 0; i < MAX_DEPTH; i++)
out->code[i] = itr->data->code[i];
return out;
}
else
itr = itr->next;
flag = 1;
}
return NULL;
}
HuffmanCodesList* Terminatelist(HuffmanCodesList* head)
{
if (head == NULL)
return head;
HuffmanCodesList* tmp = head;
head = head->next;
tmp->data = TerminateNode(tmp->data);
return Terminatelist(head);
}
PriorityQueue* readfile(PriorityQueue* head, FILE* ptr){
int ASCII[255];
tree* input;
for (int i = 0; i < 255; i++)
ASCII[i] = 0;
for (char character = fgetc(ptr); character != EOF; character = fgetc(ptr))
ASCII[character]++;
for (int i = 0; i < 255; i++)
if (ASCII[i] > 0)
{
input = (tree*)malloc(sizeof(tree));
input->character = (char)i;
input->frequency = ASCII[i];
input->left = NULL;
input->right = NULL;
head = enqueue(head, input);
}
return head;
}
tree* BuildHuffmanTree(PriorityQueue* Frequencies)
{
tree* result = NULL;
//extract the last remaining huffman tree from the priority queue when all the subtrees have been attached together
if (Frequencies->size == 1)
{
result = peek(Frequencies);
Frequencies->Queue->node = NULL;
free(Frequencies);
return result;
}
result = (tree*)malloc(sizeof(tree));
//dequeue the 2 subtrees with lowest frequencies
tree* node1 = peek(Frequencies);
dequeue(Frequencies);
tree* node2 = peek(Frequencies);
dequeue(Frequencies);
//add them together to a parent node
result->left = node1;
result->right = node2;
result->frequency = node1->frequency + node2->frequency; //parent frequency = child 1 + child 2
result->character = NULL;
//enqueue the parent node and reccur
enqueue(Frequencies, result);
return BuildHuffmanTree(Frequencies);
}
void TerminateTree(tree* root)
{
if (root == NULL)
return;
TerminateTree(root->left);
TerminateTree(root->right);
free(root);
root = NULL;
}
HuffmanCodesList* savecodes(tree *haffmantree, HuffmanCodesList** list)
{
int arr[MAX_DEPTH], top = 0;
for (int i = 0; i < MAX_DEPTH; i++)
arr[i] = 2;
getcodes(haffmantree, arr, top, list);
return *list;
}
int getcodes(tree* root,int arr[MAX_DEPTH], int top,HuffmanCodesList** head)
{
if (root->left != NULL) {
// Assign 0 to left edge and recur
arr[top] = 0;
getcodes(root->left, arr,top + 1, head);
}
if (root->right != NULL) {
// Assign 1 to right edge and recur
arr[top] = 1;
getcodes(root->right, arr,top + 1, head);
}
// Add character and huffman code to the huffman codes list
if (root->right == NULL && root->left == NULL)
{
HuffmanCodes* node = InitiateNode();
node->character = root->character;
for (int i = 0; i < MAX_DEPTH; i++)
node->code[i] = arr[i];
*head = add(*head, node);
}
}
int printcodes(HuffmanCodesList* codes)
{
printf("\nHuffman codes:-");
for (HuffmanCodesList* itr = codes; itr != NULL; itr = itr->next)
{
if(itr->data->character != '\n')
printf("\ncharacter : %c | code : ", itr->data->character);
else
printf("\ncharacter : \\n | code : ");
for (int i = 0; i < MAX_DEPTH; i++)
if(itr->data->code[i] == 0 || itr->data->code[i] == 1)
printf("%d", itr->data->code[i]);
}
return 1;
}
int HuffmanStringToFile(int* HuffmanString, FILE* write, int NumberOfBits)
{
//write the huffman string to file 8 bits at a time
printf("\n");
int Buffer[8] = {2,2,2,2,2,2,2,2}; //buffer to store the 8 bits to be written
// calculate buffer over flow
int overflow = 8 - NumberOfBits % 8;
unsigned char character;
int start = 0;
int counter = 0;
//write binary 0 to the file to adjust the number of bits to be divisable by 8
if (overflow != 8)
{
for (int i = 0; i < overflow; i++)
Buffer[i] = 0;
for (start = overflow; start < 8; start++)
Buffer[start] = HuffmanString[counter++];
character = (unsigned char)BinaryToDecimal(Buffer);
fprintf(write, "%c", character);
}
start = 0;
//write the huffman string to the file 8 bits per loop
for (int i = counter; i <= NumberOfBits + overflow; i++)
{
Buffer[start++] = HuffmanString[i];
if (start % 8 == 0)
{
start = 0;
character = (unsigned char)BinaryToDecimal(Buffer);
fprintf(write, "%c", character);
}
}
return 1;
}
int* PrinfHuffmanString(FILE* read, HuffmanCodesList* codes, int* NumberOfBits)
{
printf("\n");
int* buffer = NULL;
int bufferCounter = 0;
rewind(read);
HuffmanCodes* tmp = NULL;
//printf huffman string on the screen and return it as an array of integers
for (char character = fgetc(read); !feof(read); character = fgetc(read))
{
tmp = searchwithCharacter(codes, character);
for (int i = 0; tmp->code[i] == 1 || tmp->code[i] == 0; i++)
{
buffer = (int*)realloc(buffer, sizeof(int) * (bufferCounter + 1));
buffer[bufferCounter++] = tmp->code[i];
printf("%d", tmp->code[i]);
*NumberOfBits = *NumberOfBits + 1;
}
}
printf("\nNumber of bits : %d\n", *NumberOfBits);
return buffer;
}
int HuffmanCodesToFile( FILE* write, HuffmanCodesList* codes,int bits)
{
fprintf(write,"%d\n%d\n",bits,(bits%8 != 0)? 8 - bits%8 : 0);
for (HuffmanCodesList* itr = codes; itr != NULL; itr = itr->next)
{
fprintf(write, "%c", itr->data->character);
for (int i = 0; i < MAX_DEPTH; i++)
if (itr->data->code[i] == 1)
fprintf(write, "1");
else if (itr->data->code[i] == 0)
fprintf(write, "0");
fprintf(write, "\n");
}
return 1;
}
HuffmanCodesList* FileToHuffmanCodes(FILE* read, HuffmanCodesList** codes, int* bits, int* overflow)
{
rewind(read);
int counter = 0;
HuffmanCodes* tmp = (HuffmanCodes*)malloc(sizeof(HuffmanCodes));
tmp->code = (int*)malloc(sizeof(int) * MAX_DEPTH);
fscanf(read, "%d\n", bits);
fscanf(read, "%d\n", overflow);
for (char character = fgetc(read); character != EOF; character = fgetc(read))
{
if (counter == 0)
{
if(character != '~')
tmp->character = character;
else
tmp->character = '\n';
}
if (counter > 0)
tmp->code[counter - 1] = character - 48;
if (character == '\n' && counter != 0)
{
for (int i = counter - 1; i < MAX_DEPTH; i++)
tmp->code[i] = 2;
counter = 0;
*codes = add(*codes, tmp);
tmp = InitiateNode();
continue;
}
counter++;
}
return *codes;
}
int power(int x, int y)
{
if (y == 0)
return 1;
return x * power(x, y - 1);
}
int BinaryToDecimal(int binary[8])
{
int x;
int weigth = 7;
int decimal = 0;
for (int i = 0; i < 8; i++)
{
x = binary[i];
if(binary[i] == 1)
decimal += power(2, weigth);
weigth--;
}
return decimal;
}
int DecimalToBinary(int Decimal,int Binary[8])
{
for(int i = 7; i >= 0;i--)
{
Binary[i] = Decimal % 2;
Decimal = Decimal / 2;
}
return 1;
}
int* FileToHuffmanString(FILE* read, int overflow, int NumberOfBits)
{
rewind(read);
int* Buffer = (int*)malloc(sizeof(int)* 8);
int counter = 0;
int flag = 0;
unsigned char character;
int tmp[8];
for (fscanf(read,"%c",&character); flag <= (NumberOfBits + overflow / 8); fscanf(read, "%c", &character))
{
if(counter != 0)
Buffer = (int*)realloc(Buffer, sizeof(int) * (counter + 1) * 8);
DecimalToBinary((int)character,tmp);
for (int i = 0; i < 8; i++)
Buffer[counter * 8 + i] = tmp[i];
counter++;
flag++;
}
printf("\nHaffman String : ");
for (int i = 0; i <= NumberOfBits; i++)
if (Buffer[i] == 0 || Buffer[i] == 1)
printf("%d", Buffer[i]);
return Buffer;
}
int DecodeHuffmanString(FILE* write, int* HuffmanString,HuffmanCodesList* codes, int overflow, int NumberOfBits)
{
printf("\nText after decoding :\n");
printf("---------------------\n");
int buffer[MAX_DEPTH];
for (int i = 0; i < MAX_DEPTH; i++)
buffer[i] = 2;
int counter = 0;
HuffmanCodes* tmp;
for (int i = overflow; i <= NumberOfBits + overflow; i++)
{
buffer[counter++] = HuffmanString[i];
tmp = searchwithcode(codes,buffer);
if (tmp != NULL)
{
printf("%c", tmp->character);
fprintf(write, "%c", tmp->character);
for (int i = 0; i < MAX_DEPTH; i++)
buffer[i] = 2;
counter = 0;
}
}
printf("\n");
return 1;
}