-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
67 lines (61 loc) · 2.03 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahaifoul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/20 10:14:25 by ahaifoul #+# #+# */
/* Updated: 2021/11/23 12:11:40 by ahaifoul ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int is_valid(char c)
{
if (c == 'd' || c == 's' || c == 'i' || c == 'x' || c == 'c'
|| c == 'X' || c == 'u' || c == 'p')
return (1);
return (0);
}
static int ft_eval(va_list va, const char *format, int i)
{
int ret;
ret = 0;
if (format[i] == 'd' || format[i] == 'i')
ret += ft_itoa(va_arg(va, int));
else if (format[i] == 'c')
ret += ft_putchar(va_arg(va, int));
else if (format[i] == 's')
ret += ft_putstr(va_arg(va, char *));
else if (format[i] == 'p')
ret += base_convert_p(va_arg(va, void *));
else if (format[i] == 'u')
ret += ft_itoa(va_arg(va, unsigned int));
else if (format[i] == 'x' || format[i] == 'X')
ret += base_convert_hex(va_arg(va, unsigned int), format[i]);
return (ret);
}
int ft_printf(const char *format, ...)
{
va_list va;
int i;
int ret;
ret = 0;
i = 0;
va_start(va, format);
while (format[i])
{
if (format[i] == '%' && is_valid(format[i + 1]))
ret += ft_eval(va, format, ++i);
else if (format[i] == '%' && format[i + 1] == '%')
{
ret += ft_putchar(format[i]);
i++;
}
else if (format[i] != '%')
ret += ft_putchar(format[i]);
i++;
}
va_end(va);
return (ret);
}