Compare commits

..

1 Commits

Author SHA1 Message Date
Mthandazo Ndhlovu
56537c0917 Semi Final Parser
Parser working fine with valid maps.
only a few adjustments are yet to be made.
The adjustments will not affect the output
of the adjacency list.
FYI leaks like a garden pipe.
2019-04-08 15:27:09 +02:00
15 changed files with 648 additions and 316 deletions

View File

@@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ # # By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# # # Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2019/04/05 13:54:08 by tmaze ### ########.fr # # Updated: 2019/03/31 19:48:45 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@@ -27,7 +27,7 @@ endif
# Compilator # Compilator
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -Werror -g FLAGS = -Wall -Wextra -Werror -g -O0 -fsanitize=address
# Folders # Folders
LIBDIR = libft LIBDIR = libft
@@ -36,15 +36,7 @@ OBJDIR = objs
INCDIR = includes libft/includes INCDIR = includes libft/includes
# Source files # Source files
SRC = lm_parser.c \ SRC = lem_in.c lm_parser.c lm_mem_utils.c lm_graph_utils.c lm_check_errors.c \
lm_mem_utils.c \
lm_graph_utils.c \
lm_check_errors.c \
bfs.c \
lst_ind.c \
edmunds_karp.c \
# lem_in.c \
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */ /* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */
/* Updated: 2019/04/03 15:57:39 by tmaze ### ########.fr */ /* Updated: 2019/03/31 17:58:52 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -125,7 +125,6 @@ int lm_getparams(t_lmdata *data);
t_ind *lst_indadd(t_ind **lst, int ind); t_ind *lst_indadd(t_ind **lst, int ind);
void lst_inddel(t_ind **lst); void lst_inddel(t_ind **lst);
void tablst_inddel(t_ind **tab);
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind); void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind);
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind); t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind);

View File

@@ -6,12 +6,26 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */ /* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */
/* Updated: 2019/04/05 13:24:22 by tmaze ### ########.fr */ /* Updated: 2019/03/31 18:23:21 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #include "lem_in.h"
static void bfs_print(t_bfs *tab, int nb_nodes)
{
int i;
i = 0;
ft_printf("===== bfs print =====\n");
while (i < nb_nodes)
{
ft_printf("index: %d\nparent: %d\nvisited: %d\nqueue: %d\n\n", i
, tab[i].parent, tab[i].visited != 0, tab[i].queue);
i++;
}
}
static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes) static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes)
{ {
int i; int i;
@@ -29,26 +43,13 @@ static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes)
static void bfs_checkadj(t_lmdata *data, t_bfs *tab, int i) static void bfs_checkadj(t_lmdata *data, t_bfs *tab, int i)
{ {
t_ind *it; t_ind *it;
char used;
it = data->adj[tab[i].queue]; it = data->adj[tab[i].queue];
used = 0;
while (it != NULL) while (it != NULL)
{ {
if (tab[it->index].visited == 0 && it->weight == 2) if (tab[it->index].visited == 0 && it->weight > 0)
used = 1;
if (tab[it->index].visited == 0 && it->weight == 2)
break ;
it = it->next;
}
it = data->adj[tab[i].queue];
while (it != NULL)
{
if (tab[it->index].visited == 0 && ((!used && it->weight > 0)
|| (used && it->weight == 2)))
bfs_addtoqueue(tab, it->index, data->nb_nodes); bfs_addtoqueue(tab, it->index, data->nb_nodes);
if (tab[it->index].visited == 0 && ((!used && it->weight > 0) if (tab[it->index].visited == 0 && it->weight > 0)
|| (used && it->weight == 2)))
tab[it->index].parent = tab[i].queue; tab[it->index].parent = tab[i].queue;
it = it->next; it = it->next;
} }
@@ -68,7 +69,7 @@ static void bfs_init(t_bfs *tab, t_lmdata *data)
} }
} }
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind) void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
{ {
int i; int i;
@@ -78,4 +79,5 @@ void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
while (i < data->nb_nodes && tab[i].queue != -1 while (i < data->nb_nodes && tab[i].queue != -1
&& tab[end_ind].parent == -1 && (tab[tab[i].queue].visited = 1)) && tab[end_ind].parent == -1 && (tab[tab[i].queue].visited = 1))
bfs_checkadj(data, tab, i++); bfs_checkadj(data, tab, i++);
bfs_print(tab, data->nb_nodes);
} }

View File

@@ -6,98 +6,106 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */ /* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */
/* Updated: 2019/04/03 17:18:35 by tmaze ### ########.fr */ /* Updated: 2019/03/31 19:42:22 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #include "lem_in.h"
static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind) t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path)
{
t_ind *it;
int i;
i = end_ind;
while (i != -1)
{
it = data->adj[tab[i].parent];
while (it && it->index != i)
it = it->next;
if (it && it->index == i)
it->weight--;
it = data->adj[i];
while (it && it->index != tab[i].parent)
it = it->next;
if (it && it->index == tab[i].parent)
it->weight++;
i = tab[i].parent;
}
}
static int reset_weights(t_lmdata *data, int s_ind, int node)
{
t_ind *it;
t_ind *it2;
it = data->adj[node];
while (it && it->weight != 2)
it = it->next;
if (it && it->weight == 2)
it->weight--;
it2 = data->adj[it->index];
while (it2 && it2->index != node)
it2 = it2->next;
if (it2 && it2->index == node)
it2->weight++;
return ((node == s_ind) ? -1 : it->index);
}
static t_ind **clean_ret(t_ind **ret)
{
tablst_inddel(ret);
return (NULL);
}
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind
, int nb_path)
{ {
t_ind **ret; t_ind **ret;
t_ind *it;
int i; int i;
int j; int j;
int k;
ret = NULL; ret = NULL;
i = 0; i = 0;
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1)))
* (nb_path + 1))) != NULL) != NULL)
{ {
while (i < nb_path && (j = e_ind) == e_ind) ret[nb_path] = NULL;
while (i < nb_path)
{ {
while (j != s_ind) j = end_ind;
ft_printf("===== start resolv %d =====\n", i);
it = data->adj[j];
while (j != start_ind)
{ {
if (lst_indadd(&(ret[i]), j) == NULL) if (lst_indadd(&(ret[i]), j) == NULL)
return (clean_ret(ret)); return (NULL); //protect this
j = reset_weights(data, s_ind, j); it = data->adj[j];
ft_printf("===== parents of %d =====\n", j);
while (it && it->weight != 2)
{
ft_printf("index: %d\nweight: %d\n\n", it->index, it->weight);
it = it->next;
}
if (it && it->weight == 2)
{
ft_printf("parent selected: %d\n", it->index);
it->weight--;
}
k = it->index;
it = data->adj[k];
while (it && it->index != j)
it = it->next;
if (it && it->index == j)
it->weight++;
j = (j == start_ind) ? -1 : k;
} }
if (j != -1 && lst_indadd(&(ret[i]), j) == NULL) if (lst_indadd(&(ret[i]), j) == NULL)
return (clean_ret(ret)); return (NULL); //protect this
i++; i++;
} }
} }
return (ret); return (ret);
} }
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind) t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
{ {
t_bfs tab[data->nb_nodes]; t_bfs bfs_tab[data->nb_nodes];
int i;
int nb_path; int nb_path;
t_ind *it;
nb_path = 0; nb_path = 0;
bfs(data, tab, start_ind, end_ind); ft_printf("===== Init bfs =====\n");
while (tab[end_ind].parent != -1) bfs(data, bfs_tab, start_ind, end_ind);
while (bfs_tab[end_ind].parent != -1)
{ {
nb_path++; nb_path++;
update_weights(data, tab, end_ind); i = end_ind;
bfs(data, tab, start_ind, end_ind); while (i != -1)
{
it = data->adj[bfs_tab[i].parent];
while (it && it->index != i)
it = it->next;
if (it && it->index == i)
it->weight--;
it = data->adj[i];
while (it && it->index != bfs_tab[i].parent)
it = it->next;
if (it && it->index == bfs_tab[i].parent)
it->weight++;
i = bfs_tab[i].parent;
}
ft_printf("===== new bfs =====\n");
bfs(data, bfs_tab, start_ind, end_ind);
}
ft_printf("===== list of adj =====\n");
i = 0;
while (i < 8)
{
ft_printf("===== adj of %d =====\n", i);
it = data->adj[i];
while (it)
{
ft_printf("index: %d\nweight: %d\n\n", it->index, it->weight);
it = it->next;
}
i++;
} }
return (resolve_path(data, start_ind, end_ind, nb_path)); return (resolve_path(data, start_ind, end_ind, nb_path));
} }

View File

@@ -6,59 +6,91 @@
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */ /* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */ /* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
/* Updated: 2019/04/05 13:43:21 by tmaze ### ########.fr */ /* Updated: 2019/04/08 15:15:17 by mndhlovu ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #include "lem_in.h"
int lm_error_exit(int flag) int lm_error_exit(void)
{ {
if (flag == 0) ft_putendl_fd("ERROR", 2);
ft_printf("Invalid Map\n"); return (1);
if (flag == 1) }
ft_printf("Usage ./lem_in < map-file\n");
if (flag == 2) void ft_print_graph(t_graph *graph)
ft_printf("Error opening the Map file\n"); {
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, t_graph *graph)
{
if (!(lm_parser(synt, lmdata, holder)))
return (0);
graph = lm_creategraph(lmdata->nb_nodes);
if (!(lm_adj_parser(graph, holder)))
return (0);
ft_print_graph(graph);
return (1); return (1);
} }
int main(int ac, char **av) int main(int ac, char **av)
{ {
t_syntax synt; t_syntax synt;
//t_graph graph; t_graph graph;
t_lmdata ldata; t_lmdata ldata;
t_holder holder; t_holder holder;
int fd;
t_list *it;
t_node *start;
t_node *end;
if (ac == 2) (void)av;
if (ac == 1)
{ {
if ((fd = open (av[1], O_RDONLY)) < -1) if (!(lm_init_memory(&synt, &holder, &ldata)))
return (lm_error_exit(2)); return (lm_error_exit());
lm_init_data(&synt, &ldata, &holder); if (!(lem_in(&synt, &holder, &ldata, &graph)))
lm_parser(fd, &synt, &ldata, &holder); return (lm_error_exit());
if (lm_validate(&synt, &ldata)) return (0);
{
//validation works
ft_printf("Success\n");
it = ldata.nodes;
while (it && ((t_node*)(it->content))->role != 's')
{
ft_printf("=== node ===\nindex: %d\nrole: %c\n\n", ((t_node*)(it->content))->ind, ((t_node*)(it->content))->role);
it = it->next;
}
start = it->content;
it = ldata.nodes;
while (it && ((t_node*)(it->content))->role != 'e')
it = it->next;
end = it->content;
edmunds_karp(&ldata, start->ind, end->ind);
}
else
ft_printf("Fail\n");
} }
return (0); return (1);
} }

View File

@@ -6,7 +6,7 @@
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */ /* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/02 08:39:09 by mndhlovu #+# #+# */ /* Created: 2019/04/02 08:39:09 by mndhlovu #+# #+# */
/* Updated: 2019/04/02 08:39:22 by mndhlovu ### ########.fr */ /* Updated: 2019/04/08 15:15:44 by mndhlovu ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -14,15 +14,21 @@
int lm_check_room_before(char **tab, t_syntax *synt) int lm_check_room_before(char **tab, t_syntax *synt)
{ {
if ((ft_atoi(tab[1]) > INT_MIN && ft_atoi(tab[1]) < INT_MAX) if (tab[0] != NULL && tab[1] != NULL && tab[2] != NULL)
&& (ft_atoi(tab[2]) > INT_MIN && ft_atoi(tab[2]) < INT_MAX) {
&& !(*tab[0] == 'L' || *tab[0] == '#')) if (!lm_validate_rooms(tab[0], tab[1], tab[2]))
{ {
return (1); synt->v_error = 1;
} return (0);
else }
{ }
synt->error = 1; return (1);
return (0); }
}
int lm_verify_cmd(t_syntax *synt)
{
if (synt->s_cmd && synt->e_cmd && synt->v_error
&& synt->l_error && synt->e_error)
return (1);
return (0);
} }

39
srcs/lm_data_utils.c Normal file
View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lm_data_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/05 06:35:40 by mndhlovu #+# #+# */
/* Updated: 2019/04/05 06:35:54 by mndhlovu ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
t_rdata *lm_new_raw_line(char *raw)
{
t_rdata *line;
if (!(line = (t_rdata *)malloc(sizeof(t_rdata))))
return (NULL);
line->line = ft_strdup(raw);
line->next = NULL;
return (line);
}
void lm_append_line(t_rdata **line, char *raw)
{
if (*line)
{
if((*line)->next)
lm_append_line(&(*line)->next, raw);
else
(*line)->next = lm_new_raw_line(raw);
}
else
{
*line = lm_new_raw_line(raw);
}
}

View File

@@ -12,10 +12,22 @@
#include "lem_in.h" #include "lem_in.h"
t_ind *lm_new_adjnode(int dest)
t_adjlist *lm_new_node(int dest)
{ {
t_adjlist *newnode; t_ind *new;
if (!(new = (t_ind *)ft_memalloc(sizeof(t_ind))))
return (NULL);
new->index = dest;
new->weight = 1;
new->next = NULL;
return (new);
}
t_adjlist *lm_new_node(int dest)
{
t_adjlist *newnode;
if (!(newnode = (t_adjlist *)malloc(sizeof(t_adjlist)))) if (!(newnode = (t_adjlist *)malloc(sizeof(t_adjlist))))
return (NULL); return (NULL);
@@ -24,17 +36,17 @@ t_adjlist *lm_new_node(int dest)
return (newnode); return (newnode);
} }
t_graph *lm_creategraph(int v) t_graph *lm_creategraph(int v)
{ {
t_graph *newgraph; t_graph *newgraph;
int index; int index;
index = -1; index = -1;
if (!(newgraph = (t_graph *)malloc(sizeof(t_graph)))) if (!(newgraph = (t_graph *)ft_memalloc(sizeof(t_graph))))
return (NULL); return (NULL);
newgraph->v = v; newgraph->v = v;
//create an array of adjacency list. size of array will be v //create an array of adjacency list. size of array will be v
if (!(newgraph->array = (t_adj *)malloc(sizeof(t_adj) * v))) if (!(newgraph->array = (t_lm_adj *)ft_memalloc(sizeof(t_lm_adj) * v)))
return (NULL); return (NULL);
while (++index < v) while (++index < v)
newgraph->array[ newgraph->array[
@@ -42,21 +54,41 @@ t_graph *lm_creategraph(int v)
return (newgraph);; return (newgraph);;
} }
int lm_adj_parser(t_graph *graph, t_holder *holder)
{
t_temp *data;
if (holder != NULL)
{
data = holder->data;
if (data != NULL)
{
while (data)
{
lm_add_edge(graph, data->src_ind, data->dest_ind);
data = data->next;
}
return (1);
}
}
return (0);
}
void lm_add_edge(t_graph *graph, int src, int dest) void lm_add_edge(t_graph *graph, int src, int dest)
{ {
t_adjlist *newnode; t_ind *newnode;
/* /*
* Add an edge from src to dest. A new node is added to the adjacency list of * 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 * src. The node is added at the beginning
*/ */
newnode = lm_new_node(dest); newnode = lm_new_adjnode(dest);
newnode->next = graph->array[src].head; newnode->next = graph->array[src].head;
graph->array[src].head = newnode; graph->array[src].head = newnode;
/* /*
* Since graph is undirected, add an edge from dest to src also * Since graph is undirected, add an edge from dest to src also
*/ */
newnode = lm_new_node(src); newnode = lm_new_adjnode(src);
newnode->next = graph->array[dest].head; newnode->next = graph->array[dest].head;
graph->array[dest].head = newnode; graph->array[dest].head = newnode;
} }

