-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_printf.c
93 lines (87 loc) · 2.7 KB
/
ft_printf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/27 15:49:01 by marsoare #+# #+# */
/* Updated: 2024/05/12 17:40:38 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *str, ...)
{
va_list args;
t_flags flags;
int i;
int count;
if (!str || (str[0] == '%' && str[1] == '\0'))
return (-1);
i = 0;
count = 0;
va_start(args, str);
while (str[i])
{
ft_bzero(&flags, sizeof(t_flags));
if (str[i] == '%')
{
i++;
while (ft_memchr("-0.# +", str[i], 6) || ft_isdigit(str[i]))
update_struct(&flags, str, &i);
count += print_format(&flags, str, &i, args);
}
else
count += ft_putchar(str[i++]);
}
va_end(args);
return (count);
}
int print_format(t_flags *flags, const char *str, int *pos, va_list args)
{
int count;
count = 0;
if (str[*pos] == 'c')
count += putcharf(flags, va_arg(args, int));
if (str[*pos] == 's')
count += putstrf(flags, va_arg(args, char *));
if (str[*pos] == 'p')
count += print_ptr(flags, va_arg(args, size_t));
if (str[*pos] == 'd' || str[*pos] == 'i')
count += print_decimal(flags, va_arg(args, int));
if (str[*pos] == 'u')
count += print_decimal(flags, va_arg(args, unsigned int));
if (str[*pos] == 'x' || str[*pos] == 'X')
count += print_hexa(flags, va_arg(args, unsigned int), str[*pos]);
if (str[*pos] == '%')
count += ft_putchar('%');
(*pos)++;
return (count);
}
void update_struct(t_flags *flags, const char *str, int *pos)
{
while (str[*pos] == '-' || str[*pos] == '+'
|| str[*pos] == ' ' || str[*pos] == '#')
{
if (str[*pos] == '-')
flags -> align_left = TRUE;
if (str[*pos] == '+')
flags -> plus = TRUE;
if (str[*pos] == ' ')
flags -> space = TRUE;
if (str[*pos] == '#')
flags -> hash = TRUE;
(*pos)++;
}
if (str[*pos] == '0')
flags -> zero = TRUE;
if (ft_isdigit(str[*pos]) || str[*pos] == '.')
{
if (str[*pos] == '.')
flags -> precision = ft_atoi(&str[++*pos]) + 1;
else
flags -> width = ft_atoi(&str[*pos]);
while (ft_isdigit(str[*pos]))
(*pos)++;
}
}