summaryrefslogtreecommitdiffstats
path: root/src/ft_s_lcom.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/ft_s_lcom.c93
1 files changed, 85 insertions, 8 deletions
diff --git a/src/ft_s_lcom.c b/src/ft_s_lcom.c
index 737c6eb..8b61f69 100644
--- a/src/ft_s_lcom.c
+++ b/src/ft_s_lcom.c
@@ -12,24 +12,101 @@
#include <libft.h>
#include <stdlib.h>
+#include <stdint.h>
#include "ft_s_struct.h"
+static int8_t
+ ft_fill_lcom(char *words[],
+ t_lcom **lcom)
+{
+ int64_t i;
+ int64_t j;
+
+ i = 0;
+ if (!((*lcom)->com = (char*)malloc((ft_strlen(words[0]) + 1) *
+ sizeof(char))))
+ return (-1);
+ ft_strlcpy((*lcom)->com, words[0], ft_strlen(words[0]) + 1);
+ while(words[i + 1])
+ i++;
+ if (i > 0)
+ {
+ if (!((*lcom)->args = (char**)malloc((i + 1) * sizeof(char*))))
+ return (-1);
+ j = -1;
+ while (++j < i)
+ {
+ if (!((*lcom)->args[j] =
+ (char*)malloc((ft_strlen(words[j + 1]) + 1) * sizeof(char))))
+ return (-1);
+ ft_strlcpy((*lcom)->args[j], words[j + 1],
+ ft_strlen(words[j + 1]) + 1);
+ }
+ (*lcom)->args[j] = 0;
+ }
+ return (0);
+}
+
+t_lcom
+ *ft_lcom_last(t_lcom *lcom)
+{
+ while (lcom->next != NULL)
+ lcom = lcom->next;
+ return (lcom);
+}
+
+void
+ ft_lcom_add_back(t_lcom **alcom,
+ t_lcom *new)
+{
+ t_lcom *tmp;
+
+ if (!*alcom)
+ *alcom = new;
+ else
+ {
+ tmp = ft_lcom_last(*alcom);
+ tmp->next = new;
+ }
+}
+
+void
+ ft_lcom_clear(t_lcom **lcom)
+{
+ t_lcom *tmp;
+ t_lcom *renext;
+
+ if (!lcom)
+ return ;
+ tmp = *lcom;
+ while (tmp)
+ {
+ renext = tmp->next;
+ ft_memdel((void*)&tmp->com);
+ if (tmp->args)
+ ft_delwords(tmp->args);
+ ft_memdel((void*)&tmp);
+ tmp = renext;
+ }
+ *lcom = NULL;
+}
+
t_lcom
*ft_lcom_new(const char word[])
{
- t_lcom *nlcom;
+ t_lcom *link;
char **words;
- if (!(nlcom = (t_lcom*)malloc(sizeof(t_lcom))))
- {
+ if (!(link = (t_lcom*)malloc(sizeof(t_lcom))))
return (NULL);
- }
if (!(words = ft_split(word, ' ')))
+ return (NULL);
+ if (ft_fill_lcom(words, &link) < 0)
{
+ ft_delwords(words);
return (NULL);
}
- nlcom->com = words[0];
- nlcom->args = words + 1;
- nlcom->next = NULL;
- return (nlcom);
+ link->next = NULL;
+ ft_delwords(words);
+ return (link);
}