-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_unbr.c
89 lines (80 loc) · 2.77 KB
/
print_unbr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_unbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrios-es <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 14:43:03 by mrios-es #+# #+# */
/* Updated: 2023/11/01 14:43:03 by mrios-es ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_number_with_precision(unsigned long nbr, t_flags *flags)
{
int char_count;
int num_len;
int pad_count;
char_count = 0;
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_unum_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(t_flags *flags)
{
int width_change;
width_change = min(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(unsigned 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_unbr(unsigned int nbr, t_flags *flags)
{
int num_len;
int precision_count;
int pad_count;
int char_count;
int width_change;
width_change = min(flags->signed_form + flags->space_sign, 1);
num_len = count_unum_len(nbr);
if (nbr == 0 && flags->precision == 0)
num_len = 0;
update_flags(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);
}