This repository has been archived by the owner on Nov 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_parse_format.c
123 lines (117 loc) · 3.14 KB
/
ft_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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 22:27:10 by mchen #+# #+# */
/* Updated: 2017/02/03 22:46:01 by mchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/ft_printf.h"
void set_type_field(t_placehold *p, const char *e)
{
if (*e && ft_strchr("sSpdDioOuUxXcCbfFeEgGaA", *e))
{
p->uppercase = (ft_strchr("XEGA", *e) ? 1 : p->uppercase);
p->base = (*e == 'b' ? 2 : p->base);
p->base = (ft_strchr("oO", *e) ? 8 : p->base);
p->base = (ft_strchr("pxXaA", *e) ? 16 : p->base);
p->exp_base = (ft_strchr("aA", *e) ? 2 : p->exp_base);
p->exp_len = (ft_strchr("aA", *e) ? 1 : p->exp_len);
p->exp_char = (ft_strchr("aA", *e) ? *e + 15 : p->exp_char);
p->exp_char = (ft_strchr("eEgG", *e) ? 'e' : p->exp_char);
if (ft_strchr("DOUCS", *e))
{
if (p->length)
free(p->length);
p->length = ft_strdup("l");
}
p->signed_num = (ft_strchr("dDi", *e) ? 1 : p->signed_num);
set_hash(p, e);
}
else
p->prec = 1;
p->sign = (!ft_strchr("dDifFfFeEgGaA", *e) ? 0 : p->sign);
p->padding = (ft_strchr("pdDioOuUxXb", *e) && p->prec >= 0 &&
p->leftalign == 0) ? ' ' : p->padding;
p->type = *e;
}
void set_flag_field(t_placehold *p, const char **e)
{
while (1)
{
if (**e == '+')
p->sign = '+';
else if (**e == ' ')
p->sign = (p->sign == 0 ? ' ' : p->sign);
else if (**e == '-')
{
p->leftalign = 1;
p->padding = ' ';
}
else if (**e == '0')
p->padding = (p->leftalign == 0 ? '0' : p->padding);
else if (**e == '#')
p->hash = "#";
else
break ;
(*e)++;
}
}
void set_width_field(t_placehold *p, const char **e, va_list a_list)
{
if (**e == '*')
{
if ((p->width = va_arg(a_list, int)) < 0)
{
p->width = p->width * -1;
p->leftalign = 1;
p->padding = ' ';
}
(*e)++;
}
else if (**e >= '1' && **e <= '9')
{
while (**e >= '0' && **e <= '9')
{
p->width = p->width * 10 + **e - '0';
(*e)++;
}
}
}
void set_precision_field(t_placehold *p, const char **e, va_list a_list)
{
if (**e == '.')
{
p->prec = 0;
(*e)++;
if (**e == '*')
{
p->prec = va_arg(a_list, int);
if (p->prec < 0)
p->prec = -1;
(*e)++;
}
else
{
while (**e >= '0' && **e <= '9')
{
p->prec = p->prec * 10 + **e - '0';
(*e)++;
}
}
}
}
void set_length_field(t_placehold *p, const char **e)
{
if (**e && ft_strchr("hljzqL", **e))
{
(*e)++;
if (**e == 'h' || **e == 'l')
p->length = ft_strndup((*e)++ - 1, 2);
else
p->length = ft_strndup(*e - 1, 1);
}
}