-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_lex.c
124 lines (113 loc) · 2.4 KB
/
get_lex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_lex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mle-roy <mle-roy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/03/05 18:16:58 by mle-roy #+# #+# */
/* Updated: 2014/03/06 19:26:22 by mle-roy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "get_next_line.h"
#include "lemmin.h"
static int is_start_end(char *str)
{
char *buf;
static int start = 0;
static int end = 0;
buf = ft_strtrim(str);
if (!ft_strcmp(buf, "##start"))
{
free(buf);
if (start == 1)
return (0);
start = 1;
return (1);
}
else if (!ft_strcmp(buf, "##end"))
{
free(buf);
if (end == 1)
return (0);
end = 1;
return (1);
}
free(buf);
return (0);
}
static int is_valid_lem(char *buf)
{
int flag;
int i;
i = 0;
flag = 0;
while (buf[i])
{
if (is_valid_char_lem(buf[i]) && (i == 0 || ft_isspace(buf[i - 1])))
flag++;
i++;
}
if (flag > 3)
return (0);
else
return (1);
}
static int check_buf(char *buf)
{
int i;
i = 0;
while (ft_isspace(buf[i]))
i++;
if (buf[i] == '\0')
return (-1);
if (buf[i] == '#')
{
if (is_start_end(buf))
return (1);
return (0);
}
if (!is_valid_lem(buf))
return (-1);
return (1);
}
void add_to_save(t_lx *lex, char *buf)
{
t_lex *node;
node = init_node(buf);
if (lex->save == NULL)
{
lex->save = node;
lex->save_end = node;
}
else
{
lex->save_end->next = node;
node->prev = lex->save_end;
lex->save_end = node;
}
}
t_lx *get_lex(void)
{
char *buf;
t_lx *list;
int ret;
ret = 0;
if ((list = init_lex()) == NULL)
return (NULL);
while ((ret = get_next_line(0, &buf)))
{
if (ret == -1)
return (NULL);
add_to_save(list, buf);
if ((ret = check_buf(buf)))
{
if (ret == -1)
return (list);
add_to_list(list, buf);
}
free(buf);
}
return (list);
}