aboutsummaryrefslogtreecommitdiff
path: root/parse.c
blob: 66ff9ee08d59e399d85502851c1f16c127d23706 (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
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
#include <stdlib.h>
#include "header.h"

#define STRRCHR_CONVERSIONS(c) (ft_strrchr(CONVERSIONS_STR, c))
#define IS_STANDALONE_FLAG(c) (c == '0' || 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));
}

// %[position][dollar][flags][width][.precision][length]type
t_pformat	*parse_conversion(char *conversion)
{
	int i;
	char	*start;
	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->len = i + 1;
	pformat->conversion = strrchr_index(CONVERSIONS_STR, conversion[i]);
	i--;
	if ((conversion = parse_arg_position(conversion, pformat)) == NULL)
		return (NULL);
	start = conversion;
	pformat->zero_padding = FALSE;
	pformat->left_adjusted = FALSE;
	while (IS_STANDALONE_FLAG(*start))
	{
		if (!pformat->zero_padding)
			pformat->zero_padding = *start == '0';
		if (!pformat->left_adjusted)
			pformat->left_adjusted = *start == '-';
		start++;
	}
	pformat->min_field_width_wildcard = FALSE;
	pformat->min_field_width = -1;
	if (*start == '*')
	{
		pformat->min_field_width_wildcard = TRUE;
		start++;
	}
	if (ft_isdigit(*start))
	{
		/* pformat->min_field_width_wildcard = FALSE; */
		pformat->min_field_width = ft_atoi(start);
		while (ft_isdigit(*start))
			start++;
	}
	pformat->precision = -1;
	pformat->precision_wildcard = FALSE;
	if (*start == '.')
	{
		start++;
		if (*start == '*')
			pformat->precision_wildcard = TRUE;
		else
		{
			pformat->precision = ft_atoi(start);
			while (ft_isdigit(*start))
				start++;
		}
	}
	return (pformat);
}

char	*parse_arg_position(char *conversion, t_pformat *pformat)
{
	if (strrchr_index(conversion, '$') != -1)
	{
		if ((pformat->ap_index = ft_atoi(conversion)) == 0)
		{
			free(pformat);
			return (NULL);
		}
		while (ft_isdigit(*conversion))
			conversion++;
		conversion++;
	}
	else
		pformat->ap_index = -1;
	return (conversion);
}