-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
executable file
·69 lines (62 loc) · 1.75 KB
/
ft_strsplit.c
File metadata and controls
executable file
·69 lines (62 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ioleksiu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/26 19:17:16 by ioleksiu #+# #+# */
/* Updated: 2016/12/29 16:02:34 by ioleksiu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **ft_mem_dl(void **ap)
{
int i;
i = 0;
if (ap)
{
while (ap[i])
i++;
while (ap[i])
free(ap[i--]);
*ap = NULL;
}
return (0);
}
static char **alloc(char *s, char c)
{
char **dst;
if (!(dst = (char **)malloc(sizeof(char *) * ft_countw(s, c))))
return (0);
if (!(dst[0] = (char*)malloc(sizeof(char) * ft_counts(s, c))))
return (0);
return (dst);
}
char **ft_strsplit(char const *s, char c)
{
char **dst;
size_t x;
size_t y;
x = 0;
if (!s)
return (0);
if (!(dst = alloc((char *)s, c)))
return (0);
while (*s != '\0')
{
while (*s == c && *s != '\0')
s++;
if (*s != c && *s != '\0')
{
y = 0;
if (!(dst[x] = (char*)malloc(sizeof(char) * ft_counts(s, c))))
return (ft_mem_dl((void **)(dst)));
while (*s != c && *s != '\0')
dst[x][y++] = *s++;
dst[x++][y] = '\0';
}
}
dst[x] = 0;
return (dst);
}