-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexa.c
103 lines (94 loc) · 2.71 KB
/
hexa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hexa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: moboustt <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/12 20:29:34 by moboustt #+# #+# */
/* Updated: 2019/12/20 23:24:08 by moboustt ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "ft_printf.h"
char *hexa_convert(t_struct *list, unsigned long deci, char s, char *r)
{
unsigned long res;
res = 0;
ft_memset(r, 0, 100);
while (deci != 0)
{
res = deci % 16;
if (res < 10)
res += 48;
else
{
if (s == 'A')
res += 55;
if (s == 'a')
res += 87;
}
r[list->h++] = res;
deci = deci / 16;
}
return (ft_strrev(r));
}
void handle_widthprecision(t_struct *list, unsigned int hexa_len)
{
if (list->dot && list->precision > (int)hexa_len)
list->precision -= hexa_len;
else
list->precision = 0;
list->width = list->width - (list->precision + hexa_len);
}
void is_hexa(const char *fmt, t_struct *list, va_list ap)
{
char *tb;
long out_hexa;
char *r;
r = malloc(100 * sizeof(char));
out_hexa = va_arg(ap, unsigned int);
if (zero_input(list, out_hexa) == 1)
return ;
else if (out_hexa == 0 && list->dot == 1 && list->width > 0 &&
(list->precision == 0 || list->precision == -1))
tb = "";
else if (out_hexa == 0)
tb = "0";
else
tb = hexa_convert(list, out_hexa, (fmt[list->i] == 'x') ? 'a' : 'A', r);
if (pppp(list, ft_strlen(tb), tb) == 1)
return ;
else
handle_widthprecision(list, ft_strlen(tb));
if (fmt[list->i] == 'x')
list->minus == 1 ? right_hexa(list, tb) : left_hexa(list, tb);
else
list->minus == 1 ? right_hexa(list, tb) : left_hexa(list, tb);
free(r);
}
void right_hexa(t_struct *list, char *tb)
{
while (list->precision-- > 0)
list->n += write(1, "0", 1);
list->n += print_hexa(tb);
while (list->width-- > 0)
list->n += write(1, " ", 1);
}
void left_hexa(t_struct *list, char *tb)
{
int rc;
rc = '\0';
if (list->zero && !list->dot)
rc = '0';
else
rc = ' ';
while (list->width > 0)
{
list->n += write(1, &rc, 1);
list->width--;
}
while (list->precision-- > 0)
list->n += write(1, "0", 1);
list->n += print_hexa(tb);
}