-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_printf.c
144 lines (131 loc) · 3.64 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcombeau <mcombeau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/10 13:21:32 by mcombeau #+# #+# */
/* Updated: 2022/01/27 17:31:12 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_print_arg(char type, va_list args, t_flags flags)
{
int count;
count = 0;
if (type == '%')
count += ft_print_char('%', flags);
else if (type == 'c')
count += ft_print_char(va_arg(args, int), flags);
else if (type == 's')
count += ft_print_str(va_arg(args, const char *), flags);
else if (type == 'd' || type == 'i')
count += ft_print_int(va_arg(args, int), flags);
else if (type == 'x')
count += ft_print_hex(va_arg(args, unsigned int), 0, flags);
else if (type == 'X')
count += ft_print_hex(va_arg(args, unsigned int), 1, flags);
else if (type == 'u')
count += ft_print_unsigned(va_arg(args, unsigned int), flags);
else if (type == 'p')
count += ft_print_ptr((unsigned long int)va_arg(args, void *), flags);
return (count);
}
int ft_parse_flags(const char *str, int i, va_list args, t_flags *flags)
{
while (str[++i] && ft_isflag(str[i]))
{
if (str[i] == '-')
*flags = ft_flag_left(*flags);
if (str[i] == '#')
flags->hash = 1;
if (str[i] == ' ')
flags->space = 1;
if (str[i] == '+')
flags->plus = 1;
if (str[i] == '0' && flags->left == 0 && flags->width == 0)
flags->zero = 1;
if (str[i] == '.')
i = ft_flag_precision(str, i, args, flags);
if (str[i] == '*')
*flags = ft_flag_width(args, *flags);
if (ft_isdigit(str[i]))
*flags = ft_flag_digit(str[i], *flags);
if (ft_istype(str[i]))
{
flags->spec = str[i];
break ;
}
}
return (i);
}
#if defined(__linux__) || defined(__gnu_linux__)
int ft_parse(char *str, va_list args)
{
int i;
int x;
int count;
t_flags flags;
i = -1;
count = 0;
while (str[++i])
{
flags = ft_flags_init();
if (str[i] == '%' && str[i + 1] != '\0')
{
x = ft_parse_flags(str, i, args, &flags);
if (flags.spec > 0)
i = x;
if (str[i] != '\0' && flags.spec > 0 && ft_istype(str[i]))
count += ft_print_arg(str[i], args, flags);
else if (str[i] != '\0')
count += ft_print_c(str[i]);
}
else
count += ft_print_c(str[i]);
}
return (count);
}
#else
int ft_parse(char *str, va_list args)
{
int i;
int count;
t_flags flags;
i = -1;
count = 0;
while (str[++i])
{
flags = ft_flags_init();
if (str[i] == '%' && str[i + 1] != '\0')
{
i = ft_parse_flags(str, i, args, &flags);
if (str[i] != '\0' && flags.spec > 0 && ft_istype(str[i]))
count += ft_print_arg(str[i], args, flags);
else if (str[i] != '\0')
count += ft_print_char(str[i], flags);
}
else
count += ft_print_c(str[i]);
}
return (count);
}
#endif
int ft_printf(const char *format, ...)
{
va_list args;
int count;
char *str;
if (!format || *format == '\0')
return (0);
str = ft_strdup(format);
if (!str || *str == '\0')
return (0);
count = 0;
va_start(args, format);
count = ft_parse(str, args);
va_end(args);
free(str);
return (count);
}