buggy version of the parser. debugging with fsanitizer
This commit is contained in:
48
srcs/lem_in.c
Normal file
48
srcs/lem_in.c
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lem_in.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/25 06:31:09 by mndhlovu ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_error_exit(int flag)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_syntax synt;
|
||||
t_graph graph;
|
||||
t_lmdata ldata;
|
||||
int fd;
|
||||
|
||||
lm_init_data(&synt, &ldata);
|
||||
if (ac > 1)
|
||||
{
|
||||
if ((fd = open (av[1], O_RDONLY)) < -1)
|
||||
return (lm_error_exit(2));
|
||||
if (lm_parser(fd, &synt, &ldata, &graph))
|
||||
{
|
||||
//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);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
68
srcs/lm_graph_utils.c
Normal file
68
srcs/lm_graph_utils.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_graph_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/29 07:17:06 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/29 07:17:26 by mndhlovu ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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_graph *lm_creategraph(int v)
|
||||
{
|
||||
t_graph *new_graph;
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
77
srcs/lm_mem_utils.c
Normal file
77
srcs/lm_mem_utils.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_mem_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:37 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/25 06:31:45 by mndhlovu ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void lm_init_data(t_syntax *synt, t_lmdata *ldata)
|
||||
{
|
||||
synt->s_cmd = 0;
|
||||
synt->s_pos = 0;
|
||||
synt->e_cmd = 0;
|
||||
synt->e_pos = 0;
|
||||
synt->gr_status = 0;
|
||||
synt->e_vert = 0;
|
||||
synt->s_vert = 0;
|
||||
synt->v_flag = 0;
|
||||
ldata->nbants = 0;
|
||||
ldata->nb_nodes = 0;
|
||||
ldata->nodes_data = NULL;
|
||||
ldata->nodes = NULL;
|
||||
ldata->adj = NULL;
|
||||
}
|
||||
|
||||
void lm_add_vertex(t_lmdata *ldata, char *raw, char flag)
|
||||
{
|
||||
char **tab;
|
||||
t_node *room;
|
||||
t_node *tmp;
|
||||
|
||||
if (!(room = (t_node *)malloc(sizeof(t_node))))
|
||||
return ;
|
||||
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
|
||||
{
|
||||
tmp = ldata->nodes_data;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = room;
|
||||
}
|
||||
(ldata->nb_nodes)++;
|
||||
}
|
||||
}
|
||||
|
||||
int lm_find_index(t_lmdata *data, char *str)
|
||||
{
|
||||
t_node *nodes;
|
||||
|
||||
nodes = data->nodes_data;
|
||||
if (nodes != NULL)
|
||||
{
|
||||
while (nodes->next)
|
||||
{
|
||||
if (ft_strcmp(nodes->name, str))
|
||||
return (nodes->ind);
|
||||
nodes = nodes->next;
|
||||
}
|
||||
}
|
||||
return (-1);
|
||||
}
|
126
srcs/lm_parser.c
Normal file
126
srcs/lm_parser.c
Normal file
@@ -0,0 +1,126 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:30:51 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/25 06:30:57 by mndhlovu ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
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 (!s_state)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##start"))
|
||||
{
|
||||
synt->s_cmd = 1;
|
||||
synt->e_pos = count;
|
||||
}
|
||||
}
|
||||
if (!e_state)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##end"))
|
||||
{
|
||||
synt->e_cmd = 1;
|
||||
synt->s_pos = count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void lm_get_vertices(int count, char *raw, t_syntax *synt,
|
||||
t_lmdata *data, t_graph *graph)
|
||||
{
|
||||
int index;
|
||||
char *tmp;
|
||||
char *tmp2;
|
||||
int value;
|
||||
|
||||
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');
|
||||
}
|
||||
if (synt->e_cmd > 0 && (synt->s_pos == count - 1))
|
||||
{
|
||||
synt->e_vert = count;
|
||||
lm_add_vertex(data, raw, 'e');
|
||||
}
|
||||
if (count > 0 && (count != synt->s_vert && count != synt->e_vert)
|
||||
&& (count != synt->s_pos && count != synt->e_pos))
|
||||
{
|
||||
tmp = ft_strchr(raw, '-');
|
||||
tmp2 = ft_strchr(raw, '#');
|
||||
if (tmp == NULL && tmp2 == NULL)
|
||||
lm_add_vertex(data, raw, 'v');
|
||||
else if (tmp2 == NULL && tmp != NULL)
|
||||
{
|
||||
lm_ext_conn(graph, data, raw, synt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void lm_get_ant_num(int count, char *raw, t_lmdata *ldata, t_syntax *synt)
|
||||
{
|
||||
int index;
|
||||
int value;
|
||||
|
||||
index = 0;
|
||||
if (count == 0)
|
||||
{
|
||||
if (!ft_isdigit(raw[index + 1]))
|
||||
{
|
||||
value = raw[index] - '0';
|
||||
ldata->nbants = (value > 0) ? value : 0;
|
||||
synt->nb_state = 1;
|
||||
}
|
||||
if (ft_isdigit(raw[index + 1]))
|
||||
{
|
||||
ldata->nbants = (ft_atoi(raw) > 0) ? ft_atoi(raw) : 0;
|
||||
synt->nb_state = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int lm_parser(int fd, t_syntax *synt, t_lmdata *ldata,
|
||||
t_graph *graph)
|
||||
{
|
||||
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);
|
||||
lm_get_ant_num(index, raw, ldata, synt);
|
||||
lm_locate_cmd(index, raw, synt);
|
||||
lm_get_vertices(index, raw, synt, ldata, graph);
|
||||
index++;
|
||||
}
|
||||
if (index > 1)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
Reference in New Issue
Block a user