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.
142 lines
4.0 KiB
C
142 lines
4.0 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* 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_destroy_data(t_syntax *synt, t_holder *holder, t_lmdata *ldata)
|
|
// {
|
|
// // 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->e_cmd = 0;
|
|
synt->s_error = 0;
|
|
synt->e_error = 0;
|
|
synt->v_error = 0;
|
|
synt->l_error = 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;
|
|
holder->count = 0;
|
|
holder->data = NULL;
|
|
ldata->nbants = 0;
|
|
ldata->nb_nodes = 0;
|
|
ldata->nodes_data = NULL;
|
|
}
|
|
|
|
int lm_init_memory(t_syntax *synt, t_holder *holder,
|
|
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)
|
|
{
|
|
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 *new;
|
|
|
|
if (!(new = (t_node *)malloc(sizeof(t_node))))
|
|
return (0);
|
|
tab = ft_strsplit(raw, ' ');
|
|
if (tab != NULL)
|
|
{
|
|
if (lm_check_room_before(tab, synt))
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
int lm_find_index(t_lmdata *data, char *str)
|
|
{
|
|
t_node *nodes;
|
|
|
|
nodes = data->nodes_data;
|
|
while (nodes)
|
|
{
|
|
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]);
|
|
new->next = NULL;
|
|
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);
|
|
}
|