-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
94 lines (81 loc) · 2.25 KB
/
main.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
/* ************************************************************************** */
/* LE - / */
/* / */
/* main.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: aviscogl <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/01/10 13:33:17 by aviscogl #+# ## ## #+# */
/* Updated: 2018/01/10 16:03:20 by aviscogl ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "gc.h"
#include <string.h>
#define IS_DEL(c, d) (c == d)
static int count_words(char *s, char delimiter)
{
int i;
int words;
i = -1;
words = 0;
while (s[++i])
if (((i != 0 && IS_DEL(s[i], delimiter) &&
!IS_DEL(s[i - 1], delimiter)) ||
(s[i + 1] == '\0' && !IS_DEL(s[i], delimiter))))
words++;
return (words);
}
static char *get_word(char *s, char delimiter)
{
int i;
int size;
char *word;
i = -1;
size = 0;
while (s[++i] && !IS_DEL(s[i], delimiter))
size++;
word = GC_ALLOC(sizeof(char) * (size + 1));
word[size] = '\0';
while (*s && !IS_DEL(*s, delimiter))
{
*word++ = *s;
s++;
}
return (word - size);
}
char **ft_strsplit(char *s, char delimiter)
{
int size;
char **tab;
char *str;
if (!s)
return (NULL);
size = count_words(s, delimiter);
if (!(tab = GC_ALLOC((size + 1) * sizeof(char*))))
return (NULL);
tab[size] = 0;
str = s;
while (*str)
{
if (!IS_DEL(*str, delimiter))
{
*tab++ = get_word(str, delimiter);
str += strlen(get_word(str, delimiter)) - 1;
}
str++;
}
return (tab - size);
}
int main(int argc, char *argv[])
{
gc_init(&argc, 1);
char **test;
//GC_REGISTER(global_test);
test = ft_strsplit("helloj=world", '=');
printf("%s\n", test[0]);
printf("%s\n", test[1]);
gc_run();
debug_pointer_list();
gc_destroy();
}