-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
112 lines (102 loc) · 2.44 KB
/
ft_printf.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
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alischyn <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/22 16:46:40 by alischyn #+# #+# */
/* Updated: 2017/03/23 19:02:41 by alischyn ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
t_str g_res;
int ft_printf(const char *format, ...)
{
va_list ap;
t_fmt fmt;
int res;
va_start(ap, format);
str_init(&g_res);
while (*format != '\0')
{
if (*format == '%')
{
format = parse(&fmt, ++format, ap);
format_fmt(&fmt, ap);
}
else
APPEND_CHAR(*(format++));
}
va_end(ap);
write(1, g_res.string, g_res.length);
res = g_res.length;
str_free(&g_res);
return (res);
}
int ft_fdprintf(int fd, const char *format, ...)
{
va_list ap;
t_fmt fmt;
int res;
va_start(ap, format);
str_init(&g_res);
while (*format != '\0')
{
if (*format == '%')
{
format = parse(&fmt, ++format, ap);
format_fmt(&fmt, ap);
}
else
APPEND_CHAR(*(format++));
}
va_end(ap);
write(fd, g_res.string, g_res.length);
res = g_res.length;
str_free(&g_res);
return (res);
}
int ft_sprintf(char *dest, const char *format, ...)
{
va_list ap;
t_fmt fmt;
int res;
va_start(ap, format);
str_init(&g_res);
while (*format != '\0')
{
if (*format == '%')
{
format = parse(&fmt, ++format, ap);
format_fmt(&fmt, ap);
}
else
APPEND_CHAR(*(format++));
}
va_end(ap);
STRCPY(dest, g_res.string);
res = g_res.length;
str_free(&g_res);
return (g_res.length);
}
int ft_asprintf(char **dest, const char *format, ...)
{
va_list ap;
t_fmt fmt;
va_start(ap, format);
str_init(&g_res);
while (*format != '\0')
{
if (*format == '%')
{
format = parse(&fmt, ++format, ap);
format_fmt(&fmt, ap);
}
else
APPEND_CHAR(*(format++));
}
va_end(ap);
*dest = g_res.string;
return (g_res.length);
}