-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
64 lines (60 loc) · 1.84 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kchan <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/01 14:53:09 by kchan #+# #+# */
/* Updated: 2023/05/02 15:57:18 by kchan ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "lib/libft/libft.h"
void ft_eval_format(t_print *tab, const char *format, int pos)
{
if (format[pos] == 'c')
ft_print_c(tab);
else if (format[pos] == 's')
ft_print_s(tab);
else if (format[pos] == 'p')
ft_print_p(tab);
else if (format[pos] == 'd' || format[pos] == 'i')
ft_print_di(tab);
else if (format[pos] == 'u')
ft_print_unsignedint(tab);
else if (format[pos] == 'x')
ft_print_hex(tab, 32);
else if (format[pos] == 'X')
ft_print_hex(tab, 0);
else if (format[pos] == '%')
ft_print_percentage(tab);
else
return ;
}
int ft_printf(const char *format, ...)
{
int i;
int ret;
t_print *tab;
tab = ft_calloc(1, sizeof(t_print));
if (!tab)
return (-1);
va_start((*tab).args, format);
i = -1;
ret = 0;
while (format[++i])
{
if (format[i] == '%')
{
i += 1;
ft_eval_format(tab, format, i);
}
else
ret += write(1, &format[i], 1);
}
va_end((*tab).args);
ret += (*tab).tl;
free (tab);
return (ret);
}