-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putwchar.c
89 lines (81 loc) · 2.15 KB
/
ft_putwchar.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
83
84
85
86
87
88
89
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_putwchar.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: bpajot <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/01/18 14:23:00 by bpajot #+# ## ## #+# */
/* Updated: 2018/02/19 10:09:05 by bpajot ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_putwchar3(wchar_t c)
{
int i;
int ret;
ret = 0;
if (c > 0x7F)
{
i = 0xC0 + ((c >> 6) & 0x1F);
ret += write(1, &i, 1);
i = 0x80 + (c & 0x3F);
ret += write(1, &i, 1);
return (ret);
}
else if (c >= 0)
{
ret += write(1, &c, 1);
return (ret);
}
return (-1);
}
static int ft_putwchar2(wchar_t c)
{
int i;
int ret;
ret = 0;
if (c > 0x7FF && (c < 0xD800 || c > 0xDFFF))
{
i = 0xE0 + ((c >> 12) & 0xF);
ret += write(1, &i, 1);
i = 0x80 + ((c >> 6) & 0x3F);
ret += write(1, &i, 1);
i = 0x80 + (c & 0x3F);
ret += write(1, &i, 1);
return (ret);
}
return (ft_putwchar3(c));
}
int ft_putwchar(wchar_t c)
{
int i;
int ret;
ret = 0;
if (c > 0xFFFF)
{
i = 0xF0 + ((c >> 18) & 0x07);
ret += write(1, &i, 1);
i = 0x80 + ((c >> 12) & 0x3F);
ret += write(1, &i, 1);
i = 0x80 + ((c >> 6) & 0x3F);
ret += write(1, &i, 1);
i = 0x80 + (c & 0x3F);
ret += write(1, &i, 1);
return (ret);
}
return (ft_putwchar2(c));
}
int ft_wcharlen(wchar_t c)
{
if (c > 0xFFFF)
return (4);
else if (c > 0x7FF && (c < 0xD800 || c > 0xDFFF))
return (3);
else if (c > 0x7F)
return (2);
else if (c >= 0)
return (1);
return (-1);
}