-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
99 lines (89 loc) · 2.39 KB
/
Copy pathft_split.c
File metadata and controls
99 lines (89 loc) · 2.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wweerasi <wweerasi@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 12:23:14 by wweerasi #+# #+# */
/* Updated: 2024/05/07 21:29:26 by wweerasi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **ft_split_alloc(char const *s, char c)
{
char **arr_s;
char *temp_s;
int split_count;
if (s != 0)
{
temp_s = (char *) s;
split_count = 0;
while (*temp_s != '\0')
{
if ((temp_s == s && *temp_s != c) \
|| (temp_s != s && *(temp_s - 1) == c && *(temp_s) != c))
split_count++;
temp_s++;
}
arr_s = malloc((split_count + 1) * sizeof(char *));
return (arr_s);
}
return (0);
}
static char *ft_substr_alloc(char const *s, char c)
{
char *substr_s;
int substr_len;
substr_len = 0;
while (*s != '\0' && *s != c)
{
substr_len++;
s++;
}
substr_s = malloc((substr_len + 1) * sizeof(char));
return (substr_s);
}
static char **ft_free_split(char **split, char **tmp)
{
while (tmp-- > split)
free (*tmp);
free (tmp + 1);
return (NULL);
}
static char *ft_substr_copy(char *substr, char const *s, char c)
{
char *sub;
sub = substr;
while (*s != '\0' && *s != c)
*sub++ = *s++;
*sub = '\0';
return (substr);
}
char **ft_split(char const *s, char c)
{
char **split_s;
char *tmp_s;
char **tmp_split;
tmp_s = (char *) s;
split_s = ft_split_alloc(s, c);
if (split_s != NULL)
{
tmp_split = split_s;
while (*tmp_s != '\0')
{
if ((tmp_s == s && *tmp_s != c) \
|| (tmp_s != s && *(tmp_s - 1) == c && *(tmp_s) != c))
{
*tmp_split = ft_substr_alloc(tmp_s, c);
if (*tmp_split == NULL)
return (ft_free_split(split_s, tmp_split));
ft_substr_copy(*tmp_split, tmp_s, c);
tmp_split++;
}
tmp_s++;
}
*tmp_split = 0;
}
return (split_s);
}