-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
72 lines (65 loc) · 2.22 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qsomarri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 12:30:46 by qsomarri #+# #+# */
/* Updated: 2024/07/09 14:57:25 by qsomarri ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar_count(char c)
{
ssize_t res;
res = write (1, &c, 1);
return (res);
}
static int ft_mini_printf(va_list ap, const char *format, size_t index)
{
int res;
res = 0;
if (format[index] == 'c')
res = ft_putchar_count(va_arg(ap, int));
if (format[index] == 's')
res = ft_putstr_count(va_arg(ap, char *));
if (format[index] == 'd' || format[index] == 'i')
res = ft_putnbr_count(va_arg(ap, int));
if (format[index] == 'u')
res = ft_put_unsigned_nbr_count(va_arg(ap, unsigned int));
if (format[index] == 'x' || format[index] == 'X')
res = ft_put_hexa_count(va_arg(ap, unsigned int), format[index]);
if (format[index] == 'p')
res = ft_putptr_count (va_arg(ap, void *));
if (format[index] == '%')
res = ft_putchar_count('%');
return (res);
}
int ft_printf(const char *format, ...)
{
va_list ap;
int res;
size_t i;
i = -1;
res = 0;
if (format == NULL)
return (-1);
va_start(ap, format);
while (format[++i])
{
if (format[i] != '%')
res += ft_putchar_count(format[i]);
if (format[i] == '%' && format[i + 1] == '\0')
return (va_end(ap), -1);
else if (format[i] == '%' && ft_strchr("cspdiuxX%", format[i + 1]))
{
res += ft_mini_printf(ap, format, i + 1);
i++;
}
else if (format[i] == '%' && !ft_strchr("cspdiuxX%", format[i + 1]))
return (va_end(ap), -1);
}
va_end(ap);
return (res);
}