-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_putformat.c
More file actions
76 lines (69 loc) · 2 KB
/
ft_putformat.c
File metadata and controls
76 lines (69 loc) · 2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putformat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: belkarto <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/30 07:43:32 by belkarto #+# #+# */
/* Updated: 2022/11/29 16:45:22 by belkarto ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_puthex(va_list para, char c)
{
char *ptr;
int len;
char *tmp;
if (c == 'x')
ptr = ft_tohex(va_arg(para, unsigned int), 0);
else if (c == 'X')
ptr = ft_tohex(va_arg(para, unsigned int), 1);
else
{
tmp = ft_tohex(va_arg(para, unsigned long), 0);
ptr = ft_strjoin("0x", tmp);
free(tmp);
}
len = ft_putstr_fd(ptr, 1);
free(ptr);
return (len);
}
static int ft_format(va_list arg, char c)
{
int len;
len = 1;
if (c == 'd' || c == 'i')
len = ft_putnbr_fd(va_arg(arg, int), 1);
else if (c == 'p' || c == 'x' || c == 'X')
len = ft_puthex(arg, c);
else if (c == 'c')
ft_putchar_fd(va_arg(arg, int), 1);
else if (c == 's')
len = ft_putstr_fd(va_arg(arg, char *), 1);
else if (c == 'u')
len = ft_putuint(va_arg(arg, unsigned int));
else if (c == '%')
ft_putchar_fd('%', 1);
else
return (0);
return (len);
}
int ft_putformat(const char *str, va_list args)
{
int i;
int counter;
i = 0;
counter = 0;
while (str[i] != '\0')
{
if (str[i] == '%')
{
counter += ft_format(args, str[i++ + 1]);
i++;
}
else
counter += write(1, &str[i++], 1);
}
return (counter);
}