Repository navigation
Expand file tree
/
Copy pathft_printf.c
More file actions
57 lines (53 loc) · 1.7 KB
/
Copy pathft_printf.c
File metadata and controls
57 lines (53 loc) · 1.7 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tyassine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/04/07 10:04:37 by tyassine #+# #+# */
/* Updated: 2016/04/07 10:04:56 by tyassine ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdarg.h>
#include <stdio.h>
static int do_d_stuff(t_data *d, t_form **c, const char *restrict f, char *r)
{
free(*c);
if (!(reinit_form(c)))
return (-1);
d->i += read_until(f + d->i, r, d);
d->ib = d->i;
d->i += modify_f(*c, f + d->i, d);
d->type = f[d->i];
d->string = NULL;
return (1);
}
int ft_printf(const char *restrict format, ...)
{
char *result;
t_form *c;
t_data *d;
va_list ap;
t_type var;
if (!(reinit_form(&c)) || !(init_data(&d)))
return (-1);
va_start(ap, format);
result = NULL;
while ((d->ib != d->i || c->percent != -1) && format[d->i])
{
if ((do_d_stuff(d, &c, format, result)) == -1)
return (-1);
if (d->nargs > 0)
{
d->retplusreal += do_va(&ap, d, &var, c);
d->nargs--;
d->i++;
}
}
free(c);
var.d = d->retplusreal;
free(d);
return (var.d);
}