-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_arg_list.c
113 lines (103 loc) · 2.69 KB
/
gen_arg_list.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* gen_arg_list.c :+: :+: */
/* +:+ */
/* By: jsaariko <jsaariko@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/02/19 14:05:00 by jsaariko #+# #+# */
/* Updated: 2020/03/06 17:50:16 by jsaariko ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
t_printf_arg *gen_elem(t_printf_arg **head)
{
t_printf_arg *cur;
t_printf_arg *new;
new = malloc(sizeof(t_printf_arg));
if (!new)
return (NULL);
new->next = NULL;
new->pad_type = p_normal;
new->field_width = 0;
new->precision = -1;
if (*head == NULL)
*head = new;
else
{
cur = *head;
while (cur->next != NULL)
cur = cur->next;
cur->next = new;
}
return (new);
}
int store_conv(char chr, t_printf_arg **cur, va_list ap)
{
if (ft_strchr("uxX", chr))
store_uint(chr, cur, ap);
else if (ft_strchr("di", chr))
store_int(chr, cur, ap);
else if (ft_strchr("c%", chr))
store_char(chr, cur, ap);
else if (ft_strchr("f", chr))
store_float(cur, ap);
else if (ft_strchr("sp", chr))
{
if (store_other(chr, cur, ap) == -1)
return (-1);
}
return (1);
}
char *get_format_str(const char *str, int len)
{
char *format_str;
format_str = (char *)ft_calloc(len + 1, sizeof(char));
if (!format_str)
return (NULL);
ft_strlcpy(format_str, str, len + 1);
return (format_str);
}
int parse_arg(t_printf_arg **head, char **format_str, va_list ap,
char conv)
{
t_printf_arg *cur;
cur = gen_elem(head);
if (!cur)
return (-1);
manage_parser(&cur, *format_str, ap);
if (store_conv(conv, &cur, ap) == -1)
{
free(*format_str);
*format_str = NULL;
return (-1);
}
return (1);
}
int gen_arg_list(t_printf_arg **head, const char *str, va_list ap)
{
char *format_str;
int i;
int len;
i = 0;
while (str[i] != '\0')
{
if (str[i] == '%')
{
i++;
len = ft_strmatch(str + i, "0123456789-*.");
format_str = get_format_str(str + i, len);
if (!format_str)
return (-1);
i += len;
if (ft_strchr("cspdiuxXf%", str[i]) != NULL)
{
if (parse_arg(head, &format_str, ap, str[i]) == -1)
return (-1);
}
free(format_str);
}
i++;
}
return (1);
}