buggy version of the parser. debugging with fsanitizer

This commit is contained in:
Mthandazo Ndhlovu 2019-04-01 11:48:33 +02:00
parent ab578f6039
commit 4e2fc9f19e
33 changed files with 3641 additions and 4 deletions

View File

@ -27,7 +27,7 @@ endif
# Compilator
CC = gcc
FLAGS = -Wall -Wextra -Werror
FLAGS = -Wall -Wextra -Werror -g -O0 -fsanitize=address
# Folders
LIBDIR = libft
@ -36,7 +36,7 @@ OBJDIR = objs
INCDIR = includes libft/includes
# Source files
SRC = bfs.c \
SRC = bfs.c lem_in.c lm_parser.c lm_mem_utils.c lm_graph_utils.c \
lst_ind.c \
edmunds_karp.c
@ -87,7 +87,7 @@ clean :
fclean : clean
@$(ECHO) "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Binary File <===$(DEF)"
@$(RM) -f $(NAME1) $(NAME2)
@$(RM) -f $(NAME) $(NAME2)
@$(RM) -rf *.dSYM
@$(MAKE) -C $(LIBDIR) fclean

2
author
View File

@ -1,2 +1,2 @@
mndhlovu
tmaze
tmaze

View File

@ -14,6 +14,7 @@
# define LEM_IN_H
# include "libft.h"
# include <fcntl.h>
typedef struct s_node
{
@ -22,6 +23,7 @@ typedef struct s_node
int y;
char role;
int ind;
struct s_node *next;
} t_node;
typedef struct s_ind
@ -36,6 +38,7 @@ typedef struct s_lmdata
int nbants;
int nb_nodes;
t_list *nodes;
t_node *nodes_data;
t_ind **adj;
} t_lmdata;
@ -46,9 +49,67 @@ typedef struct s_bfs
int queue;
} t_bfs;
//LEM_IN MT STRUCTS
typedef struct s_syntax
{
int nb_state;
int s_cmd;
int s_pos;
int s_vert;
int e_vert;
int gr_status;
int e_cmd;
int e_pos;
int v_flag;
} t_syntax;
typedef struct s_adjnode
{
int dest;
struct s_adjnode *next;
} t_adjnode;
typedef struct s_adjlist
{
t_adjnode *head;
} t_adjlist;
typedef struct s_graph
{
int vert;
t_adjlist *array;
} t_graph;
typedef struct s_neighbour
{
int vertnum;
struct s_neighbor *next;
} t_neighbor;
//MT-FUNCTIONS
int lm_error_exit(int flag);
int lm_parser(int fd, t_syntax *synt,
t_lmdata *ldata, t_graph *graph);
void lm_init_data(t_syntax *synt, t_lmdata *ldata);
void lm_add_vertex(t_lmdata *ldata, char *raw, char flag);
t_adjnode *lm_new_node(int dest);
t_graph *lm_creategraph(int v);
void lm_add_edge(t_graph *graph, int src, int dest);
void lm_ext_conn(t_graph *graph, t_lmdata *data, char *raw, t_syntax *synt);
int lm_find_index(t_lmdata *data, char *str);
void lm_initdata(t_lmdata *data);
int lm_getparams(t_lmdata *data);
t_ind *lst_indadd(t_ind **lst, int ind);
void lst_inddel(t_ind **lst);

BIN
maps/.DS_Store vendored Normal file

Binary file not shown.

3050
maps/big Normal file

File diff suppressed because it is too large Load Diff

11
maps/comments Normal file
View File

@ -0,0 +1,11 @@
3
#you
##start
start 0 0
#lost
##end
end 1 1
#the
#game
start-end
#!!

7
maps/duplicatepipe Normal file
View File

@ -0,0 +1,7 @@
3
##start
start 0 0
##end
end 0 0
start-end
end-start

7
maps/duplicatepipe1 Normal file
View File

@ -0,0 +1,7 @@
3
##start
start 0 0
##end
end 0 0
start-end
start-end

0
maps/empty Normal file
View File

