-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibft_sub_ft.c
More file actions
93 lines (88 loc) · 3.21 KB
/
libft_sub_ft.c
File metadata and controls
93 lines (88 loc) · 3.21 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft_sub_ft.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acastrov <acastrov@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/08 19:33:00 by acastrov #+# #+# */
/* Updated: 2024/10/18 14:47:14 by acastrov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "printf.h"
// Writes a given string
int ft_putstr_printf(char *s)
{
if (!s) // We check for null string and print warning
return ((int)write(1, "(null)", 6));
if (*s) // We print the entire string, managing the output bites via ft_strlen
return ((int)write(1, s, ft_strlen(s)));
return (0);
}
// Writes an int
int ft_putnbr_printf(int n)
{
int d;
int i;
if (n == -2147483648) // Hardcoding MIN_INT to avoid negative to positive conflicts
{
return ((int)write(1, "-2147483648", 11));
}
i = 0;
if (n < 0)
{
i += (int)write(1, "-", 1); // We manage negative numbers and convert them to positive
n = -n;
}
if (n >= 10)
{
i += ft_putnbr_printf(n / 10); // Recursive call of putnbr_printf, so we print in the correct order
}
d = (n % 10) + '0'; // Conversion of int to char
i += (int)write(1, &d, 1); // We print the modulus of 10
return (i);
}
// Writes an unsigned int
int ft_putuint_printf(unsigned int n) // The same as putnbr, but without negative conversion
{
unsigned int d;
unsigned int i;
i = 0;
if (n >= 10)
i += ft_putuint_printf(n / 10);
d = (n % 10) + '0';
i += (unsigned int)write(1, &d, 1);
return (i);
}
// Writes a hex decimal number
int ft_hex(unsigned int n, char specifier) // We print a number in hex decimal in upper or lower casse, according to the specifier
{
int i; // Return value
char *hex_base; // Chars of hexbase
if (specifier == 'X') // Only write in upper case if specified
hex_base = "0123456789ABCDEF";
else
hex_base = "0123456789abcdef";
i = 0;
if (n >= 16) // Recursive call to ft_hex, with n divided by 16
i += ft_hex(n / 16, specifier);
i += (int)write(1, &hex_base[n % 16], 1); // We print the hex_base index position
return (i);
}
// Writes a pointer address
int ft_ptr(uintptr_t ptr, int flag)
{
int i;
char *hex_base;
if (!ptr)
return ((int)write(1, "(nil)", 5)); // Warning for null pointer
i = 0;
hex_base = "0123456789abcdef";
if (flag == 1) // As we are going to call ft_ptr on recursive, we have to manage the first 0x. When we call ft_ptr from the switcher, it will print 0x just once thx to 1 flag
i += (int)write(1, "0x", 2);
if (ptr > 16)
i += ft_ptr(ptr / 16, 0); // The recursive call of ft_ptr is with 0 flag, wich avoids repeating the 0x prefix
i += (int)write(1, &hex_base[ptr % 16], 1); // We print in the hex_base
return (i);
}