-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlem_in.c
147 lines (133 loc) · 3.92 KB
/
lem_in.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lem_in.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: avanhers <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/05 11:51:03 by avanhers #+# #+# */
/* Updated: 2019/08/28 12:06:20 by avanhers ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "../includes/lem_in.h"
/*
** Check if the line is a valid command and return value corresponding
** to the command
*/
static int num_command(char *line)
{
if (!(ft_strcmp(line, "##start")))
return (1);
else if (!(ft_strcmp(line, "##end")))
return (2);
else if (line[0] == '#')
return (3);
return (0);
}
/*
***********************Read and Parse Room*************************************
*/
static int read_and_parse_room(int fd, char **line, t_anthill *anthill,
t_buff *buff)
{
char **name;
int last_cmd;
int cmd;
last_cmd = 0;
while ((cmd = num_command(*line)) || is_room(anthill, *line))
{
if (is_room(anthill, *line))
{
if (!(name = ft_strsplit(*line, ' ')))
error_message_pars(anthill, "MALLOC_ERROR\n", *line);
add_tab_gc(anthill, (void **)name);
add_room(anthill, name[0], last_cmd);
fill_buff_str(buff, *line);
}
if (cmd)
fill_buff_str(buff, *line);
free(*line);
if (get_next_line(fd, line) <= 0)
error_message_eof(anthill, "ERROR\n", *line);
last_cmd = (cmd != 3) ? cmd : last_cmd;
}
if (anthill->id_start == -1 || anthill->id_end == -1)
error_message_pars(anthill, "ERROR\n", *line);
return (1);
}
/*
***********************Read and Parse Edge*************************************
*/
void read_and_parse_edge(int fd, char **line, t_anthill *anthill,
t_buff *buff)
{
t_edge *edge;
int ret;
int cmd;
while (num_command(*line) || (edge = is_edge(*line, anthill)))
{
cmd = num_command(*line);
if (edge)
{
add_edge(anthill, anthill->graph, edge);
fill_buff_str(buff, *line);
}
if (cmd)
fill_buff_str(buff, *line);
free(*line);
if ((ret = get_next_line(fd, line)) < 0)
error_message_pars(anthill, "ERROR\n", *line);
if (ret == 0)
return ;
edge = NULL;
}
free(*line);
}
/*
*********************Read and Parse Global*************************************
*/
void read_and_parse(int fd, t_anthill *anthill, t_buff *buff)
{
char *line;
if (get_next_line(fd, &line) <= 0)
error_message_eof(anthill, "ERROR\n", line);
while (num_command(line))
{
fill_buff_str(buff, line);
free(line);
if (get_next_line(fd, &line) <= 0)
error_message_pars(anthill, "ERROR\n", line);
}
if (!check_ant(line))
error_message_pars(anthill, "ERROR\n", line);
fill_buff_str(buff, line);
if ((anthill->nb_ant = ft_atoi(line)) <= 0 || anthill->nb_ant > 100000)
error_message_pars(anthill, "ERROR\n", line);
free(line);
if (get_next_line(fd, &line) <= 0)
error_message_eof(anthill, "ERROR\n", line);
read_and_parse_room(fd, &line, anthill, buff);
if (!is_edge(line, anthill))
error_message_pars(anthill, "ERROR\n", line);
create_tabroom_graph(anthill);
read_and_parse_edge(fd, &line, anthill, buff);
get_next_line(-2, &line);
}
void lem_in(int i)
{
int fd;
t_buff buff;
t_anthill *anthill;
init_buff(&buff);
anthill = init_anthill(i);
fd = 0;
read_and_parse(fd, anthill, &buff);
if (!bfs(anthill))
error_message(anthill, "ERROR\n");
write(1, &buff, buff.i);
ft_putchar('\n');
algo(anthill);
free_gc(anthill->head_gar_c);
free(anthill);
}