diff --git a/src/parser/wordlist/wordlist.h b/src/parser/wordlist/wordlist.h index 35fe5a4..f3d789b 100644 --- a/src/parser/wordlist/wordlist.h +++ b/src/parser/wordlist/wordlist.h @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:46:02 by khais #+# #+# */ -/* Updated: 2025/03/21 10:11:30 by jguelen ### ########.fr */ +/* Updated: 2025/03/21 10:16:55 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ typedef struct s_wordlist } t_wordlist; t_wordlist *wordlist_create(t_worddesc *word); +t_wordlist *wordlist_independant_create(t_worddesc *word); void wordlist_destroy(t_wordlist *wordlist); t_worddesc *wordlist_get(t_wordlist *wordlist, int idx); t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc); diff --git a/src/parser/wordlist/wordlist_utils.c b/src/parser/wordlist/wordlist_utils.c index 9506c3a..f32153b 100644 --- a/src/parser/wordlist/wordlist_utils.c +++ b/src/parser/wordlist/wordlist_utils.c @@ -6,12 +6,41 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */ -/* Updated: 2025/03/19 13:43:24 by jguelen ### ########.fr */ +/* Updated: 2025/03/20 14:43:17 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ #include "wordlist.h" +#include "../worddesc/worddesc.h" #include +#include "../../../libft/libft.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 (NULL); + desc_copy = ft_calloc(1, sizeof(t_worddesc)); + if (!desc_copy) + return (free(new), NULL); + desc_copy->word = ft_strdup(word->word); + if (!desc_copy->word) + return (free(desc_copy), free(new), 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), NULL); + return (new); +} /* ** Returns the number of words present in the wordlist given as parameter. diff --git a/src/subst/wildcard_exp.c b/src/subst/wildcard_exp.c index f505283..c0dfe2c 100644 --- a/src/subst/wildcard_exp.c +++ b/src/subst/wildcard_exp.c @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/23 15:02:59 by jguelen #+# #+# */ -/* Updated: 2025/03/19 16:48:12 by jguelen ### ########.fr */ +/* Updated: 2025/03/20 14:17:37 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,10 @@ #include "replace_substr.h" /******************************************************************************/ +/* */ /* NOTE: The use of errno and the setting of it was OKed by Alexandru in this */ /* context. */ +/* */ /******************************************************************************/ /* ** Returns a directory stream corresponding to the current directory. @@ -76,6 +78,9 @@ static t_wordlist *add_file_to_list(t_wordlist **list, char *filename) ** that matches pattern->word if any file matches in the current directory. ** Otherwise return file_pattern itself if nothing matches the perceived ** pattern. This list should be alphabetically sorted. +** Can return NULL only in case of error. +** NOTE: this function never becomes the owner of file_pattern to maintain +** coherency of use. */ static t_wordlist *expand_star_core(t_worddesc *file_pattern) { @@ -102,7 +107,7 @@ static t_wordlist *expand_star_core(t_worddesc *file_pattern) if (errno) return (wordlist_destroy(file_wordlist), NULL); if (!file_wordlist) - wordlist_push(file_wordlist, file_pattern); + file_wordlist = wordlist_independant_create(file_pattern); return (wordlist_quicksort_full(file_wordlist)); } diff --git a/tests/expansion.c b/tests/expansion.c index 93c07c4..a24e8d1 100644 --- a/tests/expansion.c +++ b/tests/expansion.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 17:52/50 by khais #+# #+# */ -/* Updated: 2025/03/20 10:36:23 by jguelen ### ########.fr */ +/* Updated: 2025/03/20 14:47:23 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -167,7 +167,7 @@ static void test_filename_star_expansion(void) //test ., .. and .plop filepattern = create_single_word(".*"); expanded = expand_star(filepattern); - //assert(wordlist_size(expanded) == 3); + assert(wordlist_size(expanded) == 3); //assert_strequal(".", expanded->word->word); //assert_strequal("..", expanded->next->word->word); //assert_strequal(".plop", expanded->next->next->word->word); @@ -176,8 +176,9 @@ static void test_filename_star_expansion(void) //test zero result filepattern = create_single_word("e*x***p*b"); expanded = expand_star(filepattern); + assert(expanded); + assert_strequal(filepattern->word, expanded->word->word); worddesc_destroy(filepattern); - assert(!expanded); wordlist_destroy(expanded); do_leak_check(); }