-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_print_p.c
74 lines (69 loc) · 2.09 KB
/
ft_print_p.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_p.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hboudar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/29 22:20:45 by hboudar #+# #+# */
/* Updated: 2024/05/30 16:08:29 by hboudar ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_fprintf.h"
static int ft_treat_nbr(int fd, unsigned long long nbr, int *count, int op)
{
int len;
len = 2;
if (op == 1)
{
if (nbr < 0)
{
len++;
nbr *= -1;
}
else if (nbr == 0)
return (3);
while (nbr != 0)
{
len++;
nbr /= 16;
}
}
else
{
if (nbr > 15)
ft_treat_nbr(fd, nbr / 16, count, 2);
*count += ft_write_c(fd, "0123456789abcdef"[nbr % 16]);
}
return (len);
}
static void ft_treat_flag(int fd, unsigned long long nbr, t_menu *menu, int *count)
{
(*menu).lenght = 2;
ft_write_s(fd, "0x", count, menu);
if (menu->precision && !nbr)
return ;
ft_treat_nbr(fd, nbr, count, 2);
}
void ft_print_p(int fd, unsigned long long nbr, t_menu *menu, int *count, char c)
{
if (menu->width < ft_treat_nbr(fd, nbr, count, 1))
ft_treat_flag(fd, nbr, menu, count);
else if (menu->minus)
{
menu->zero = 0;
ft_treat_flag(fd, nbr, menu, count);
if (menu->precision && !nbr)
menu->width++;
while (menu->width-- - ft_treat_nbr(fd, nbr, count, 1) > 0)
*count += ft_write_c(fd, c);
}
else
{
if (menu->precision && !nbr)
menu->width++;
while (menu->width-- - ft_treat_nbr(fd, nbr, count, 1) > 0)
*count += ft_write_c(fd, c);
ft_treat_flag(fd, nbr, menu, count);
}
}