-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
58 lines (54 loc) · 1.89 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vgroux <vgroux@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/18 15:55:55 by vgroux #+# #+# */
/* Updated: 2022/11/30 15:58:29 by vgroux ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf_conv(char format, va_list ap)
{
if (format == '%')
return (ft_printf_char('%'));
else if (format == 'c')
return (ft_printf_char(va_arg(ap, int)));
else if (format == 's')
return (ft_printf_str(va_arg(ap, char *)));
else if (format == 'i' || format == 'd')
return (ft_printf_n_base(va_arg(ap, int), "0123456789"));
else if (format == 'x')
return (ft_printf_n_base(va_arg(ap, unsigned int), "0123456789abcdef"));
else if (format == 'X')
return (ft_printf_n_base(va_arg(ap, unsigned int), "0123456789ABCDEF"));
else if (format == 'p')
return (ft_printf_ptr((unsigned long)va_arg(ap, void *)));
else if (format == 'u')
return (ft_printf_ui(va_arg(ap, unsigned int)));
return (0);
}
int ft_printf(const char *format, ...)
{
va_list ap;
int len;
int i;
va_start(ap, format);
len = 0;
i = 0;
while (format[i])
{
if (format[i] == '%')
{
i++;
len += ft_printf_conv(format[i], ap);
}
else
len += ft_printf_char(format[i]);
i++;
}
va_end(ap);
return (len);
}