-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_wchar.c
80 lines (73 loc) · 2.32 KB
/
handle_wchar.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_wchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gmelisan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/25 13:07:42 by gmelisan #+# #+# */
/* Updated: 2019/01/30 14:28:35 by gmelisan ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_wctomb_utf8_2(char *s, wchar_t wc)
{
s[1] = (wc & 0x3F) | 0x80;
s[0] = ((wc >> 6) & 0x1F) | 0xC0;
return (2);
}
static int ft_wctomb_utf8_3(char *s, wchar_t wc)
{
s[2] = (wc & 0x3F) | 0x80;
s[1] = ((wc >> 6) & 0x3f) | 0x80;
s[0] = ((wc >> 12) & 0xF) | 0xE0;
return (3);
}
static int ft_wctomb_utf8_4(char *s, wchar_t wc)
{
s[3] = (wc & 0x3F) | 0x80;
s[2] = ((wc >> 6) & 0x3F) | 0x80;
s[1] = ((wc >> 12) & 0x3F) | 0x80;
s[0] = ((wc >> 18) & 0x07) | 0xF0;
return (4);
}
int ft_wctomb_utf8(char *s, wchar_t wc)
{
if ((t_uint)wc <= 0x7f)
{
s[0] = wc;
return (1);
}
if (((t_uint)wc <= 0x9f &&
(t_uint)wc != 0x24 && (t_uint)wc != 0x40 && (t_uint)wc != 0x60) ||
((t_uint)wc >= 0xd800 && (t_uint)wc <= 0xdfff))
return (-1);
else if ((t_uint)wc <= 0x7FF)
return (ft_wctomb_utf8_2(s, wc));
else if ((t_uint)wc <= 0xFFFF)
return (ft_wctomb_utf8_3(s, wc));
else if ((t_uint)wc <= 0x10FFFF)
return (ft_wctomb_utf8_4(s, wc));
else
return (-1);
}
void handle_wchar(va_list ap, t_conversion *conv)
{
wint_t wc;
char s[5];
int wclen;
int len;
wc = (wint_t)va_arg(ap, wint_t);
ft_bzero(s, 5);
wclen = ft_wctomb_utf8(s, wc);
conv->outlen = wclen;
if (wclen == -1)
return ;
len = conv->width > (t_uint)wclen ? (int)conv->width : wclen;
conv->out = prepare_out(conv, len);
if (conv->flags.minus)
ft_memcpy(conv->out, s, wclen);
else
ft_memcpy(conv->out + (len - wclen), s, wclen);
conv->outlen = len;
}