-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
84 lines (76 loc) · 1.94 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmery <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/08 14:07:48 by lmery #+# #+# */
/* Updated: 2021/12/17 19:22:16 by lmery ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_fpos(int *i, int *l, int *f)
{
*l -= *f;
*i += *f;
}
static int ft_fneg(int *i, int l, int *f)
{
l += *f;
*i -= *f;
return (l);
}
static int ft_parse(const char *s, va_list toprint, int i, int l)
{
int f;
va_list temp;
va_copy(temp, toprint);
f = 0;
while (s[i])
{
if (s[i] != '%')
ft_putchar(s[i++]);
if (s[i] == '%')
{
i++;
f = ft_flag(s, i);
if (f > 0)
ft_fpos(&i, &l, &f);
if (f < 0)
l += ft_apply_flag(s, i, temp) + ft_fneg(&i, l, &f);
l += ft_type(s[i++], toprint) - 2;
if (f > 0)
l += ft_apply_flag(s, (i - f - 1), temp);
}
}
l += i;
return (l);
}
int ft_printf(const char *echo, ...)
{
va_list toprint;
int i;
int l;
const char *s;
s = ft_strdup(echo);
if (!s)
return (0);
va_start (toprint, echo);
i = 0;
l = 0;
l = ft_parse(s, toprint, i, l);
free((char *)s);
va_end (toprint);
return (l);
}
/*
int main(void)
{
int a;
char b[] = "centimes";
a = INT_MIN;
printf("%d\n", ft_printf(" yo %-023d\n", a));
printf("%d\n", printf(" yo %-023d\n", a));
return (0);
}*/