-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_handle_hexa_upper.c
131 lines (119 loc) · 3.72 KB
/
ft_handle_hexa_upper.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
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_handle_hexa_upper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebassi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 12:30:20 by ebassi #+# #+# */
/* Updated: 2022/02/02 11:50:20 by ebassi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_handle_hexa_pl_wdth_up(t_params *my_struct, unsigned int arg, int len, \
char *len_nbr);
int ft_handle_hexa_point_up(int nbr, char *len_nbr, unsigned int arg, int len);
int ft_handle_hexa_zeros_up(int nbr, char *len_nbr, int len, unsigned int arg);
int ft_handle_hexa_all_up(t_params *my_struct, int len, unsigned int arg, \
int nbr);
int ft_handle_hexa_upper(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_up(my_struct, len, arg, nbr);
else
len += ft_putnbr_base_x(arg, "0123456789ABCDEF");
free(len_nbr);
return (len);
}
int ft_handle_hexa_pl_wdth_up(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_up(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_up(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_up(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_up(nbr, len_nbr, len, arg);
else if (my_struct->plus || (my_struct->wdth && !my_struct->point))
len += ft_handle_hexa_pl_wdth_up(my_struct, arg, len, len_nbr);
else if (my_struct->point)
len += ft_handle_hexa_point_up(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);
}