diff options
Diffstat (limited to 'src/s_lpipes.c')
-rw-r--r-- | src/s_lpipes.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/s_lpipes.c b/src/s_lpipes.c new file mode 100644 index 0000000..cb78df3 --- /dev/null +++ b/src/s_lpipes.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lpipes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdlib.h> +#include <stdint.h> + +#include "s_lcom.h" +#include "s_lpipes.h" +#include "s_struct.h" + +struct s_lpipes + *lpipes_last(struct s_lpipes *lpipes) +{ + while (lpipes->next != NULL) + lpipes = lpipes->next; + return (lpipes); +} + +void + lpipes_add_back(struct s_lpipes **alpipes, + struct s_lpipes *new) +{ + struct s_lpipes *tmp; + + if (!*alpipes) + *alpipes = new; + else + { + tmp = lpipes_last(*alpipes); + tmp->next = new; + } +} + +void + lpipes_clear(struct s_lpipes **lpipes) +{ + struct s_lpipes *tmp; + struct s_lpipes *renext; + + if (!lpipes) + return ; + tmp = *lpipes; + while (tmp) + { + renext = tmp->next; + lcom_clear(&tmp->one); + ft_memdel((void*)&tmp); + tmp = renext; + } + *lpipes = NULL; +} + +struct s_lpipes + *lpipes_new(const char pipedword[], + t_msh *msh) +{ + struct s_lpipes *link; + + if (!(link = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) + return (NULL); + link->one = NULL; + if (!(link->one = lcom_new(pipedword, msh))) + { + return (NULL); + } + link->next = NULL; + return (link); +} + +struct s_lpipes + *split_pipes(const char word[], + t_lcom *lcom, + t_msh *msh) +{ + struct s_lpipes *lpipes; + char **words; + size_t i; + + if (!(words = ft_split(word, '|'))) + return (NULL); + i = 0; + if (!(lpipes = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) + return (NULL); + while (words[i]) + { + if (!(lpipes = lpipes_new(words[i], msh))) + { + return (NULL); + } + lpipes_add_back(&lcom->pipes, lpipes); + i++; + } + ft_delwords(words); + return (lpipes); +} |