-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
85 lines (77 loc) · 2.18 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
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsamli <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 20:37:16 by bsamli #+# #+# */
/* Updated: 2022/10/26 21:15:46 by bsamli ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_dhex(unsigned int i, int *result)
{
char *base;
base = "0123456789abcdef";
if (i >= 16)
{
ft_dhex(i / 16, result);
ft_dhex(i % 16, result);
}
else
ft_putchar(base[i], result);
}
void ft_uhex(unsigned int i, int *result)
{
char *base;
base = "0123456789ABCDEF";
if (i >= 16)
{
ft_uhex(i / 16, result);
ft_uhex(i % 16, result);
}
else
ft_putchar(base[i], result);
}
void ft_check(va_list *list, char a, int *result)
{
if (a == 'c')
ft_putchar(va_arg(*list, int), result);
else if (a == '%')
ft_putchar('%', result);
else if (a == 's')
ft_putstr(va_arg(*list, char *), result);
else if (a == 'd' || a == 'i')
ft_putnbr(va_arg(*list, int), result);
else if (a == 'x')
ft_dhex(va_arg(*list, unsigned long long), result);
else if (a == 'X')
ft_uhex(va_arg(*list, unsigned long long), result);
else if (a == 'u')
ft_unsigned(va_arg(*list, unsigned int), result);
else if (a == 'p')
{
ft_putstr("0x", result);
ft_pointer(va_arg(*list, unsigned long long), result);
}
}
int ft_printf(const char *s, ...)
{
int i;
int result;
va_list list;
i = 0;
result = 0;
va_start(list, s);
while (s[i] && s)
{
if (s[i] == '%' && s[i + 1])
ft_check(&list, s[++i], &result);
else
ft_putchar(s[i], &result);
i++;
}
va_end (list);
return (result);
}