-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.c
123 lines (116 loc) · 2.73 KB
/
reader.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttshivhu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/17 09:22:55 by ttshivhu #+# #+# */
/* Updated: 2017/10/17 09:23:06 by ttshivhu ### ########.fr */
/* */
/* ************************************************************************** */
#include "expert.h"
char *ignore_comment(char *str)
{
int i;
int j;
char *ret;
i = 0;
j = 0;
while (str[i] && str[i] != '#')
{
if (str[i] != ' ' && str[i] != '\t')
j++;
i++;
}
ret = (char *)malloc(sizeof(char) * j + 2);
i = 0;
j = 0;
while (str[i] && str[i] != '#')
{
if (str[i] != ' ' && str[i] != '\t')
ret[j++] = str[i];
i++;
}
if (j >= 1)
ret[j++] = '\n';
ret[j] = '\0';
return (ret);
}
int reader(char *file, char **instructions)
{
char *line;
int fd;
char *tmp;
char *fre;
fd = open(file, O_RDONLY);
if (fd == -1)
return (-1);
*instructions = ft_strnew(1);
while (get_next_line(fd, &line))
{
fre = *instructions;
tmp = ignore_comment(line);
*instructions = ft_strjoin(*instructions, tmp);
free(tmp);
free(fre);
free(line);
}
return (1);
}
char **get_instructions(char *file, char **queries, char **facts)
{
char *inst;
char **spl;
char **rules;
int i;
int j;
i = 0;
if (reader(file, &inst) == -1)
return (NULL);
if (strlen((inst = ft_strtrim(inst))))
spl = ft_strsplit(inst, '\n');
else
return (NULL);
while (spl[i])
{
if (spl[i][0] == '=')
{
if (*facts != NULL)
*facts = ft_strjoin(*facts, spl[i] + 1);
else
*facts = ft_strdup(spl[i] + 1);
}
if (spl[i][0] == '?')
{
if (*queries != NULL)
*queries = ft_strjoin(*queries, spl[i] + 1);
else
*queries = ft_strdup(spl[i] + 1);
}
i++;
}
i = 0;
j = 0;
while (spl[i])
{
if (spl[i][0] != '=' && spl[i][0] != '?')
j++;
i++;
}
rules = (char **)malloc(sizeof(char *) * (j + 1));
i = 0;
j = 0;
while (spl[i])
{
if (spl[i][0] != '=' && spl[i][0] != '?')
rules[j++] = ft_strdup(spl[i]);
i++;
}
i = -1;
while (spl[++i])
free(spl[i]);
free(spl);
rules[j] = NULL;
return (rules);
}