-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_check.c
119 lines (111 loc) · 3.22 KB
/
ft_check.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mlegeay <mlegeay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/05/18 05:44:32 by mlegeay #+# #+# */
/* Updated: 2017/05/30 07:30:44 by mlegeay ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_check_flags(t_list *f)
{
if (f->frmt[f->index] == '#')
f->flags[2] = 1;
else if (f->frmt[f->index] == '0')
f->flags[3] = 1;
else if (f->frmt[f->index] == '-')
f->flags[4] = 1;
else if (f->frmt[f->index] == '+')
f->flags[5] = 1;
else if (f->frmt[f->index] == ' ')
f->flags[6] = 1;
f->index++;
}
void ft_check_precision(t_list *f)
{
int i;
char *str;
if (f->frmt[f->index] == '.')
{
f->flags[0] = 0;
i = ++f->index;
while (ft_isdigit(f->frmt[f->index]))
f->index++;
if (f->index - i > 0)
{
str = ft_strsub(f->frmt, i, f->index - i);
f->flags[0] = ft_atoi((const char*)str);
free(str);
}
if (f->flags[0] == 0)
f->flags[0] = -1;
}
}
void ft_check_width(t_list *f)
{
int i;
char *str;
i = f->index;
while (ft_isdigit(f->frmt[f->index]))
f->index++;
if (f->index - i > 0)
{
f->flags[1] = 0;
str = ft_strsub(f->frmt, i, f->index - i);
f->flags[1] = ft_atoi((const char*)str);
free(str);
}
}
void ft_check_modifier(t_list *f)
{
while (f->frmt[f->index] == 'h' || f->frmt[f->index] == 'l' ||
f->frmt[f->index] == 'j' || f->frmt[f->index] == 'z')
{
if (f->frmt[f->index] == 'h' && f->frmt[f->index + 1] == 'h')
{
f->flags[7] = 1;
f->index++;
}
else if (f->frmt[f->index] == 'h')
f->flags[8] = 1;
if (f->frmt[f->index] == 'l' && f->frmt[f->index + 1] == 'l')
{
f->flags[9] = 1;
f->index++;
}
else if (f->frmt[f->index] == 'l')
f->flags[10] = 1;
if (f->frmt[f->index] == 'j')
f->flags[11] = 1;
if (f->frmt[f->index] == 'z')
f->flags[12] = 1;
f->index++;
}
}
int ft_check(t_list *f)
{
ft_flags_init(f);
while (f->frmt[f->index] == '#' || f->frmt[f->index] == '0'
|| f->frmt[f->index] == '-' || f->frmt[f->index] == '+'
|| f->frmt[f->index] == ' ' || f->frmt[f->index] == 'h'
|| f->frmt[f->index] == 'l' || f->frmt[f->index] == 'j'
|| f->frmt[f->index] == 'z' || f->frmt[f->index] == '.'
|| ft_isdigit(f->frmt[f->index]))
{
if (f->frmt[f->index] == '#' || f->frmt[f->index] == '0'
|| f->frmt[f->index] == '-' || f->frmt[f->index] == '+'
|| f->frmt[f->index] == ' ')
ft_check_flags(f);
if (ft_isdigit(f->frmt[f->index]) && f->frmt[f->index] != '0')
ft_check_width(f);
if (f->frmt[f->index] == '.')
ft_check_precision(f);
ft_check_modifier(f);
}
if (f->index == ft_strlen(f->frmt))
return (-1);
return (0);
}