-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_write_funcs3.c
101 lines (92 loc) · 3.45 KB
/
ft_printf_write_funcs3.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
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_write_funcs3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: toramo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 12:02:47 by toramo #+# #+# */
/* Updated: 2023/11/08 12:02:49 by toramo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void printf_write_percentage(t_printf *tab)
{
if (tab -> width != 0 && tab -> minus == 0)
write_pad_chars(' ', (tab -> width - 1), tab);
tab -> written += write(1, "%", 1);
if (tab -> width != 0 && tab -> minus == 1)
write_pad_chars(' ', (tab -> width - 1), tab);
}
void printf_write_null(t_printf *tab)
{
int strl;
strl = 6;
if (tab -> dot && 6 > tab -> precision)
strl = tab -> precision;
if (tab -> width != 0 && tab -> minus == 0)
write_pad_chars(' ', (tab -> width - strl), tab);
tab -> written += write (1, "(null)", strl);
if (tab -> width != 0 && tab -> minus == 1)
write_pad_chars(' ', (tab -> width - strl), tab);
}
void printf_write_int_us(t_printf *tab)
{
char pad;
int padsize;
int intlength;
intlength = int_str_size_printf_us((unsigned long int)tab -> arg, tab);
pad = 32;
if (tab -> zero == 1 && tab -> precision == 0)
pad = '0';
if (intlength >= tab -> precision)
padsize = tab -> width - intlength;
else
padsize = tab -> width - tab -> precision;
if (tab -> minus == 0 && padsize > 0)
write_pad_chars(pad, padsize, tab);
if (tab -> precision > intlength)
write_pad_chars('0', ((tab -> precision) - intlength), tab);
if (!(tab -> dot == 1 && tab -> precision == 0
&& (unsigned long int)tab -> arg == 0))
ft_putnbr_fd_us((unsigned long int)tab -> arg, 1);
tab -> written += intlength;
if (tab -> minus == 1 && padsize > 0)
write_pad_chars(' ', padsize, tab);
}
void printf_write_hex(t_printf *tab)
{
char *str;
unsigned int i;
int padsize;
char pad;
i = (unsigned long int)tab -> arg;
str = ft_calloc(64, sizeof(char));
if (!str)
return ;
pad = 32;
if (tab -> zero == 1 && tab -> dot == 0)
pad = '0';
unsigned_to_hex_str(str, i, tab, 0);
padsize = tab -> width - ft_hexlength(str, tab);
if (tab -> alt && i != 0)
padsize = padsize - 2;
if (tab -> minus == 0 && padsize > 0 && pad == ' ')
write_pad_chars(pad, padsize, tab);
printf_write_hex2(tab, str, padsize, pad);
}
void printf_write_hex2(t_printf *tab, char *str, int padsize, char pad)
{
if (tab -> alt && (unsigned long int)tab -> arg != 0 && tab -> type == 'x')
tab -> written += write(1, "0x", 2);
if (tab -> alt && (unsigned long int)tab -> arg != 0 && tab -> type == 'X')
tab -> written += write(1, "0X", 2);
if (tab -> minus == 0 && padsize > 0 && pad == '0')
write_pad_chars(pad, padsize, tab);
if (tab -> precision > (int)ft_strlen(str))
write_pad_chars('0', tab -> precision - ft_strlen(str), tab);
tab -> written += write(1, str, ft_strlen(str));
if (tab -> minus == 1 && padsize > 0)
write_pad_chars(pad, padsize, tab);
free(str);
}