IT WORKS !

functionning ./lem_in that can read maps and display ants path
still need to clean leaks
still need remove debug info
This commit is contained in:
Tanguy MAZE
2019-04-09 19:09:29 +02:00
parent 434b629055
commit 3b893147e5
11 changed files with 364 additions and 235 deletions

View File

@@ -6,65 +6,134 @@
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
/* Updated: 2019/04/05 14:40:50 by tmaze ### ########.fr */
/* Updated: 2019/04/09 19:08:36 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
int lm_error_exit(int flag)
int lm_error_exit(void)
{
if (flag == 0)
ft_printf("Invalid Map\n");
if (flag == 1)
ft_printf("Usage ./lem_in < map-file\n");
if (flag == 2)
ft_printf("Error opening the Map file\n");
return (1);
ft_putendl_fd("ERROR", 2);
return (1);
}
void ft_print_graph(t_graph *graph)
{
int v;
t_ind *crawl;
v = -1;
while (++v < graph->v)
{
crawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d\n head", v);
while (crawl)
{
printf("->%d",crawl->index);
crawl = crawl->next;
}
printf("\n");
}
}
void lm_print_report(t_lmdata *ldata, t_syntax *synt, t_holder *holder)
{
t_node *nodes;
t_temp *data;
if (ldata != NULL)
{
nodes = ldata->nodes_data;
while (nodes)
{
ft_printf("name: %s x: %d y: %d index: %d role: %c\n", nodes->name, nodes->x, nodes->y, nodes->ind, nodes->role);
nodes = nodes->next;
}
}
if (holder != NULL)
{
data = holder->data;
while (data)
{
ft_printf("src: %d - dest: %d\n", data->src_ind, data->dest_ind);
data = data->next;
}
}
ft_printf("nb_state: %d\n s_cmd: %d\n e_cmd: %d\n s_error: %d\n e_rror: %d\n v_error: %d\n l_error: %d\n",
synt->nb_state, synt->s_cmd, synt->e_cmd, synt->s_error, synt->e_error, synt->v_error, synt->l_error);
}
static int lem_in(t_syntax *synt, t_holder *holder,
t_lmdata *lmdata)
{
if (!(lm_parser(synt, lmdata, holder)))
return (0);
if (!lst_indinit(lmdata))
return (0);
if (!(lm_adj_parser(lmdata, holder)))
return (0);
return (1);
}
int main(int ac, char **av)
{
t_syntax synt;
//t_graph graph;
t_lmdata ldata;
t_holder holder;
int fd;
t_syntax synt;
t_lmdata ldata;
t_holder holder;
t_ind **ret;
t_node *it;
t_node *start;
t_node *end;
t_ind *it2;
int i;
if (ac == 2)
{
if ((fd = open (av[1], O_RDONLY)) < -1)
return (lm_error_exit(2));
lm_init_data(&synt, &ldata, &holder);
lm_parser(fd, &synt, &ldata, &holder);
if (lm_validate(&synt, &ldata))
{
//validation works
ft_printf("Success\n");
ft_printf("=== ldata ===\nnbants: %d\nnb_nodes: %d\nnodes: %p\nnodes_data: %p\nadj: %d\n\n", ldata.nbants, ldata.nb_nodes, ldata.nodes, ldata.nodes_data, ldata.adj);
it = ldata.nodes_data;
while (it && it->role != 's')
(void)av;
i = 0;
if (ac == 1)
{
lm_init_data(&synt, &holder, &ldata);
if (!(lem_in(&synt, &holder, &ldata)))
return (lm_error_exit());
it = ldata.nodes_data;
start = NULL;
end = NULL;
while (it && (!start || !end))
{
if (it->role == 's')
start = it;
else if (it->role == 'e')
end = it;
it = it->next;
}
ft_printf("data->adj: %p\n", ldata.adj);
if ((!start) || (ret = edmunds_karp(&ldata, start->ind, end->ind)) == NULL)
return (lm_error_exit());
if (ret != NULL)
{
i = 0;
while (ret[i])
{
ft_printf("=== node ===\nindex: %d\nrole: %c\n\n", it->ind, it->role);
it = it->next;
it2 = ret[i];
while (it2)
{
ft_printf(" %d -> ", it2->index);
it2 = it2->next;
}
ft_putchar('\n');
i++;
}
ft_printf("=== node ===\nindex: %d\nrole: %c\n\n", it->ind, it->role);
start = it;
it = ldata.nodes_data;
while (it && it->role != 'e')
{
ft_printf("=== node ===\nindex: %d\nrole: %c\n\n", it->ind, it->role);
it = it->next;
}
ft_printf("=== node ===\nindex: %d\nrole: %c\n\n", it->ind, it->role);
end = it;
edmunds_karp(&ldata, start->ind, end->ind);
}
else
ft_printf("Fail\n");
}
return (0);
}
push_ants(&ldata, ret, i);
tablst_inddel(ret);
i = 0;
while (i < ldata.nb_nodes)
{
lst_inddel(&(ldata.adj[i]));
i++;
}
ft_memdel((void**)&(ldata.adj));
return (0);
}
return (1);
}