-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
123 lines (109 loc) · 3.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: johnavar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/19 16:01:25 by johnavar #+# #+# */
/* Updated: 2024/02/05 18:06:10 by sebasnadu ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../include/ft_printf.h"
static int ft_print_spec(int fd, char spec, va_list *ap, t_print *flags)
{
int count;
count = 0;
if (spec == '%')
count += ft_print_char(fd, '%', flags);
else if (spec == 'c')
count += ft_print_char(fd, (char)va_arg(*ap, int), flags);
else if (spec == 's')
count += ft_print_string(fd, va_arg(*ap, char *), flags);
else if (spec == 'd' || spec == 'i')
count += ft_print_int(fd, (long)(va_arg(*ap, int)), spec, flags);
else if (spec == 'x' || spec == 'X')
count += ft_print_hex(fd, (long)(va_arg(*ap, unsigned int)), flags);
else if (spec == 'p')
count += ft_print_ptr(fd, (unsigned long)va_arg(*ap, void *), flags);
else if (spec == 'u')
count += ft_print_unsigned(
fd, (long)(va_arg(*ap, unsigned int)), flags);
return (count);
}
#ifdef __linux__
static int ft_parse_format(int fd, const char *format, va_list *ap)
{
int i;
int x;
int count;
t_print flags;
i = -1;
count = 0;
while (format[++i])
{
if (format[i] == '%' && format[i + 1] != '\0')
{
flags = ft_initialize_tab();
x = ft_parse_flags(format, i, ap, &flags);
if (flags.spec > 0)
i = x;
if (format[i] != '\0' && flags.spec > 0 && ft_istype(format[i]))
count += ft_print_spec(fd, format[i], ap, &flags);
else if (format[i] != '\0')
count += ft_print_c(fd, format[i]);
}
else
count += ft_print_c(fd, format[i]);
}
return (count);
}
#else
static int ft_parse_format(int fd, const char *format, va_list *ap)
{
int i;
int count;
t_print flags;
i = -1;
count = 0;
while (format[++i])
{
if (format[i] == '%' && format[i + 1] != '\0')
{
flags = ft_initialize_tab();
i = ft_parse_flags(format, i, ap, &flags);
if (format[i] != '\0' && flags.spec > 0 && ft_istype(format[i]))
count += ft_print_spec(fd, format[i], ap, &flags);
else if (format[i] != '\0')
count += ft_print_char(fd, format[i], &flags);
}
else
count += ft_print_c(fd, format[i]);
}
return (count);
}
#endif
int ft_printf(const char *format, ...)
{
va_list ap;
int count;
if (!format || *format == '\0')
return (0);
count = 0;
va_start(ap, format);
count = ft_parse_format(1, format, &ap);
va_end(ap);
return (count);
}
int ft_printf_fd(int fd, const char *format, ...)
{
va_list ap;
int count;
if (!format || *format == '\0')
return (0);
count = 0;
va_start(ap, format);
count = ft_parse_format(fd, format, &ap);
va_end(ap);
return (count);
}