-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathft_split.c
More file actions
102 lines (91 loc) · 2.48 KB
/
ft_split.c
File metadata and controls
102 lines (91 loc) · 2.48 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_split.c :+: :+: */
/* +:+ */
/* By: ksmorozo <ksmorozo@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/11/09 11:35:51 by ksmorozo #+# #+# */
/* Updated: 2021/07/07 12:18:19 by ksmorozo ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static char **free_all(char **final_array)
{
int count;
count = 0;
while (final_array[count])
{
free(final_array[count]);
count++;
}
free(final_array);
return (NULL);
}
static int get_word_count(char *str, char delimiter)
{
int count;
count = 1;
if (ft_strlen(str) == 0)
return (0);
while (*str != '\0')
{
if (*str == delimiter)
{
count++;
while (*str == delimiter)
str++;
}
str++;
}
return (count);
}
static int number_of_delimeters_to_skip(char *trimed_str, char dlm)
{
int count;
count = 0;
while (*trimed_str == dlm)
{
count++;
trimed_str++;
}
return (count);
}
static char **split(char *trimed_str, char dlm, int word_count)
{
int count;
char **final_array;
char *start;
count = 0;
final_array = (char **)malloc((word_count + 1) * sizeof(char *));
if (final_array == NULL)
return (NULL);
while (count < word_count)
{
start = trimed_str;
while (*trimed_str != dlm && *trimed_str)
trimed_str++;
final_array[count] = (char *)malloc(trimed_str - start + 1);
if (!(final_array[count]))
free_all(final_array);
ft_strlcpy(final_array[count], start, trimed_str - start + 1);
count++;
trimed_str = trimed_str + number_of_delimeters_to_skip(trimed_str, dlm);
}
final_array[count] = 0;
return (final_array);
}
char **ft_split(char const *str, char dlm)
{
int word_count;
char *trimed_str;
char **result;
trimed_str = ft_strtrim(str, &dlm);
if (trimed_str == NULL)
return (NULL);
word_count = get_word_count((char *)trimed_str, dlm);
result = split(trimed_str, dlm, word_count);
free(trimed_str);
return (result);
}