-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
111 lines (103 loc) · 2.36 KB
/
ft_printf.c
File metadata and controls
111 lines (103 loc) · 2.36 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abadidi <abadidi@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/18 00:20:24 by abadidi #+# #+# */
/* Updated: 2021/04/23 00:28:45 by abadidi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void init_flags(t_flags *flags)
{
flags->minus = 0;
flags->point = 0;
flags->zero = 0;
flags->minwidth = 0;
flags->precission = 0;
flags->check = 0;
}
void help_flags(const char **s, va_list list, t_flags *flags)
{
if (**s == '.')
{
flags->point = 1;
(*s)++;
if (ft_isdigit(**s))
{
flags->precission = ft_atoi(++*s);
while (ft_isdigit(**s))
*s += 1;
}
else if (**s == '*')
{
(*s)++;
flags->precission = va_arg(list, int);
if (flags->precission < 0)
flags->precission = 0;
}
}
if (flags->minwidth < 0)
{
flags->minwidth *= -1;
flags->minus = 1;
flags->zero = 0;
}
}
void help_flags2(const char **s, t_flags *flags)
{
while (**s == '0' || **s == '-')
{
if (**s == '0')
flags->zero = 1;
else
flags->minus = 1;
(*s)++;
}
}
static int get_flags(const char **s, va_list list)
{
t_flags *flags;
flags = malloc(sizeof(t_flags));
if (!flags)
return (-1);
init_flags(flags);
help_flags2(s, flags);
if (**s == '*')
{
(*s)++;
flags->minwidth = va_arg(list, int);
}
else if (ft_isdigit(**s))
{
flags->minwidth = ft_atoi(*s);
while (ft_isdigit(**s))
*s += 1;
}
help_flags(s, list, flags);
set_route(**s++, flags, list);
free(flags);
return (g_nbr);
}
int ft_printf(const char *s, ...)
{
va_list list;
g_nbr = 0;
va_start(list, s);
while (*s)
{
if (*s == '%')
{
s++;
if (get_flags(&s, list) < 0)
return (-1);
}
else
ft_putchar(*s);
s++;
}
va_end(list);
return (g_nbr);
}