-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
75 lines (59 loc) · 1.38 KB
/
main.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 <string.h>
#include <stdlib.h>
#include "list.h"
#include "encoding.h"
#define ASCII_SIZE 256
struct stream {
unsigned char* string;
unsigned int size;
unsigned int freq[ASCII_SIZE];
};
void read_stream (struct stream * myStream, unsigned char character)
{
myStream->size++;
myStream->string = (unsigned char*) realloc (myStream->string, myStream->size * sizeof (unsigned char));
myStream->string[myStream->size - 1] = character;
myStream->freq[character]++;
}
int main ( )
{
FILE* file = fopen ("test.txt", "rb");
FILE* pict = fopen ("snake.jpeg", "rb");
FILE* copy = fopen ("descubra.jpg", "wb");
int i;
unsigned char character;
struct stream myStream;
myStream.string = NULL;
for (i = 0; i < ASCII_SIZE; ++i)
{
myStream.freq[i] = 0;
}
myStream.size = 0;
while (fscanf (pict, "%c", &character) != EOF)
{
read_stream (&myStream, character);
}
HUFFLIST* myList = create_linked_list ();
for (i = 0; i < ASCII_SIZE; ++i)
{
if (myStream.freq[i])
{
insert_with_priority (myList, create_node(i, myStream.freq[i]));
}
}
while (myList->size > 1)
{
insert_with_priority (myList, merge (remove_head(myList), remove_head(myList)));
}
for(i = 0;i < myStream.size;i++)
{
fprintf(copy, "%c", myStream.string[i]);
}
//print_pre_order (myList->head);
//printf("\n");
fclose(file);
fclose(copy);
fclose(pict);
return 0;
}