From cffe15a5a0e499003f98e9e6aa88258d00906bf6 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Fri, 14 Aug 2020 17:50:33 +0200
Subject: Just a few more line and we're good

---
 src/p_split.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file 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);
 }
-- 
cgit v1.2.3