minishell/src/parser/wordlist/wordlist.c

58 lines
1.8 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wordlist.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
/* Updated: 2025/02/14 13:29:16 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordlist.h"
#include "libft.h"
#include <stdlib.h>
/*
** create a new wordlist element, with next set to null and word set to the
** passed worddesc.
**
** in case of error, return null
*/
t_wordlist *wordlist_create(t_worddesc *word)
{
t_wordlist *retvalue;
retvalue = ft_calloc(1, sizeof(t_wordlist));
if (retvalue == NULL)
return (NULL);
retvalue->next = NULL;
retvalue->word = word;
return (retvalue);
}
/*
** free all memory associated with this wordlist.
*/
void wordlist_destroy(t_wordlist *wordlist)
{
free(wordlist);
}
/*
** get the worddesc at position idx in the given wordlist.
**
** return null if out of range or wordlist is null
*/
t_worddesc *wordlist_get(t_wordlist *wordlist, int idx)
{
if (wordlist == NULL || idx < 0)
return (NULL);
while (idx > 0 && wordlist != NULL)
wordlist = wordlist->next;
if (wordlist == NULL)
return (NULL);
return (wordlist->word);
}