-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_parse.c
98 lines (92 loc) · 2.57 KB
/
ft_parse.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
#include "ft_printf.h"
void ft_check_flag(char *str, va_list ap, t_tab *tab)
{
if (str[tab->i] == '*')
{
if (tab->precision_parsing)
tab->precision_width = va_arg(ap, int);
else
tab->width = va_arg(ap, int);
}
if (str[tab->i] == '0' && !tab->minus && !tab->precision_parsing)
tab->zero = 1;
if (str[tab->i] > '0' && str[tab->i] <= '9' && !tab->precision_parsing)
tab->width = ft_atoi_printf(str, &tab->i);
if (str[tab->i] >= '0' && str[tab->i] <= '9' && tab->precision_parsing)
tab->precision_width = ft_atoi_printf(str, &tab->i);
tab->precision_parsing = 0;
if (str[tab->i] == '-')
{
tab->zero = 0;
tab->minus = 1;
}
str[tab->i] == '.' ? ft_set_precision(tab) : 0;
str[tab->i] == ' ' ? tab->space = 1 : 0;
str[tab->i] == '+' ? tab->plus = 1 : 0;
str[tab->i] == '#' ? tab->sharp = 1 : 0;
str[tab->i] == 'l' ? tab->l_count += 1 : 0;
str[tab->i] == 'h' ? tab->h_count += 1 : 0;
}
int ft_parse2(char *str, va_list ap, t_tab *tab)
{
char *sp;
char *c;
while (!ft_is_flag(str[tab->i]))
{
ft_check_flag(str, ap, tab);
if (str[tab->i + 1] == '\0')
return (0);
if (!ft_is_from_pf(str[tab->i + 1]))
{
c = ft_c_to_str(str[tab->i + 1]);
tab->len = 1;
(tab->minus) ? ft_add_to_buff(tab, c, 1) : 0;
sp = ft_print_sp(tab);
ft_add_to_buff(tab, sp, tab->sp_len);
(!tab->minus) ? ft_add_to_buff(tab, c, 1) : 0;
free(c);
free(sp);
tab->i++;
return (0);
}
tab->i++;
}
return (1);
}
size_t ft_is_flag(char c)
{
return (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i'
|| c == 'u' || c == 'x' || c == 'n' || c == 'X' || c == '%');
}
size_t ft_is_from_pf(char c)
{
return (ft_is_flag(c) || (c >= '0' && c <= '9')
|| c == '-' || c == ' ' || c == '.' || c == '+' || c == '#' || c == '*'
|| c == 'l' || c == 'h');
}
void ft_parse(char *str, va_list ap, t_tab *tab)
{
tab->i++;
ft_reset_flags(tab);
if (!ft_parse2(str, ap, tab))
return ;
if (tab->width < 0)
{
tab->minus = 1;
tab->zero = 0;
tab->width = -tab->width;
}
if (tab->precision_width < 0)
tab->precision = 0;
(tab->precision && tab->precision_width >= 0) ? tab->zero = 0 : 0;
tab->conv = str[tab->i];
(str[tab->i] == 'c') ? ft_conv_c(ap, tab) : 0;
(str[tab->i] == 's') ? ft_conv_str(ap, tab) : 0;
(str[tab->i] == 'p') ? ft_conv_p(ap, tab) : 0;
(str[tab->i] == 'd' || str[tab->i] == 'i') ? ft_conv_int(ap, tab) : 0;
(str[tab->i] == 'u') ? ft_conv_uint(ap, tab) : 0;
(str[tab->i] == 'x') ? ft_conv_x(ap, tab) : 0;
(str[tab->i] == 'X') ? ft_conv_x(ap, tab) : 0;
(str[tab->i] == '%') ? ft_conv_c(ap, tab) : 0;
(str[tab->i] == 'n') ? ft_conv_n(ap, tab) : 0;
}