-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
78 lines (69 loc) · 1.87 KB
/
ft_strsplit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgillot- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/05/12 19:44:17 by lgillot- #+# #+# */
/* Updated: 2015/05/12 20:04:44 by lgillot- ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int word_len(const char *str, char c)
{
int i;
i = 0;
while (*str && *str != c)
{
i++;
str++;
}
return (i);
}
static void copy_word(const char **src, char *dest, char c)
{
while (**src && **src != c)
{
*dest = **src;
dest++;
(*src)++;
}
*dest = '\0';
}
static char **split_rec(const char *str, int word_num, char c)
{
int w_len;
char *word;
char **tab_end;
int tab_size;
w_len = word_len(str, c);
word = malloc(sizeof(char) * (w_len + 1));
copy_word(&str, word, c);
while (*str == c)
str++;
if (!*str)
{
tab_size = word_num + 1;
tab_end = (char**)malloc(sizeof(char*) * tab_size) + tab_size - 1;
*tab_end = 0;
}
else
{
tab_end = split_rec(str, word_num + 1, c);
}
tab_end--;
*tab_end = word;
return (tab_end);
}
char **ft_strsplit(char const *s, char c)
{
char **result;
while (*s == c)
s++;
result = split_rec(s, 1, c);
if (result && result[0] && result[0][0] == '\0')
ft_strdel(&result[0]);
return (result);
}