-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
59 lines (55 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bregneau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 12:02:32 by bregneau #+# #+# */
/* Updated: 2021/12/04 08:22:35 by bregneau ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdarg.h>
int ft_parse_arg(char c, va_list ap)
{
if (c == 'c')
return (ft_print_char(va_arg(ap, int)));
if (c == 's')
return (ft_print_str(va_arg(ap, char *)));
if (c == 'p')
return (ft_print_ptr(va_arg(ap, void *)));
if (c == 'i' || c == 'd')
return (ft_print_int(va_arg(ap, int)));
if (c == 'u')
return (ft_print_uint(va_arg(ap, unsigned int)));
if (c == 'x' || c == 'X')
return (ft_print_hexa(va_arg(ap, unsigned int), c));
if (c == '%')
return (ft_print_percent());
return (0);
}
int ft_printf(const char *format, ...)
{
va_list ap;
int i;
int count;
va_start(ap, format);
i = 0;
count = 0;
while (format[i])
{
if (format[i] == '%')
{
count += ft_parse_arg(format[++i], ap);
i++;
}
else
{
ft_putchar_fd(format[i++], 1);
count++;
}
}
va_end(ap);
return (count);
}