-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_printf_handle.c
97 lines (93 loc) · 3.17 KB
/
ft_printf_handle.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_handle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smonroe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/08/05 18:40:28 by smonroe #+# #+# */
/* Updated: 2018/08/27 17:17:33 by smonroe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_uni width(t_var v, va_list ap)
{
if (v.wide[1] == 'h')
v.uni.c = va_arg(ap, int);
else if (v.wide[0] == 'h')
v.uni.sh = va_arg(ap, int);
else if (ft_strchr("ljz", v.wide[0]) && ft_strchr("id", v.flag))
v.uni.l = va_arg(ap, int64_t);
else if (ft_strchr("ljz", v.wide[0]) && ft_strchr("uUxXoOb", v.flag))
v.uni.ul = va_arg(ap, uint64_t);
else if (ft_strchr("idcC", v.flag))
v.uni.i = va_arg(ap, int32_t);
else if (ft_strchr("uxXob", v.flag))
v.uni.ui = va_arg(ap, uint32_t);
else if (v.flag == 'D')
v.uni.l = va_arg(ap, int64_t);
else if (v.flag == 'U' || v.flag == 'O')
v.uni.ul = va_arg(ap, uint64_t);
else if (v.flag == 's')
v.uni.s = va_arg(ap, char *);
else if (v.flag == 'S')
v.uni.w = va_arg(ap, int *);
else if (v.flag == 'p')
v.uni.v = va_arg(ap, void *);
else if (v.flag == '%')
v.uni.c = '%';
return (v.uni);
}
static char *type_field_norm(t_var v)
{
if (ft_strchr("xobu", v.flag))
v.str = ft_itoa_base_ubig(v.uni.ui, v.base, 'l');
else if (v.flag == 'X')
v.str = ft_itoa_base_ubig(v.uni.ui, v.base, 'u');
else if (v.flag == 'i' || v.flag == 'd')
v.str = ft_itoa_base(v.uni.i, v.base, 'u');
else if (v.flag == 'U' || v.flag == 'O')
v.str = ft_itoa_base_ubig(v.uni.ul, v.base, 'l');
else if (v.flag == 'D')
v.str = ft_itoa_base_big(v.uni.l, v.base, 'l');
else if (v.flag == 'p')
v.str = ft_strappfr("0x", ft_itoa_base_ubig(v.uni.ul, 16, 'l'));
else if (v.flag == 'c' || v.flag == '%')
{
v.str = ft_strnew(2);
v.str[0] = (v.flag == '%') ? '%' : v.uni.c;
if (!v.str[0])
{
write(1, v.str, 1);
return (v.str);
}
}
else if (v.flag == 's')
v.str = ft_strdup((v.uni.s) ? v.uni.s : "(null)");
return (printer(v));
}
char *type_field_wide(t_var v, va_list ap)
{
v.uni = width(v, ap);
if (v.wide[0])
{
if (ft_strchr("XuUoOb", v.flag))
v.str = ft_itoa_base_ubig(v.uni.ul, v.base, 'u');
else if (ft_strchr("idD", v.flag))
v.str = ft_itoa_base_big(v.uni.l, v.base, 'l');
else if (v.flag == 'x')
v.str = ft_itoa_base_ubig(v.uni.ul, v.base, 'l');
else if (v.flag == 's')
v.str = ft_wider(v.uni.w);
else
v.str = ft_strdup("WATBOI");
return (printer(v));
}
if (v.flag == 'C')
v.str = ft_wide(v.uni.i);
else if (v.flag == 'S')
v.str = ft_wider(v.uni.w);
else
return (type_field_norm(v));
return (printer(v));
}