-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
118 lines (109 loc) · 2.86 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgoncalv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/27 00:57:31 by hugogoncalv #+# #+# */
/* Updated: 2021/04/22 14:07:57 by hgoncalv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "libftprintf.h"
t_strk ft_init_strk(void)
{
t_strk strk;
strk.space = 0;
strk.spaces = 0;
strk.dot = -1;
strk.upper = 0;
strk.minus = 0;
strk.star = 0;
strk.type = 'd';
strk.width = 0;
strk.zero = 0;
strk.nbr_len = 0;
strk.base = 10;
strk.push_front = 0;
strk.signal = ' ';
strk.nbr_len = 0;
strk.padchar = ' ';
strk.already_print = 0;
return (strk);
}
void ft_itostr_letter_for_10(t_strk *strk, int index, long nbr)
{
if (strk->upper == 1)
strk->nbr_str[index] = nbr + 'A' - 10;
else
strk->nbr_str[index] = nbr + 'a' - 10;
}
size_t ft_itostr_base(size_t nbr, t_strk *strk, int nbr_len)
{
int index;
if (nbr_len == 0)
{
if (nbr == 0)
{
if (strk->nbr_len == 0)
strk->nbr_len += 1;
return (0);
}
strk->nbr_len += 1;
ft_itostr_base((nbr / strk->base), strk, 0);
return (nbr);
}
if (nbr == 0 && nbr_len == 1)
strk->nbr_str[0] = '0';
if (nbr == 0)
return (0);
ft_itostr_base((nbr / strk->base), strk, nbr_len + 1);
index = strk->nbr_len - nbr_len + strk->push_front;
if (nbr % strk->base > 9)
ft_itostr_letter_for_10(strk, index, nbr % strk->base);
else
strk->nbr_str[index] = nbr % strk->base + '0';
return (nbr);
}
static char *ft_fprint_parseflags(char *fmt, va_list *args, t_strk *strk)
{
while (1)
{
if (*fmt == '0' && strk->width == 0 && strk->minus == 0)
strk->zero = 1;
if (*fmt == '-' || *fmt == '+' || ft_isdigit(*fmt)
|| *fmt == ' ' || *fmt == '#')
ft_flag_parser(*fmt, strk);
if (*fmt == '.')
fmt = ft_flag_dot(fmt, strk, args);
if (*fmt == '*')
ft_flag_width(args, strk);
if (ft_is_spec(*fmt))
break ;
fmt++;
}
ft_set_nbrstr(*fmt, args, strk);
return (fmt);
}
void ft_printf(char *fmt, ...)
{
va_list args;
t_strk strk;
va_start(args, fmt);
while (*fmt)
{
if (*fmt == '%')
{
if (ft_is_flag(*fmt))
strk = ft_init_strk();
fmt++;
fmt = ft_fprint_parseflags(fmt, &args, &strk);
ft_printoscreen(&strk);
}
else
ft_putchar_fd(*fmt, 1);
fmt++;
}
va_end(args);
}