From 0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 4 Sep 2020 22:02:44 +0200 Subject: Time to escape chars --- src/p_args_len.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/p_args_len.c (limited to 'src/p_args_len.c') diff --git a/src/p_args_len.c b/src/p_args_len.c new file mode 100644 index 0000000..8bab9d7 --- /dev/null +++ b/src/p_args_len.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_len.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" + +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_skip_unquote(const char word[], size_t end) +{ + while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + { + end++; + if (ft_iswhitespace(word[end]) == TRUE && end == 1 && + word[end - 1] == C_BACKSLASH) + { + end++; + } + else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && + word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + { + end++; + } + } + return (end); +} + +static size_t + p_skip_quote(const char word[], size_t end) +{ + 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); +} + +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) + { + end = p_skip_unquote(word, end); + } + else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + { + end = p_skip_quote(word, end); + } + return (end); +} + -- cgit v1.2.3