-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
67 lines (60 loc) · 990 Bytes
/
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
#include "printf.h"
int is_flag(char c)
{
char flags[] = {'#','0','-',' ','+','\0'};
int i;
i = 0;
while (flags[i])
{
if (flags[i] == c)
return (1);
++i;
}
return (0);
}
t_print *p_reset(t_print *p)
{
p->f_sign = '\0';
p->f_left = 0;
p->f_alt = 0;
p->f_pad = ' ';
p->width = -1;
p->precision = -1;
p->length = -1;
p->conversion = -1;
p->base = 10;
p->capital = 0;
return (p);
}
t_print *p_init(const char * buf)
{
t_print *p;
if (!buf)
return (NULL);
p = ft_memalloc(sizeof(*p));
p->buf = ft_strdup(buf);
p->out = NULL;
p->i = 0;
p->r = 0;
return (p_reset(p));
}
int ft_printf(const char *format, ...)
{
t_print *p;
int len;
p = p_init(format);
va_start(p->ap, format);
while (p->buf[p->i])
{
p_reset(p);
parse_conversion(parse_length(parse_precision(parse_width(parse_flags(parse_percent(p))))));
}
va_end(p->ap);
len = p->r;
write(1, p->out, len);
ft_strdel(&p->buf);
ft_strdel(&p->out);
free(p);
p = NULL;
return (len);
}