42-archive/42sh/src/completion/c_parser.c
2017-02-03 17:36:07 +01:00

80 lines
2.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* c_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:52:07 by alao #+# #+# */
/* Updated: 2017/02/03 17:35:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "completion.h"
/*
** Add the matching element to the list
*/
static void c_add_to_lst(t_comp *c, t_clst *node)
{
if (c->lst == NULL)
{
c->lst = node;
node->next = node;
node->prev = node;
node->cursor = 1;
node->id = 1;
}
else
{
node->id = c->lst->prev->id + 1;
c->lst->prev->next = node;
node->prev = c->lst->prev;
node->next = c->lst;
c->lst->prev = node;
}
}
/*
** Determine if the element should be added to the list. If so, the node is
** created and the function above is called to add it.
*/
static int c_storing(t_comp *c, char *value, unsigned char type)
{
t_clst *tmp;
if (ft_strncmp(".", value, 1) == 0 || ft_strncmp("..", value, 2) == 0)
return (0);
if (c->match && ft_strnequ(c->match, value, ft_strlen(c->match)) != 1)
return (0);
if (c->match && ft_strequ(c->match, value))
return (0);
if (!(tmp = (t_clst *)malloc(sizeof(t_clst))))
return (-1);
tmp->name = ft_strdup(value);
tmp->len = ft_strlen(tmp->name);
tmp->type = (int)type;
tmp->cursor = 0;
c_add_to_lst(c, tmp);
return (0);
}
/*
** Cycle through the path specified.
*/
int c_parser(t_comp *c, char *path, char *name)
{
DIR *rep;
struct dirent *dirc;
(void)name;
if (!(rep = opendir(path)))
return (-1);
while ((dirc = readdir(rep)))
c_storing(c, dirc->d_name, dirc->d_type);
closedir(rep);
return (0);
}