-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
93 lines (86 loc) · 2.57 KB
/
ft_printf.c
File metadata and controls
93 lines (86 loc) · 2.57 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: spunyapr <spunyapr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/04 13:44:58 by spunyapr #+# #+# */
/* Updated: 2024/12/05 17:33:15 by spunyapr ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
char find_percent(const char *format, int index)
{
while (format[index])
{
if (format[index] == '%')
return (format[index + 1]);
index++;
}
return (0);
}
size_t call_function(const char *format, int index, va_list ptr, char percent)
{
size_t len;
len = 0;
if (percent == 'c')
len += ft_printchar(va_arg(ptr, int));
else if (percent == 's')
len += ft_printstr(va_arg(ptr, char *));
else if (percent == 'p')
len += ft_print_ptr(va_arg(ptr, void *));
else if (percent == 'd' || percent == 'i')
len += ft_printnbr(va_arg(ptr, int));
else if (percent == 'u')
len += ft_print_unsigned_nbr(va_arg(ptr, unsigned int));
else if (percent == 'x')
len += (ft_convert_base(va_arg(ptr, int), "0123456789abcdef", 87));
else if (percent == 'X')
len += (ft_convert_base(va_arg(ptr, int), "0123456789ABCDEF", 55));
else if (percent == '%')
len += ft_printchar('%');
else
{
len += ft_printchar('%');
len += ft_printchar(format[index + 1]);
}
return (len);
}
int ft_printf(const char *format, ...)
{
int i;
int len;
char percent;
va_list ptr;
va_start(ptr, format);
i = 0;
len = 0;
if (!format)
return (-1);
while (format[i])
{
if (format[i] != '%')
len += ft_printchar(format[i]);
else if (format[i] == '%')
{
percent = find_percent(format, i);
len += call_function(format, i, ptr, percent);
i++;
}
i++;
}
va_end(ptr);
return (len);
}
// int main ()
// {
// // // int test = ft_printf("%cte%yst%s%%hex is%Xss%d%y",
//'a',"hello",11545,
// -1);
// // // printf("\n%d\n", test);
// // // int test2 = printf("%cte%yst%s%%hex is%Xss%d%y", 'a',"hello",11545,
// -1);
// // printf("\n%d\n", test2);
// printf(" %p ", -1);
// }