-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
64 lines (59 loc) · 1.88 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: zkharbac <zkharbac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 12:25:55 by zkharbac #+# #+# */
/* Updated: 2024/11/27 11:14:52 by zkharbac ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void function_parametr(char c, va_list args, int *count)
{
if (c == 'c')
*count += ft_putchar(va_arg(args, int));
else if (c == 's')
*count += ft_putstr(va_arg(args, char *));
else if (c == 'd' || c == 'i')
*count += ft_putnbr_d(va_arg(args, int));
else if (c == 'x' || c == 'X')
*count += ft_putnbr_hex(va_arg(args, unsigned int), c);
else if (c == 'u')
*count += ft_putnbr_u(va_arg(args, unsigned int));
else if (c == 'p')
{
*count += ft_putstr("0x");
*count += ft_putnbr_p(va_arg(args, unsigned long));
}
else
*count += ft_putchar(c);
}
void handle_percent(const char *str, va_list args, int *i, int *count)
{
if (str[*i + 1] != '\0')
{
function_parametr(str[*i + 1], args, count);
(*i)++;
}
}
int ft_printf(const char *str, ...)
{
int i;
int count;
va_list args;
i = 0;
count = 0;
va_start(args, str);
while (str[i])
{
if (str[i] == '%')
handle_percent(str, args, &i, &count);
else
count += ft_putchar(str[i]);
i++;
}
va_end(args);
return (count);
}