-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_format.c
95 lines (85 loc) · 2.07 KB
/
parse_format.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lesantos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/05 09:54:55 by lesantos #+# #+# */
/* Updated: 2022/01/23 12:09:33 by lesantos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int parse_flags(const char *s, t_flags *flags)
{
int i;
i = 0;
while (1)
{
if (s[i] == '-')
flags->minus = 1;
else if (s[i] == '+')
flags->plus = 1;
else if (s[i] == '#')
flags->alternate = 1;
else if (s[i] == '0')
flags->zero = 1;
else if (s[i] == ' ')
flags->space = 1;
else
break ;
i++;
}
return (i);
}
int parse_width(const char *s, t_flags *flags)
{
int i;
i = 0;
while (s[i] >= '0' && s[i] <= '9')
{
flags->width *= 10;
flags->width += s[i] - '0';
i++;
}
return (i);
}
int parse_precision(const char *s, t_flags *flags)
{
int i;
i = 0;
if (*s == '.')
{
flags->precision = 0;
i++;
while (s[i] >= '0' && s[i] <= '9')
{
flags->precision *= 10;
flags->precision += s[i] - '0';
i++;
}
}
return (i);
}
void init_flags(t_flags *flags)
{
flags->zero = 0;
flags->plus = 0;
flags->minus = 0;
flags->space = 0;
flags->alternate = 0;
flags->precision = -1;
flags->width = 0;
}
int parse_format(const char *s, t_flags *flags)
{
int r;
init_flags(flags);
r = 0;
r += parse_flags(s, flags);
r += parse_width(s + r, flags);
r += parse_precision(s + r, flags);
if (flags->precision >= 0)
flags->zero = 0;
return (r);
}