blob: 11316bad3ef5091c158bde8f1b455c69681c4848 (
plain)
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
|
#include <stdlib.h>
#include "header.h"
#define STRRCHR_CONVERSIONS(c) (ft_strrchr(CONVERSIONS_STR, c))
t_list *parse(const char *format)
{
t_list *format_list;
t_list *tmp;
t_pformat *parsed;
format_list = NULL;
while (*format)
{
if (*format != '%')
{
format++;
continue;
}
format++;
if ((parsed = parse_conversion(isolate_conversion(format))) == NULL)
return (list_destroy(format_list));
if ((tmp = list_new(parsed)) == NULL)
return (list_destroy(format_list));
list_push_back(&format_list, tmp);
}
return (format_list);
}
char *isolate_conversion(const char *conversion_start)
{
int i;
i = 0;
while (strrchr_index(CONVERSIONS_STR, conversion_start[i]) == -1)
i++;
return (ft_strndup(conversion_start, i + 1));
}
t_pformat *parse_conversion(char *conversion)
{
int i;
t_pformat *pformat;
if (conversion == NULL)
return (NULL);
if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL)
return (NULL);
i = ft_strlen(conversion) - 1;
pformat->conversion = strrchr_index(CONVERSIONS_STR, conversion[i]);
i--;
pformat->arg_position = 0;
pformat->left_adjusted = FALSE;
pformat->zero_padding = FALSE;
pformat->min_field_width = -1;
pformat->len = 1;
return (pformat);
}
|