-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
86 lines (74 loc) · 2.17 KB
/
Copy pathft_split.c
File metadata and controls
86 lines (74 loc) · 2.17 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cpoulain <cpoulain@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 13:18:56 by cpoulain #+# #+# */
/* Updated: 2024/10/17 14:38:30 by cpoulain ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* Static declarations */
static size_t count_strings(const char *s, char c);
static void free_strs(char ***strs, size_t len);
static const char *cpy_until_c(char **strs, const char *s, char c);
/* ft_split */
char **ft_split(const char *s, char c)
{
char **strs;
size_t string_count;
size_t current_idx;
string_count = count_strings(s, c);
strs = malloc((sizeof(char *) * (string_count + 1)));
if (strs)
{
strs[string_count] = NULL;
current_idx = 0;
while (current_idx < string_count)
{
s = cpy_until_c(strs + current_idx++, s, c);
if (!s)
return (free_strs(&strs, current_idx), NULL);
}
}
return (strs);
}
/* Static definitions */
static const char *cpy_until_c(char **dst, const char *s, char c)
{
size_t len;
while (*s && *s == c)
s++;
len = 0;
while (s[len] && s[len] != c)
len++;
*dst = malloc(sizeof(char) * (len + 1));
if (*dst == NULL)
return (NULL);
return (ft_strlcpy(*dst, s, len + 1), s += len + 1, s);
}
static size_t count_strings(const char *s, char c)
{
size_t string_count;
string_count = 0;
while (*s)
{
if (*s == c)
s++;
else
{
while (*s && *s != c)
s++;
string_count++;
}
}
return (string_count);
}
static void free_strs(char ***strs, size_t len)
{
while (len)
free((*strs)[(len-- - 1)]);
free(*strs);
}