-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
117 lines (107 loc) · 2.28 KB
/
ft_split.c
File metadata and controls
117 lines (107 loc) · 2.28 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_split.c :+: :+: */
/* +:+ */
/* By: Vtrentos <Vtrentos@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/11/11 11:19:14 by Vtrentos #+# #+# */
/* Updated: 2021/01/11 17:28:19 by Vtrentos ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static void free_array(char **arr, int words)
{
if (words <= 0)
free(arr);
else
{
while (words > 0)
{
free(arr[words]);
words--;
}
}
free(arr);
}
static int count_words_letters(char *str3, char c)
{
int i;
int j;
size_t f;
size_t len;
j = 0;
f = 0;
len = ft_strlen(str3);
while (f < len)
{
i = 0;
while (str3[f] != c && str3[f] != '\0')
{
i++;
f++;
}
if (i != 0)
j++;
if (f >= len)
break ;
f++;
}
return (j);
}
static char *string_ptr(char *str2, char c)
{
int i;
char *str;
char *string;
i = 0;
str = str2;
while (*str != c && *str != '\0')
{
i++;
str++;
}
string = (char *)malloc(i + 1);
if (!string)
return (NULL);
ft_strlcpy(string, str - i, i + 1);
return (string);
}
static char **malloc_array(int words, char c, char *str2)
{
char **ar;
int l;
size_t len;
l = 0;
ar = (char **)malloc(sizeof(char *) * (words + 1));
if (!ar)
return (NULL);
while (l < words)
{
while (*str2 == c && *str2)
str2++;
ar[l] = string_ptr(str2, c);
if (!ar[l])
{
free_array(ar, l - 1);
return (NULL);
}
len = ft_strlen(ar[l]);
str2 = str2 + len;
l++;
}
ar[words] = NULL;
return (ar);
}
char **ft_split(char const *s, char c)
{
int words;
char **arr;
if (!s)
return (NULL);
words = count_words_letters((char *)s, c);
arr = malloc_array(words, c, (char *)s);
if (!arr)
return (NULL);
return (arr);
}