-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
84 lines (77 loc) · 2.2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: skarras <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 08:46:56 by skarras #+# #+# */
/* Updated: 2024/11/26 14:38:04 by skarras ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int sorting_hat(const char *s, va_list args)
{
void *p;
if (*s == 'c')
return (print_char(va_arg(args, int)));
else if (*s == 'i' || *s == 'd')
return (putnbr_count(va_arg(args, int)));
else if (*s == 's')
return (put_str_count(va_arg(args, char *)));
else if (*s == 'u')
return (print_usnigned(va_arg(args, unsigned int), 0));
else if (*s == 'x')
return (print_hex(va_arg(args, unsigned int), 'x'));
else if (*s == 'X')
return (print_hex(va_arg(args, unsigned int), 'X'));
else if (*s == '%')
return (print_char('%'));
else if (*s == 'p')
{
p = va_arg(args, void *);
if (p == NULL)
return (put_str_count("(nil)"));
return (put_str_count("0x") + print_pointer((unsigned long) p, 0));
}
return (-1);
}
int outoflines(const char *s, va_list args)
{
int count;
int i;
int temp;
i = 0;
count = 0;
while (s[i])
{
temp = ft_printf_normal(&s[i]);
if (temp == -1)
return (-1);
i += temp;
if (s[i] == '%')
{
temp = sorting_hat(&s[i + 1], args);
if (temp == -1)
return (-1);
count += temp;
i += 2;
}
}
return (count);
}
int ft_printf(const char *s, ...)
{
va_list args;
int count;
int temp;
count = 0;
va_start(args, s);
temp = outoflines(s, args);
if (temp == -1)
return (-1);
count += temp;
va_end(args);
count += (strlen_count(s));
return (count);
}