-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
108 lines (98 loc) · 2.22 KB
/
Copy pathft_strsplit.c
File metadata and controls
108 lines (98 loc) · 2.22 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
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ophuong <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/15 12:51:43 by ophuong #+# #+# */
/* Updated: 2020/05/24 12:00:14 by Student ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t f_cutdelim(char const *s, char c, size_t od)
{
while (s[od] == c)
{
od++;
}
return (od);
}
static size_t f_words(const char *s, char c)
{
size_t id;
size_t n;
id = 0;
n = 0;
while (s[id] != '\0')
{
if (s[id] != c && (s[id + 1] == c || s[id + 1] == '\0'))
{
n++;
}
id++;
}
return (n);
}
static char *f_new(const char *str, char c)
{
size_t i;
size_t xd;
char *new;
i = 0;
xd = 0;
while (str[i] != c && str[i] != '\0')
i++;
new = ft_strnew(i + 1);
if (new == NULL)
return (NULL);
while (xd < i)
{
new[xd] = str[xd];
xd++;
}
return (new);
}
static int f_fresh(char const *s, char c, size_t id, char **fresh)
{
size_t od;
od = 0;
while (s[od] != '\0')
{
if (s[od] == c)
od = f_cutdelim(s, c, od);
else if (s[od] != c)
{
fresh[id] = f_new(s + od, c);
if (fresh[id] == NULL)
{
while (id > 0)
{
ft_strdel(&fresh[id]);
id--;
}
ft_strdel(&fresh[id]);
return (0);
}
od = od + ft_strlen(fresh[id]);
id++;
}
}
return (id);
}
char **ft_strsplit(char const *s, char c)
{
char **fresh;
size_t id;
id = 0;
fresh = 0;
if (s == NULL)
return (NULL);
if (!(fresh = (char**)malloc(sizeof(char*) * (f_words(s, c) + 1))))
return (NULL);
id = f_fresh(s, c, id, fresh);
fresh[id] = 0;
if (id == 0)
ft_strdel(fresh);
return (fresh);
}