-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_encoded_string.c
66 lines (63 loc) · 2.17 KB
/
get_encoded_string.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_encoded_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fnacarel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/15 05:29:50 by fnacarel #+# #+# */
/* Updated: 2023/01/15 11:18:28 by fnacarel ### ########.fr */
/* */
/* ************************************************************************** */
#include "./encoder.h"
/*
It will set an dictionary where the numeric representation of an letter will
store it's frequency in the data received
e.g: "foo baarr" (int)'f' = 102; [102] = (Huffman code of char f)
str[100] it's just an auxiliary str to build the encoded value of the char
*/
void set_dict(t_min_heap_node *root, char **dict, char str[100], int top)
{
if (root->left)
{
str[top] = '0';
set_dict(root->left, dict, str, top + 1);
}
if (root->right)
{
str[top] = '1';
set_dict(root->right, dict, str, top + 1);
}
if (is_leaf_node(root))
{
dict[(int)root->c] = calloc(sizeof(char *), top + 1);
dict[(int)root->c] = strncpy(dict[(int)root->c], str, top);
}
}
unsigned char *get_encoded_string(char *str_to_encode, char **dict, int size)
{
int i;
char c;
unsigned char *encoded_str;
char **encoded_arr;
int total_len_of_strings;
i = 0;
total_len_of_strings = 0;
encoded_arr = calloc(sizeof(char *), size);
while (str_to_encode[i])
{
c = str_to_encode[i];
encoded_arr[i] = strdup(dict[(int)c]);
total_len_of_strings += strlen(encoded_arr[i]);
i++;
}
encoded_str = calloc(sizeof(unsigned char), total_len_of_strings + 1);
i = 0;
while (i < size - 1)
{
strcat((char *)encoded_str, encoded_arr[i]);
i++;
}
free_matr((void **)encoded_arr, size);
return (encoded_str);
}