-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_printf_flags.c
78 lines (73 loc) · 2.64 KB
/
ft_printf_flags.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_flags.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/20 15:03:08 by apuchill #+# #+# */
/* Updated: 2020/05/30 23:13:49 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void print_width(int *len, t_flags fl)
{
if (fl.sign == '-' || (fl.plus == 1 && fl.sign == '+') ||
(fl.space == 1 && fl.plus == 0 && fl.sign == '+'))
fl.width--;
if (fl.precision <= (int)fl.strlen)
fl.precision = (int)fl.strlen;
if (fl.pad_c == '0' && (fl.minus == 1 || fl.point == 1) &&
!(fl.spe_c == 'f' && fl.minus == 0))
fl.pad_c = ' ';
while (fl.width > fl.precision)
{
ft_putchar_len(fl.pad_c, len);
fl.width--;
}
}
static void print_zeros(int *len, t_flags fl)
{
int j;
if ((fl.spe_c == 'x' && fl.hash == 1) || fl.spe_c == 'p')
ft_putcstr_len("0x", len, 2);
if (fl.spe_c == 'X' && fl.hash == 1)
ft_putcstr_len("0X", len, 2);
if (fl.spe_c == 'o' && fl.hash == 1)
ft_putchar_len('0', len);
if (fl.minus == 0 && fl.pad_c == '0')
print_width(len, fl);
if (fl.point == 1)
{
j = fl.precision;
while (j-- > (int)fl.strlen)
ft_putchar_len('0', len);
}
}
void print_flags(int *len, t_flags fl)
{
fl.strlen = ft_strlen(fl.a);
if (fl.ulli == 0 && fl.point == 1 && fl.precision == 0 && fl.print_n0 == 0)
fl.width++;
if (((fl.spe_c == 'x' || fl.spe_c == 'X') && fl.hash == 1) ||
fl.spe_c == 'p')
fl.width -= 2;
if (fl.spe_c == 'o' && fl.hash == 1)
fl.width -= 1;
if (fl.spe_c == 'o' && fl.hash == 1 && fl.point == 1)
fl.precision -= 1;
if (fl.point == 1 && fl.spe_c != 'f')
fl.pad_c = ' ';
if (fl.minus == 0 && fl.pad_c == ' ')
print_width(len, fl);
if (fl.sign == '-' || (fl.plus == 1 && fl.sign == '+'))
ft_putchar_len(fl.sign, len);
if (fl.space == 1 && fl.plus == 0 && fl.sign == '+')
ft_putchar_len(' ', len);
print_zeros(len, fl);
if (!(fl.ulli == 0 && fl.point == 1 && fl.precision == 0) ||
fl.print_n0 == 1)
ft_putcstr_len(fl.a, len, ft_strlen(fl.a));
if (fl.minus == 1)
print_width(len, fl);
}