-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
86 lines (79 loc) · 2.27 KB
/
Copy pathft_printf.c
File metadata and controls
86 lines (79 loc) · 2.27 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: trgoel <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/06 17:46:39 by trgoel #+# #+# */
/* Updated: 2024/10/06 17:47:01 by trgoel ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_resultset(char *set, char c)
{
int x;
x = 0;
while (set[x])
{
if (set[x] == c)
return (1);
x++;
}
return (0);
}
int ft_isinset(char c)
{
return (ft_resultset("cspdiuxX", c));
}
int ft_putdsc(char after_p, void *parsed_arg, int x)
{
if (after_p == 'c')
x += ft_putchar_fd((char)parsed_arg, 1);
else if (after_p == 's')
{
if ((char *)parsed_arg == NULL)
x += ft_putstr_fd("(null)", 1);
else
x += ft_putstr_fd((char *)parsed_arg, 1);
}
else if (after_p == 'p')
{
x += ft_putstr_fd("0x", 1);
x += ft_puthex_fd((unsigned long int)parsed_arg, 0, 1);
}
else if (after_p == 'd' || after_p == 'i')
x += ft_putnbr_fd((int)parsed_arg, 1);
else if (after_p == 'u')
x += ft_putnbr_fd((unsigned int)parsed_arg, 1);
else if (after_p == 'x')
x += ft_puthex_fd((unsigned int)parsed_arg, 0, 1);
else if (after_p == 'X')
x += ft_puthex_fd((unsigned int)parsed_arg, 1, 1);
return (x);
}
int ft_printf(const char *sentence, ...)
{
int x;
int rtn_value;
va_list params;
rtn_value = 0;
x = 0;
va_start(params, sentence);
while (sentence[x])
{
if (sentence[x] == '%')
{
x++;
if (sentence[x] == '%')
rtn_value += ft_putchar_fd('%', 1);
if (ft_isinset(sentence[x]) == 1)
rtn_value += ft_putdsc(sentence[x], va_arg(params, void *), 0);
}
else
rtn_value += ft_putchar_fd(sentence[x], 1);
x++;
}
va_end(params);
return (rtn_value);
}