-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_print_string.c
106 lines (99 loc) · 2.65 KB
/
ft_print_string.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
102
103
104
105
106
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tblaase <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/03 10:03:34 by tblaase #+# #+# */
/* Updated: 2021/07/13 15:34:19 by tblaase ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static void ft_precision_str_helper(t_print *tab, char *str, int len, int i)
{
if (tab->precision < tab->width && tab->dash)
{
while (i < len)
{
tab->printlen = tab->printlen + write(1, &str[i], 1);
i++;
}
while (i < tab->width)
{
tab->printlen = tab->printlen + write(1, &" ", 1);
i++;
}
}
else
{
while (i < tab->precision)
{
tab->printlen = tab->printlen + write(1, &str[i], 1);
i++;
}
}
}
static void ft_precision_str(t_print *tab, char *str)
{
int i;
int c;
int len;
i = 0;
c = 0;
len = (int)ft_strlen(str);
if (len > tab->precision)
len = tab->precision;
if (tab->precision < tab->width && !tab->dash)
{
while (i < (tab->width - len))
{
tab->printlen = tab->printlen + write(1, &" ", 1);
i++;
}
while (i < tab->width)
{
tab->printlen = tab->printlen + write(1, &str[c], 1);
c++;
i++;
}
}
ft_precision_str_helper(tab, str, len, i);
}
static void ft_is_string_help(t_print *tab, int len, char *str)
{
if (tab->width > len && !tab->dash)
{
ft_width_right(tab, len);
tab->printlen = tab->printlen + ft_putstr_fd(str, 1);
}
else if (tab->width > len && tab->dash)
{
tab->printlen = tab->printlen + ft_putstr_fd(str, 1);
ft_width_left(tab, len);
}
else
tab->printlen = tab->printlen + ft_putstr_fd(str, 1);
}
void ft_is_string(t_print *tab)
{
char *str;
int len;
str = va_arg(tab->args, char *);
if (!str)
{
str = "(null)";
}
len = ft_strlen(str);
if ((tab->precision >= len || tab->precision == -1) && tab->width <= len)
tab->printlen = tab->printlen + ft_putstr_fd(str, 1);
else if (tab->precision < len && tab->precision > 0)
ft_precision_str(tab, str);
else if (tab->precision == 0)
{
ft_width_right(tab, 0);
return ;
}
else
ft_is_string_help(tab, len, str);
}