-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
104 lines (93 loc) · 2.11 KB
/
Copy pathft_split.c
File metadata and controls
104 lines (93 loc) · 2.11 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
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: youngcch <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/21 20:08:41 by youngcch #+# #+# */
/* Updated: 2023/03/22 17:50:29 by youngcch ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t split_size(char const *s, char c)
{
size_t i;
size_t cnt;
i = 0;
cnt = 0;
while (s[i] != '\0')
{
while (s[i] != '\0' && s[i] == c)
i++;
if (s[i] != '\0')
cnt++;
while (s[i] != '\0' && s[i] != c)
i++;
}
return (cnt);
}
static char *set_word(char *s, char c)
{
char *tmp;
size_t i;
i = 0;
while (s[i] != '\0' && s[i] != c)
i++;
tmp = (char *)malloc((i + 1) * sizeof(char));
if (!tmp)
return (0);
i = 0;
while (s[i] != '\0' && s[i] != c)
{
tmp[i] = s[i];
i++;
}
tmp[i] = 0;
return (tmp);
}
static void free_split(char **tmp)
{
size_t i;
i = 0;
while (*(tmp + i))
{
free(*(tmp + i));
i++;
}
free(tmp);
}
static char **set_split(char **tmp, char *s, char c)
{
size_t i;
size_t cnt;
i = 0;
cnt = 0;
while (s[i] != '\0')
{
while (s[i] != '\0' && s[i] == c)
i++;
if (s[i] != '\0')
{
tmp[cnt] = set_word(s + i, c);
if (!tmp[cnt])
{
free_split(tmp);
return (NULL);
}
cnt++;
}
while (s[i] != '\0' && s[i] != c)
i++;
}
tmp[cnt] = 0;
return (tmp);
}
char **ft_split(char const *s, char c)
{
char **tmp;
tmp = (char **)malloc((split_size(s, c) + 1) * sizeof(char *));
if (!tmp)
return (0);
return (set_split(tmp, (char *)s, c));
}