-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
67 lines (62 loc) · 1.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yochered <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/26 10:14:10 by yochered #+# #+# */
/* Updated: 2018/11/26 10:14:13 by yochered ### ########.fr */
/* */
/* ************************************************************************** */
#include <ft_printf.h>
static int handle_expression(va_list ap, const char **format)
{
t_params params;
int ret;
int index;
int (*f[10])(va_list, t_params*);
ret = 0;
fill_function_arr(f);
(*format)++;
if (!**format || !allowed_symbols((char*)*format))
return (0);
init_params(¶ms);
reach_type(format, ¶ms, ap);
index = type_id(**format, ¶ms);
if (index != -1 || **format == '%' || ft_isalpha(**format))
{
ret += index == -1 ?
print_percent(**format, ¶ms) : f[index](ap, ¶ms);
(*format)++;
}
else
ret += print_percent(0, ¶ms);
return (ret);
}
int ft_printf(const char *format, ...)
{
va_list ap;
int ret;
char *next_arg;
ret = 0;
va_start(ap, format);
while (*format)
{
if (*format == '%')
ret += handle_expression(ap, &format);
else
{
if (!(next_arg = ft_strchr(format, '%')))
{
next_arg = (char*)format;
next_arg += ft_strlen(format);
}
write(1, format, next_arg - format);
ret += next_arg - format;
format += next_arg - format;
}
}
va_end(ap);
return (ret);
}