-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_utils.c
More file actions
95 lines (84 loc) · 2.37 KB
/
ft_printf_utils.c
File metadata and controls
95 lines (84 loc) · 2.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sarfreit <sarfreit@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/16 03:36:37 by sarfreit #+# #+# */
/* Updated: 2025/11/16 03:36:37 by sarfreit ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
// %s: Format as a null-terminated string.
int ft_print_str(char *str)
{
int len;
len = 0;
if (!str)
str = "(null)";
len = ft_strlen(str);
ft_putstr_fd(str, 1);
return (len);
}
// %x, %X (type): Convert to hexadecimal and print it
int ft_puthex_unsigned(unsigned long nbr, char type)
{
int len;
char hex;
len = 0;
if ((nbr >= 16))
len += ft_puthex_unsigned(nbr / 16, type);
hex = "0123456789abcdef"[nbr % 16];
if ((type == 'X') && ((hex >= 'a') && (hex <= 'f')))
hex -= 32;
ft_putchar_fd(hex, 1);
len++;
return (len);
}
// %p: The void * pointer argument has to be printed in hexadecimal format
int ft_print_ptr(void *ptr)
{
int len;
unsigned long int hex;
if (!ptr)
{
ft_putstr_fd("(nil)", 1);
return (5);
}
hex = (long unsigned int)ptr;
ft_putstr_fd("0x", 1);
len = 2;
len += ft_puthex_unsigned(hex, 'p');
return (len);
}
//%d %i (type): Print nbr in base 10 (numbers with signs)
int ft_print_base(int nbr)
{
int len;
long int num;
len = 0;
num = nbr;
if (num < 0)
{
ft_putchar_fd('-', 1);
num = -num;
len += 1;
}
if (num >= 10)
len += ft_print_base(num / 10);
ft_putnbr_fd((num % 10), 1);
len++;
return (len);
}
// %u: Print an unsigned decimal in base 10
int ft_print_base_unsigned(unsigned int nbr)
{
int len;
len = 0;
if (nbr >= 10)
len += ft_print_base_unsigned(nbr / 10);
ft_putnbr_fd((nbr % 10), 1);
len++;
return (len);
}