-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
62 lines (56 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kakiba <kotto555555@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 17:00:55 by kakiba #+# #+# */
/* Updated: 2022/12/04 14:35:37 by kakiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_printf(char *format, ...)
{
va_list ap;
int sum;
va_start(ap, format);
sum = 0;
output_format(format, &ap, &sum);
va_end(ap);
return (sum);
}
void output_format(char *format, va_list *ap, int *sum)
{
int i;
i = 0;
while (format[i])
{
while (format[i] && format[i] != '%')
*sum += write(1, format + i++, 1);
if (format[i] == '%')
{
i += filter(format + i, ap, sum);
}
}
}
int filter(char *format, va_list *ap, int *sum)
{
if (format[1] == 'c')
print_char(ap, sum);
else if (format[1] == 's')
print_str(ap, sum);
else if (format[1] == 'd' || format[1] == 'i')
*sum += ft_putllnbr_fd(va_arg(*ap, int), 1);
else if (format[1] == 'u')
*sum += ft_putllnbr_fd(va_arg(*ap, unsigned int), 1);
else if (format[1] == 'p')
print_pointer(ap, sum);
else if (format[1] == 'x' || format[1] == 'X')
*sum += ft_puthex_fd(va_arg(*ap, unsigned int), 1, format[1]);
else if (format[1] == '%')
*sum += write(1, format + 1, 1);
else
return (1);
return (2);
}