-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split_quotes.c
99 lines (88 loc) · 2.23 KB
/
ft_split_quotes.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:29 by zhabri #+# #+# */
/* Updated: 2023/01/09 13:46:58 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
bool is_in_str(char *sep, char c)
{
int i;
i = 0;
while (sep[i])
if (sep[i++] == c)
return (true);
return (false);
}
static int tab_len(char *str, char *sep)
{
int i;
int n;
i = 0;
n = 0;
while (str[i])
{
if (!is_in_str(sep, str[i]))
if ((!is_in_str(sep, str[i + 1]))
|| !str[i + 1])
n++;
if (str[i] == '"')
i += find_quote(str + i + 1, '"');
else if (str[i] == '\'')
i += find_quote(str + i + 1, '\'');
i++;
}
return (n);
}
static char **split_loop(char **tab, char const *s, char *sep, int skip_sep)
{
size_t i;
size_t j;
char *tmp;
i = 0;
j = 0;
while (i <= ft_strlen(s))
{
if (!s[i] || (is_in_str(sep, s[i])))
{
if (j < i)
{
tmp = ft_substr(s, j, i - j);
if (!tmp)
{
free_tab(tab);
return (NULL);
}
add_to_tab(tab, remove_quotes(tmp));
}
j = i + skip_sep;
}
i += skip_if_quotes(s, i);
}
return (tab);
}
char **ft_split_quotes(char const *s, char *sep)
{
char **tab;
if (!s)
return (NULL);
tab = ft_calloc(tab_len((char *)s, sep) + 2, sizeof(char *));
if (!tab)
return (NULL);
return (split_loop(tab, s, sep, 1));
}
char **ft_split_quotes_keep_sep(char const *s, char *sep)
{
char **tab;
if (!s)
return (NULL);
tab = ft_calloc(tab_len((char *)s, sep) + 2, sizeof(char *));
if (!tab)
return (NULL);
return (split_loop(tab, s, sep, 0));
}