-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpf_parse_format.c
81 lines (73 loc) · 2.23 KB
/
pf_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pf_parse_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mg <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/02 15:06:44 by mg #+# #+# */
/* Updated: 2020/06/28 12:43:57 by mg ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int pf_parse_format(const char *format, va_list *ap, t_fmt *flag)
{
char *end;
end = pf_parse_specifier(format, flag);
if (end)
{
pf_parse_flag(format, flag, end);
pf_parse_width(format, flag, end, ap);
pf_parse_precision(format, flag, end, ap);
pf_parse_length(flag, end);
pf_parse_base(flag);
if (!(IS_MACOS && *end == '%'))
if (*end == 'n' || *end == '%')
pf_reset_format(flag);
}
return (end ? (end - format) + 1 : 1);
}
int pf_islength(char chr)
{
if (chr == 'h' || chr == 'l' || chr == 'j' ||
chr == 'z' || chr == 't' || chr == 'L')
return (1);
return (0);
}
int pf_is_valid_specifier(char chr)
{
if (ft_isalpha(chr))
if (!(pf_islength(chr)))
return (0);
return (1);
}
/*
** https://en.wikipedia.org/wiki/Printf_format_string#Type_field
*/
char *pf_parse_specifier(const char *format, t_fmt *flag)
{
char *spec_list;
char *spec_flag;
char *tmp;
spec_list = IS_MACOS ? "%dDiufFeEgGxXoscpaAn" : "%diufFeEgGxXoscpaAn";
spec_flag = NULL;
++format;
while (*spec_list)
{
tmp = ft_strchr(format, *spec_list);
if (tmp)
{
if (!spec_flag)
spec_flag = tmp;
else if (tmp < spec_flag)
spec_flag = tmp;
}
++spec_list;
}
while (spec_flag && format < spec_flag)
if (!(pf_is_valid_specifier(*format++)))
spec_flag = NULL;
if (spec_flag)
flag->spec = *spec_flag;
return (spec_flag);
}