This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
63 lines (56 loc) · 1.86 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: jgo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 05:44:53 by jgo #+# #+# */
/* Updated: 2022/12/28 14:55:12 by jgo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int func_confirm(va_list *ap, const char conversion)
{
const t_funcptr fp[8] = {ft_printf_c, ft_printf_s, ft_printf_p, \
ft_printf_d, ft_printf_d, ft_printf_u, ft_printf_x, ft_printf_x};
const int func_index = ft_strchr(CONVERSION_LIST, conversion);
if (func_index == -1)
return (-1);
return (fp[func_index](ap, conversion));
}
static int process_str(va_list *ap, const char *format)
{
int return_val;
int add_val;
int iter;
return_val = 0;
iter = 0;
while (format[iter])
{
if (format[iter] == '%')
{
if (format[iter + 1] == '%')
add_val = ft_putchar('%');
else
add_val = func_confirm(ap, format[iter + 1]);
iter += 1;
}
else
add_val = ft_putchar(format[iter]);
if (add_val == -1)
return (-1);
return_val += add_val;
iter++;
}
return (return_val);
}
int ft_printf(const char *format, ...)
{
va_list ap;
int return_val;
va_start(ap, format);
return_val = process_str(&ap, format);
va_end(ap);
return (return_val);
}