-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_parser.c
62 lines (57 loc) · 2.09 KB
/
ft_parser.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrosie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/19 12:29:44 by mrosie #+# #+# */
/* Updated: 2020/11/24 16:02:44 by mrosie ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_flags_handler(const char *str, char flag, t_spec *spec)
{
spec->flag = flag;
while (*str == '-' || *str == '0')
{
spec->flag = (*str == '-') ? *str : flag;
spec->offset++;
str++;
}
return (spec->offset);
}
static int ft_mods_handler(const char *str, int *mod, t_spec *spec)
{
int offset;
offset = 0;
*mod = ft_atoi(str);
while (ft_isdigit(str[offset]))
offset++;
spec->offset += offset;
return (offset);
}
int ft_parser(const char *str, t_spec *spec, va_list ar)
{
if (*str == '-' || *str == '0')
str += ft_flags_handler(str, *str, spec);
if (ft_isdigit(*str))
str += ft_mods_handler(str, &(spec->width), spec);
else if (*str == '*' && (spec->offset += 1))
{
spec->width = va_arg(ar, int);
spec->flag = (spec->width < 0) ? '-' : spec->flag;
spec->width = (spec->width < 0) ? spec->width * -1 : spec->width;
str++;
}
if (*str == '.' && str++ && (spec->precision = 0) == 0)
spec->offset++;
if (ft_isdigit(*str))
str += ft_mods_handler(str, &(spec->precision), spec);
else if (*str == '*' && str++ && spec->offset++)
{
spec->precision = va_arg(ar, int);
spec->precision = (spec->precision < -1) ? -1 : spec->precision;
}
return (ft_processor(*str, spec, ar));
}