-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
94 lines (83 loc) · 2.48 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
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: babreton <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 16:54:13 by babreton #+# #+# */
/* Updated: 2023/03/09 12:17:44 by babreton ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *format, ...)
{
int count_print;
int i;
va_list args;
i = -1;
count_print = 0;
va_start(args, format);
while (format[++i])
count_print += brain(&i, format[i], format[i + 1], args);
return (count_print);
}
int brain(int *i, char now, char next, va_list args)
{
int count_print;
if (now == '%' && next)
{
count_print = check_args(next, args);
(*i)++;
}
else
return (write(1, &now, 1), 1);
return (count_print);
}
int check_args(char next, va_list args)
{
int j;
int count_print;
j = -1;
while (FORMAT[++j])
{
if (next == FORMAT[j])
count_print = thumb(FORMAT[j], args);
}
return (count_print);
}
int thumb(char thumbnail, va_list args)
{
if (thumbnail == 'c')
return (ft_putchar(va_arg(args, int)));
if (thumbnail == 's')
return (ft_putstr(va_arg(args, char *)));
if (thumbnail == '%')
return (ft_putchar('%'));
if (thumbnail == 'd')
return (ft_putnbr(va_arg(args, int)));
if (thumbnail == 'i')
return (ft_putnbr(va_arg(args, int)));
if (thumbnail == 'X')
return (ft_puthexa(va_arg(args, unsigned int), "0123456789ABCDEF"));
if (thumbnail == 'x')
return (ft_puthexa(va_arg(args, unsigned int), "0123456789abcdef"));
if (thumbnail == 'p')
return (ft_adress(va_arg(args, void *)));
if (thumbnail == 'u')
return (ft_putunbr(va_arg(args, unsigned int)));
return (-1);
}
int ft_putchar(int c)
{
return (write(1, &c, 1), 1);
}
/*int main(void)
{
int ret;
ret = ft_printf(" %s %% %% ", "nik");
printf("\nValeur mon printf : %d\n", ret);
ret = printf(" %s %% %% ", "nik");
printf("\nValeur printf basique : %d", ret);
return 0;
}*/