-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_decimal.c
More file actions
90 lines (84 loc) · 2.46 KB
/
ft_decimal.c
File metadata and controls
90 lines (84 loc) · 2.46 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_decimal.c :+: :+: */
/* +:+ */
/* By: oswin <oswin@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/02/12 13:41:44 by oswin #+# #+# */
/* Updated: 2021/02/17 22:51:47 by obult ######## odam.nl */
/* */
/* ************************************************************************** */
#include "printf.h"
int ft_prepdi(va_list *ap, char **format)
{
t_prep n;
n.right = 0;
n.width = ft_width(*format + 1, ap, &(n.right));
n.precision = ft_precision(*format, ap);
n.nbr = va_arg(*ap, int);
n.usi = (unsigned int)n.nbr;
n.min = 0;
n.zero = 32;
ft_prepdi_helper(&n);
if ((*format)[1] == '0')
n.zero = 48;
if ((*format)[1] == '-' || (*format)[2] == '-')
n.right = 1;
if (n.precision == 0 && n.usi == 0)
{
n.len = 0;
n.superiorlen = 0;
}
return (ft_putdi(n));
}
void ft_prepdi_helper(t_prep *p)
{
if (p->nbr < 0)
{
p->len = 1 + ft_writelen_b((size_t)p->nbr * -1, 10);
p->min = 1;
p->superiorlen = p->len;
if (p->precision + 1 > p->len)
p->superiorlen = p->precision + 1;
}
else
{
p->len = ft_writelen_b(p->nbr, 10);
p->superiorlen = p->len;
if (p->precision > p->len)
p->superiorlen = p->precision;
}
}
int ft_putdi(t_prep info)
{
if (info.min)
return (ft_putnegative(info));
return (ft_putpositive(info, "0123456789"));
}
int ft_putnegative(t_prep info)
{
if (info.precision >= 0)
{
if (!info.right)
ft_putwidth(info.width - info.superiorlen, ' ');
ft_putchar('-');
ft_putwidth(info.precision - info.len + 1, '0');
}
else
{
if (!info.right)
{
if (info.zero == 48)
ft_putchar('-');
ft_putwidth(info.width - info.len, info.zero);
}
}
if ((info.zero != 48 && info.precision < 0)
|| (info.right && info.precision < 0))
ft_putchar('-');
ft_putnbr_b((unsigned int)(-1 * info.nbr), "0123456789", 10);
if (info.right)
ft_putwidth(info.width - info.superiorlen, ' ');
return (ft_retour(info.superiorlen, info.width));
}