/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* wordlist_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */ /* Updated: 2025/04/30 14:52:32 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "wordlist.h" #include "../worddesc/worddesc.h" #include #include "../../../libft/libft.h" #include "../../ft_errno.h" /* ** Creates a new wordlist composed of only one element. Its next field is NULL ** and its word field is a copy of the worddesc passed as a parameter. ** Returns NULL in case of error. */ t_wordlist *wordlist_independant_create(t_worddesc *word) { t_wordlist *new; t_worddesc *desc_copy; new = ft_calloc(1, sizeof(t_wordlist)); if (!new) return (ft_errno(FT_ENOMEM), NULL); desc_copy = ft_calloc(1, sizeof(t_worddesc)); if (!desc_copy) return (free(new), ft_errno(FT_ENOMEM), NULL); desc_copy->word = ft_strdup(word->word); if (!desc_copy->word) return (free(desc_copy), free(new), ft_errno(FT_ENOMEM), NULL); desc_copy->flags = word->flags; desc_copy->marker = ft_strdup(word->marker); new->word = desc_copy; if (!desc_copy->marker && word->marker) return (wordlist_destroy(new), ft_errno(FT_ENOMEM), NULL); return (new); } /* ** Returns the number of words present in the wordlist given as parameter. ** NOTE: Assumes list not to be circular. */ int wordlist_size(t_wordlist *wordlist) { int size; size = 0; while (wordlist) { size++; wordlist = wordlist->next; } return (size); } t_wordlist *wordlist_last(t_wordlist *list) { if (!list) return (NULL); while (list->next) list = list->next; return (list); } /* ** Returns the node at index idx in list or NULL if idx is out ** of bounds. */ t_wordlist *wordlist_get_elem(t_wordlist *list, int idx) { if (list == NULL || idx < 0) return (NULL); while (idx > 0 && list != NULL) { list = list->next; idx--; } if (list == NULL) return (NULL); return (list); }