-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
98 lines (90 loc) · 2.64 KB
/
ft_printf.c
File metadata and controls
98 lines (90 loc) · 2.64 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jzubizar <jzubizar@student.42urduliz.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/21 10:25:55 by novadecordi #+# #+# */
/* Updated: 2023/07/10 13:00:29 by jzubizar ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_nbr_args(const char *str)
{
unsigned int i;
int nbr;
i = 0;
nbr = 0;
while (str[i])
{
if (str[i] == '%' && str[i + 1] == '%')
{
nbr++;
i++;
}
else if (str[i] == '%')
nbr++;
i++;
}
return (nbr);
}
static int ft_len_arg_def(const char *str, char *type, t_flag *flags)
{
unsigned int i;
i = 0;
if (str[i])
i++;
i = ft_process_flags(i, flags, str);
*type = ft_is_type(str[i]);
if (str[i])
i++;
return (i);
}
static void ft_put_arg(va_list *value, char type, int *cont, t_flag flags)
{
if (type == 'c')
*cont += ft_print_char_fd(va_arg(*value, int), flags, 1);
else if (type == 's')
*cont += ft_pfputargstr_fd(va_arg(*value, const char *), 1, flags);
else if (type == 'u')
*cont += ft_putunbr_fd(va_arg(*value, unsigned int), 1, flags);
else if (type == 'x')
*cont += ft_putxnbr_fd(va_arg(*value, unsigned int), flags);
else if (type == 'X')
*cont += ft_putxmnbr_fd(va_arg(*value, unsigned int), flags);
else if (type == 'p')
*cont += ft_putxptr_fd(va_arg(*value, void *), 1, flags);
else if (type == 'i' || type == 'd')
*cont += ft_pfputnbr_fd(va_arg(*value, int), 1, flags);
else if (type == '%')
*cont += ft_print_char_fd('%', flags, 1);
}
// Variadic function to add numbers
int ft_printf(const char *str, ...)
{
int n;
va_list ptr;
char type;
int cont;
t_flag flags;
cont = 0;
flags = ft_flags_zeros();
n = ft_nbr_args(str);
type = 0;
va_start(ptr, str);
cont += ft_pfputstr_fd(str, 1);
str += ft_pfstrlen(str);
str += ft_len_arg_def(str, &type, &flags);
while (n >= 0)
{
ft_put_arg(&ptr, type, &cont, flags);
cont += ft_pfputstr_fd(str, 1);
str += ft_pfstrlen(str);
flags = ft_flags_zeros();
str += ft_len_arg_def(str, &type, &flags);
n--;
}
va_end(ptr);
return (cont);
}