2025-02-13 15:17:30 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* wordlist.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
|
2025-02-26 14:07:55 +01:00
|
|
|
/* Updated: 2025/02/26 16:57:59 by khais ### ########.fr */
|
2025-02-13 15:17:30 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "wordlist.h"
|
2025-02-26 14:07:55 +01:00
|
|
|
#include "ft_printf.h"
|
2025-02-13 15:17:30 +01:00
|
|
|
#include "libft.h"
|
2025-02-26 14:07:55 +01:00
|
|
|
#include "unistd.h"
|
2025-02-13 15:17:30 +01:00
|
|
|
#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)
|
|
|
|
|
{
|
2025-02-14 15:44:10 +01:00
|
|
|
t_wordlist *prev;
|
|
|
|
|
|
|
|
|
|
while (wordlist != NULL)
|
|
|
|
|
{
|
2025-02-26 14:07:55 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "wordlist_destroy: %p\n", wordlist);
|
2025-02-14 15:44:10 +01:00
|
|
|
worddesc_destroy(wordlist->word);
|
|
|
|
|
prev = wordlist;
|
|
|
|
|
wordlist = wordlist->next;
|
|
|
|
|
free(prev);
|
|
|
|
|
}
|
2025-02-13 15:17:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 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)
|
2025-02-14 15:44:10 +01:00
|
|
|
{
|
2025-02-13 15:17:30 +01:00
|
|
|
wordlist = wordlist->next;
|
2025-02-14 15:44:10 +01:00
|
|
|
idx--;
|
|
|
|
|
}
|
2025-02-13 15:17:30 +01:00
|
|
|
if (wordlist == NULL)
|
|
|
|
|
return (NULL);
|
|
|
|
|
return (wordlist->word);
|
|
|
|
|
}
|
2025-02-14 15:44:10 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** push the given worddesc to the end of the given wordlist.
|
|
|
|
|
**
|
|
|
|
|
** if wordlist is null, create a new wordlist.
|
|
|
|
|
**
|
|
|
|
|
** returns a pointer to the first element in the wordlist.
|
|
|
|
|
**
|
|
|
|
|
** if malloc failure is encountered, all memory for this wordlist is freed, and
|
|
|
|
|
** null is returned.
|
|
|
|
|
*/
|
|
|
|
|
t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc)
|
|
|
|
|
{
|
|
|
|
|
t_wordlist *start;
|
|
|
|
|
|
|
|
|
|
if (wordlist == NULL)
|
|
|
|
|
return (wordlist_create(worddesc));
|
|
|
|
|
start = wordlist;
|
|
|
|
|
while (wordlist->next != NULL)
|
|
|
|
|
wordlist = wordlist->next;
|
|
|
|
|
wordlist->next = wordlist_create(worddesc);
|
|
|
|
|
if (wordlist->next == NULL)
|
|
|
|
|
return (wordlist_destroy(start), NULL);
|
|
|
|
|
return (start);
|
|
|
|
|
}
|
2025-02-21 14:13:51 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** remove and return the first element in the given wordlist
|
|
|
|
|
**
|
|
|
|
|
** If wordlist is empty, return null.
|
|
|
|
|
*/
|
|
|
|
|
t_worddesc *wordlist_pop(t_wordlist **wordlist)
|
|
|
|
|
{
|
|
|
|
|
t_wordlist *first;
|
|
|
|
|
t_worddesc *word;
|
|
|
|
|
|
|
|
|
|
if ((*wordlist) == NULL)
|
|
|
|
|
return (NULL);
|
|
|
|
|
first = *wordlist;
|
|
|
|
|
(*wordlist) = first->next;
|
|
|
|
|
word = first->word;
|
|
|
|
|
free(first);
|
2025-02-26 14:07:55 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "freed a wordlist in wordlist_pop: %p\n", first);
|
2025-02-21 14:13:51 +01:00
|
|
|
return (word);
|
|
|
|
|
}
|