-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_args.c
138 lines (131 loc) · 3.53 KB
/
get_args.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aimelda <aimelda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/15 20:19:47 by aimelda #+# #+# */
/* Updated: 2020/08/23 19:24:33 by aimelda ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void sasha2(t_printf *cur, va_list ap)
{
if (!(cur->argt % 'l'))
{
*(long long*)cur->content = va_arg(ap, long);
cur->argt /= 'l';
}
else if (!(cur->argt % 'h'))
{
*(long long*)cur->content = (short)va_arg(ap, int);
cur->argt /= 'h';
if (cur->argt != 'd' && cur->argt != 'i')
*(long long*)cur->content &= 65535;
}
else
{
*(long long*)cur->content = va_arg(ap, int);
if (cur->argt != 'd' && cur->argt != 'i')
*(long long*)cur->content &= 4294967295;
}
}
static int get_long_long(t_printf *cur, va_list ap)
{
if (!(cur->content = malloc(sizeof(long long))))
return (-1);
if (!(cur->argt % ('l' * 'l')))
{
*(long long*)cur->content = va_arg(ap, long long);
cur->argt /= 'l' * 'l';
}
else if (!(cur->argt % ('h' * 'h')))
{
*(long long*)cur->content = (char)va_arg(ap, int);
cur->argt /= 'h' * 'h';
if (cur->argt != 'd' && cur->argt != 'i')
*(long long*)cur->content &= 255;
}
else
sasha2(cur, ap);
if (cur->argt != 'c' && !cur->precision_ast)
cur->zero = ' ';
return (0);
}
static int get_long_double(t_printf *cur, va_list ap)
{
if (!(cur->content = malloc(sizeof(long double))))
return (-1);
if (!(cur->argt % 'l'))
{
*(long double*)cur->content = va_arg(ap, double);
cur->argt /= 'l';
}
else if (!(cur->argt % 'L'))
{
*(long double*)cur->content = va_arg(ap, long double);
cur->argt /= 'L';
}
else
*(long double*)cur->content = va_arg(ap, double);
if (cur->precision_ast == -1)
cur->precision = 6;
if (((char*)cur->content)[9] < 0)
cur->sign = '-';
return (0);
}
static int get_precision_or_width(t_printf *cur, va_list ap, int i, int t)
{
if (cur->width_asterisk == i)
{
if ((t = va_arg(ap, int)) < 0)
{
cur->left_adjusted = 1;
cur->zero = ' ';
cur->width = ft_abs(t);
}
else if (!cur->width)
cur->width = t;
}
if (cur->precision_ast == i)
{
if ((t = va_arg(ap, int)) < 0)
{
cur->precision = 0;
cur->precision_ast = -1;
}
else if (!cur->precision)
cur->precision = t;
return (1);
}
if (cur->width_asterisk == i)
return (1);
return (0);
}
int ft_printf_get_args(t_args **args, va_list ap, int i)
{
t_printf *tmp;
while (++i < MAX_PRINTF_ARG)
if (args[i])
{
tmp = args[i]->usedin;
free_args(args[i]);
if (get_precision_or_width(tmp, ap, i, 0))
continue;
if (!(tmp->argt % 'f') || !(tmp->argt % 'e') || !(tmp->argt % 'g'))
{
if (get_long_double(tmp, ap) == -1)
return (-1);
}
else if (!(tmp->argt % 's'))
{
if (!(tmp->content = va_arg(ap, void*)))
if (!(tmp->content = ft_strdup("(null)")))
return (-1);
}
else if (get_long_long(tmp, ap) == -1)
return (-1);
}
return (0);
}