-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.c
80 lines (71 loc) · 2.39 KB
/
conversions.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
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* :::::::: */
/* conversions.c :+: :+: */
/* +:+ */
/* By: rcappend <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/12/07 11:48:33 by rcappend #+# #+# */
/* Updated: 2020/12/11 16:40:41 by rcappend ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int string_converter(va_list args, t_track *c)
{
char *temp;
temp = va_arg(args, char*);
c->value = ft_substr(temp, 0, ft_strlen(temp));
if (!c->value)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
static int int_converter(va_list args, t_track *c)
{
c->value = ft_itoa(va_arg(args, int));
if (!c->value)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
static int uns_int_converter(va_list args, t_track *c)
{
unsigned int temp;
temp = va_arg(args, unsigned int);
if (c->type == 'u')
c->value = ft_itoa_base(temp, "0123456789");
else if (c->type == 'x')
c->value = ft_itoa_base(temp, "0123456789abcdef");
else
c->value = ft_itoa_base(temp, "0123456789ABCDEF");
if (!c->value)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
static int pointer_converter(va_list args, t_track *c)
{
c->value = ft_itoa_ptr(va_arg(args, unsigned long long));
if (!c->value)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
int value_converter(va_list args, t_track *c)
{
int ret;
ret = 1;
if (c->type == '%' || c->type == 'c')
{
c->value = ft_calloc(2, 1);
if (!c->value)
return (EXIT_FAILURE);
c->value[0] = (c->type == 'c') ? va_arg(args, int) : '%';
ret = 0;
}
else if (c->type == 'i' || c->type == 'd')
ret = int_converter(args, c);
else if (c->type == 's')
ret = string_converter(args, c);
else if (c->type == 'x' || c->type == 'X' || c->type == 'u')
ret = uns_int_converter(args, c);
else if (c->type == 'p')
ret = pointer_converter(args, c);
return (ret);
}