-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpf_ft_wchar_to_char.c
78 lines (72 loc) · 2.53 KB
/
pf_ft_wchar_to_char.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pf_ft_wchar_to_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: idemchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 21:50:01 by idemchen #+# #+# */
/* Updated: 2017/02/17 21:49:37 by idemchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
/*
** 0xxxxxxx // 0x00
** No action
** 110xxxxx 10xxxxxx // 0xC0 0x80
** I move 6 on the right and add 11000000
** I filter by 111111 and add 10000000
** 1110xxxx 10xxxxxx 10xxxxxx // 0xE0 0x80 0x80
** I move 12 on the right and add 11100000
** I move 6, I filter by 111111 and I add 10000000
** I filter by 111111 and I add 10000000
** 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx // 0xF0 0x80 0x80 0x80
** I move 18 on the right and add 11110000
** I move 12 on the right, I filter by 111111 and I add 10000000
** I move 6 on the right, I filter by 111111 and I add 10000000
** I filter by 111111 and add 10000000
*/
int pf_ft_wchar_str_initializer(wchar_t wchar, int counter, char *new)
{
short length;
length = pf_ft_wchar_length(wchar);
if (length == 1)
new[counter++] = wchar;
else if (length == 2)
{
new[counter++] = (wchar >> 6) + 0xC0;
new[counter++] = (wchar & 0x3F) + 0x80;
}
else if (length == 3)
{
new[counter++] = (wchar >> 12) + 0xE0;
new[counter++] = ((wchar >> 6) & 0x3F) + 0x80;
new[counter++] = (wchar & 0x3F) + 0x80;
}
else
{
new[counter++] = (wchar >> 18) + 0xF0;
new[counter++] = ((wchar >> 12) & 0x3F) + 0x80;
new[counter++] = ((wchar >> 6) & 0x3F) + 0x80;
new[counter++] = (wchar & 0x3F) + 0x80;
}
return (counter);
}
char *pf_ft_wchar_to_char(wchar_t *wstr)
{
char *new;
int counter;
int iterator;
int length;
if (!wstr)
return (0);
counter = 0;
iterator = 0;
length = pf_ft_wchar_length_inbyte(wstr);
if (!(new = (char*)malloc(sizeof(char) * length)))
return (NULL);
new[length] = '\0';
while (wstr[iterator])
counter = pf_ft_wchar_str_initializer(wstr[iterator++], counter, new);
return (new);
}