diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-14 17:50:33 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-14 17:50:33 +0200 |
commit | cffe15a5a0e499003f98e9e6aa88258d00906bf6 (patch) | |
tree | 71d4eea100b5ae80351ffd3e53c750a1f9d770e4 /src/p_split.c | |
parent | I must split (diff) | |
download | 42-minishell-cffe15a5a0e499003f98e9e6aa88258d00906bf6.tar.gz 42-minishell-cffe15a5a0e499003f98e9e6aa88258d00906bf6.tar.bz2 42-minishell-cffe15a5a0e499003f98e9e6aa88258d00906bf6.tar.xz 42-minishell-cffe15a5a0e499003f98e9e6aa88258d00906bf6.tar.zst 42-minishell-cffe15a5a0e499003f98e9e6aa88258d00906bf6.zip |
Just a few more line and we're good
Diffstat (limited to '')
-rw-r--r-- | src/p_split.c | 113 |
1 files changed, 110 insertions, 3 deletions
diff --git a/src/p_split.c b/src/p_split.c index a53eec9..d834e58 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -13,13 +13,105 @@ #include <libft.h> #include <stdlib.h> +static size_t + p_count_semi_words(const char line[]) +{ + size_t count; + size_t i; + + count = 0; + i = 0; + while (line[i] != '\0') + { + if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0') + count += 1; + i++; + } + return (count); +} + +static size_t + p_count_and_or_words(const char line[], const char c) +{ + size_t count; + size_t i; + + count = 0; + i = 0; + while (line[i] != '\0') + { + if (line[i] == c && line[i + 1] == c && + line[i + 2] != c && line[i + 2] != '\0') + count += 1; + i++; + } + return (count); +} + +static char + *p_get_first_occur(const char *line_ptr) +{ + char *ptr[3]; + size_t len[3]; + + ptr[0] = ft_strnstr(line_ptr, ";", ft_strlen(line_ptr) + 1); + ptr[1] = ft_strnstr(line_ptr, "&&", ft_strlen(line_ptr) + 1); + ptr[2] = ft_strnstr(line_ptr, "||", ft_strlen(line_ptr) + 1); + len[0] = ft_strlen(ptr[0]); + len[1] = ft_strlen(ptr[1]); + len[2] = ft_strlen(ptr[2]); + if (len[0] > len[1] && len[0] > len[2]) + return (ptr[0]); + else if (len[1] > len[0] && len[1] > len[2]) + return (ptr[1]); + else if (len[2] > len[0] && len[2] > len[1]) + return (ptr[2]); + return ((char*)line_ptr); +} + static char - **p_split_to_semis(const char line[]) + **p_split_to_stuff(const char line[], + const size_t count) { char **words; + char *line_ptr; + char *need_ptr; + size_t i; + char c; - if ((words = ft_split(line, ';')) == NULL) + if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL) return (NULL); + line_ptr = (char*)line; + i = 0; + while (i < count) + { + need_ptr = p_get_first_occur(line_ptr); + c = need_ptr[0]; + need_ptr += (c == ';') ? (1) : (0); + need_ptr += (c == '&') ? (2) : (0); + need_ptr += (c == '|') ? (2) : (0); + if (need_ptr - line_ptr == 0) + { + if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) * + sizeof(char)))) == NULL) + return (NULL); + ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr)); + words[i][ft_strlen(line_ptr)] = ';'; + words[i][ft_strlen(line_ptr) + 1] = '\0'; + } + else + { + if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) * + sizeof(char))) == NULL) + return (NULL); + ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1); + words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c; + words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0'; + line_ptr = need_ptr; + } + i++; + } + words[i] = NULL; return (words); } @@ -27,8 +119,23 @@ char **p_split_line(const char line[]) { char **words; + size_t i; + size_t count; - if ((words = p_split_to_semis(line)) == NULL) + count = p_count_semi_words(line); + count += p_count_and_or_words(line, '&'); + count += p_count_and_or_words(line, '|'); + count += 1; + if ((words = p_split_to_stuff(line, count)) == NULL) return (NULL); + /* TODO: delete this */ + ft_printf("words[]:\n--------\n"); + i = 0; + while (words[i] != NULL) { + ft_printf("[%s]\n", words[i]); + i++; + } + ft_printf("[%s]\n", words[i]); + exit(0); return (words); } |