-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_hexa.c
65 lines (59 loc) · 1.96 KB
/
print_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_hexa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abkssiba <abkssiba@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/11 17:37:42 by abkssiba #+# #+# */
/* Updated: 2019/12/14 13:50:14 by abkssiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void to_upper_x(char *str)
{
int i;
i = 0;
while (str[i])
{
str[i] = ft_toupper(str[i]);
i++;
}
}
static void manage_width(t_flags flg, int *count, int p)
{
if (!flg.minus && flg.width)
{
if (!flg.zero)
*count += apply_width_int(flg.width - p);
else if (flg.precision > 0)
*count += apply_width_int(flg.width - p);
else if (flg.precision == 0)
*count += apply_width_int(flg.width - p);
}
}
int print_hexa(va_list *arg, t_flags flg)
{
int count;
char *res;
int len;
int p;
unsigned int nbr;
nbr = va_arg(*arg, unsigned int);
count = 0;
res = (!nbr && !flg.precision) ? ft_strdup("") : to_hexa_uint(nbr);
if (flg.specifier == 'X')
to_upper_x(res);
len = ft_strlen(res);
p = (flg.precision != -1 && flg.precision > len)
? flg.precision : len;
manage_width(flg, &count, p);
if (!flg.minus && flg.zero && flg.precision == -1)
count += apply_zero_int(flg.width - len);
count += apply_zero_int(p - len);
count += ft_putstr(res);
if (flg.minus)
count += apply_width_int(flg.width - p);
free(res);
return (count);
}