-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
71 lines (64 loc) · 2.15 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lburkins <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/28 10:13:48 by lburkins #+# #+# */
/* Updated: 2023/12/01 10:48:33 by lburkins ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *format, ...)
{
va_list args;
int count;
int check;
check = 0;
va_start(args, format);
count = iterate(format, args, &check);
va_end(args);
if (check == -1)
return (-1);
return (count);
}
int iterate(const char *format, va_list args, int *check)
{
int temp_count;
int count;
count = 0;
while (*format)
{
temp_count = 0;
if (*format != '%')
temp_count = printchar(*format, check);
else
temp_count = execute_fmt(*++format, args, check);
count += temp_count;
format++;
}
return (count);
}
int execute_fmt(const char specifier, va_list args, int *check)
{
int count;
count = 0;
if (specifier == 'c')
count = printchar(va_arg(args, int), check);
else if (specifier == 's')
count = printstr(va_arg(args, const char *), check);
else if (specifier == 'p')
count = printptr((unsigned long int)va_arg(args, void *), check);
else if (specifier == 'd' || specifier == 'i')
count = printnbr_diu(va_arg(args, int), check);
else if (specifier == 'u')
count = printnbr_diu(va_arg(args, unsigned), check);
else if (specifier == 'x' || specifier == 'X')
count = printnbr_hex(va_arg(args, unsigned int), specifier, check);
else if (specifier == '%')
count = printchar('%', check);
else
return (0);
return (count);
}