-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_string.c
98 lines (89 loc) · 2.51 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abkssiba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/07 11:56:01 by abkssiba #+# #+# */
/* Updated: 2019/12/13 14:58:30 by abkssiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_rest(const char *str, char *tmp, int len, t_flags flg)
{
int i;
int count;
count = 0;
i = 0;
if (str[i] != flg.specifier)
{
if (flg.minus == 0)
{
count += apply_width(flg.width, len);
if (flg.precision != -1)
return (count + ft_putstr(tmp));
}
else
{
count += ft_putstr(tmp);
count += apply_width(flg.width, len);
}
i = flg.skip_flags - 1;
}
if ((str[i] == 's' && i > 0 && !flg.minus) || (i == 0))
count += ft_putstr(tmp);
return (count);
}
int print_string(const char *str, va_list *arg, t_flags flg)
{
int count;
char *tmp;
int len;
count = 0;
tmp = va_arg(*arg, char*);
if (!tmp)
tmp = "(null)";
if (flg.precision >= 0)
tmp = apply_precision(tmp, flg.precision);
len = ft_strlen(tmp);
count += print_rest(str, tmp, len, flg);
if (flg.precision >= 0)
free(tmp);
return (count);
}
static int manage_char(char c, char ch, int i, t_flags flg)
{
int count;
count = 0;
if (((c == 'c' || ch == 0 || is_not_specifier(flg.specifier))
&& i > 0 && !flg.minus) || i == 0)
count += ft_putchar(ch);
return (count);
}
int print_char(const char *str, va_list *arg, t_flags flg)
{
int count;
char c;
int i;
i = 0;
count = 0;
c = (is_not_specifier(flg.specifier)) ? flg.specifier : va_arg(*arg, int);
if (str[i] != flg.specifier)
{
if (flg.minus == 0)
{
count = (flg.zero) ? apply_zero(flg.width, 1)
: apply_width(flg.width, 1);
if (flg.precision != -1 || flg.zero)
return (count + ft_putchar(c));
}
else
{
count += ft_putchar(c);
count += apply_width(flg.width, 1);
}
i++;
}
return (count + manage_char(str[i], c, i, flg));
}