-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_handle_hexa_lower.c
129 lines (117 loc) · 3.66 KB
/
ft_handle_hexa_lower.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_handle_hexa_lower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebassi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 12:26:35 by ebassi #+# #+# */
/* Updated: 2022/02/02 11:49:47 by ebassi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_handle_hexa_pl_wdth(t_params *my_struct, unsigned int arg, int len, \
char *len_nbr);
int ft_handle_hexa_point(int nbr, char *len_nbr, unsigned int arg, int len);
int ft_handle_hexa_zeros(int nbr, char *len_nbr, int len, unsigned int arg);
int ft_handle_hexa_all(t_params *my_struct, int len, unsigned int arg, int nbr);
int ft_handle_hexa_lower(va_list args, t_params *my_struct, int nbr)
{
int len;
unsigned int arg;
int tmp;
char *len_nbr;
int flag;
len = 0;
flag = 0;
tmp = nbr;
arg = va_arg(args, unsigned int);
len_nbr = ft_convert_base(arg, "0123456789abcdef");
if (my_struct->hash)
{
if (arg)
len += write(1, "0x", 2);
len += ft_putnbr_base_x(arg, "0123456789abcdef");
}
else if (my_struct->minus || my_struct->plus || my_struct->point \
|| my_struct->space || my_struct->wdth || my_struct->zeros)
len += ft_handle_hexa_all(my_struct, len, arg, nbr);
else
len += ft_putnbr_base_x(arg, "0123456789abcdef");
free(len_nbr);
return (len);
}
int ft_handle_hexa_pl_wdth(t_params *my_struct, unsigned int arg, \
int len, char *len_nbr)
{
int flag;
flag = 0;
if (my_struct->plus)
{
if (arg > 0)
{
len += write(1, "+", 1);
flag = 1;
}
if (arg < 0)
len += write(1, "-", 1);
}
else if (my_struct->wdth && !my_struct->point)
{
while (my_struct->wdth-- > (int)ft_strlen(len_nbr) - flag)
len += write(1, " ", 1);
len += ft_putnbr_base_x(arg, "0123456789abcdef");
}
return (len);
}
int ft_handle_hexa_point(int nbr, char *len_nbr, unsigned int arg, int len)
{
int tmp;
tmp = nbr;
if (tmp > (int)ft_strlen(len_nbr))
{
while (tmp-- > (int)ft_strlen(len_nbr))
len += ft_putchar('0');
if (arg == 0)
len += ft_putchar('0');
}
if (tmp || arg != 0)
len += ft_putnbr_base_x(arg, "0123456789abcdef");
return (len);
}
int ft_handle_hexa_zeros(int nbr, char *len_nbr, int len, unsigned int arg)
{
int tmp;
tmp = nbr;
while (tmp > (int)ft_strlen(len_nbr))
{
len += write(1, "0", 1);
tmp--;
}
len += ft_putnbr_base_x(arg, "0123456789abcdef");
return (len);
}
int ft_handle_hexa_all(t_params *my_struct, int len, unsigned int arg, int nbr)
{
int tmp;
char *len_nbr;
tmp = nbr;
len_nbr = ft_convert_base(arg, "0123456789abcdef");
if (my_struct->space)
len += write(1, " ", 1);
else if (my_struct->zeros && !my_struct->point)
len += ft_handle_hexa_zeros(nbr, len_nbr, len, arg);
else if (my_struct->plus || (my_struct->wdth && !my_struct->point))
len += ft_handle_hexa_pl_wdth(my_struct, arg, len, len_nbr);
else if (my_struct->point)
len += ft_handle_hexa_point(nbr, len_nbr, arg, len);
else if (my_struct->minus)
{
tmp = nbr;
len += ft_putnbr_base_x(arg, "0123456789abcdef");
while (my_struct->minus-- > (int)ft_strlen(len_nbr))
len += write(1, " ", 1);
}
free(len_nbr);
return (len);
}