-
Notifications
You must be signed in to change notification settings - Fork 0
/
flaghandlers.c
80 lines (74 loc) · 2.1 KB
/
flaghandlers.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* flaghandlers.c :+: :+: */
/* +:+ */
/* By: rcappend <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/12/04 15:01:13 by rcappend #+# #+# */
/* Updated: 2020/12/10 10:02:50 by rcappend ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void t_track_init(t_track *c)
{
c->value = NULL;
c->type = 0;
c->lalign = FALSE;
c->space = ' ';
c->width = -1;
c->prec = -1;
}
/*
** Reads the width or precision flag.
*/
static int wi_pr_reader(int *ret, const char *str, va_list args, int i)
{
if (str[i] == '*')
{
*ret = va_arg(args, int);
i++;
}
else if (ft_isdigit(str[i]))
{
*ret = ft_atoi(str + i);
while (ft_isdigit(str[i]))
i++;
}
return (i);
}
static int flag_reader(t_track *c, const char *str, int i)
{
while (str[i] == '-' || str[i] == '0')
{
if (str[i] == '-')
{
c->lalign = TRUE;
c->space = ' ';
}
else if (str[i] == '0' && c->lalign == FALSE)
c->space = '0';
i++;
}
return (i);
}
/*
** Reads the flags, width and precision for the special character.
** Finally, it returns (i) the amount of steps needed
** to parse over all the flags. Or 0 in case of an error.
*/
int flaghandler(t_track *c, const char *str, va_list args)
{
int i;
char *types;
types = "cspdiuxX%";
i = 1;
i = flag_reader(c, str, i);
i = wi_pr_reader(&c->width, str, args, i);
if (str[i] == '.')
i = wi_pr_reader(&c->prec, str, args, i + 1);
if (ft_strchr(types, str[i]) == NULL)
return (0);
c->type = str[i];
return (i + 1);
}