-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge.c
93 lines (82 loc) · 2.38 KB
/
edge.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* edge.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: avanhers <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/09 10:33:34 by avanhers #+# #+# */
/* Updated: 2019/08/21 16:23:21 by avanhers ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/lem_in.h"
t_edge *create_edge(t_anthill *anthill)
{
t_edge *new_edge;
if (!(new_edge = (t_edge*)malloc(sizeof(t_edge))))
error_message(anthill, "MALLOC ERROR\n");
garbage_collector(&(anthill->head_gar_c), new_edge);
return (new_edge);
}
/*
**this function look into all room in l_room and look if a room with the same
**name exist.
**If the room already exists, return his len_name
*/
t_room *is_known_room_get(char *line, t_anthill *anthill, int i)
{
t_room *actual;
actual = anthill->l_room;
if (i == 0)
{
while (actual)
{
if (!ft_strncmp(line, actual->name, actual->name_len))
return (actual);
actual = actual->next;
}
}
if (i == 1)
{
while (actual)
{
if (!ft_strcmp(line, actual->name))
return (actual);
actual = actual->next;
}
}
return (NULL);
}
int is_known_room(char *line, t_anthill *anthill)
{
t_room *actual;
actual = anthill->l_room;
while (actual)
{
if (!ft_strcmp(line, actual->name))
return (1);
actual = actual->next;
}
return (0);
}
/*
** check if the current line is a valid edge and return the edge in this case
*/
t_edge *is_edge(char *line, t_anthill *anthill)
{
t_room *room;
int len;
t_edge *edge;
edge = create_edge(anthill);
if (!(room = is_known_room_get(line, anthill, 0)))
return (NULL);
len = room->name_len;
edge->src = room->id;
if (line[len] != '-')
return (NULL);
len++;
if (!line[len] || !(room = is_known_room_get(line + len, anthill, 1)))
return (NULL);
edge->dst = room->id;
return (edge);
}