Repository navigation
Expand file tree
/
Copy pathft_split.c
More file actions
104 lines (95 loc) · 2.23 KB
/
Copy pathft_split.c
File metadata and controls
104 lines (95 loc) · 2.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmasuda <mmasuda@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/11 22:30:20 by mmasuda #+# #+# */
/* Updated: 2021/04/15 00:01:37 by mmasuda ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *free_memory(char **newstr, int word_count)
{
int i;
i = 0;
while (i < word_count)
{
free(newstr[i]);
i++;
}
free(newstr);
return (NULL);
}
static char *ft_strndup(const char *s1, size_t n, size_t i)
{
char *s2;
size_t j;
j = 0;
if (!s1)
return (NULL);
s2 = (char *)malloc(sizeof(char) * (n + 1));
if (!s2)
return (NULL);
while (j < n)
{
s2[j] = s1[i + j];
j++;
}
s2[j] = '\0';
return (s2);
}
static char **split_words(char **newstr, char const *s, char c, int qty)
{
size_t i;
size_t word_count;
size_t split;
word_count = 0;
i = 0;
while (ft_strlen(s) > i)
{
split = 0;
if (s[i] != c)
{
while (s[i + split] && s[i + split] != c)
split++;
newstr[word_count] = ft_strndup(s, split, i);
if (!newstr[word_count])
return (free_memory(newstr, word_count));
word_count++;
}
i += split + 1;
}
if (word_count == 0)
newstr[word_count] = ft_strndup(NULL, 0, 0);
newstr[qty] = NULL;
return (newstr);
}
char **ft_split(char const *s, char c)
{
char **newstr;
int qty;
int i;
qty = 0;
i = 0;
if (!s)
return (NULL);
while (s[i])
{
if (s[i] != c)
{
while (s[i] != c && s[i])
i++;
qty++;
}
else
i++;
}
if (qty == 0)
qty = 1;
newstr = (char **)malloc(sizeof(char *) * (qty + 1));
if (!newstr)
return (NULL);
return (split_words(newstr, s, c, qty));
}