-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_convert.c
101 lines (89 loc) · 2.21 KB
/
ft_convert.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
#include "ft_printf.h"
void ft_conv_str(va_list ap, t_tab *tab)
{
char *str;
char *sp;
if (tab->precision && tab->precision_width < 0)
tab->precision_width = -tab->precision_width;
if (!(str = va_arg(ap, char *)))
str = ft_strdup_l("(null)", tab);
else
str = ft_strdup_l(str, tab);
tab->len = ft_strlen(str);
sp = ft_print_sp(tab);
ft_join_all(str, sp, tab);
}
void ft_conv_c(va_list ap, t_tab *tab)
{
char *str;
char *sp;
str = NULL;
tab->len = 1;
(tab->conv == 'c') ? str = ft_c_to_str(va_arg(ap, int)) : 0;
(tab->conv == '%') ? str = ft_c_to_str('%') : 0;
sp = ft_print_sp(tab);
ft_join_all(str, sp, tab);
}
void ft_conv_n(va_list ap, t_tab *tab)
{
intmax_t *n;
if (tab->l_count >= 2)
n = (intmax_t *)va_arg(ap, long long *);
else if (tab->l_count == 1)
n = (intmax_t *)va_arg(ap, long *);
else if (tab->h_count && ((tab->h_count % 2) == 0))
n = (intmax_t *)((char *)va_arg(ap, int *));
else if (tab->h_count && ((tab->h_count % 2) != 0))
n = (intmax_t *)((short *)va_arg(ap, int *));
else
n = (intmax_t *)va_arg(ap, int *);
if (!n)
return ;
*n = tab->ret;
}
void ft_conv_x(va_list ap, t_tab *tab)
{
char *str;
char *sp;
str = NULL;
tab->is_int = 1;
ft_size_u(ap, tab);
(tab->conv == 'x') ? str = ft_itoa_base(tab->u, "0123456789abcdef") : 0;
(tab->conv == 'X') ? str = ft_itoa_base(tab->u, "0123456789ABCDEF") : 0;
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(" ");
}
(tab->sharp && tab->u) ? (tab->len += 2) : 0;
sp = ft_print_sp(tab);
ft_join_all(str, sp, tab);
}
void ft_conv_p(va_list ap, t_tab *tab)
{
char *str;
char *sp;
tab->u = va_arg(ap, long unsigned);
str = ft_itoa_base(tab->u, "0123456789abcdef");
(tab->precision) ? (tab->zero = 0) : 0;
tab->len = ft_strlen(str);
str = ft_num_precision(str, tab);
tab->len = ft_strlen(str) + 2;
if (tab->u == 0 && tab->precision && tab->precision_width == 0)
{
free(str);
str = ft_strdup("");
tab->len -= 1;
}
sp = ft_print_sp(tab);
ft_join_all(str, sp, tab);
}