/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* p_args_next.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include "d_define.h" #include "p_args.h" static size_t p_skip_delim_size(const char word[], char c, size_t end) { end++; if (word[end] == c) return (end + 1); while (word[end] != C_NULL && word[end] != c) { end++; if (word[end] == c && c == C_DQUOTE) { if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) { end++; } } } if (word[end] != C_NULL) { end++; } return (end); } static size_t p_arg_len(const char word[], const size_t start) { size_t end; end = start; if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && word[start] != C_NULL) { while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) end++; } else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) { while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) { if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) end = p_skip_delim_size(word, word[end], end); else { while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) end++; } } } return (end); } static char *p_give_me_an_arg(char tmp[], const char word[], const uint16_t i, const size_t start[]) { tmp[0] = '\0'; ft_strlcpy(tmp, word + start[i], (p_arg_len(word, start[i]) - start[i]) + 1); return (tmp); } void p_del_alloced_words(char *words[], uint16_t to_del) { uint16_t i; i = 0; while (i < to_del) { ft_memdel((void*)&words[i]); i++; } } uint16_t p_dup_words(char *words[], const char word[], const uint16_t argc, const size_t start[]) { char tmp[4096]; uint16_t i; i = 0; while (i < argc) { if ((words[i] = ft_strdup(p_give_me_an_arg(tmp, word, i, start))) == NULL) return (i); i++; } return (i); }