invalid maps
This commit is contained in:
@@ -26,23 +26,24 @@ int lm_error_exit(int flag)
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_syntax synt;
|
||||
t_graph graph;
|
||||
//t_graph graph;
|
||||
t_lmdata ldata;
|
||||
t_holder holder;
|
||||
int fd;
|
||||
|
||||
lm_init_data(&synt, &ldata);
|
||||
if (ac > 1)
|
||||
if (ac == 2)
|
||||
{
|
||||
if ((fd = open (av[1], O_RDONLY)) < -1)
|
||||
return (lm_error_exit(2));
|
||||
if (lm_parser(fd, &synt, &ldata, &graph))
|
||||
lm_init_data(&synt, &ldata, &holder);
|
||||
lm_parser(fd, &synt, &ldata, &holder);
|
||||
if (lm_validate(&synt, &ldata))
|
||||
{
|
||||
//the parser was successful in extracting data from the map
|
||||
//here goes the function for making the adjacency list
|
||||
ft_printf("number of ants %d\n", ldata.nbants);
|
||||
ft_printf("start flags status %d, location %d \n", synt.s_cmd, synt.s_pos);
|
||||
ft_printf("end flag status %d, location %d \n", synt.e_cmd, synt.e_pos);
|
||||
//validation works
|
||||
ft_printf("Success\n");
|
||||
}
|
||||
else
|
||||
ft_printf("Fail\n");
|
||||
}
|
||||
return (0);
|
||||
}
|
28
srcs/lm_check_errors.c
Normal file
28
srcs/lm_check_errors.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_check_errors.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/02 08:39:09 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/02 08:39:22 by mndhlovu ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_check_room_before(char **tab, t_syntax *synt)
|
||||
{
|
||||
if ((ft_atoi(tab[1]) > INT_MIN && ft_atoi(tab[1]) < INT_MAX)
|
||||
&& (ft_atoi(tab[2]) > INT_MIN && ft_atoi(tab[2]) < INT_MAX)
|
||||
&& !(*tab[0] == 'L' || *tab[0] == '#'))
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
synt->error = 1;
|
||||
return (0);
|
||||
}
|
||||
}
|
@@ -12,57 +12,51 @@
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_adjnode *lm_new_node(int dest)
|
||||
{
|
||||
t_adjnode *new;
|
||||
|
||||
if (!(new = (t_adjnode *)malloc(sizeof(t_adjnode))))
|
||||
return (NULL);
|
||||
new->dest = dest;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
t_adjlist *lm_new_node(int dest)
|
||||
{
|
||||
t_adjlist *newnode;
|
||||
|
||||
if (!(newnode = (t_adjlist *)malloc(sizeof(t_adjlist))))
|
||||
return (NULL);
|
||||
newnode->dest = dest;
|
||||
newnode->next = NULL;
|
||||
return (newnode);
|
||||
}
|
||||
|
||||
t_graph *lm_creategraph(int v)
|
||||
{
|
||||
t_graph *new_graph;
|
||||
int index;
|
||||
t_graph *newgraph;
|
||||
int index;
|
||||
|
||||
index = -1;
|
||||
if (!(new_graph = (t_graph *)malloc(sizeof(t_graph))))
|
||||
return (NULL);
|
||||
new_graph->vert = v;
|
||||
if (!(new_graph->array = (t_adjlist *)malloc(sizeof(t_adjlist) * v)))
|
||||
return (NULL);
|
||||
while (++index < v)
|
||||
new_graph->array[index].head = NULL;
|
||||
return (new_graph);
|
||||
index = -1;
|
||||
if (!(newgraph = (t_graph *)malloc(sizeof(t_graph))))
|
||||
return (NULL);
|
||||
newgraph->v = v;
|
||||
//create an array of adjacency list. size of array will be v
|
||||
if (!(newgraph->array = (t_adj *)malloc(sizeof(t_adj) * v)))
|
||||
return (NULL);
|
||||
while (++index < v)
|
||||
newgraph->array[
|
||||
index].head = NULL;
|
||||
return (newgraph);;
|
||||
}
|
||||
|
||||
void lm_ext_conn(t_graph *graph, t_lmdata *data,
|
||||
char *raw, t_syntax *synt)
|
||||
{
|
||||
char **tab;
|
||||
|
||||
if (synt->gr_status == 5)
|
||||
{
|
||||
graph = lm_creategraph(data->nb_nodes);
|
||||
synt->gr_status = 1;
|
||||
}
|
||||
tab = ft_strsplit(raw, '-');
|
||||
if (tab != NULL)
|
||||
lm_add_edge(graph, lm_find_index(data, tab[0]), lm_find_index(data, tab[1]));
|
||||
}
|
||||
|
||||
//function to add the edges and create a proper adjacency list
|
||||
void lm_add_edge(t_graph *graph, int src, int dest)
|
||||
{
|
||||
t_adjnode *newnode;
|
||||
t_adjlist *newnode;
|
||||
|
||||
newnode = lm_new_node(dest);
|
||||
newnode->next = graph->array[src].head;
|
||||
graph->array[src].head = newnode;
|
||||
newnode = lm_new_node(src);
|
||||
newnode->next = graph->array[dest].head;
|
||||
graph->array[dest].head = newnode;
|
||||
/*
|
||||
* Add an edge from src to dest. A new node is added to the adjacency list of
|
||||
* src. The node is added at the beginning
|
||||
*/
|
||||
newnode = lm_new_node(dest);
|
||||
newnode->next = graph->array[src].head;
|
||||
graph->array[src].head = newnode;
|
||||
/*
|
||||
* Since graph is undirected, add an edge from dest to src also
|
||||
*/
|
||||
newnode = lm_new_node(src);
|
||||
newnode->next = graph->array[dest].head;
|
||||
graph->array[dest].head = newnode;
|
||||
}
|
@@ -12,13 +12,15 @@
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void lm_init_data(t_syntax *synt, t_lmdata *ldata)
|
||||
void lm_init_data(t_syntax *synt, t_lmdata *ldata,
|
||||
t_holder *holder)
|
||||
{
|
||||
synt->s_cmd = 0;
|
||||
synt->s_pos = 0;
|
||||
synt->e_cmd = 0;
|
||||
synt->e_pos = 0;
|
||||
synt->gr_status = 0;
|
||||
synt->error = 0;
|
||||
synt->e_vert = 0;
|
||||
synt->s_vert = 0;
|
||||
synt->v_flag = 0;
|
||||
@@ -27,36 +29,50 @@ void lm_init_data(t_syntax *synt, t_lmdata *ldata)
|
||||
ldata->nodes_data = NULL;
|
||||
ldata->nodes = NULL;
|
||||
ldata->adj = NULL;
|
||||
holder->count = 0;
|
||||
holder->data = NULL;
|
||||
}
|
||||
|
||||
void lm_add_vertex(t_lmdata *ldata, char *raw, char flag)
|
||||
static void lm_add_vertex_sub(t_lmdata *ldata, t_node *new)
|
||||
{
|
||||
t_node *tmp;
|
||||
|
||||
if (ldata->nodes_data == NULL)
|
||||
ldata->nodes_data = new;
|
||||
else
|
||||
{
|
||||
tmp = ldata->nodes_data;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = new;
|
||||
}
|
||||
(ldata->nb_nodes)++;
|
||||
}
|
||||
|
||||
int lm_add_vertex(t_lmdata *ldata, char *raw, char flag,
|
||||
t_syntax *synt)
|
||||
{
|
||||
char **tab;
|
||||
t_node *room;
|
||||
t_node *tmp;
|
||||
t_node *new;
|
||||
|
||||
if (!(room = (t_node *)malloc(sizeof(t_node))))
|
||||
return ;
|
||||
if (!(new = (t_node *)malloc(sizeof(t_node))))
|
||||
return (0);
|
||||
tab = ft_strsplit(raw, ' ');
|
||||
if (tab != NULL)
|
||||
{
|
||||
room->name = tab[0];
|
||||
room->x = ft_atoi(tab[1]);
|
||||
room->y = ft_atoi(tab[2]);
|
||||
room->role = flag;
|
||||
room->ind = ldata->nb_nodes;
|
||||
room->next = NULL;
|
||||
if (ldata->nodes_data == NULL)
|
||||
ldata->nodes_data = room;
|
||||
else
|
||||
if (lm_check_room_before(tab, synt))
|
||||
{
|
||||
tmp = ldata->nodes_data;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = room;
|
||||
new->name = tab[0];
|
||||
new->x = ft_atoi(tab[1]);
|
||||
new->y = ft_atoi(tab[2]);
|
||||
new->role = flag;
|
||||
new->ind = ldata->nb_nodes;
|
||||
new->next = NULL;
|
||||
lm_add_vertex_sub(ldata, new);
|
||||
return (1);
|
||||
}
|
||||
(ldata->nb_nodes)++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_find_index(t_lmdata *data, char *str)
|
||||
@@ -64,14 +80,41 @@ int lm_find_index(t_lmdata *data, char *str)
|
||||
t_node *nodes;
|
||||
|
||||
nodes = data->nodes_data;
|
||||
if (nodes != NULL)
|
||||
while (nodes)
|
||||
{
|
||||
while (nodes->next)
|
||||
{
|
||||
if (ft_strcmp(nodes->name, str))
|
||||
return (nodes->ind);
|
||||
nodes = nodes->next;
|
||||
}
|
||||
if (ft_strcmp(nodes->name, str) == 0)
|
||||
return (nodes->ind);
|
||||
nodes = nodes->next;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
int lm_ext_conn(t_holder *holder, t_lmdata *data,
|
||||
char *raw)
|
||||
{
|
||||
char **tab;
|
||||
t_temp *temp;
|
||||
t_temp *new;
|
||||
|
||||
if (!(new = (t_temp *)malloc(sizeof(t_temp))))
|
||||
return (0);
|
||||
tab = ft_strsplit(raw, '-');
|
||||
if (tab != NULL)
|
||||
{
|
||||
new->src_ind = lm_find_index(data, tab[0]);
|
||||
new->dest_ind = lm_find_index(data, tab[1]);
|
||||
if (holder->data == NULL)
|
||||
holder->data = new;
|
||||
else
|
||||
{
|
||||
temp = holder->data;
|
||||
while (temp->next)
|
||||
temp = temp->next;
|
||||
temp->next = new;
|
||||
|
||||
}
|
||||
(holder->count)++;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
@@ -14,18 +14,12 @@
|
||||
|
||||
static void lm_locate_cmd(int count, char *raw, t_syntax *synt)
|
||||
{
|
||||
int s_state;
|
||||
int e_state;
|
||||
char *tmp;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
s_state = synt->s_cmd;
|
||||
e_state = synt->e_cmd;
|
||||
tmp = ft_strchr(raw, '#');
|
||||
if ((!s_state || !e_state) && (tmp != NULL && raw[index + 1] == '#'))
|
||||
if ((!synt->s_cmd || !synt->e_cmd) && (tmp != NULL && raw[1] == '#'))
|
||||
{
|
||||
if (!s_state)
|
||||
if (!synt->s_cmd)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##start"))
|
||||
{
|
||||
@@ -33,7 +27,7 @@ static void lm_locate_cmd(int count, char *raw, t_syntax *synt)
|
||||
synt->e_pos = count;
|
||||
}
|
||||
}
|
||||
if (!e_state)
|
||||
if (!synt->e_cmd)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##end"))
|
||||
{
|
||||
@@ -44,8 +38,8 @@ static void lm_locate_cmd(int count, char *raw, t_syntax *synt)
|
||||
}
|
||||
}
|
||||
|
||||
static void lm_get_vertices(int count, char *raw, t_syntax *synt,
|
||||
t_lmdata *data, t_graph *graph)
|
||||
static int lm_get_vertices(int count, char *raw, t_syntax *synt,
|
||||
t_lmdata *data, t_holder *holder)
|
||||
{
|
||||
int index;
|
||||
char *tmp;
|
||||
@@ -54,16 +48,15 @@ static void lm_get_vertices(int count, char *raw, t_syntax *synt,
|
||||
|
||||
index = 0;
|
||||
value = 0;
|
||||
graph = NULL;
|
||||
if (synt->s_cmd > 0 && (synt->e_pos == count - 1))
|
||||
{
|
||||
synt->s_vert = count;
|
||||
lm_add_vertex(data, raw, 's');
|
||||
return (lm_add_vertex(data, raw, 'e', synt));
|
||||
}
|
||||
if (synt->e_cmd > 0 && (synt->s_pos == count - 1))
|
||||
{
|
||||
synt->e_vert = count;
|
||||
lm_add_vertex(data, raw, 'e');
|
||||
return (lm_add_vertex(data, raw, 's', synt));
|
||||
}
|
||||
if (count > 0 && (count != synt->s_vert && count != synt->e_vert)
|
||||
&& (count != synt->s_pos && count != synt->e_pos))
|
||||
@@ -71,12 +64,11 @@ static void lm_get_vertices(int count, char *raw, t_syntax *synt,
|
||||
tmp = ft_strchr(raw, '-');
|
||||
tmp2 = ft_strchr(raw, '#');
|
||||
if (tmp == NULL && tmp2 == NULL)
|
||||
lm_add_vertex(data, raw, 'v');
|
||||
return (lm_add_vertex(data, raw, 'v', synt));
|
||||
else if (tmp2 == NULL && tmp != NULL)
|
||||
{
|
||||
lm_ext_conn(graph, data, raw, synt);
|
||||
}
|
||||
return (lm_ext_conn(holder, data, raw));
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static void lm_get_ant_num(int count, char *raw, t_lmdata *ldata, t_syntax *synt)
|
||||
@@ -101,26 +93,37 @@ static void lm_get_ant_num(int count, char *raw, t_lmdata *ldata, t_synt
|
||||
}
|
||||
}
|
||||
|
||||
int lm_validate(t_syntax *synt, t_lmdata *lmdata)
|
||||
{
|
||||
if (!synt->nb_state || !synt->s_cmd || !synt->e_cmd || synt->error)
|
||||
return (0);
|
||||
if (lmdata->nbants == 0 || lmdata->nb_nodes == 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int lm_parser(int fd, t_syntax *synt, t_lmdata *ldata,
|
||||
t_graph *graph)
|
||||
void lm_parser(int fd, t_syntax *synt, t_lmdata *ldata,
|
||||
t_holder *tmp_data)
|
||||
{
|
||||
char *raw;
|
||||
int ret;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
ft_printf("debug flags %d \n", index);
|
||||
while ((ret = get_next_line(fd, &raw)))
|
||||
{
|
||||
if (ret == -1)
|
||||
return (0);
|
||||
{
|
||||
synt->error = 1;
|
||||
return ;
|
||||
}
|
||||
lm_get_ant_num(index, raw, ldata, synt);
|
||||
lm_locate_cmd(index, raw, synt);
|
||||
lm_get_vertices(index, raw, synt, ldata, graph);
|
||||
if (!lm_get_vertices(index, raw, synt, ldata, tmp_data))
|
||||
{
|
||||
synt->error = 1;
|
||||
return ;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
if (index > 1)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
Reference in New Issue
Block a user