-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
102 lines (93 loc) · 2.44 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apereira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/23 16:03:31 by apereira #+# #+# */
/* Updated: 2022/11/28 17:38:48 by apereira ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *string, ...)
{
int i;
int count;
va_list arg;
va_start(arg, string);
i = 0;
count = 0;
while (string[i])
{
if (string[i] == '%')
{
count += ft_check(arg, string[i + 1]);
i++;
}
else
count += ft_putchar(string[i]);
i++;
}
va_end(arg);
return (count);
}
int ft_check(va_list arg, char format)
{
int count;
count = 0;
if (format == 'c')
return (ft_putchar(va_arg(arg, int)));
else if (format == 's')
return (ft_putstr(va_arg(arg, char *)));
else if (format == 'p')
return (ft_print_address(va_arg(arg, unsigned long long), count));
else if (format == 'd' || format == 'i')
return (ft_putnbr(va_arg(arg, int), count));
else if (format == 'u')
return (ft_putunbr(va_arg(arg, unsigned int), format));
else if (format == 'x' || format == 'X')
return (ft_to_hex(va_arg(arg, unsigned int), format));
return (ft_putchar('%'));
}
int ft_print_address(unsigned long long address, int count)
{
count = 0;
if (address < 1)
count += write(1, "(nil)", 5);
else
{
count += write(1, "0x", 2);
ft_putnbr_ptr(address);
count += ft_nbr_len(address, 16);
}
return (count);
}
void ft_putnbr_ptr(unsigned long long address)
{
if (address > 15)
{
ft_putnbr_ptr(address / 16);
ft_putnbr_ptr(address % 16);
}
else
{
if (address < 10)
ft_putchar(address + '0');
else
ft_putchar(address - 10 + 'a');
}
}
int ft_nbr_len(unsigned long long address, int div_by)
{
int i;
i = 0;
if (address == 0)
return (1);
while (address != 0)
{
i++;
address = address / div_by;
}
return (i);
}