-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_flags.c
More file actions
99 lines (92 loc) · 2.32 KB
/
Copy pathget_flags.c
File metadata and controls
99 lines (92 loc) · 2.32 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_flags.c :+: :+: */
/* +:+ */
/* By: rvan-sch <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/02/05 23:26:57 by rvan-sch #+# #+# */
/* Updated: 2020/02/05 23:27:04 by rvan-sch ######## odam.nl */
/* */
/* ************************************************************************** */
#include "header.h"
void ft_clear_flags(t_flag *flag)
{
flag->dot = 0;
flag->dash = 0;
flag->zero = 0;
flag->sign = 0;
flag->space = 0;
flag->hash = 0;
flag->mod_el = 0;
flag->mod_ha = 0;
flag->width = 0;
flag->prec = 0;
}
static int ft_isflag(t_flag *flag, char c)
{
if (c == '-')
flag->dash = 1;
if (c == '0')
flag->zero = 1;
if (c == '+')
flag->sign = 1;
if (c == ' ')
flag->space = 1;
if (c == '#')
flag->hash = 1;
if (c != '-' && c != '0' && c != '+' && c != ' ' && c != '#')
return (0);
return (1);
}
static int ft_ismod(t_flag *flag, char c)
{
if (c == 'l')
flag->mod_el++;
if (c == 'h')
flag->mod_ha++;
if (c != 'l' && c != 'h')
return (0);
return (1);
}
static int get_width_or_prec(const char *format, va_list arg, int *i)
{
int nbr;
nbr = 0;
if (format[*i] == '*')
{
nbr = va_arg(arg, int);
*i += 1;
}
else
while (format[*i] >= '0' && format[*i] <= '9')
{
nbr *= 10;
nbr += format[*i] - '0';
(*i)++;
}
return (nbr);
}
int get_flag_all(t_flag *flag, const char *format, va_list *arg, int i)
{
ft_clear_flags(flag);
i++;
while (ft_isflag(flag, format[i]) && format[i] != '.')
i++;
if (ft_isdigit(format[i]))
flag->width = get_width_or_prec(format, *arg, &i);
else if (format[i] == '*')
{
flag->width = va_arg(*arg, int);
i++;
}
if (format[i] == '.')
{
i++;
flag->dot = 1;
flag->prec = get_width_or_prec(format, *arg, &i);
}
while (ft_ismod(flag, format[i]))
i++;
return (i);
}