-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
73 lines (67 loc) · 2.19 KB
/
ft_printf.c
File metadata and controls
73 lines (67 loc) · 2.19 KB
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
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aktomiza <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/29 14:45:16 by aktomiza #+# #+# */
/* Updated: 2023/08/29 14:45:19 by aktomiza ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_print_char(int c)
{
ft_putchar_fd(c, 1);
return (1);
}
int check_format(va_list args, const char *format)
{
int print_len;
print_len = 0;
if (*format == 'c')
print_len += ft_print_char(va_arg(args, int));
else if (*format == 's')
print_len += ft_print_str(va_arg(args, char *));
else if (*format == 'p')
print_len += ft_print_ptr(va_arg(args, uintptr_t));
else if (*format == 'd' || *format == 'i')
print_len += ft_print_int(va_arg(args, int));
else if (*format == 'u')
print_len += ft_print_uint(va_arg(args, unsigned int));
else if (*format == 'x')
print_len += ft_print_hex(va_arg(args, unsigned int), *format);
else if (*format == 'X')
print_len += ft_print_hex(va_arg(args, unsigned int), *format);
else if (*format == '%')
print_len += ft_print_percent();
else
return (-1);
return (print_len);
}
int ft_printf(const char *format, ...)
{
int print_len;
va_list args;
int res_check_format;
va_start(args, format);
print_len = 0;
while (*format != '\0')
{
if (*format == '%')
{
if (*(++format) == '\0')
break ;
res_check_format = check_format(args, format);
if (res_check_format == -1)
return (-1);
print_len += res_check_format - 1;
}
else
ft_putchar_fd(*format, 1);
print_len++;
format++;
}
va_end(args);
return (print_len);
}