-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_tools_flag.c
97 lines (92 loc) · 3.17 KB
/
ft_printf_tools_flag.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_printf_tools_flag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <guilmira@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/15 13:53:22 by guilmira #+# #+# */
/* Updated: 2021/09/23 08:23:33 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/** PURPOSE : Init struct. Does not intialize counter */
void init_flag(t_flag *flag)
{
flag->signal = '9';
flag->zerofilled = 0;
flag->zerofilled_total_digits = 0;
flag->alignment = 0;
flag->alignment_sign = '+';
flag->alignment_total_spaces = 0;
flag->precision = 0;
flag->precision_total_digits = 0;
flag->invisible_sign = 0;
flag->plus_sign = 0;
flag->alternative = 0;
}
/** PURPOSE : prints integer and unsigned integer left part */
void left_align_int(int sign, int lenght, int number_zeros, t_flag *flag)
{
if (flag->alignment && flag->alignment_sign == '+')
{
if (!sign && (flag->plus_sign || flag->invisible_sign) \
&& (flag->signal == 'i' || flag->signal == 'd'))
flag->alignment_total_spaces--;
if (sign)
flag->alignment_total_spaces--;
while (flag->alignment_total_spaces-- > (lenght + number_zeros))
pf_putchar_fd(' ', 1, flag);
}
if (!sign && flag->invisible_sign \
&& (flag->signal == 'i' || flag->signal == 'd'))
pf_putchar_fd(' ', 1, flag);
if (!sign && flag->plus_sign \
&& (flag->signal == 'i' || flag->signal == 'd'))
pf_putchar_fd('+', 1, flag);
}
/** PURPOSE : print end part for %d, %i and %u
* Used in functions print_integer and print_unsigned. */
void print_end(int number_zeros, int lenght, char *str, t_flag *flag)
{
while (number_zeros > 0)
{
pf_putchar_fd('0', 1, flag);
number_zeros--;
lenght++;
}
if (!ft_strncmp("0", str, 1) && flag->precision == -1)
;
else
pf_putstr_fd(str, 1, flag);
if (flag->alignment && flag->alignment_sign == '-')
while (flag->alignment_total_spaces-- - lenght > 0)
pf_putchar_fd(' ', 1, flag);
}
/** PURPOSE : to output number of zeros
* 1. Check all the conditions in case zerofilled exists */
int check_flag_zerofilled(t_flag *flag, int lenght)
{
if (flag->zerofilled_total_digits < 0)
{
flag->zerofilled_total_digits *= -1;
flag->alignment_sign = '-';
}
if (flag->zerofilled_total_digits > flag->precision_total_digits)
{
flag->alignment = 1;
flag->alignment_total_spaces = flag->alignment_total_spaces + \
(flag->zerofilled_total_digits);
if (flag->precision_total_digits > lenght)
return (flag->precision_total_digits - lenght);
else
return (0);
}
else
{
if (flag->precision_total_digits > lenght)
return (flag->precision_total_digits - lenght);
else
return (0);
}
}