-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_print_5hexa.c
97 lines (87 loc) · 3.02 KB
/
ft_print_5hexa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_5hexa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/18 10:07:07 by guilmira #+# #+# */
/* Updated: 2021/09/23 08:23:16 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void itoa_writer_base(unsigned int n, char *ptr, char *base)
{
static int i;
if (n / 16)
itoa_writer_base(n / 16, ptr, base);
else
i = 0;
ptr[i] = base[n % 16];
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_base(unsigned int n, char *base)
{
char *ptr;
ptr = ft_calloc(ft_count_digits_unsigned(n) + 2, sizeof(char));
if (!ptr)
return (NULL);
itoa_writer_base(n, ptr, base);
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 char *init_hexa(unsigned int hexa, int *lenght, t_flag *flag, char *base)
{
char *str;
str = ft_itoa_base(hexa, base);
*lenght = ft_strlen(str);
if (!hexa && flag->precision && !flag->precision_total_digits)
{
*lenght = 0;
flag->precision = -1;
}
return (str);
}
/** PURPOSE : prints %i and %d converter
* Takes into account: Alignment, precision, zero filled */
void print_hexa(unsigned int hexa, t_flag *flag, char *base, char *prefix)
{
int lenght;
int sign;
char *str;
int number_zeros;
if (flag->alternative && flag->alignment_sign == '+' && hexa)
flag->alignment_total_spaces -= 2;
sign = 0;
str = init_hexa(hexa, &lenght, flag, base);
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);
if (flag->alternative && hexa)
{
pf_putstr_fd(prefix, 1, flag);
if (flag->precision_total_digits < flag->zerofilled_total_digits)
number_zeros -= 2;
flag->alignment_total_spaces -= 2;
}
print_end(number_zeros, lenght, str, flag);
free(str);
}