-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
66 lines (61 loc) · 1.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jikarunw <jikarunw@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/16 22:31:15 by jikarunw #+# #+# */
/* Updated: 2023/09/18 16:30:42 by jikarunw ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_handle_printf(const char *input, unsigned int i, va_list *args)
{
unsigned int count;
count = 0;
if (input[i] == 'd')
count += ft_printf_nbr(va_arg(*args, int));
else if (input[i] == 'c')
count += ft_printf_char(va_arg(*args, int));
else if (input[i] == 's')
count += ft_printf_string(va_arg(*args, char *));
else if (input[i] == 'p')
count += ft_printf_ptr(va_arg(*args, void *));
else if (input[i] == 'x')
count += ft_printf_hex(va_arg(*args, int), 1);
else if (input[i] == 'X')
count += ft_printf_hex(va_arg(*args, int), 0);
else if (input[i] == 'u')
count += ft_printf_unit(va_arg(*args, unsigned int));
else if (input[i] == 'i')
count += ft_printf_nbr(va_arg(*args, int));
else if (input[i] == '%')
{
ft_printf_char('%');
return (1);
}
return (count);
}
int ft_printf(const char *str, ...)
{
va_list args;
unsigned int i;
unsigned int len;
i = 0;
len = 0;
va_start(args, str);
while (str[i])
{
if (str[i] == '%')
{
i++;
len += ft_handle_printf(str, i, &args);
}
else
len += ft_printf_char(str[i]);
i++;
}
va_end(args);
return (len);
}