-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_printf.c
66 lines (60 loc) · 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gmacias- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/22 16:03:54 by gmacias- #+# #+# */
/* Updated: 2022/02/23 19:26:10 by gmacias- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_select_format(va_list argument, const char word);
int ft_print_character(int character)
{
write(1, &character, 1);
return (1);
}
int ft_printf(const char *str, ...)
{
int i;
int size;
va_list argument;
i = 0;
size = 0;
va_start(argument, str);
while (str[i])
{
if (str[i] == '%')
{
size += ft_select_format(argument, str[i + 1]);
i++;
}
else
size += ft_print_character(str[i]);
i++;
}
va_end(argument);
return (size);
}
static int ft_select_format(va_list argument, const char word)
{
int size;
size = 0;
if (word == 'c')
size += ft_print_character(va_arg(argument, int));
else if (word == 's')
size += ft_print_string(va_arg(argument, char *));
else if (word == 'p')
size += ft_print_pointer(va_arg(argument, unsigned long long));
else if (word == 'd' || word == 'i')
size += ft_print_number(va_arg(argument, int));
else if (word == 'u')
size += ft_print_unsigned(va_arg(argument, unsigned int));
else if (word == 'x' || word == 'X')
size += ft_print_hexadecimal(va_arg(argument, unsigned int), word);
else
size += ft_print_character(word);
return (size);
}