-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
122 lines (116 loc) · 3.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alex <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/13 16:50:48 by alex #+# #+# */
/* Updated: 2021/01/13 17:25:33 by alex ### ########.fr */
/* */
/* ************************************************************************** */
#include "printfhead.h"
void ft_parse_flags(const char *format, t_struct *blocks)
{
while (1)
{
if (*format == '0')
{
blocks->f_zero = 1;
format++;
blocks->skipped_chars++;
}
else if (*format == '-')
{
blocks->f_minus = 1;
format++;
blocks->skipped_chars++;
}
else
break ;
}
parse_width(format, blocks);
}
void parse_width(const char *format, t_struct *blocks)
{
if (*format == '*')
{
blocks->f_width = 1;
blocks->skipped_chars++;
format++;
blocks->width = va_arg((blocks->args), int);
if (blocks->width < 0)
{
blocks->f_minus = 1;
blocks->width = -blocks->width;
}
ft_parse_precision(format, blocks);
return ;
}
while (ft_isdigit(*format))
{
blocks->f_width = 1;
blocks->width = blocks->width * 10 + *format - '0';
format++;
blocks->skipped_chars++;
}
ft_parse_precision(format, blocks);
}
void ft_parse_precision(const char *format, t_struct *blocks)
{
if (*format != '.')
ft_parse_format(format, blocks);
else
{
format++;
blocks->f_prec = 1;
blocks->skipped_chars++;
if (*format == '*')
{
blocks->skipped_chars++;
format++;
blocks->precision = va_arg((blocks->args), int);
ft_parse_format(format, blocks);
return ;
}
while (ft_isdigit(*format))
{
blocks->precision = blocks->precision * 10 + *format - '0';
format++;
blocks->skipped_chars++;
}
ft_parse_format(format, blocks);
}
}
void ft_parse_format(const char *format, t_struct *blocks)
{
if (ft_isformat(*format))
{
blocks->format = *format;
format++;
blocks->skipped_chars++;
if (blocks->format == 'i' || blocks->format == 'd')
ft_define_n_digits(va_arg((blocks->args), int), blocks, 10);
else if (blocks->format == 'x' || blocks->format == 'X')
ft_define_n_digits(va_arg((blocks->args), int), blocks, 16);
else if (blocks->format == 'o')
ft_define_n_digits(va_arg((blocks->args), int), blocks, 8);
else if (blocks->format == 'u')
ft_def_u_digits(va_arg((blocks->args), unsigned int), blocks, 10);
else if (blocks->format == 's')
ft_order_to_print_string(va_arg((blocks->args), char *), blocks);
else if (blocks->format == 'p')
ft_init_pointer(blocks);
else if (blocks->format == 'c')
ft_order_to_print_char(va_arg(blocks->args, int), blocks);
else
return ;
}
else
print_not_format_like_format(*format, blocks);
}
void print_not_format_like_format(char ch, t_struct *blocks)
{
blocks->skipped_chars++;
ft_order_to_print_char(ch, blocks);
}