-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
128 lines (114 loc) · 2.52 KB
/
ft_split.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xle-boul <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/17 23:28:45 by xle-boul #+# #+# */
/* Updated: 2021/10/24 10:56:56 by xle-boul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* splits a string according to the separator and creates
a new string (malloc) with every bit */
typedef struct s_len
{
size_t j;
size_t len;
size_t n;
size_t count;
size_t i;
} t_len;
static int ft_count(char const *s, char c)
{
t_len n;
n.count = 0;
n.i = 0;
if (*s == '\0')
return (0);
while (*s != '\0')
{
if (*s == c)
n.i = 0;
else if (n.i == 0)
{
n.i = 1;
n.count++;
}
s++;
}
return (n.count);
}
static int ft_word(char const *s2, char c, size_t i)
{
t_len l;
l.len = 0;
while (s2[i] != c && s2[i] != '\0')
{
l.len++;
i++;
}
return (l.len);
}
char **ft_free_stuff(char **new, size_t i)
{
while (i > 0)
{
i--;
free((void *)new[i]);
}
free(new);
return (NULL);
}
static char **ft_resolve(char const *s, char **new, char c, size_t len)
{
t_len i;
i.i = 0;
i.j = 0;
while (s[i.i] != '\0' && i.j < len)
{
i.n = 0;
while (s[i.i] == c)
i.i++;
new[i.j] = (char *)malloc(sizeof(char) * ft_word(s, c, i.i) + 1);
if (new[i.j] == NULL)
return (ft_free_stuff(new, i.j));
while (s[i.i] != '\0' && s[i.i] != c)
new[i.j][i.n++] = s[i.i++];
new[i.j][i.n] = '\0';
i.j++;
}
new[i.j] = 0;
return (new);
}
char **ft_split(char const *s, char c)
{
char **new;
t_len len;
if (s == NULL)
return (NULL);
len.len = ft_count(s, c);
new = (char **)malloc(sizeof(char *) * (len.len + 1));
if (new == NULL)
return (NULL);
return (ft_resolve(s, new, c, len.len));
}
/*
int main()
{
char s[] = " lorem ipsum dolor sit ";
char c = ' ';
char **new;
int x;
x = 0;
new = ft_split(s, c);
while (*new)
{
printf("%d %s\n", x, *new);
new++;
x++;
}
return (0);
}
*/