-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split_quotes.c
92 lines (83 loc) · 2.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emgenc <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 16:30:24 by emgenc #+# #+# */
/* Updated: 2025/02/06 16:30:24 by emgenc ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static int count_tokens(const char *s, int *i, int *cnt)
{
char q;
while (s[*i])
{
while (s[*i] == ' ' || s[*i] == '\t')
(*i)++;
if (!s[*i])
break ;
(*cnt)++;
if (s[*i] == '\'' || s[*i] == '\"')
{
q = s[(*i)++];
while (s[*i] && s[*i] != q)
(*i)++;
if (s[*i])
(*i)++;
}
else
while (s[*i] && s[*i] != ' ' && s[*i] != '\t')
(*i)++;
}
return (*cnt);
}
static void quote_seperator(int *i, const char *s, char ***tab, int *j)
{
int start;
char q;
q = s[(*i)++];
start = *i;
while (s[*i] && s[*i] != q)
(*i)++;
(*tab)[(*j)++] = ft_substr(s, start, (*i) - start);
if (s[*i])
(*i)++;
}
static void normal_splitter(int *i, const char *s, char ***tab, int *j)
{
int start;
start = *i;
while (s[*i] && s[*i] != ' ' && s[*i] != '\t')
(*i)++;
(*tab)[(*j)++] = ft_substr(s, start, (*i) - start);
}
char **ft_split_quotes(const char *s)
{
int i;
int cnt;
int j;
char **tab;
i = 0;
cnt = 0;
j = 0;
tab = malloc(sizeof(char *) * (count_tokens(s, &i, &cnt) + 1));
if (!tab)
return (NULL);
i = 0;
while (s[i])
{
while (s[i] == ' ' || s[i] == '\t')
i++;
if (!s[i])
break ;
if (s[i] == '\'' || s[i] == '\"')
quote_seperator(&i, s, &tab, &j);
else
normal_splitter(&i, s, &tab, &j);
}
tab[j] = NULL;
return (tab);
}