-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_parse_format.c
More file actions
107 lines (99 loc) · 3.51 KB
/
ft_printf_parse_format.c
File metadata and controls
107 lines (99 loc) · 3.51 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_parse_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/02 22:47:10 by dporhomo #+# #+# */
/* Updated: 2025/12/09 16:56:23 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_traverse_flags(const char *format, va_list args,
t_flags *flags, t_print *tab);
static void ft_print_spec(const char *format, va_list args,
t_flags *flags, t_print *tab);
static void ft_parse_precision(const char *format, va_list args,
t_flags *flags, t_print *tab);
static void ft_parse_width(char c, t_flags *flags, va_list args);
void ft_parse_format(const char *format, va_list args, t_print *tab)
{
t_flags flags;
flags = ft_flags_init();
tab->i++;
ft_traverse_flags(format, args, &flags, tab);
ft_print_spec(format, args, &flags, tab);
}
static void ft_traverse_flags(const char *format, va_list args,
t_flags *flags, t_print *tab)
{
while (format[tab->i] && !ft_isspec(format[tab->i]))
{
if (format[tab->i] == '-')
flags->left = 1;
else if (format[tab->i] == '0'
&& flags->left == 0
&& flags->width == 0
&& flags->precision == -1)
flags->zero = 1;
else if (format[tab->i] == '.')
{
ft_parse_precision(format, args, flags, tab);
tab->i--;
}
else if (format[tab->i] == '#')
flags->hash = 1;
else if (format[tab->i] == ' ')
flags->space = 1;
else if (format[tab->i] == '+')
flags->plus = 1;
else
ft_parse_width(format[tab->i], flags, args);
tab->i++;
}
}
static void ft_parse_precision(const char *format, va_list args,
t_flags *flags, t_print *tab)
{
tab->i++;
if (format[tab->i] == '*')
{
flags->precision = va_arg(args, int);
tab->i++;
return ;
}
flags->precision = 0;
while (ft_isdigit(format[tab->i]))
{
flags->precision = (flags->precision * 10) + (format[tab->i] - '0');
tab->i++;
}
}
static void ft_parse_width(char c, t_flags *flags, va_list args)
{
if (c == '*')
flags->width = va_arg(args, int);
if (ft_isdigit(c))
flags->width = (flags->width * 10) + (c - '0');
}
static void ft_print_spec(const char *format, va_list args,
t_flags *flags, t_print *tab)
{
if (format[tab->i] == 'c')
tab->count += ft_print_char(va_arg(args, int), *flags);
else if (format[tab->i] == 's')
tab->count += ft_print_str(va_arg(args, char *), *flags);
else if (format[tab->i] == '%')
tab->count += ft_print_char('%', *flags);
else if (format[tab->i] == 'd' || format[tab->i] == 'i')
tab->count += ft_print_int(va_arg(args, int), *flags);
else if (format[tab->i] == 'u')
tab->count += ft_print_unsigned(va_arg(args, unsigned int), *flags);
else if (format[tab->i] == 'x')
tab->count += ft_print_hex(va_arg(args, unsigned int), 0, *flags);
else if (format[tab->i] == 'X')
tab->count += ft_print_hex(va_arg(args, unsigned int), 1, *flags);
else if (format[tab->i] == 'p')
tab->count += ft_print_ptr(va_arg(args, unsigned long), *flags);
}