-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_nbr.c
98 lines (89 loc) · 2.94 KB
/
print_nbr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_nbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrios-es <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/24 21:47:31 by mrios-es #+# #+# */
/* Updated: 2023/10/24 23:10:36 by mrios-es ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_number_with_precision(long nbr, t_flags *flags)
{
int char_count;
int is_negative;
int num_len;
int pad_count;
char_count = 0;
is_negative = nbr < 0;
if (is_negative)
{
char_count += ft_putchar_fd('-', 1);
nbr *= -1;
}
else if (flags->signed_form)
char_count += ft_putchar_fd('+', 1);
else if (flags->space_sign)
char_count += ft_putchar_fd(' ', 1);
if (nbr == 0 && flags->pad_char != '0' && flags->precision == 0)
return (char_count);
num_len = count_num_len(nbr);
pad_count = max(flags->precision - num_len, 0);
char_count += print_repeat('0', pad_count);
char_count += ft_putnbr_fd(nbr, 1);
return (char_count);
}
static void update_flags(int nbr, t_flags *flags)
{
int is_negative;
int width_change;
is_negative = nbr < 0;
width_change = min(is_negative + flags->signed_form + flags->space_sign, 1);
if (flags->pad_char == '0')
{
if (flags->precision < 0)
flags->precision = flags->min_width - width_change;
else
flags->pad_char = ' ';
}
}
static int print_aligned(int nbr, int pad_count, t_flags *flags)
{
int char_count;
char_count = 0;
if (flags->left_align)
{
char_count += print_number_with_precision(nbr, flags);
char_count += print_repeat(flags->pad_char, pad_count);
}
else
{
char_count += print_repeat(flags->pad_char, pad_count);
char_count += print_number_with_precision(nbr, flags);
}
return (char_count);
}
int print_nbr(int nbr, t_flags *flags)
{
int num_len;
int precision_count;
int pad_count;
int char_count;
int width_change;
width_change = min((nbr < 0) + flags->signed_form + flags->space_sign, 1);
num_len = count_num_len(nbr);
if (nbr == 0 && flags->precision == 0)
num_len = 0;
update_flags(nbr, flags);
precision_count = flags->precision - num_len;
precision_count = max(precision_count, 0);
width_change = num_len + width_change + precision_count;
pad_count = flags->min_width - width_change;
pad_count = max(pad_count, 0);
char_count = print_aligned(nbr, pad_count, flags);
if (char_count != width_change + pad_count)
char_count = -1;
return (char_count);
}