This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
72 lines (66 loc) · 1.97 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: ctherin <ctherin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 16:31:56 by ctherin #+# #+# */
/* Updated: 2022/06/22 19:17:49 by ctherin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_handle_conversion(va_list ap, char f)
{
if (f == '%')
return (ft_print_percent());
if (f == 'd' || f == 'i')
return (ft_print_d_i(va_arg(ap, int)));
if (f == 's')
return (ft_print_str(va_arg(ap, char *)));
if (f == 'p')
return (ft_print_ptr(va_arg(ap, void *)));
if (f == 'u')
return (ft_print_u(va_arg(ap, unsigned int)));
if (f == 'c')
return (ft_print_char(va_arg(ap, int)));
if (f == 'x')
return (ft_print_lowercase_hex(va_arg(ap, unsigned int)));
if (f == 'X')
return (ft_print_uppercase_hex(va_arg(ap, unsigned int)));
return (0);
}
static int ft_print_ofs(const char *s, int from, int to)
{
int total;
total = to - from;
write(1, s + from, total);
return (total);
}
int ft_printf(const char *s, ...)
{
va_list ap;
int total;
int i;
int ofs;
if (!s)
return (0);
va_start(ap, s);
total = 0;
i = 0;
ofs = 0;
while (s[i])
{
if (s[i] == '%')
{
total += ft_print_ofs(s, ofs, i);
total += ft_handle_conversion(ap, s[++i]);
ofs = i + 1;
}
if (s[i])
++i;
}
total += ft_print_ofs(s, ofs, i);
va_end(ap);
return (total);
}