-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_flags.c
97 lines (92 loc) · 2.16 KB
/
parse_flags.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
#include "main.h"
/**
* read_flags_p2 - Parses flags after '%' character, before type specifier
* @format: String to parse
* @data: Pointer to structure containing general data
* @len: Current index on format string
* Orders to read flags : '-', '+', space, '#', '0', width,
* precision, length, specifier
* Return: Length of all flags combined
*/
size_t read_flags_p2(const char *format, format_t *data, int len)
{
if (format[len] == '.')
{
data->precision_flag = 1;
len++;
if (_isdigit(format[len + 1]))
{
data->precision_value = _atoi(format + len);
if (data->precision_value < 0)
data->precision_value = 0;
}
else if (format[len + 1] == '*')
{
data->precision_value = va_arg(data->args, int);
if (data->precision_value < 0)
{
data->precision_flag = 0;
data->precision_value = 0;
}
}
else
data->precision_value = 0;
while (_isdigit(format[len]))
len++;
}
if (format[len] == 'l')
{
data->long_flag = 1;
len++;
}
else if (format[len] == 'h')
{
data->short_flag = 1;
len++;
}
return (len);
}
/**
* read_flags - Parses flags after '%' character, before type specifier
* @format: String to parse
* @data: Pointer to structure containing general data
* Orders to read flags : '-', '+', space, '#', '0', width,
* precision, length, specifier
* Return: Length of all flags combined
*/
size_t read_flags(const char *format, format_t *data)
{
int len;
len = 0;
while (format[len] == '-' || format[len] == '+' || format[len] == ' '
|| format[len] == '0' || format[len] == '#')
{
if (format[len] == '-')
data->minus_flag = 1;
if (format[len] == '+')
data->plus_flag = 1;
if (format[len] == ' ')
data->space_flag = 1;
if (format[len] == '0')
data->zero_flag = 1;
if (format[len] == '#')
data->hash_flag = 1;
len++;
}
if (_isdigit(format[len]))
{
data->width_flag = _atoi(format + len);
if (data->width_flag < 0)
data->width_flag = 0;
while (_isdigit(format[len]))
len++;
}
else if (format[len] == '*')
{
data->width_flag = va_arg(data->args, int);
if (data->width_flag < 0)
data->width_flag = 0;
len++;
}
return (read_flags_p2(format, data, len));
}