-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
executable file
·248 lines (234 loc) · 6.15 KB
/
parser.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/15 18:29:45 by akharrou #+# #+# */
/* Updated: 2019/05/08 08:34:25 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
** NAME
** parse_flags -- parse 'flags' field in the 'printf' format string
**
** SYNOPSIS
** #include <libft.h>
**
** int8_t
** parse_flags(const char *format, int8_t *i);
**
** PARAMETERS
**
** const char *format Format string.
**
** int8_t *i Index to start parsing from in the
** format string.
**
** DESCRIPTION
** Parses for the 'flags' field in the 'printf' format string.
**
** RETURN VALUES
** Returns a mask representing the specified flags.
*/
int8_t parse_flags(const char *format, int8_t *i)
{
int8_t flags;
flags = 0;
while (format[*i])
{
if (format[*i] == '+')
flags |= PLUS;
else if (format[*i] == '-')
flags |= MINUS;
else if (format[*i] == ' ')
flags |= SPACE;
else if (format[*i] == '#')
flags |= HASH;
else if (format[*i] == '0')
flags |= ZERO;
else
break ;
++(*i);
}
return (flags);
}
/*
** NAME
** parse_width -- parse 'width' field in the 'printf' format string
**
** SYNOPSIS
** #include <libft.h>
**
** int32_t
** parse_width(const char *format, va_list *args, int8_t *i);
**
** PARAMETERS
**
** const char *format Format string.
**
** va_list *args Variable argument list.
**
** int8_t *i Index to start parsing from in the
** format string.
**
** DESCRIPTION
** Parses for the 'width' field in the 'printf' format string.
**
** RETURN VALUES
** Returns a signed 8 bit integer representing the width
** specified.
*/
int32_t parse_width(const char *format, va_list *args, int8_t *i)
{
int32_t width;
if (format[*i] == '*')
{
width = va_arg(*args, int32_t);
++(*i);
}
else
{
width = ft_atoi(format + (*i));
if (width)
(*i) += ft_intlen(width);
}
return (width);
}
/*
** NAME
** parse_precison -- parse the 'precision' field in the 'printf'
** format string.
**
** SYNOPSIS
** #include <libft.h>
**
** int32_t
** parse_precison(const char *format, va_list *args, int8_t *i);
**
** PARAMETERS
**
** const char *format Format string.
**
** va_list *args Variable argument list.
**
** int8_t *i Index to start parsing from in the
** format string.
**
** DESCRIPTION
** Parses for the 'precision' field in the 'printf' format string.
**
** RETURN VALUES
** Returns a signed 8 bit integer representing the precision
** specified or NONE (expands to -1) to indicate that no
** precision was specified.
*/
int32_t parse_precison(const char *format, va_list *args, int8_t *i)
{
int32_t precision;
if (format[*i] == '.')
{
++(*i);
if (format[*i] == '*')
{
precision = va_arg(*args, int32_t);
++(*i);
}
else
{
while (format[*i] == '0')
++(*i);
precision = ft_atoi(format + (*i));
if (precision)
(*i) += ft_intlen(precision);
}
return (precision);
}
return (NONE);
}
/*
** NAME
** parse_length -- parse the 'length' field in the 'printf'
** format string
**
** SYNOPSIS
** #include <libft.h>
**
** int8_t
** parse_length(const char *format, int8_t *i);
**
** PARAMETERS
**
** const char *format Format string.
**
** int8_t *i Index to start parsing from in the
** format string.
**
** DESCRIPTION
** Parses for the 'length' field in the 'printf' format string.
**
** RETURN VALUES
** Returns a signed 8 bit integer representing the specified
** length or NONE (expands to -1) to indicate that the default
** should be use.
*/
int8_t parse_length(const char *format, int8_t *i)
{
(*i) += 2;
if (format[(*i) - 2] == 'h' && format[(*i) - 1] == 'h')
return (HH);
else if (format[(*i) - 2] == 'l' && format[(*i) - 1] == 'l')
return (LL);
(*i) -= 1;
if (format[(*i) - 1] == 'h')
return (H);
else if (format[(*i) - 1] == 'l')
return (L);
else if (format[(*i) - 1] == 'L')
return (LLL);
else if (format[(*i) - 1] == 'j')
return (IU_MAX);
else if (format[(*i) - 1] == 'z')
return (SIZET);
else if (format[(*i) - 1] == 't')
return (PTRDIFF);
(*i) -= 1;
return (NONE);
}
/*
** NAME
** parse_specifier -- parse the 'specifier' field in the 'printf'
** format string
**
** SYNOPSIS
** #include <libft.h>
**
** int8_t
** parse_specifier(const char *format, int8_t *i);
**
** PARAMETERS
**
** const char *format Format string.
**
** int8_t *i Index to start parsing from in the
** format string.
**
** DESCRIPTION
** Parses for the 'specifier' field in the 'printf' format string.
**
** RETURN VALUES
** Returns the obtained specifier (as an 8 bit integer) or NONE
** (expands to -1) to indicate that no valid specifier was
** specified.
*/
int8_t parse_specifier(const char *format, int8_t *i)
{
if (ft_ischarset(format[*i], SPECIFIERS))
{
++(*i);
return (format[(*i) - 1]);
}
return (NONE);
}