-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_write_funcs2.c
101 lines (93 loc) · 2.43 KB
/
ft_printf_write_funcs2.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
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_write_funcs2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: toramo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 12:02:20 by toramo #+# #+# */
/* Updated: 2023/11/08 12:02:23 by toramo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void pointer_to_hex_str(char *str, unsigned long int n)
{
int temp[32];
int i;
int j;
i = 0;
j = 0;
while (n || i == 0)
{
temp[i] = n % 16;
n = n / 16;
i++;
}
i--;
while (i > -1)
{
if (temp[i] < 10)
str[j] = temp[i] + 48;
else
str[j] = temp[i] + 87;
i--;
j++;
}
str[j] = 0;
}
void unsigned_to_hex_str(char *str, unsigned int n, t_printf *tab, int i)
{
int temp[16];
int j;
j = 0;
if (tab -> dot == 1 && tab -> precision == 0 && n == 0)
return ;
while (n || i == 0)
{
temp[i] = n % 16;
n = n / 16;
i++;
}
i--;
while (i > -1)
{
if (temp[i] < 10)
str[j] = temp[i] + 48;
else
str[j] = temp[i] + 87 + tab -> type - 120;
i--;
j++;
}
str[j] = 0;
}
void printf_write_pointer(t_printf *tab)
{
char *str;
unsigned long int i;
int padsize;
i = (unsigned long int)tab -> arg;
str = malloc(sizeof(char) * 64);
if (!str)
return ;
pointer_to_hex_str(str, i);
if (tab -> precision > ft_hexlength(str, tab))
padsize = tab -> width - tab -> precision - 2;
else
padsize = tab -> width - ft_hexlength(str, tab) - 2;
if (tab -> minus == 0)
write_pad_chars(' ', padsize, tab);
tab -> written += write(1, "0x", 2);
if (tab -> precision > (int)ft_strlen(str))
write_pad_chars('0', tab -> precision - ft_strlen(str), tab);
tab -> written += write(1, str, ft_strlen(str));
if (tab -> minus == 1)
write_pad_chars(' ', padsize, tab);
free(str);
}
int ft_hexlength(char *str, t_printf *tab)
{
if ((int)ft_strlen(str) >= tab -> precision)
return (ft_strlen(str));
else
return (tab -> precision);
}