-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_print_4integers_unsigned.c
88 lines (79 loc) · 2.7 KB
/
ft_print_4integers_unsigned.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_4integers_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/18 10:07:07 by guilmira #+# #+# */
/* Updated: 2021/09/22 12:19:14 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void itoa_writer_unsigned(unsigned int n, char *ptr)
{
static int i;
if (n / 10 != 0)
itoa_writer_unsigned(n / 10, ptr);
else
i = 0;
ptr[i] = '0' + n % 10;
ptr[++i] = '\0';
}
/** PURPOSE : converts integer into its string equivalent.
* 1. Allocates memory in heap.
* 2. Calls static function itoa-writer. */
static char *ft_itoa_unsigned(unsigned int n)
{
char *ptr;
ptr = ft_calloc(ft_count_digits_unsigned(n) + 2, sizeof(char));
if (!ptr)
return (NULL);
itoa_writer_unsigned(n, ptr);
return (ptr);
}
/** PURPOSE : to output number of zeros that must be printed
* 1. Check all the conditions for zerofilled and precision */
static int check_zeros_n_precision(t_flag *flag, int lenght)
{
if (flag->zerofilled)
return (check_flag_zerofilled(flag, lenght));
else
{
if ((flag->precision_total_digits - lenght) >= 0)
return (flag->precision_total_digits - lenght);
else
return (0);
}
}
/** PURPOSE : evaluates integer and converts it to string. */
static void init_unsig(unsigned int unsig, char **str, \
int *lenght, t_flag *flag)
{
if (!*str)
*str = ft_itoa_unsigned(unsig);
*lenght = ft_strlen(*str);
if (!unsig && flag->precision && !flag->precision_total_digits)
{
*lenght = 0;
flag->precision = -1;
}
}
/** PURPOSE : prints %i and %d converter
* Takes into account: Alignment, precision, zero filled */
void print_integer_unsigned(unsigned int unsig, t_flag *flag)
{
int lenght;
int sign;
char *str;
int number_zeros;
sign = 0;
str = NULL;
init_unsig(unsig, &str, &lenght, flag);
number_zeros = check_zeros_n_precision(flag, lenght);
if (!flag->precision && flag->zerofilled && flag->alignment_sign == '+')
number_zeros = flag->zerofilled_total_digits - lenght;
left_align_int(sign, lenght, number_zeros, flag);
print_end(number_zeros, lenght, str, flag);
free(str);
}