-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing_split.c
93 lines (82 loc) · 2.03 KB
/
parsing_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jgoudema <jgoudema@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 16:01:24 by jgoudema #+# #+# */
/* Updated: 2024/03/15 18:47:51 by jgoudema ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
static int split_is_sep(char c, char *sep)
{
int i;
i = 0;
while (sep[i])
{
if (sep[i] == c)
return (1);
i++;
}
return (0);
}
static int split_count(char *str, char *sep)
{
int i;
int len;
i = 0;
len = 0;
while (str[i])
{
while (str[i] && split_is_sep(str[i], sep))
i++;
if (str[i])
len++;
while (str[i] && !split_is_sep(str[i], sep))
i++;
}
return (len);
}
static int split_size(char *str, char *sep)
{
int i;
i = 0;
while (str[i] && !split_is_sep(str[i], sep))
i++;
return (i);
}
void split_free(char **array, int len)
{
int i;
i = 0;
while (array[i] || (i != -1 && i < len))
free(array[i++]);
free(array);
}
char **ft_modif_split(char *str, char *sep)
{
char **array;
char *word;
int i;
array = malloc(sizeof(char *) * (split_count(str, sep) + 1));
i = 0;
if (!array)
return (NULL);
while (*str)
{
while (*str && split_is_sep(*str, sep))
str++;
if (!(*str))
break ;
word = ft_substr(str, 0, split_size(str, sep));
if (!word)
return (split_free(array, i), NULL);
while (*str && !split_is_sep(*str, sep))
str++;
array[i++] = word;
}
array[i] = NULL;
return (array);
}