-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_param_decimal.c
120 lines (109 loc) · 3.41 KB
/
ft_param_decimal.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_param_decimal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jraymond <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/31 18:59:02 by jraymond #+# #+# */
/* Updated: 2018/05/01 11:49:11 by jraymond ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void nb_c_add_pw(t_nbcaddpw *nbca, t_printf *elem, int intl, char *numb)
{
int i;
ft_bzero(nbca, sizeof(t_nbcaddpw));
if (elem->flags & PRECI)
nbca->preci = (intl >= elem->precision) ? 0 : elem->precision - intl;
i = nbca->preci;
if (elem->width)
{
if (elem->flags & MORE || elem->flags & SPACE || numb[0] == '-')
i++;
if (elem->width <= (i + intl))
nbca->width = 0;
else
nbca->width = elem->width - (i + intl);
}
}
void ft_handle_sign(t_printf *elem, t_nbcaddpw *nbca, char *numb)
{
char sp;
char more;
sp = ' ';
more = '+';
if (numb[0] == '-')
nbca->preci = (nbca->preci - 1) < 0 ? 0 : (nbca->preci - 1);
if (elem->flags & SPACE && numb[0] != '-')
{
ft_handle_overflow(elem, &sp, 1, 1);
nbca->width = (nbca->width - 1) < 0 ? 0 : (nbca->width - 1);
}
if (elem->flags & MORE && elem->flags & ZERO && numb[0] != '-')
{
ft_handle_overflow(elem, &more, 1, 1);
nbca->width = (nbca->width - 1) < 0 ? 0 : (nbca->width - 1);
elem->flags ^= MORE;
}
}
void ft_padding_numbposi(t_printf *elem, t_nbcaddpw *nbca, char *numb)
{
char sp;
char zero;
char more;
sp = ' ';
more = '+';
zero = '0';
if (elem->flags & ZERO || !(elem->flags & MINUS))
ft_posi_no_minus(elem, nbca, numb);
else
{
if (elem->flags & MORE)
ft_handle_overflow(elem, &more, 1, 1);
ft_handle_overflow(elem, &zero, nbca->preci, 1);
ft_handle_overflow(elem, numb, ft_strlen(numb), 2);
ft_handle_overflow(elem, &sp, nbca->width, 1);
}
}
void ft_padding_numbneg(t_printf *elem, t_nbcaddpw *nbca, char *numb)
{
char sp;
char zero;
sp = ' ';
zero = '0';
if (elem->flags & ZERO || !(elem->flags & MINUS))
ft_neg_no_minus(elem, nbca, numb);
else
{
ft_handle_overflow(elem, &numb[0], 1, 1);
ft_handle_overflow(elem, &zero, nbca->preci, 1);
numb++;
ft_handle_overflow(elem, numb, ft_strlen(numb), 2);
ft_handle_overflow(elem, &sp, nbca->width, 1);
}
}
int ft_param_decimal(t_printf *elem, va_list ap)
{
intmax_t arg;
char *numb;
t_nbcaddpw nbca;
arg = ft_handle_size(elem, ap);
numb = ft_lltoa(arg);
if (elem->flags & ZERO && (elem->flags & MINUS || elem->flags & PRECI))
elem->flags ^= ZERO;
(elem->flags & SPACE && elem->flags & MORE) ? elem->flags ^= SPACE : 0;
nb_c_add_pw(&nbca, elem, ilen(arg), numb);
ft_handle_sign(elem, &nbca, numb);
if (arg == 0 && elem->flags & PRECI && elem->precision == 0)
{
ft_padding_numbnull(elem, &nbca);
return (0);
}
if (numb[0] == '-')
ft_padding_numbneg(elem, &nbca, numb);
else
ft_padding_numbposi(elem, &nbca, numb);
ft_memdel((void**)&numb);
return (0);
}