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
102
103
104
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_put_width.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joelecle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:06:45 by joelecle #+# #+# */
/* Updated: 2020/02/14 17:06:45 by joelecle ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdlib.h>
static int
ft_printf_get_len(t_printflist *pflist)
{
int len;
int width;
int prec;
width = pflist->width;
prec = pflist->precision;
if (pflist->conv == 'd' && prec > pflist->putlen && !pflist->isneg)
len = (width - pflist->putlen - (prec - pflist->putlen));
else if (pflist->conv == 'd' && prec >= pflist->putlen && (pflist->isneg))
len = (width - pflist->putlen - 1 - (prec - pflist->putlen));
else if (pflist->conv == 'p' && prec > pflist->putlen)
len = (width - pflist->putlen - (prec - pflist->putlen + 2));
else if (pflist->conv == 's' && prec > pflist->putlen && pflist->isaz == 1)
len = (width - pflist->putlen - (prec - pflist->putlen));
else
len = (width - pflist->putlen);
if (pflist->conv == 'd' && pflist->isaspace == 1 && !pflist->isaplus
&& (pflist->putlen < 3 && !pflist->isneg))
len -= 1;
(len < 0) ? (len = 0) : 0;
return (len);
}
/*
** Puts the (correct) amount of spaces
** when needed using pflist->width
*/
void
ft_printf_put_width_pre(t_printflist *pflist)
{
char *str;
if (pflist->conv == 'd' && pflist->isaspace == 1 && !pflist->isaplus
&& (pflist->putlen < 3 && !pflist->isneg))
ft_printf_cat_output(" ", 1, pflist);
if (pflist->width - pflist->putlen > 0 && pflist->width - pflist->putlen
- (pflist->precision - pflist->putlen) >= 0 && !pflist->isreverse)
{
if (!(str = malloc((ft_printf_get_len(pflist) + 1) * sizeof(char))))
return ;
str = ft_memset(str, ' ', ft_printf_get_len(pflist));
*(str + ft_printf_get_len(pflist)) = '\0';
ft_printf_cat_output(str, ft_strlen(str), pflist);
ft_memdel((void**)&str);
}
else if (pflist->conv == 's' && pflist->width - pflist->putlen > 0
&& !pflist->isreverse)
{
if (!(str = malloc((ft_printf_get_len(pflist) + 1) * sizeof(char))))
return ;
str = ft_memset(str, ' ', ft_printf_get_len(pflist));
*(str + ft_printf_get_len(pflist)) = '\0';
ft_printf_cat_output(str, ft_strlen(str), pflist);
ft_memdel((void**)&str);
}
}
void
ft_printf_put_width_post(t_printflist *pflist)
{
char *str;
if (pflist->width - pflist->putlen > 0 && pflist->width - pflist->putlen
- (pflist->precision - pflist->putlen) >= 0 && pflist->isreverse)
{
if (!(str = (char*)malloc((ft_printf_get_len(pflist) + 1)
* sizeof(char))))
return ;
str = ft_memset(str, ' ', ft_printf_get_len(pflist));
*(str + ft_printf_get_len(pflist)) = '\0';
ft_printf_cat_output(str, ft_strlen(str), pflist);
ft_memdel((void**)&str);
}
else if (pflist->conv == 's' && pflist->width - pflist->putlen > 0
&& pflist->isreverse)
{
if (!(str = (char*)malloc((ft_printf_get_len(pflist) + 1)
* sizeof(char))))
return ;
str = ft_memset(str, ' ', ft_printf_get_len(pflist));
*(str + ft_printf_get_len(pflist)) = '\0';
ft_printf_cat_output(str, ft_strlen(str), pflist);
ft_memdel((void**)&str);
}
}
|