-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_print_hex.c
79 lines (71 loc) · 2.35 KB
/
ft_print_hex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcombeau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/11 15:30:24 by mcombeau #+# #+# */
/* Updated: 2021/12/17 15:27:40 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_print_x_prefix(int is_upper)
{
if (is_upper == 1)
ft_print_s("0X");
else
ft_print_s("0x");
return (2);
}
int ft_print_x(char *nbstr, int n, int is_upper, t_flags flags)
{
int count;
count = 0;
if (flags.zero == 0 && flags.hash == 1 && n != 0)
count += ft_print_x_prefix(is_upper);
if (flags.precision >= 0)
count += ft_pad_width(flags.precision - 1,
ft_strlen(nbstr) - 1, 1);
count += ft_print_s(nbstr);
return (count);
}
int ft_print_hexadec(char *nbstr, int n, int is_upper, t_flags flags)
{
int count;
count = 0;
if (flags.zero == 1 && flags.hash == 1 && n != 0)
count += ft_print_x_prefix(is_upper);
if (flags.left == 1)
count += ft_print_x(nbstr, n, is_upper, flags);
if (flags.precision >= 0 && (size_t)flags.precision < ft_strlen(nbstr))
flags.precision = ft_strlen(nbstr);
if (flags.precision >= 0)
{
flags.width -= flags.precision;
count += ft_pad_width(flags.width, 0, 0);
}
else
count += ft_pad_width(flags.width,
ft_strlen(nbstr) + (flags.hash * 2), flags.zero);
if (flags.left == 0)
count += ft_print_x(nbstr, n, is_upper, flags);
return (count);
}
int ft_print_hex(unsigned int n, int is_upper, t_flags flags)
{
char *nbstr;
int count;
count = 0;
if (flags.precision == 0 && n == 0)
{
count += ft_pad_width(flags.width, 0, 0);
return (count);
}
nbstr = ft_printf_xtoa(n, is_upper);
if (!nbstr)
return (0);
count += ft_print_hexadec(nbstr, n, is_upper, flags);
free(nbstr);
return (count);
}