-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
224 lines (203 loc) · 4.33 KB
/
ft_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maceccar <maceccar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 15:43:34 by maceccar #+# #+# */
/* Updated: 2023/11/10 15:43:35 by maceccar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// static size_t count_words(const char *s, char sep)
// {
// size_t count;
// int i;
// count = 0;
// i = 0;
// while (s[i])
// {
// if (s[i] != sep)
// {
// while (s[i] != sep && s[i])
// i++;
// count++;
// }
// else
// i++;
// }
// return (count);
// }
// static char *set_row(char *res, const char *s, int start, int end)
// {
// int j;
// j = 0;
// res = ft_calloc(sizeof(char) * (end - start + 1 + 1), sizeof(char));
// if (!res)
// return (NULL);
// while (start <= end)
// {
// res[j] = s[start];
// start++;
// j++;
// }
// return (res);
// }
// static void free_matrix(char **result, int count)
// {
// int i;
// i = 0;
// while (i <= count)
// {
// free(result[i]);
// i++;
// }
// free(result);
// return ;
// }
// static int split(char **result, const char *s, char sep, int i)
// {
// int current_row;
// int start;
// int end;
// current_row = 0;
// start = 0;
// end = 0;
// while (s[i])
// {
// if (s[i] != sep)
// {
// start = i;
// while (s[i] != sep && s[i])
// i++;
// end = i - 1;
// result[current_row] = set_row(result[current_row], s, start, end);
// if (!result[current_row])
// return (-1);
// current_row++;
// }
// else
// i++;
// }
// return (0);
// }
// char **ft_split(char const *s, char c)
// {
// size_t word_count;
// char **result;
// int i;
// i = 0;
// word_count = count_words(s, c);
// result = ft_calloc(sizeof(char) * (word_count + 1), sizeof(char *));
// if (!result)
// return (NULL);
// if (word_count == 0)
// return (result);
// if (split(result, s, c, i) == -1)
// {
// free_matrix(result, word_count + 1);
// return (NULL);
// }
// return (result);
// }
static size_t ft_count_arr(char const *s, char c)
{
size_t count;
size_t i;
size_t in_arr;
i = 0;
count = 0;
in_arr = 0;
while (s[i] != '\0')
{
if (s[i] != c && in_arr == 0)
{
count++;
in_arr = 1;
}
else if (s[i] == c)
in_arr = 0;
i++;
}
return (count);
}
static char *ft_make_string(char const *s, size_t start, char c)
{
char *division;
size_t i;
size_t j;
size_t str_len;
i = 0;
j = start;
str_len = 0;
if (s[i] == '\0' || s == 0)
return (NULL);
while (s[j] != c && s[j])
{
j++;
str_len++;
}
division = (char *)ft_calloc(str_len + 1, sizeof(char));
if (division == NULL)
return (NULL);
while (i < str_len)
division[i++] = s[start++];
division[i] = '\0';
return (division);
}
static void ft_clear(char **array, size_t arr_index)
{
while (arr_index-- > 0)
free(array[arr_index]);
free(array);
}
static char **ft_make_split(char **array, char const *s, char c)
{
int i;
int in_arr;
int arr_index;
i = -1;
in_arr = 0;
arr_index = 0;
while (s[++i] != '\0')
{
if (s[i] != c && in_arr == 0)
{
array[arr_index++] = ft_make_string(s, i, c);
if (array[arr_index - 1] == NULL)
{
ft_clear(array, arr_index);
return (NULL);
}
in_arr = 1;
}
else if (s[i] == c)
in_arr = 0;
}
array[arr_index] = 0;
return (array);
}
char **ft_split(char const *s, char c)
{
char **array;
array = (char **)ft_calloc(ft_count_arr(s, c) + 1, sizeof(char *));
if (array == NULL)
return (NULL);
array = ft_make_split(array, s, c);
if (array == NULL)
return (NULL);
else
return (array);
}
// int main()
// {
// char**res = ft_split("^^^1^^2a,^^^^3^^^^--h^^^^", '^');
// int i = 0;
// while(res[i])
// {
// ft_putstr_fd(res[i], 1);
// ft_putchar_fd('\n', 1);
// i++;
// }
// }