-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
78 lines (71 loc) · 2.2 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
67
68
69
70
71
72
73
74
75
76
77
78
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atoof <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 14:42:22 by mtoof #+# #+# */
/* Updated: 2022/12/27 14:40:26 by atoof ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_check_int(va_list args, int counter)
{
long i;
i = va_arg(args, int);
if (i < 0)
{
counter += ft_putchar('-');
i = -i;
}
counter += ft_convert_number(i, 10, 0);
return (counter);
}
static int ft_check_format(va_list args, const char *format)
{
int length;
length = 0;
if (*format == 'c')
length += ft_putchar(va_arg(args, int));
else if (*format == 's')
length += ft_putstr(va_arg(args, char *));
else if (*format == 'X')
length += ft_convert_number(va_arg(args, unsigned int), 16, 0);
else if (*format == 'x')
length += ft_convert_number(va_arg(args, unsigned int), 16, 32);
else if (*format == 'd' || *format == 'i')
length += ft_check_int(args, length);
else if (*format == 'u')
length += ft_convert_number(va_arg(args, unsigned int), 10, 0);
else if (*format == 'p')
{
length += ft_putstr("0x");
length += ft_convert_number((size_t)va_arg(args, void *), 16, 32);
}
else if (*format == '%')
length += ft_putchar('%');
return (length);
}
int ft_printf(const char *tmp, ...)
{
int length;
va_list args;
length = 0;
va_start(args, tmp);
while (*tmp)
{
if (*tmp != '%')
length += ft_putchar(*tmp);
else if (*tmp == '%')
{
tmp++;
length += ft_check_format(args, tmp);
}
if (!*tmp)
return (length);
tmp++;
}
va_end(args);
return (length);
}