View File

@@ -12,25 +12,46 @@
#include "lem_in.h" #include "lem_in.h"
void lm_init_data(t_syntax *synt, t_lmdata *ldata, // void lm_destroy_data(t_syntax *synt, t_holder *holder, t_lmdata *ldata)
t_holder *holder) // {
// // destroy and free memory
// }
static void lm_init_data(t_syntax *synt, t_holder *holder, t_lmdata *ldata)
{ {
synt->nb_state = 0;
synt->s_cmd = 0; synt->s_cmd = 0;
synt->s_pos = 0;
synt->e_cmd = 0; synt->e_cmd = 0;
synt->e_pos = 0; synt->s_error = 0;
synt->gr_status = 0; synt->e_error = 0;
synt->error = 0; synt->v_error = 0;
synt->e_vert = 0; synt->l_error = 0;
synt->s_vert = 0; synt->s_vert = 0;
synt->e_vert = 0;
synt->gr_status = 0;
synt->s_pos = 0;
synt->e_pos = 0;
synt->v_flag = 0; synt->v_flag = 0;
holder->count = 0;
holder->data = NULL;
ldata->nbants = 0; ldata->nbants = 0;
ldata->nb_nodes = 0; ldata->nb_nodes = 0;
ldata->nodes_data = NULL; ldata->nodes_data = NULL;
ldata->nodes = NULL; }
ldata->adj = NULL;
holder->count = 0; int lm_init_memory(t_syntax *synt, t_holder *holder,
holder->data = NULL; t_lmdata *ldata)
{
// if (!(synt = (t_syntax *)malloc(sizeof(t_syntax))))
// return (0);
// if (!(holder = (t_holder *)malloc(sizeof(t_holder))))
// return (0);
// if (!(ldata = (t_lmdata *)malloc(sizeof(t_lmdata))))
// return (0);
// if (!(rdata = (t_rdata *)malloc(sizeof(t_rdata))))
// return (0);
lm_init_data(synt, holder, ldata);
return (1);
} }
static void lm_add_vertex_sub(t_lmdata *ldata, t_node *new) static void lm_add_vertex_sub(t_lmdata *ldata, t_node *new)
@@ -75,9 +96,9 @@ int lm_add_vertex(t_lmdata *ldata, char *raw, char flag,
return (0); return (0);
} }
int lm_find_index(t_lmdata *data, char *str) int lm_find_index(t_lmdata *data, char *str)
{ {
t_node *nodes; t_node *nodes;
nodes = data->nodes_data; nodes = data->nodes_data;
while (nodes) while (nodes)
@@ -103,6 +124,7 @@ int lm_ext_conn(t_holder *holder, t_lmdata *data,
{ {
new->src_ind = lm_find_index(data, tab[0]); new->src_ind = lm_find_index(data, tab[0]);
new->dest_ind = lm_find_index(data, tab[1]); new->dest_ind = lm_find_index(data, tab[1]);
new->next = NULL;
if (holder->data == NULL) if (holder->data == NULL)
holder->data = new; holder->data = new;
else else
@@ -111,7 +133,6 @@ int lm_ext_conn(t_holder *holder, t_lmdata *data,
while (temp->next) while (temp->next)
temp = temp->next; temp = temp->next;
temp->next = new; temp->next = new;
} }
(holder->count)++; (holder->count)++;
return (1); return (1);

View File

@@ -6,124 +6,168 @@
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */ /* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/25 06:30:51 by mndhlovu #+# #+# */ /* Created: 2019/03/25 06:30:51 by mndhlovu #+# #+# */
/* Updated: 2019/03/25 06:30:57 by mndhlovu ### ########.fr */ /* Updated: 2019/04/08 15:13:44 by mndhlovu ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #include "lem_in.h"
static void lm_locate_cmd(int count, char *raw, t_syntax *synt) // void lm_locate_cmd(t_syntax *synt)
// {
// char *line;
// char *tmp;
// int index;
// index = 0;
// while (ft_getline(&line) > 0)
// {
// tmp = ft_strchr(line, '#');
// if ((!synt->s_cmd || !synt->e_cmd) && (tmp != NULL && line[1] == '#'))
// {
// if (!synt->s_cmd)
// {
// if (ft_strcmp(tmp, "##start") == 0)
// {
// synt->s_cmd = 1;
// }
// }
// if (!synt->e_cmd)
// {
// if (ft_strcmp(tmp, "##end") == 0)
// {
// synt->e_cmd = 1;
// synt->e_pos = index;
// }
// }
// }
// index++;
// }
// }
void lm_locate_cd(int index, t_syntax *synt, char *line)
{ {
char *tmp; char *tmp;
tmp = ft_strchr(raw, '#'); tmp = ft_strchr(line, '#');
if ((!synt->s_cmd || !synt->e_cmd) && (tmp != NULL && raw[1] == '#')) if ((!synt->s_cmd || !synt->e_cmd) && (tmp != NULL && line[1] == '#'))
{ {
if (!synt->s_cmd) if (!synt->s_cmd)
{ {
if (ft_strcmp(tmp, "##start")) if (ft_strcmp(tmp, "##start") == 0)
{ {
synt->s_cmd = 1; synt->s_cmd = 1;
synt->e_pos = count; synt->s_pos = index;
} }
} }
if (!synt->e_cmd) if (!synt->e_cmd)
{ {
if (ft_strcmp(tmp, "##end")) if (ft_strcmp(tmp, "##end") == 0)
{ {
synt->e_cmd = 1; synt->e_cmd = 1;
synt->s_pos = count; synt->e_pos = index;
} }
} }
} }
} }
static int lm_get_vertices(int count, char *raw, t_syntax *synt, // void lm_locate_cmd(t_syntax *synt)
t_lmdata *data, t_holder *holder) // {
{ // char *line;
int index; // char *tmp;
char *tmp; // int index;
char *tmp2;
int value;
index = 0; // index = 0;
value = 0; // while (ft_getline(&line) > 0)
if (synt->s_cmd > 0 && (synt->e_pos == count - 1)) // {
{ // tmp = ft_strchr(line, '#');
synt->s_vert = count; // if ((!synt->s_cmd || !synt->e_cmd) && (tmp != NULL && line[1] == '#'))
return (lm_add_vertex(data, raw, 'e', synt)); // {
} // if (!synt->s_cmd)
if (synt->e_cmd > 0 && (synt->s_pos == count - 1)) // {
{ // if (ft_strcmp(tmp, "##start") == 0)
synt->e_vert = count; // {
return (lm_add_vertex(data, raw, 's', synt)); // synt->s_cmd = 1;
} // }
if (count > 0 && (count != synt->s_vert && count != synt->e_vert) // }
&& (count != synt->s_pos && count != synt->e_pos)) // if (!synt->e_cmd)
{ // {
tmp = ft_strchr(raw, '-'); // if (ft_strcmp(tmp, "##end") == 0)
tmp2 = ft_strchr(raw, '#'); // {
if (tmp == NULL && tmp2 == NULL) // synt->e_cmd = 1;
return (lm_add_vertex(data, raw, 'v', synt)); // synt->e_pos = index;
else if (tmp2 == NULL && tmp != NULL) // }
return (lm_ext_conn(holder, data, raw)); // }
} // }
return (1); // index++;
} // }
// }
static void lm_get_ant_num(int count, char *raw, t_lmdata *ldata, t_syntax *synt) // int lm_get_ant_num(t_lmdata *ldata, t_syntax *synt)
// {
// char *line;
// int value;
// if (ft_getline(&line) > 0)
// {
// value = lm_get_value(line);
// if (value != -1)
// {
// ldata->nbants = value;
// synt->nb_state = 1;
// return (1);
// }
// free(line);
// }
// return (0);
// }
int lm_get_ant_(int counter, t_lmdata *ldata, t_syntax *synt, char *line)
{ {
int index;
int value; int value;
index = 0; if (counter == 0)
if (count == 0)
{ {
if (!ft_isdigit(raw[index + 1])) value = lm_get_value(line);
if (value != -1)
{ {
value = raw[index] - '0'; ldata->nbants = value;
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; synt->nb_state = 1;
return (1);
} }
free(line);
} }
return (0);
} }
int lm_validate(t_syntax *synt, t_lmdata *lmdata) static int lm_get_vertices(int count, t_syntax *synt,
t_lmdata *data, t_holder *holder, char *line)
{ {
if (!synt->nb_state || !synt->s_cmd || !synt->e_cmd || synt->error) lm_get_cmd_vert(count, synt, data, line);
return (0); if (!synt->s_error && !synt->e_error)
if (lmdata->nbants == 0 || lmdata->nb_nodes == 0) {
return (0); lm_get_vert_link(count, synt, data, holder, line);
return (1); if (!synt->v_error && !synt->l_error)
return (1);
}
return (0);
} }
void lm_parser(int fd, t_syntax *synt, t_lmdata *ldata, int lm_parser(t_syntax *synt, t_lmdata *ldata,
t_holder *tmp_data) t_holder *holder)
{ {
char *raw; char *raw;
int ret;
int index; int index;
index = 0; index = 0;
while ((ret = get_next_line(fd, &raw))) while (ft_getline(&raw) > 0)
{ {
if (ret == -1) if (!(lm_get_ant_(index, ldata, synt, raw)) && index == 0)
{ return (0);
synt->error = 1; lm_locate_cd(index, synt, raw);
return ; if (!(lm_get_vertices(index, synt, ldata, holder, raw)))
} return (0);
lm_get_ant_num(index, raw, ldata, synt);
lm_locate_cmd(index, raw, synt);
if (!lm_get_vertices(index, raw, synt, ldata, tmp_data))
{
synt->error = 1;
return ;
}
index++; index++;
} }
free(raw);
return (1);
} }

View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lm_parser_error_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/08 06:42:37 by mndhlovu #+# #+# */
/* Updated: 2019/04/08 06:42:52 by mndhlovu ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
int lm_chk_format_nbr(char *raw)
{
while (*raw)
{
if (*raw == '-' || *raw == '+')
raw++;
if (!ft_isdigit(*raw))
return (-1);
raw++;
}
return (0);
}
int lm_check_max(char *raw)
{
if (ft_atoi(raw) < INT_MIN || ft_atoi(raw) > INT_MAX)
return (-1);
return (0);
}
int lm_validate_name(char *name)
{
int index;
index = 0;
if (name[0] == 'L')
return (-1);
while (name[index])
{
if (name[index] == '-')
return (-1);
index++;
}
return (0);
}
int lm_validate_rooms(char *name, char *x, char *y)
{
if (lm_validate_name(name) == -1)
return (-1);
if (lm_chk_format_nbr(x) == -1 ||
lm_check_max(x) == -1 || (ft_atoi(x) > 0 &&
(ft_atoi(x) == 0)) ||
ft_strlen(x) > 19)
return (0);
if (lm_chk_format_nbr(y) == -1 ||
lm_check_max(y) == -1 || (ft_atoi(y) > 0 &&
(ft_atoi(y) == 0)) ||
ft_strlen(y) > 19)
return (0);
return (1);
}
int lm_error_nbr(char *raw)
{
if (lm_chk_format_nbr(raw) == -1 || lm_check_max(raw) == -1 ||
(ft_atoi(raw) > 0 && (ft_atoi(raw) == 0)) ||
ft_strlen(raw) > 19)
return (-1);
return (0);
}

90
srcs/lm_utils_parser.c Normal file
View File

@@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lm_utils_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/04 09:24:45 by mndhlovu #+# #+# */
/* Updated: 2019/04/08 15:14:34 by mndhlovu ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
int lm_get_value(char *line)
{
int index;
if (line != NULL)
{
index = ft_atoi(line);
if (index > INT_MIN && index < INT_MAX)
{
if (index > 0)
return (index);
else
{
return (-1);
}
}
}
return (-1);
}
void lm_get_cmd_vert(int count, t_syntax *synt,
t_lmdata *ldata, char *line)
{
char *tmp;
if (synt->s_pos == count - 1)
{
tmp = ft_strchr(line, '#');
if (tmp == NULL && count != synt->e_vert)
{
synt->s_vert = count;
if (!(lm_add_vertex(ldata, line, 's', synt)))
synt->e_error = 1;
}
}
if (synt->e_pos == count - 1)
{
tmp = ft_strchr(line, '#');
if (tmp == NULL && count != synt->s_vert)
{
synt->e_vert = count;
if (!(lm_add_vertex(ldata, line, 'e', synt)))
synt->s_error = 1;
}
}
}
void lm_get_vert_link(int count, t_syntax *synt, t_lmdata *ldata,
t_holder *holder, char *line)
{
char *link_sign;
char *hash;
if (count > 0 && (count != synt->s_vert && count != synt->e_vert)
&& (count != synt->s_pos && count != synt->e_pos))
{
link_sign = ft_strchr(line, '-');
hash = ft_strchr(line, '#');
if (link_sign == NULL && hash == NULL)
{
if (!(lm_add_vertex(ldata, line, 'v', synt)))
{
synt->v_error = 1;
}
}
if (link_sign != NULL && hash == NULL)
{
if (!(lm_ext_conn(holder, ldata, line)))
{
synt->l_error = 1;
return ;
}
}
}
}

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */ /* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */
/* Updated: 2019/04/03 15:57:26 by tmaze ### ########.fr */ /* Updated: 2019/03/31 17:53:47 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -30,20 +30,10 @@ void lst_inddel(t_ind **lst)
{ {
t_ind *tmp; t_ind *tmp;
while (lst && *lst != NULL) while (*lst != NULL)
{ {
tmp = *lst; tmp = *lst;
*lst = (*lst)->next; *lst = (*lst)->next;
ft_memdel((void**)&tmp); ft_memdel((void**)&tmp);
} }
} }
void tablst_inddel(t_ind **tab)
{
int i;
i = 0;
while (tab[i])
lst_inddel(&(tab[i++]));
ft_memdel((void**)&tab);
}

View File

@@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 11:37:06 by tmaze #+# #+# */ /* Created: 2019/03/28 11:37:06 by tmaze #+# #+# */
/* Updated: 2019/04/05 13:59:02 by tmaze ### ########.fr */ /* Updated: 2019/03/31 19:47:57 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #include "lem_in.h"
#define NB_NODES 4 #define NB_NODES 8
int add_link(t_lmdata *data, int n1, int n2) int add_link(t_lmdata *data, int n1, int n2)
{ {
@@ -29,8 +29,34 @@ int main(void)
data.nb_nodes = NB_NODES; data.nb_nodes = NB_NODES;
if ((data.adj = (t_ind**)ft_memalloc(sizeof(t_ind*) * NB_NODES)) == NULL) if ((data.adj = (t_ind**)ft_memalloc(sizeof(t_ind*) * NB_NODES)) == NULL)
return (1); return (1);
if (!add_link(&data, 0, 2) || !add_link(&data, 0, 1) || !add_link(&data, 0, 3)) if (!add_link(&data, 0, 1) || !add_link(&data, 0, 2))
{ {
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 1, 4))
{
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 2, 3) || !add_link(&data, 2, 5))
{
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 3, 6))
{
lst_inddel(&(data.adj[6]));
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3])); lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2])); lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1])); lst_inddel(&(data.adj[1]));
@@ -38,61 +64,10 @@ int main(void)
ft_memdel((void**)&(data.adj)); ft_memdel((void**)&(data.adj));
return (1); return (1);
} }
/* if (!add_link(&data, 1, 4)) */
/* { */
/* lst_inddel(&(data.adj[2])); */
/* lst_inddel(&(data.adj[1])); */
/* lst_inddel(&(data.adj[0])); */
/* ft_memdel((void**)&(data.adj)); */
/* return (1); */
/* } */
/* if (!add_link(&data, 2, 3) || !add_link(&data, 2, 5)) */
/* { */
/* lst_inddel(&(data.adj[3])); */
/* lst_inddel(&(data.adj[2])); */
/* lst_inddel(&(data.adj[1])); */
/* lst_inddel(&(data.adj[0])); */
/* ft_memdel((void**)&(data.adj)); */
/* return (1); */
/* } */
/* if (!add_link(&data, 3, 6)) */
/* { */
/* lst_inddel(&(data.adj[5])); */
/* lst_inddel(&(data.adj[3])); */
/* lst_inddel(&(data.adj[2])); */
/* lst_inddel(&(data.adj[1])); */
/* lst_inddel(&(data.adj[0])); */
/* ft_memdel((void**)&(data.adj)); */
/* return (1); */
/* } */
/* if (!add_link(&data, 4, 5)) */ /* if (!add_link(&data, 4, 5)) */
/* { */ /* { */
/* lst_inddel(&(data.adj[6])); */ /* lst_inddel(&(data.adj[6])); */
/* lst_inddel(&(data.adj[5])); */ /* lst_inddel(&(data.adj[5])); */
/* lst_inddel(&(data.adj[3])); */
/* lst_inddel(&(data.adj[2])); */
/* lst_inddel(&(data.adj[1])); */
/* lst_inddel(&(data.adj[0])); */
/* ft_memdel((void**)&(data.adj)); */
/* return (1); */
/* } */
/* if (!add_link(&data, 5, 6) || !add_link(&data, 5, 7)) */
/* { */
/* lst_inddel(&(data.adj[6])); */
/* lst_inddel(&(data.adj[5])); */
/* lst_inddel(&(data.adj[4])); */
/* lst_inddel(&(data.adj[3])); */
/* lst_inddel(&(data.adj[2])); */
/* lst_inddel(&(data.adj[1])); */
/* lst_inddel(&(data.adj[0])); */
/* ft_memdel((void**)&(data.adj)); */
/* return (1); */
/* } */
/* if (!add_link(&data, 6, 7)) */
/* { */
/* lst_inddel(&(data.adj[7])); */
/* lst_inddel(&(data.adj[6])); */
/* lst_inddel(&(data.adj[5])); */
/* lst_inddel(&(data.adj[4])); */ /* lst_inddel(&(data.adj[4])); */
/* lst_inddel(&(data.adj[3])); */ /* lst_inddel(&(data.adj[3])); */
/* lst_inddel(&(data.adj[2])); */ /* lst_inddel(&(data.adj[2])); */
@@ -101,6 +76,32 @@ int main(void)
/* ft_memdel((void**)&(data.adj)); */ /* ft_memdel((void**)&(data.adj)); */
/* return (1); */ /* return (1); */
/* } */ /* } */
if (!add_link(&data, 5, 7))
{
lst_inddel(&(data.adj[7]));
lst_inddel(&(data.adj[6]));
lst_inddel(&(data.adj[5]));
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 6, 7))
{
lst_inddel(&(data.adj[7]));
lst_inddel(&(data.adj[6]));
lst_inddel(&(data.adj[5]));
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
ft_printf("===== list of adj =====\n"); ft_printf("===== list of adj =====\n");
i = 0; i = 0;
while (i < NB_NODES) while (i < NB_NODES)
@@ -114,7 +115,7 @@ int main(void)
} }
i++; i++;
} }
path = edmunds_karp(&data, 0, 1); path = edmunds_karp(&data, 0, 7);
if (path != NULL) if (path != NULL)
{ {
i = 0; i = 0;
@@ -127,14 +128,15 @@ int main(void)
it = it->next; it = it->next;
} }
ft_putchar('\n'); ft_putchar('\n');
lst_inddel(&(path[i]));
i++; i++;
} }
} }
tablst_inddel(path); ft_memdel((void**)&path);
/* lst_inddel(&(data.adj[7])); */ lst_inddel(&(data.adj[7]));
/* lst_inddel(&(data.adj[6])); */ lst_inddel(&(data.adj[6]));
/* lst_inddel(&(data.adj[5])); */ lst_inddel(&(data.adj[5]));
/* lst_inddel(&(data.adj[4])); */ lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3])); lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2])); lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1])); lst_inddel(&(data.adj[1]));