-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_u_digits.c
90 lines (84 loc) · 2.36 KB
/
print_u_digits.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_u_digits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alex <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/13 17:09:29 by alex #+# #+# */
/* Updated: 2021/01/13 17:41:47 by alex ### ########.fr */
/* */
/* ************************************************************************** */
#include "printfhead.h"
void ft_print_zero_valued_u(unsigned int n,
t_struct *blocks, int base)
{
if (blocks->f_prec && !blocks->precision)
{
blocks->chars_to_print = 0;
ft_print_width_digits(blocks);
return ;
}
if (blocks->f_minus)
{
ft_print_precision_digits(blocks);
ft_putnbr_base_u(n, blocks, base);
ft_print_width_digits(blocks);
}
else
{
ft_print_width_digits(blocks);
ft_print_precision_digits(blocks);
ft_putnbr_base(n, blocks, base);
}
}
void ft_print_positive_u(unsigned int n,
t_struct *blocks, int base)
{
if (blocks->f_minus)
{
ft_print_precision_digits(blocks);
ft_putnbr_base_u(n, blocks, base);
ft_print_width_digits(blocks);
}
else
{
ft_print_width_digits(blocks);
ft_print_precision_digits(blocks);
ft_putnbr_base_u(n, blocks, base);
}
}
void ft_order_to_print_u_digits(t_struct *blocks,
unsigned int n, int base)
{
if (n > 0)
ft_print_positive_u(n, blocks, base);
if (n == 0)
ft_print_zero_valued_u(n, blocks, base);
}
void ft_putnbr_base_u(unsigned int n, t_struct *blocks, int base)
{
static char base_blocks[17] = "0123456789ABCDEF\0";
char result[15];
int i;
unsigned int tmp;
i = 0;
tmp = n;
if (tmp == 0)
{
result[i] = '0';
blocks->total_printed += write(1, &result[i], 1);
return ;
}
while (tmp > 0)
{
result[i] = ft_check_hex(blocks->format, base_blocks[tmp % base]);
tmp /= base;
i++;
}
i--;
while (i > -1)
{
blocks->total_printed += write(1, &result[i--], 1);
}
}