-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
63 lines (58 loc) · 1.88 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcoskune <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 16:44:34 by mcoskune #+# #+# */
/* Updated: 2024/05/09 17:50:01 by mcoskune ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_parse(const char *fmt, size_t *i, va_list *pa)
{
unsigned int counter;
counter = 0;
if (fmt[*i] == 'c')
counter += ft_printf_c (va_arg(*pa, int));
else if (fmt[*i] == 's')
counter += ft_printf_s(va_arg(*pa, char *));
else if (fmt[*i] == 'p')
counter += ft_printf_p(va_arg(*pa, void *));
else if (fmt[*i] == 'd' || fmt[*i] == 'i')
counter += ft_printf_di(va_arg(*pa, int));
else if (fmt[*i] == 'x')
counter += ft_printf_xx(va_arg(*pa, int), 0);
else if (fmt[*i] == 'X')
counter += ft_printf_xx(va_arg(*pa, int), 1);
else if (fmt[*i] == 'u')
counter += ft_printf_u(va_arg(*pa, int));
else if (fmt[*i] == '%')
counter += ft_printf_c('%');
return (counter);
}
int ft_printf(const char *fmt, ...)
{
va_list pa;
size_t count;
size_t i;
i = 0;
count = 0;
va_start(pa, fmt);
while (fmt[i] != '\0')
{
if (fmt[i] == '%')
{
i++;
count += ft_parse(fmt, &i, &pa);
}
else
{
count += ft_printf_c(fmt[i]);
}
i++;
}
va_end(pa);
return (count);
}