-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_numbers.c
77 lines (67 loc) · 2.08 KB
/
ft_printf_numbers.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_numbers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anebbou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 17:25:49 by anebbou #+# #+# */
/* Updated: 2024/11/18 11:41:32 by anebbou ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf_numbers(char specifier, va_list args)
{
if (specifier == 'd' || specifier == 'i')
return (ft_printf_int(va_arg(args, int)));
if (specifier == 'u')
return (ft_printf_uint(va_arg(args, unsigned int)));
if (specifier == 'x' || specifier == 'X')
return (ft_printf_hex(va_arg(args, unsigned int), specifier == 'X'));
if (specifier == 'p')
return (ft_printf_ptr(va_arg(args, void *)));
return (0);
}
int ft_printf_int(int n)
{
char *str;
int len;
str = ft_itoa(n);
len = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (len);
}
int ft_printf_uint(unsigned int n)
{
char *str;
int len;
str = ft_ltoa_base((unsigned long)n, 10);
len = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (len);
}
int ft_printf_hex(unsigned int n, int is_upper)
{
char *str;
int len;
str = ft_ltoa_base_upper((unsigned long)n, 16, is_upper);
len = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (len);
}
int ft_printf_ptr(void *ptr)
{
unsigned long address;
char *str;
int len;
address = (unsigned long)ptr;
ft_putstr_fd("0x", 1);
str = ft_ltoa_base_upper(address, 16, 0);
len = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (len + 2);
}