-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
70 lines (64 loc) · 1.91 KB
/
Copy pathft_printf.c
File metadata and controls
70 lines (64 loc) · 1.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brheaume <marvin@42quebec.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/04 11:41:21 by brheaume #+# #+# */
/* Updated: 2022/11/22 16:41:34 by brheaume ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_isconversion(const char c)
{
if (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' || c == 'u'
|| c == 'x' || c == 'X' || c == '%')
return (c);
return (0);
}
static int ft_convert(const char c, va_list *lst)
{
int res;
res = 0;
if (c == 'c')
res = ft_chrconvert(va_arg(*lst, int));
else if (c == 's')
res = ft_strconvert(va_arg(*lst, char *));
else if (c == '%')
res = ft_chrconvert('%');
else if (c == 'i' || c == 'd')
res = ft_nbconvert(va_arg(*lst, int));
else if (c == 'u' || c == 'x' || c == 'X')
res = ft_unsignedconvert(va_arg(*lst, unsigned int), c);
else if (c == 'p')
res = ft_ptrconvert(va_arg(*lst, uintptr_t));
return (res);
}
int ft_printf(const char *s, ...)
{
int i;
int nb;
va_list al;
va_start(al, s);
nb = 0;
i = 0;
if (!s)
return (0);
while (s[i])
{
if (s[i] == '%' && ft_isconversion(s[i + 1]))
nb += ft_convert(s[++i], &al);
else
{
if (s[i] != '%')
{
write(1, &s[i], 1);
nb++;
}
}
i++;
}
va_end(al);
return (nb);
}