-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_utils.c
More file actions
97 lines (86 loc) · 2.18 KB
/
ft_printf_utils.c
File metadata and controls
97 lines (86 loc) · 2.18 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lsasse <lsasse@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/21 15:31:13 by lsasse #+# #+# */
/* Updated: 2024/01/01 21:35:37 by lsasse ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putstr(char *str)
{
int count;
count = 0;
if (!str)
{
count += ft_putstr("(null)");
return (count);
}
while (str[count])
count += ft_putchar(str[count]);
return (count);
}
int ft_putnbr(int n)
{
int count;
count = 0;
if (n > 9)
count += ft_putnbr(n / 10);
if (n < -9)
count += ft_putnbr(n / 10);
else if (n < 0)
count += ft_putchar('-');
count += ft_putchar('0' + n % 10 * ((n > 0) - (n < 0)));
return (count);
}
int ft_putunbr(long long unsigned int nbr)
{
int count;
count = 0;
if (nbr > 9)
{
count += ft_putunbr(nbr / 10);
count += ft_putchar(nbr % 10 + '0');
}
else
count += ft_putchar(nbr % 10 + '0');
return (count);
}
int ft_puthex(long long unsigned int nbr, int uppercase)
{
int digit;
int count;
count = 0;
if (nbr < 16)
{
if (uppercase)
count += ft_putchar(UHEX_DIGITS[nbr]);
else
count += ft_putchar(HEX_DIGITS[nbr]);
return (count);
}
if (uppercase)
digit = UHEX_DIGITS[nbr % 16];
else
digit = HEX_DIGITS[nbr % 16];
nbr /= 16;
count += ft_puthex(nbr, uppercase);
count += ft_putchar((char)digit);
return (count);
}
int ft_putptr(void *nbr)
{
int count;
count = 0;
if (!nbr)
return (ft_putstr("(nil)"));
else
{
count += ft_putstr("0x");
count += ft_puthex((long unsigned int)nbr, 0);
}
return (count);
}