-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathft_strsplit.c
More file actions
82 lines (74 loc) · 1.99 KB
/
ft_strsplit.c
File metadata and controls
82 lines (74 loc) · 1.99 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_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlambert <rlambert@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/05 15:26:03 by rlambert #+# #+# */
/* Updated: 2015/01/02 18:42:50 by rlambert ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static const char *ft_str_find_next(const char *str, char c, int skip)
{
if (skip)
while (*str != '\0' && *str == c)
str++;
else
while (*str != '\0' && *str != c)
str++;
return (str);
}
static int ft_str_count_splits(const char *str, char seps)
{
int i;
i = 0;
while (*str != '\0')
{
str = ft_str_find_next(str, seps, 1);
if (*str != '\0')
{
i++;
str = ft_str_find_next(str, seps, 0);
}
}
return (i);
}
static char **ft_tabledel(char **ret, int len)
{
int i;
i = 0;
while (i < len)
free(ret[i]);
free(ret);
return (NULL);
}
char **ft_strsplit(char const *str, char c)
{
char **ret;
int i;
const char *next;
if (str == NULL)
return (NULL);
ret = (char**)malloc(sizeof(char*) * (ft_str_count_splits(str, c) + 1));
if (ret == NULL)
return (NULL);
i = 0;
while (*str != '\0')
{
str = ft_str_find_next(str, c, 1);
if (*str != '\0')
{
next = ft_str_find_next(str, c, 0);
ret[i] = ft_strsub(str, 0, next - str);
if (ret[i] == NULL)
return (ft_tabledel(ret, i));
i++;
str = next;
}
}
ret[i] = 0;
return (ret);
}