-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
82 lines (73 loc) · 1.92 KB
/
Copy pathft_split.c
File metadata and controls
82 lines (73 loc) · 1.92 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariyad <ariyad@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/24 14:12:16 by ariyad #+# #+# */
/* Updated: 2024/11/01 20:51:54 by ariyad ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static t_bool ft_issep(char s, char c)
{
return (s == c || s == '\0');
}
static size_t word_c(const char *s, char c)
{
size_t i;
size_t count;
i = 0;
count = 0;
while (s[i])
{
if (!ft_issep(s[i], c) && ft_issep(s[i + 1], c))
count++;
i++;
}
return (count);
}
static void freemem(char **split, size_t i)
{
while (i--)
{
free(split[i]);
}
free(split);
}
static t_bool ft_fill(char **split, const char *s, char sep)
{
size_t i;
size_t pos;
size_t count;
i = 0;
pos = 0;
count = 0;
while (s[i])
{
if (!ft_issep(s[i], sep) && ft_issep(s[i + 1], sep))
{
split[pos] = ft_substr(s, i - count, count + 1);
if (!split[pos++])
return (freemem(split, pos - 1), FALSE);
count = -1;
}
if (!ft_issep(s[i], sep))
count++;
i++;
}
split[pos] = NULL;
return (TRUE);
}
char **ft_split(char const *s, char c)
{
char **split;
if (!s)
return (NULL);
split = (char **)malloc((word_c(s, c) + 1) * sizeof(char *));
if (!split || !ft_fill(split, s, c))
return (NULL);
return (split);
}