-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
82 lines (74 loc) · 2.21 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
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_printf.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: bpajot <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/01/26 10:37:00 by bpajot #+# ## ## #+# */
/* Updated: 2018/03/06 16:14:06 by bpajot ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "ft_printf.h"
static void clean(t_field *field)
{
t_field *cur;
t_field *next;
cur = field;
while (cur)
{
next = cur->next;
ft_memdel((void**)&cur);
cur = next;
}
}
static t_field *ft_printf_arg(t_field *cur, va_list *va, int *i, int *ret)
{
if (cur->width_ast)
va_arg(*va, int);
if (cur->preci_ast)
va_arg(*va, int);
*ret += ft_printf_type(cur, va, ret);
*i += cur->len;
cur = cur->next;
return (cur);
}
static int end(t_field *field, va_list *va, int ret)
{
clean(field);
va_end(*va);
return (ret);
}
static void ft_printf_txt(t_field *cur, const char *format, int *i,
int *ret)
{
*ret += (cur) ? ft_putstr_sizel((char*)&(format[*i]),
cur->text - *i + 1) : ft_putstr_size((char*)&format[*i]);
*i = (cur) ? (int)cur->text : (int)ft_strlen(format) - 1;
}
int ft_printf(const char *format, ...)
{
t_field *field;
t_field *cur;
int i;
int ret;
va_list va;
i = -1;
ret = 0;
va_start(va, format);
field = ft_parse(format, &va);
va_end(va);
va_start(va, format);
cur = field;
while (format[++i])
{
if (cur && cur->error == 1)
return (end(field, &va, -1));
else if (format[i] == '%')
cur = ft_printf_arg(cur, &va, &i, &ret);
else
ft_printf_txt(cur, format, &i, &ret);
}
return (end(field, &va, ret));
}