-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_bonus.c
109 lines (100 loc) · 2.48 KB
/
ft_printf_bonus.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
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmendes- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/26 13:31:44 by tmendes- #+# #+# */
/* Updated: 2020/08/07 08:15:21 by tmendes- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf_bonus.h"
static char *str_srch(char *begin, char *set)
{
int i;
char *conv;
i = 0;
while (*(begin + i))
{
conv = ft_strchr(set, *(begin + i));
if (conv)
return (begin + i);
i++;
}
return (NULL);
}
static t_printf ptf_loop(t_printf ptf, va_list ap)
{
*ptf.begin = 0;
ptf.begin++;
if (!(ptf.final = ft_concat(ptf.final, ptf.ptr)) ||
!(ptf.end = str_srch(ptf.begin, "cspdiuxX%nfgeo")))
{
ptf.rtrn = -1;
return (ptf);
}
ptf = format_txt(ptf, ap);
if (!(ptf.final = ft_concat(ptf.final, ptf.txt)) ||
!(ptf.ptr = ft_memmove(ptf.ptr, (ptf.end + 1),
ft_strlen(ptf.end + 1) + 1)))
{
ptf.rtrn = -1;
return (ptf);
}
free(ptf.txt);
ptf.txt = NULL;
return (ptf);
}
static t_printf printf_str(const char *format, t_printf ptf, va_list ap)
{
ptf.ptr = ft_strdup(format);
ptf.final = ft_strdup("");
while (ft_strlen(ptf.ptr))
{
if ((ptf.begin = ft_strchr(ptf.ptr, '%')))
ptf = ptf_loop(ptf, ap);
else
{
ptf.final = ft_concat(ptf.final, ptf.ptr);
*ptf.ptr = 0;
}
}
return (ptf);
}
static t_printf write_ptf(t_printf ptf)
{
int k;
ptf.len = ft_strlen(ptf.final);
if (ptf.pstn)
{
k = 0;
while (*(ptf.pstn + k) != -1)
{
ptf.final[*(ptf.pstn + k)] = 0;
k++;
}
}
write(1, ptf.final, ptf.len);
ptf = end_printf(ptf);
return (ptf);
}
int ft_printf(const char *format, ...)
{
t_printf ptf;
va_list ap;
va_start(ap, format);
ptf = init_printf();
ptf = printf_str(format, ptf, ap);
va_end(ap);
if (ptf.final != NULL && ptf.rtrn != -1)
{
ptf = write_ptf(ptf);
return (ptf.len);
}
else
{
ptf = end_printf(ptf);
return (-1);
}
}