-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_x.c
127 lines (117 loc) · 3.33 KB
/
ft_x.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_xx.c :+: :+: */
/* +:+ */
/* By: greed <[email protected]> +#+ */
/* +#+ */
/* Created: 2019/11/15 13:39:27 by greed #+# #+# */
/* Updated: 2019/11/21 13:22:27 by greed ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "libftprintf.h"
void ft_print_x(t_conv *conv, va_list a_list, int *lv)
{
unsigned int num;
num = va_arg(a_list, unsigned int);
ft_conv_x(conv, &num);
if (conv->hash && num != 0 && (conv->left || conv->padzero))
ft_putstr_c_fd("0x", 1, 2, lv);
if (conv->left)
{
ft_pad_width(conv->precision, conv->numlen, '0', lv);
if (conv->precision)
ft_x_res_c_fd(num, lv);
}
if (!conv->left && conv->padzero)
ft_pad_width(conv->width, conv->precision + (conv->hash * 2), '0', lv);
else
ft_pad_width(conv->width, conv->precision + (conv->hash * 2), ' ', lv);
if (!conv->left)
{
if (conv->hash && num != 0 && !conv->padzero)
ft_putstr_c_fd("0x", 1, 2, lv);
ft_pad_width(conv->precision, conv->numlen, '0', lv);
if (conv->precision)
ft_x_res_c_fd(num, lv);
}
}
void ft_conv_x(t_conv *conv, unsigned int *num)
{
conv->numlen = ft_x_size(*num);
if (conv->precision != -2)
conv->padzero = 0;
if (*num == 0)
conv->hash = 0;
if (conv->precision == -2 || (conv->precision < conv->numlen && *num != 0))
conv->precision = conv->numlen;
}
void ft_x_res_c_fd(unsigned int num, int *lv)
{
unsigned int res;
unsigned int power;
char *hex;
hex = "0123456789abcdef";
res = num;
power = 1;
while (res / 16)
{
res /= 16;
power *= 16;
}
res = num;
while (power)
{
ft_putchar_c_fd(hex[num / power], 1, lv);
num %= power;
power /= 16;
}
}
void ft_upx_res_c_fd(unsigned int num, int *lv)
{
unsigned int res;
unsigned int power;
char *hex;
hex = "0123456789ABCDEF";
res = num;
power = 1;
while (res / 16)
{
res /= 16;
power *= 16;
}
res = num;
while (power)
{
ft_putchar_c_fd(hex[num / power], 1, lv);
num = num % power;
power /= 16;
}
}
void ft_print_up_x(t_conv *conv, va_list a_list, int *lv)
{
unsigned int num;
num = va_arg(a_list, unsigned int);
ft_conv_x(conv, &num);
if (conv->hash && num != 0 && (conv->left || conv->padzero))
ft_putstr_c_fd("0X", 1, 2, lv);
if (conv->left)
{
ft_pad_width(conv->precision, conv->numlen, '0', lv);
if (conv->precision)
ft_upx_res_c_fd(num, lv);
}
if (!conv->left && conv->padzero)
ft_pad_width(conv->width, conv->precision + (conv->hash * 2), '0', lv);
else
ft_pad_width(conv->width, conv->precision + (conv->hash * 2), ' ', lv);
if (!conv->left)
{
if (conv->hash && num != 0 && !conv->padzero)
ft_putstr_c_fd("0X", 1, 2, lv);
ft_pad_width(conv->precision, conv->numlen, '0', lv);
if (conv->precision)
ft_upx_res_c_fd(num, lv);
}
}