-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.c
147 lines (130 loc) · 2.85 KB
/
nodes.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
/*
** nodes.c for lemin in /home/franck_r/rendu/lemin
**
** Made by Romain Franck
** Login <franck_r@epitech.net>
**
** Started on Tue Mar 25 12:21:39 2014 Romain Franck
** Last update Sun May 4 18:04:41 2014 root
*/
#include <stdio.h>
#include <stdlib.h>
#include "lemin.h"
void get_links(char *line, t_lnk *maillon)
{
int i;
i = 0;
while (line[i] != '-' && line[i])
i++;
maillon->first = my_strdup_no_troll(line);
maillon->first[i] = 0;
maillon->second = my_strdup_no_troll(line + i + 1);
}
void in_empty_link_list(t_sln *links, t_lnk *maillon, char *line)
{
links->first = maillon;
links->last = maillon;
links->size += 1;
maillon->link = my_strdup_no_troll(line);
get_links(line, maillon);
maillon->next = 0;
maillon->prev = 0;
}
void in_link_list(t_sln *links, t_lnk *maillon, char *line)
{
maillon->prev = links->last;
maillon->next = 0;
maillon->link = my_strdup_no_troll(line);
get_links(line, maillon);
links->last->next = maillon;
links->last = maillon;
links->size += 1;
}
t_lnk *add_link_to_list(t_sln *links, char *line)
{
t_lnk *maillon;
if ((maillon = malloc(sizeof(t_nd))) == 0)
return (NULL);
if (links->size == 0)
in_empty_link_list(links, maillon, line);
else
in_link_list(links, maillon, line);
return (maillon);
}
t_nd *add_node_to_list(t_frm *farm, int type, char *line)
{
t_nd *maillon;
if ((maillon = malloc(sizeof(t_nd))) == 0)
return (NULL);
if (type == 2)
farm->start = maillon;
else if (type == 1)
farm->exit = maillon;
if (farm->size == 0)
put_in_empty_node_list(farm, maillon, line);
else
put_in_node_list(farm, maillon, line);
return (maillon);
}
char *get_name(char *line)
{
char *name;
int size;
int i;
i = 0;
while (line[i] && line[i] != ' ')
i++;
size = i;
i = 0;
if ((name = malloc(sizeof(char) * (size + 1))) == NULL)
return (NULL);
while (line[i] && line[i] != ' ')
{
name[i] = line[i];
i++;
}
name[i] = 0;
return (name);
}
void put_in_empty_node_list(t_frm *farm, t_nd *maillon, char *line)
{
farm->first = maillon;
farm->last = maillon;
farm->size += 1;
maillon->name = get_name(line);
maillon->weight = 0;
maillon->full = 0;
maillon->next = 0;
maillon->prev = 0;
}
void put_in_node_list(t_frm *farm, t_nd *maillon, char *line)
{
maillon->name = get_name(line);
maillon->prev = farm->last;
maillon->next = 0;
maillon->weight = 0;
maillon->full = 0;
farm->last->next = maillon;
farm->last = maillon;
farm->size += 1;
}
t_nd *nd_find(t_nd *f, char *name)
{
t_nd *ptr;
ptr = f;
while (my_strcmp(ptr->name, name) == 0 && ptr->next != NULL)
ptr = ptr->next;
return (ptr);
}
int pth_find(t_pn *list, t_nd *room)
{
t_pth *ptr;
ptr = list->first;
while (ptr != list->last)
{
if (ptr->node == room)
return (1);
ptr = ptr->next;
}
return (0);
}