-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_split.c
79 lines (73 loc) · 2.01 KB
/
ft_split.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mougnou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/30 20:24:03 by mougnou #+# #+# */
/* Updated: 2019/11/03 17:45:32 by mougnou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "gnl/get_next_line.h"
#include "minishell.h"
int delimc(char *s, char c, t_source *src)
{
int i;
int n;
i = 0;
n = 0;
while (s[i] != '\0')
{
while (s[i] != c && s[i] != '\0')
{
finding_quotes2(s, i, src);
i++;
}
if (s[i] == '\0')
break ;
if (s[i] == c && src->dquotes == 0 && src->squotes == 0
&& src->aslash == 0)
n++;
i++;
}
return (n);
}
int count_len(char *s, char c, t_source *src, int *len)
{
while (s[(*len)] != '\0' && s[(*len)] == c && src->dquotes == 0
&& src->squotes == 0 && src->aslash == 0)
{
finding_quotes2(s, (*len), src);
(*len)++;
}
return ((*len));
}
char **my_ft_split(char *s, char c, t_source *src)
{
int j;
int len;
int start;
int k;
char **p;
j = delimc(s, c, src) + 2;
p = (char **)malloc(j * sizeof(char *));
k = 0;
len = 0;
while (k < j - 1)
{
start = count_len(s, c, src, &len);
while (s[len] != '\0')
{
if (s[len] == c && src->dquotes == 0
&& src->squotes == 0 && src->aslash == 0)
break ;
finding_quotes2(s, len, src);
len++;
}
p[k++] = ft_substr(s, start, (len - start));
}
p[k] = NULL;
return (p);
}