-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_int_digits.c
82 lines (76 loc) · 2.3 KB
/
print_int_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_int_digits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alex <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/13 17:26:04 by alex #+# #+# */
/* Updated: 2021/01/13 17:53:51 by alex ### ########.fr */
/* */
/* ************************************************************************** */
#include "printfhead.h"
void ft_print_positive_int(int n, t_struct *blocks, int base)
{
if (blocks->f_minus)
{
ft_print_precision_digits(blocks);
ft_putnbr_base(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_zero_valued_int(int n, t_struct *blocks, int base)
{
if (blocks->f_prec && !blocks->precision)
{
if (!blocks->f_width || n == 0)
blocks->chars_to_print = 0;
ft_print_width_digits(blocks);
return ;
}
ft_print_positive_int(-n, blocks, base);
}
void ft_order_to_print_digits(t_struct *blocks, int n, int base)
{
if (n > 0)
ft_print_positive_int(n, blocks, base);
if (n == 0)
ft_print_zero_valued_int(n, blocks, base);
if (n < 0 && base == 10)
ft_print_negative_int(n, blocks, base);
if (n < 0 && base != 10)
ft_print_positive_int(n, blocks, base);
}
void ft_putnbr_base(int n, t_struct *blocks, int base)
{
static char base_blocks[17] = "0123456789ABCDEF\0";
char result[12];
unsigned int tmp;
int i;
i = 0;
if (n < 0 && base == 10)
tmp = -n;
else
tmp = n;
if (n == 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);
}