-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspilt.c
More file actions
107 lines (97 loc) · 2.37 KB
/
spilt.c
File metadata and controls
107 lines (97 loc) · 2.37 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
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* spilt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: salee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/26 07:13:16 by salee #+# #+# */
/* Updated: 2022/02/26 08:50:58 by salee ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static int garbage_collect(char **result, int j)
{
while (j >= 0)
{
free(result[j]);
j--;
}
free(result);
return (0);
}
static int str_numbers_calculate(char *str, char c)
{
int i;
int result;
int str_flag;
i = 0;
result = 0;
str_flag = 1;
while (str[i])
{
if (str[i] == c)
{
str_flag = 1;
}
if (str[i] != c && str_flag == 1)
{
result++;
str_flag = 0;
}
i++;
}
return (result);
}
static void my_strncpy(char *dest, char *src, int src_len, int src_index)
{
int i;
i = 0;
while (src_len > 0)
{
dest[i] = src[src_index - src_len];
src_len--;
i++;
}
dest[i] = '\0';
}
static int str_make(char **result, char c, char *str, int str_numbers)
{
int i;
int j;
int str_len;
i = 0;
j = 0;
while (str[i] && j < str_numbers)
{
str_len = 0;
while (str[i] == c && str[i])
i++;
while (str[i] != c && str[i])
{
str_len++;
i++;
}
result[j] = (char *)malloc(str_len + 1);
if (result[j] == NULL)
return (garbage_collect(result, j - 1));
my_strncpy(result[j], str, str_len, i);
j++;
}
result[j] = NULL;
return (1);
}
char **ft_split(char const *str, char c)
{
int str_numbers;
int malloc_exception_check;
char **result;
str_numbers = str_numbers_calculate((char *)str, c);
result = (char **)malloc(sizeof(char *) * (str_numbers + 1));
if (result == NULL)
return (NULL);
malloc_exception_check = str_make(result, c, (char *)str, str_numbers);
if (malloc_exception_check == 0)
return (NULL);
return (result);
}