-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_utils.c
88 lines (80 loc) · 2.07 KB
/
ft_printf_utils.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf_utils.c :+: :+: */
/* +:+ */
/* By: cherrewi <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/06/14 16:32:47 by cherrewi #+# #+# */
/* Updated: 2023/02/08 16:12:35 by cherrewi ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
void printf_putstr(char *str, int *print_len)
{
if (str)
{
ft_putstr_fd(str, 1);
*print_len += ft_strlen(str);
}
else
*print_len += write(1, "(null)", 6);
}
void printf_putnbr(int nbr, int *print_len)
{
char c;
if (nbr == -2147483648)
*print_len += write(1, "-2147483648", 11);
else
{
if (nbr < 0)
{
nbr = nbr * (-1);
*print_len += write(1, "-", 1);
}
if (nbr < 10)
{
c = nbr + '0';
*print_len += write(1, &c, 1);
}
else
{
printf_putnbr(nbr / 10, print_len);
printf_putnbr(nbr % 10, print_len);
}
}
}
void printf_putunsnbr(unsigned int nbr, int *print_len)
{
char c;
if (nbr < 10)
{
c = nbr + '0';
*print_len += write(1, &c, 1);
}
else
{
printf_putunsnbr(nbr / 10, print_len);
printf_putunsnbr(nbr % 10, print_len);
}
}
void printf_putunsignedhex(unsigned long long nbr,
int *print_len, char var_case)
{
char *base;
base = NULL;
if (var_case == 'l')
base = "0123456789abcdef";
else if (var_case == 'u')
base = "0123456789ABCDEF";
if (base)
{
if (nbr < 16)
*print_len += write(1, &base[nbr], 1);
else
{
printf_putunsignedhex(nbr / 16, print_len, var_case);
*print_len += write(1, &base[nbr % 16], 1);
}
}
}