Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/parsing/parsing_utils4.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* parsing_utils4.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/07/26 18:17:15 by yabtaour #+# #+# */
9
/* Updated: 2022/08/08 16:59:06 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../minishell.h"
14
15
static size_t word_count(const char *s, char c)
16
{
17
int i;
18
int count;
19
20
i = 0;
21
count = 0;
22
while (s[i] != '\0')
23
{
24
while (s[i] != '\0' && s[i] == c)
25
i++;
26
if (s[i] != '\0')
27
{
28
if ((s[i - 1] == c || i == 0))
29
count++;
30
i++;
31
}
32
}
33
return (count);
34
}
35
36
char **ft_free_tab(char **str, int i)
37
{
38
while (i >= 0)
39
{
40
free(str[i]);
41
i--;
42
}
43
free(str);
44
return (str);
45
}
46
47
static char **ft_allocation(char const *s, char c)
48
{
49
char **ptr;
50
51
if (s == NULL)
52
return (NULL);
53
ptr = ft_calloc((word_count(s, c) + 1), sizeof(char *));
54
return (ptr);
55
}
56
57
char **ft_split(char *s, char c)
58
{
59
char **ptr;
60
int j;
61
int i;
62
63
ptr = ft_allocation(s, c);
64
if (!ptr)
65
return (ptr);
66
i = 0;
67
while (*s)
68
{
69
while (*s && *s == c)
70
s++;
71
j = 0;
72
while (s[j] != '\0' && s[j] != c)
73
j++;
74
if (j != 0)
75
{
76
ptr[i] = ft_substr(s, 0, j);
77
if (ptr[i] == NULL)
78
return (ft_free_tab(ptr, i));
79
i++;
80
}
81
s = s + j;
82
}
83
return (ptr);
84
}
85
86