-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwide_string.c
99 lines (91 loc) · 2.3 KB
/
wide_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wide_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nrepak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/07 15:53:50 by nrepak #+# #+# */
/* Updated: 2018/02/07 16:26:51 by nrepak ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static wchar_t *ft_precision(wchar_t *a, t_flags *f)
{
int size;
int b;
wchar_t *str;
b = 0;
while (b[a])
b++;
if (b)
size = ft_wstrlen(a) / b;
if (f->precision && f->precision < ft_wstrlen(a))
str = ft_strwsub(a, 0, f->precision / size);
else
str = ft_strwdup(a);
return (str);
}
static int ft_width(int len, wchar_t **str, t_flags *f)
{
if (f->minus)
{
ft_putwstr(*str);
ft_put_specific_char(' ', f->width - len);
}
else if (f->zero)
{
ft_put_specific_char('0', f->width - len);
ft_putwstr(*str);
}
else if (f->zero_precision)
ft_put_specific_char(' ', f->width);
else
{
ft_put_specific_char(' ', f->width - len);
ft_putwstr(*str);
}
free(*str);
return (f->width);
}
int ft_collect_for_wide_string(wchar_t *a, t_flags *f)
{
wchar_t *str;
int len;
str = ft_precision(a, f);
len = ft_wstrlen(str);
if (f->width && f->width > len)
return (ft_width(len, &str, f));
if (f->zero_precision)
{
if (f->zero)
ft_put_specific_char('0', f->width);
else
ft_put_specific_char(' ', f->width);
free(str);
return (f->width);
}
free(str);
return (ft_putwstr(str));
}
int ft_wide_string(va_list lst, t_flags *f)
{
wchar_t *a;
int i;
i = 0;
if (!(a = va_arg(lst, wchar_t*)))
{
ft_putstr("(null)");
return (6);
}
if (MB_CUR_MAX == 1)
{
while (a[i])
{
if (a[i] > 255)
return (-1);
i++;
}
}
return (ft_collect_for_wide_string(a, f));
}