7
maps/illegalname Normal file
View File

@ -0,0 +1,7 @@
3
Lillegalname 2 2
##start
start 0 0
##end
end 1 1
start-end

7
maps/illegalname1 Normal file
View File

@ -0,0 +1,7 @@
3
ill-egalname 2 2
##start
start 0 0
##end
end 1 1
start-end

25
maps/in0 Normal file
View File

@ -0,0 +1,25 @@
3
##start
1 23 3
2 16 7
#commentaire
3 16 3
4 16 5
5 9 3
6 1 5
7 4 8
##end
0 9 5
0-4
0-6
1-3
4-3
5-2
3-5
#autre commentaire
4-2
2-1
7-6
7-2
7-4
6-5

8
maps/invalidcommand Normal file
View File

@ -0,0 +1,8 @@
3
##start
start 0 0
##end
end 1 1
##invalidcommand
room 2 2
start-end

6
maps/invalidcommand1 Normal file
View File

@ -0,0 +1,6 @@
3
start 0 0
##end
end 1 1
##start
start-end

11
maps/loop Normal file
View File

@ -0,0 +1,11 @@
3
##start
start 0 0
##end
end 1 1
mid0 2 2
mid1 3 3
start-mid0
mid0-mid1
mid1-start
start-end

10
maps/loop1 Normal file
View File

@ -0,0 +1,10 @@
3
##start
start 0 0
##end
end 1 1
mid0 2 2
mid1 3 3
start-mid0
mid0-mid1
mid1-start

5
maps/noants Normal file
View File

@ -0,0 +1,5 @@
##start
start 0 0
##end
end 1 1
start-end

5
maps/noend Normal file
View File

@ -0,0 +1,5 @@
3
##start
start 0 0
end 1 1
start-end

5
maps/nopath Normal file
View File

@ -0,0 +1,5 @@
3
##start
start 0 0
##end
end 1 1

5
maps/nostart Normal file
View File

@ -0,0 +1,5 @@
3
start 0 0
##end
end 1 1
start-end

6
maps/overflow Normal file
View File

@ -0,0 +1,6 @@
3
##start
start 0 0
##end
end 999999999999999999999 9999999999999999999999999999
start-end

20
maps/shortest Normal file
View File

@ -0,0 +1,20 @@
3
##start
start 0 0
##end
end 1 1
long0 2 2
long1 3 3
short 4 4
longer0 5 5
longer1 6 6
longer2 7 7
start-long0
long0-long1
long1-end
start-short
short-end
start-longer0
longer0-longer1
longer1-longer2
longer2-end

6
maps/simple Normal file
View File

@ -0,0 +1,6 @@
3
##start
start 0 0
##end
end 1 1
start-end

10
maps/simple1 Normal file
View File

@ -0,0 +1,10 @@
4
##start
start 0 0
##end
end 1 1
dead0 2 2
dead1 3 3
start-dead0
start-dead1
start-end

16
maps/simple2 Normal file
View File

@ -0,0 +1,16 @@
4
##start
start 0 0
##end
end 1 1
dead0 2 2
dead1 3 3
mid0 4 4
mid1 5 5
mid2 6 6
start-dead0
start-mid0
mid0-dead0
start-mid1
mid1-mid2
mid2-end

4
maps/startisend Normal file
View File

@ -0,0 +1,4 @@
3
##start
##end
startend 0 0

13
maps/subject22.map Normal file
View File

@ -0,0 +1,13 @@
3
1 0 2
##start
0 2 0
##end
4 2 6
2 4 2
3 4 4
0-1
0-2
2-3
3-4
4-1

6
maps/underflow Normal file
View File

@ -0,0 +1,6 @@
3
##start
start 0 0
##end
end 1 -9999999999999999999999999
start-end

7
maps/whitespaces Normal file
View File

@ -0,0 +1,7 @@
3
# #end
##start
start 0 0
##end
end 1 1
start-end

48
srcs/lem_in.c Normal file
View 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
View 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
View 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
View 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);
}