-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_handle_numbers.c
99 lines (91 loc) · 2.56 KB
/
ft_handle_numbers.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_handle_numbers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ptroger <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/07 12:27:29 by ptroger #+# #+# */
/* Updated: 2020/02/08 20:23:32 by ptroger ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_handle_unsigned(unsigned int nb, struct s_val *val,
struct s_opt *opt, const char format)
{
char *str;
if (nb == 0 && val->precision == 0 && opt->dot == '.')
{
handle_zero_zero(val, opt, format);
val->result--;
return ;
}
str = ft_unsigned_itoa(nb);
ft_handle_nb(val, opt, 1, str);
free(str);
return ;
}
void handle_zero_zero(struct s_val *val, struct s_opt *opt,
const char format)
{
if (format == 'p' && opt->negative)
{
if (opt->dot == '.')
val->result += ft_putstr("0x", val, opt, format);
else
val->result += ft_putstr("0x0", val, opt, format);
}
while (val->width > 0)
{
val->width--;
val->result += ft_putchar(' ');
}
if (format == 'p' && !opt->negative)
{
if (opt->dot == '.')
val->result += ft_putstr("0x", val, opt, format);
else
val->result += ft_putstr("0x0", val, opt, format);
}
if (format == 'd' || format == 'i')
val->result--;
}
char *ft_handle_int(int nb)
{
char *str;
int neg;
neg = 1;
if (nb < 0)
neg = -1;
if (nb == -2147483648)
str = ft_strdup("2147483648");
else
str = ft_itoa(nb * neg);
return (str);
}
void ft_handle_numbers(const char format, va_list tab, struct s_val *val,
struct s_opt *opt)
{
char *str;
int neg;
int nb;
neg = 1;
if (format == 'u')
{
nb = va_arg(tab, unsigned int);
ft_handle_unsigned(nb, val, opt, format);
reset_opt(val, opt);
return ;
}
nb = va_arg(tab, int);
if (nb == 0 && val->precision == 0 && opt->dot == '.')
handle_zero_zero(val, opt, format);
if (format == 'u' || (nb == 0 && val->precision == 0 && opt->dot == '.'))
return ;
if (nb < 0)
neg = -1;
str = ft_handle_int(nb);
ft_handle_nb(val, opt, neg, str);
free(str);
reset_opt(val, opt);
}