-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_split.c
110 lines (100 loc) · 2.43 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marsoare <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 15:28:13 by marsoare #+# #+# */
/* Updated: 2024/04/25 23:49:59 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *word_get(char const *str, char c, int *i, int *in_quotes);
static char **ft_free(char **strs, int count);
static char *ft_strdupword(char const *str, int len);
char **ft_split(char const *s, char c)
{
char **result;
int i;
int j;
int iq;
result = NULL;
i = 0;
j = 0;
iq = 0;
result = ft_calloc((ft_count_words(s, c) + 1), sizeof(char *));
if (!result || !s)
return (NULL);
while (s[i] && j < ft_count_words(s, c))
{
if ((s[i] == c && !iq) || (s[i] == '\'' && s[i + 1] == '\''))
i++;
else
{
result[j] = word_get(s, c, &i, &iq);
if (!result)
return (ft_free(result, j), NULL);
j++;
}
}
return (result[j] = NULL, result);
}
static char **ft_free(char **strs, int count)
{
int i;
i = 0;
while (i < count)
{
free(strs[i]);
i++;
}
free(strs);
return (NULL);
}
static char *word_get(char const *str, char c, int *i, int *in_quotes)
{
int start;
int len;
start = *i;
len = 0;
while (str[*i])
{
if (str[*i] == '\'' && !(*in_quotes))
{
*in_quotes = 1;
if ((*i)++ | len++)
continue ;
}
if (str[*i] == '\'' && *in_quotes)
{
*in_quotes = 0;
if ((*i)++ | len++)
continue ;
}
if (str[*i] == c && !(*in_quotes))
break ;
(*i)++;
len++;
}
return (ft_strdupword(&str[start], len));
}
static char *ft_strdupword(char const *str, int len)
{
char *dup;
int i;
int j;
i = 0;
j = 0;
dup = malloc((len + 1) * sizeof(char));
if (!dup)
return (NULL);
while (i < len && str[i])
{
if (str[i] == '\'')
i++;
dup[j++] = str[i++];
}
dup[j] = '\0';
return (dup);
}