-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.c
More file actions
97 lines (88 loc) · 2.13 KB
/
split.c
File metadata and controls
97 lines (88 loc) · 2.13 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sakllam <sakllam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/30 13:46:33 by sakllam #+# #+# */
/* Updated: 2021/12/06 13:04:29 by sakllam ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static size_t ft_count(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i] != '\0')
{
while (s[i] && s[i] == c)
i++;
if (s[i] != '\0')
{
if ((s[i - 1] == c || i == 0))
count++;
i++;
}
}
return (count);
}
static char **free_tab(char **tab)
{
int i;
i = 0;
while (tab[i])
free(tab[i++]);
free(tab);
return (NULL);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
int i;
char *p;
i = 0;
if (!s)
return (NULL);
if (ft_strlen(s + start) < len)
p = (char *) malloc((ft_strlen(s + start) + 1) * sizeof(char));
else
p = (char *) malloc(((int) len + 1) * sizeof(char));
if (!p)
return (NULL);
while (i < (int) len && s[i + start] && !(start >= ft_strlen(s)))
{
p[i] = s[i + start];
i++;
}
p[i] = '\0';
return (p);
}
char **ft_split(char const *s, char c)
{
char **tab;
size_t i;
size_t len;
i = 0;
if (!s)
return (0);
tab = (char **)malloc((ft_count(s, c) + 1) * sizeof(*tab));
if (!tab)
return (tab);
while (*s)
{
while (*s && *s == c)
s++;
len = 0;
while (s[len] && s[len] != c)
len++;
if (len != 0)
tab[i++] = ft_substr(s, 0, len);
if (len != 0 && !tab[i - 1])
return (free_tab(tab));
s += len;
}
tab[i] = NULL;
return (tab);
}