-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_convert_int.c
83 lines (77 loc) · 1.97 KB
/
ft_convert_int.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
#include "ft_printf.h"
void ft_size_int(va_list ap, t_tab *tab)
{
if (tab->l_count >= 2)
tab->n = (intmax_t)va_arg(ap, long long);
else if (tab->l_count == 1)
tab->n = (intmax_t)va_arg(ap, long);
else if (tab->h_count && ((tab->h_count % 2) == 0))
tab->n = (intmax_t)((char)va_arg(ap, int));
else if (tab->h_count && ((tab->h_count % 2) != 0))
tab->n = (intmax_t)((short)va_arg(ap, int));
else
tab->n = (intmax_t)va_arg(ap, int);
}
void ft_size_u(va_list ap, t_tab *tab)
{
if (tab->l_count >= 2)
tab->u = (uintmax_t)va_arg(ap, unsigned long long);
else if (tab->l_count == 1)
tab->u = (uintmax_t)va_arg(ap, unsigned long);
else if (tab->h_count && ((tab->h_count % 2) == 0))
tab->u = (uintmax_t)((unsigned char)va_arg(ap, int));
else if (tab->h_count && ((tab->h_count % 2) != 0))
tab->u = (uintmax_t)((unsigned short)va_arg(ap, int));
else
tab->u = (uintmax_t)va_arg(ap, unsigned int);
}
void ft_conv_int(va_list ap, t_tab *tab)
{
char *str;
char *sp;
tab->is_int = 1;
ft_size_int(ap, tab);
str = itoa_printf(tab->n);
tab->len = ft_intlen(tab->n);
str = ft_num_precision(str, tab);
tab->len = ft_strlen(str);
(tab->n < 0) ? tab->len++ : 0;
(tab->n >= 0) && (tab->plus || tab->space) ? tab->len++ : 0;
if (tab->n == 0 && tab->precision && tab->precision_width == 0
&& !tab->width)
{
free(str);
return ;
}
if (tab->n == 0 && tab->precision && tab->precision_width == 0)
{
free(str);
str = ft_strdup(" ");
}
sp = ft_print_sp(tab);
ft_join_all(str, sp, tab);
}
void ft_conv_uint(va_list ap, t_tab *tab)
{
char *str;
char *sp;
tab->is_int = 1;
ft_size_u(ap, tab);
str = ft_uitoa(tab->u);
tab->len = ft_strlen(str);
str = ft_num_precision(str, tab);
tab->len = ft_strlen(str);
if (tab->u == 0 && tab->precision && tab->precision_width == 0
&& !tab->width)
{
free(str);
return ;
}
if (tab->u == 0 && tab->precision && tab->precision_width == 0)
{
free(str);
str = ft_strdup(" ");
}
sp = ft_print_sp(tab);
ft_join_all(str, sp, tab);
}