-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
104 lines (96 loc) · 2.93 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
93
94
95
96
97
98
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: isousa <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/29 12:51:01 by isousa #+# #+# */
/* Updated: 2021/03/29 12:51:07 by isousa ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_letter(t_flags s_fl, va_list args)
{
int counter;
counter = 0;
if (s_fl.letter == 'c')
counter = ft_convchar(va_arg(args, int), s_fl);
else if (s_fl.letter == '%')
counter = ft_convper('%', s_fl);
else if (s_fl.letter == 's')
counter = ft_convstr(va_arg(args, char *), s_fl);
else if (s_fl.letter == 'i' || s_fl.letter == 'd')
counter = ft_convint(va_arg(args, int), s_fl);
else if (s_fl.letter == 'u')
counter = ft_convunint(va_arg(args, unsigned int), s_fl);
else if (s_fl.letter == 'x')
counter = ft_convhexa(va_arg(args, unsigned int), 0, s_fl);
else if (s_fl.letter == 'X')
counter = ft_convhexa(va_arg(args, unsigned int), 1, s_fl);
else if (s_fl.letter == 'p')
counter = ft_convpointer(va_arg(args, unsigned long long), s_fl);
return (counter);
}
int ft_spec(const char *format, va_list args, t_flags *s_fl, int i)
{
while (format[i] != '\0')
{
if (!ft_digit(format[i]) && !ft_let(format[i]) && !ft_flag(format[i]))
break ;
if (format[i] == '0' && s_fl->width == 0 && s_fl->hiffen == 0)
s_fl->zero = 1;
if (format[i] == '.')
i = ft_dot(format, args, s_fl, i);
if (format[i] == '-')
ft_hiffen(s_fl);
if (format[i] == '*')
ft_star(args, s_fl);
if (ft_digit(format[i]))
ft_width(format[i], s_fl);
if (ft_let(format[i]))
{
s_fl->letter = format[i];
break ;
}
i++;
}
return (i);
}
int ft_start(const char *format, va_list args)
{
int i;
int counter;
t_flags s_fl;
i = 0;
counter = 0;
while (1)
{
s_fl = ft_startflags();
if (!format[i])
return (counter);
else if (format[i] == '%' && format[i + 1] != '\0')
{
i = ft_spec(format, args, &s_fl, i + 1);
if (s_fl.letter == 0)
return (counter);
if (ft_let(format[i]))
counter += ft_letter(s_fl, args);
else
counter += ft_putchar(format[i]);
}
else if (format[i] != '%')
counter += ft_putchar(format[i]);
i++;
}
}
int ft_printf(const char *format, ...)
{
va_list args;
int counter;
counter = 0;
va_start(args, format);
counter = ft_start(format, args);
va_end(args);
return (counter);
}