wordsplit: handle multiple words

This commit is contained in:
Khaïs COLIN 2025-02-14 15:44:10 +01:00
parent 00fd2380cf
commit 4171a3d07b
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 68 additions and 17 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
/* Updated: 2025/02/14 13:56:45 by khais ### ########.fr */
/* Updated: 2025/02/14 16:47:04 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,10 +37,15 @@ t_wordlist *wordlist_create(t_worddesc *word)
*/
void wordlist_destroy(t_wordlist *wordlist)
{
if (wordlist == NULL)
return ;
worddesc_destroy(wordlist->word);
free(wordlist);
t_wordlist *prev;
while (wordlist != NULL)
{
worddesc_destroy(wordlist->word);
prev = wordlist;
wordlist = wordlist->next;
free(prev);
}
}
/*
@ -53,8 +58,36 @@ t_worddesc *wordlist_get(t_wordlist *wordlist, int idx)
if (wordlist == NULL || idx < 0)
return (NULL);
while (idx > 0 && wordlist != NULL)
{
wordlist = wordlist->next;
idx--;
}
if (wordlist == NULL)
return (NULL);
return (wordlist->word);
}
/*
** 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);
}