-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_dprintf.c
81 lines (74 loc) · 2.16 KB
/
ft_dprintf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintf.c :+: :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amahla <amahla@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:59:27 by amahla #+# #+# */
/* Updated: 2023/10/29 22:47:31 by amahla ### ######## ######## */
/* */
/* ************************************************************************** */
#include"ft_printf.h"
int fprocess_print(int fd, va_list ap, char c, t_flag *fg)
{
char *str;
int len;
len = 0;
if (c == 'c')
str = ft_printf_char(fg, va_arg(ap, int), &len);
else if (c == 's')
str = ft_printf_str(fg, va_arg(ap, char *));
else if (c == 'p')
str = ft_printf_ptr(fg, va_arg(ap, void *));
else if (c == 'd' || c == 'i')
str = ft_printf_int(fg, va_arg(ap, int));
else if (c == 'u')
str = ft_printf_uint(fg, va_arg(ap, unsigned int));
else if (c == 'x' || c == 'X')
str = ft_printf_hex(fg, va_arg(ap, int), c);
else
str = ft_print_pct();
if (!len)
len = ft_strlen(str);
ft_putstr_len(fd, str, len);
init_flag(fg);
if (str)
free(str);
return (len);
}
void fprocess(int fd, va_list ap, char *str, int *count)
{
int i;
int y;
t_flag fg;
i = 0;
init_flag(&fg);
while (*(str + i))
{
y = 0;
if (*(str + i) == '%')
{
process_format(str + ++i, &fg, y);
while (*(str + i) && !is_conv(str[i]))
i++;
if (*(str + i) && is_conv(str[i]))
*count = *count + fprocess_print(fd, ap, *(str + i++), &fg);
}
else
{
write(fd, str + i++, 1);
(*count)++;
}
}
}
int ft_dprintf(int fd, const char *format, ...)
{
va_list ap;
int count;
count = 0;
va_start(ap, format);
fprocess(fd, ap, (char *)format, &count);
va_end(ap);
return (count);
}