-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
91 lines (83 loc) · 1.96 KB
/
Copy pathft_split.c
File metadata and controls
91 lines (83 loc) · 1.96 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: user <user@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/29 15:30:00 by user #+# #+# */
/* Updated: 2023/04/29 15:30:00 by user ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_check_sep(char *s, char c)
{
size_t i;
size_t count;
int sep_flg;
i = 0;
sep_flg = 1;
count = 0;
while (s[i])
{
if (s[i] == c)
sep_flg = 1;
else if (s[i] != c && sep_flg == 1)
{
sep_flg = 0;
count++;
}
else if (s[i] != c)
sep_flg = 0;
i++;
}
return (count);
}
static void all_free(char **rtn, size_t size)
{
while (size > 0)
free(rtn[size--]);
free(rtn[0]);
free(rtn);
}
static char **ft_cpy(char *s, char **rtn, char c, size_t sep)
{
size_t i;
size_t j;
size_t k;
i = 0;
k = 0;
while (k < sep)
{
j = 0;
while (s[i] == c && s[i])
i++;
while (s[i + j] != c && s[i + j])
j++;
rtn[k] = ft_substr(s, i, j);
if (!rtn[k])
{
all_free(rtn, k);
return (NULL);
}
i += j;
k += 1;
}
return (rtn);
}
char **ft_split(char const *s, char c)
{
char **rtn;
size_t sep;
if (!s)
return (NULL);
sep = ft_check_sep((char *)s, c);
rtn = (char **)malloc(sizeof(char *) * (sep + 1));
if (!rtn)
return (NULL);
rtn = ft_cpy((char *)s, rtn, c, sep);
if (!rtn)
return (NULL);
rtn[sep] = NULL;
return (rtn);
}