-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_vprintf.c
More file actions
91 lines (81 loc) · 2.33 KB
/
ft_vprintf.c
File metadata and controls
91 lines (81 loc) · 2.33 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmerabet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/13 18:17:07 by mmerabet #+# #+# */
/* Updated: 2017/12/17 23:43:37 by mmerabet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "handlers.h"
static t_ret get_ret(char *buffer, int err)
{
t_ret ret;
ret.len = ft_strlen(buffer);
ret.err = err;
ret.buf = ft_strrepc(buffer, -1, 0);
return (ret);
}
t_ret ft_inner_printf(const char *format, t_pcur *ap)
{
char *cs;
char *tmp;
t_ret ret;
ft_init_formats();
cs = NULL;
ret.buf = NULL;
while (*format)
{
if (*format == '%')
{
++format;
if (!(tmp = ft_printf_parser(&format, ret.buf ? ret.buf : cs, ap)))
{
ret = get_ret(ret.buf, 1);
free(cs);
return (ret);
}
ret.buf = ft_strjoin_clr(ft_strjoin_clr(ret.buf, cs, 2), tmp, 2);
cs = NULL;
}
else
cs = ft_strjoinc_clr(cs, *format++);
}
return (get_ret(ft_strjoin_clr(ret.buf, cs, 2), 0));
}
int ft_vprintf_s(char **buffer, const char *format, va_list ap)
{
t_ret ret;
t_pcur pcur;
if (!buffer || !format)
return (-1);
va_copy(pcur.ap, ap);
va_copy(pcur.ap_cur, ap);
ret = ft_inner_printf(format, &pcur);
va_end(pcur.ap_cur);
va_end(pcur.ap);
*buffer = ret.buf;
return (ret.len);
}
int ft_vprintf_fd(int fd, const char *format, va_list ap)
{
t_ret ret;
t_pcur pcur;
if (!format)
return (-1);
va_copy(pcur.ap, ap);
va_copy(pcur.ap_cur, ap);
ret = ft_inner_printf(format, &pcur);
va_end(pcur.ap_cur);
va_end(pcur.ap);
ret.len = write(fd, ret.buf, ret.len);
free(ret.buf);
return (ret.err ? -1 : ret.len);
}
int ft_vprintf(const char *format, va_list ap)
{
return (ft_vprintf_fd(1, format, ap));
}