-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
67 lines (62 loc) · 1.98 KB
/
ft_printf.c
File metadata and controls
67 lines (62 loc) · 1.98 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gargrigo <gargrigo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/20 15:35:42 by gargrigo #+# #+# */
/* Updated: 2026/03/03 12:37:47 by gargrigo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_validate(const char *str, va_list list, int i)
{
int count;
unsigned long ptr;
count = 0;
if (str[i + 1] == 'd' || str[i + 1] == 'i')
count += ft_putnbr_base(va_arg(list, int), "0123456789");
if (str[i + 1] == 'u')
count += ft_putnbr_base(va_arg(list, unsigned int), "0123456789");
if (str[i + 1] == 'x')
count += ft_putnbr_base(va_arg(list, unsigned int), "0123456789abcdef");
if (str[i + 1] == 'X')
count += ft_putnbr_base(va_arg(list, unsigned int), "0123456789ABCDEF");
if (str[i + 1] == 'c')
count += ft_putchar(va_arg(list, int));
if (str[i + 1] == 's')
count += ft_putstr(va_arg(list, char *));
if (str[i + 1] == '%')
count += ft_putchar('%');
if (str[i + 1] == 'p')
{
ptr = va_arg(list, unsigned long);
count += ptr_case(ptr);
}
return (count);
}
int ft_printf(const char *str, ...)
{
va_list list;
int i;
int count;
va_start(list, str);
i = 0;
count = 0;
while (str[i])
{
if (str[i] == '%')
{
count += ft_validate(str, list, i);
i += 2;
}
else
{
ft_putchar(str[i++]);
count++;
}
}
va_end(list);
return (count);
}