-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
131 lines (116 loc) · 2.03 KB
/
parse.c
File metadata and controls
131 lines (116 loc) · 2.03 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "main.h"
/**
* pf_init_format - init format structure
* @f: format
*/
static void pf_init_format(pf_format_t *f)
{
f->flags = 0;
f->width = 0;
f->precision = -1;
f->length = 0;
f->spec = 0;
}
/**
* pf_parse_flags - parse flags
* @p: pointer to format pointer
* @f: format
*/
static void pf_parse_flags(const char **p, pf_format_t *f)
{
while (**p == '-' || **p == '+' || **p == ' ' || **p == '#' || **p == '0')
{
if (**p == '-')
f->flags |= PF_F_MINUS;
if (**p == '+')
f->flags |= PF_F_PLUS;
if (**p == ' ')
f->flags |= PF_F_SPACE;
if (**p == '#')
f->flags |= PF_F_HASH;
if (**p == '0')
f->flags |= PF_F_ZERO;
(*p)++;
}
}
/**
* pf_parse_width - parse width
* @p: pointer to format pointer
* @f: format
* @ap: args
*/
static void pf_parse_width(const char **p, pf_format_t *f, va_list *ap)
{
int w;
if (**p == '*')
{
w = va_arg(*ap, int);
if (w < 0)
{
f->flags |= PF_F_MINUS;
w = -w;
}
f->width = w;
(*p)++;
return;
}
while (pf_is_digit(**p))
{
f->width = f->width * 10 + (**p - '0');
(*p)++;
}
}
/**
* pf_parse_precision - parse precision
* @p: pointer to format pointer
* @f: format
* @ap: args
*/
static void pf_parse_precision(const char **p, pf_format_t *f, va_list *ap)
{
int pr;
if (**p != '.')
return;
(*p)++;
f->precision = 0;
if (**p == '*')
{
pr = va_arg(*ap, int);
if (pr >= 0)
f->precision = pr;
else
f->precision = -1;
(*p)++;
return;
}
while (pf_is_digit(**p))
{
f->precision = f->precision * 10 + (**p - '0');
(*p)++;
}
}
/**
* pf_parse - parse after '%' and fill format
* @p: pointer to format pointer (points after '%')
* @f: format output
* @ap: variadic args
*
* Return: 0 on success, -1 on error (missing spec)
*/
int pf_parse(const char **p, pf_format_t *f, va_list *ap)
{
pf_init_format(f);
pf_parse_flags(p, f);
pf_parse_width(p, f, ap);
pf_parse_precision(p, f, ap);
if (**p == 'l' || **p == 'h')
{
f->length = **p;
(*p)++;
}
if (**p == '\0')
return (-1);
f->spec = **p;
(*p)++;
return (0);
}