-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
92 lines (85 loc) · 2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: javellis <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/14 10:41:32 by javellis #+# #+# */
/* Updated: 2022/10/14 10:41:35 by javellis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "ft_printf.h"
#include <stdlib.h>
t_print *ft_initialise_tab(t_print *tab)
{
tab->sign = 0;
tab->sp = 0;
tab->hastag = 0;
tab->tl = 0;
return (tab);
}
int ft_format(t_print *tab, char *format, int pos)
{
while (format[pos])
{
if (format[pos] == ' ')
{
tab->sp = 1;
pos++;
}
else if (format[pos] == '+')
{
tab->sign = 1;
pos++;
}
else if (format[pos] == '#')
{
tab->hastag = 1;
pos++;
}
else
{
tab->tl = ft_print_format(tab, format, pos);
break ;
}
}
return (pos + 1);
}
int ft_printf2(t_print *tab, char *format, int *i)
{
int res;
res = 0;
tab = ft_initialise_tab(tab);
if (format[*i] == '%')
{
*i = ft_format(tab, (char *)format, *i + 1);
res += tab->tl;
}
else
{
ft_putchar_fd(format[(*i)++], 1);
res++;
}
return (res);
}
int ft_printf(const char *format, ...)
{
t_print *tab;
int i;
int res;
i = 0;
res = 0;
tab = (t_print *)malloc(sizeof(t_print));
va_start(tab->args, format);
if (!tab)
return (-1);
while (format[i])
{
res += ft_printf2(tab, (char *)format, &i);
}
va_end(tab->args);
free(tab);
return (res);
}