-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_unichar.c
82 lines (74 loc) · 1.98 KB
/
print_unichar.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_unichar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: imelnych <imelnych@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/27 13:34:41 by imelnych #+# #+# */
/* Updated: 2018/02/03 15:47:40 by imelnych ### ########.fr */
/* */
/* ************************************************************************** */
#include "printflib.h"
static int find_bit(int len)
{
if (len > 16)
return (4);
else if (len > 11)
return (3);
else if (len > 7)
return (2);
return (0);
}
static char *fill_first_mask(int bit)
{
int i;
char *first_mask;
i = 0;
first_mask = ft_strdup("00000000");
while (i < bit)
first_mask[i++] = '1';
return (first_mask);
}
static char *fill_sub_mask(char *str, int bit, int len, char *f_mask)
{
int i;
char *sub_mask;
int j;
char *res;
if (!(res = ft_strnew(bit)))
return (0);
i = bit - 1;
while (bit-- > 1)
{
sub_mask = ft_strdup("10000000");
j = 8;
while (j-- > 2 && len >= 0)
sub_mask[j] = str[--len];
res[i--] = (char)ft_atoibase(sub_mask);
free(sub_mask);
}
i = 0;
j = 8;
while (len--)
f_mask[--j] = str[len];
res[i] = (char)ft_atoibase(f_mask);
return (res);
}
char *print_unichar(int symb)
{
char *res;
char *str;
char *f_mask;
int bit;
int len;
bit = 0;
str = ft_itoabase(symb, 2, 'b');
len = (int)ft_strlen(str);
bit = find_bit(len);
f_mask = fill_first_mask(bit);
res = fill_sub_mask(str, bit, len, f_mask);
free(str);
free(f_mask);
return (res);
}