-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
116 lines (107 loc) · 2.73 KB
/
ft_split.c
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
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanmetol <sanmetol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 15:08:30 by sanmetol #+# #+# */
/* Updated: 2024/10/02 16:35:46 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int count_words(char *str, char separator)
{
int count;
bool inside_word;
count = 0;
while (*str)
{
inside_word = false;
while (*str == separator && *str)
++str;
while (*str != separator && *str)
{
if (!inside_word)
{
++count;
inside_word = true;
}
++str;
}
}
return (count);
}
static char *get_next_word(char *str, char separator)
{
static int cursor = 0;
char *next_str;
int len;
int i;
len = 0;
i = 0;
while (str[cursor] == separator)
++cursor;
while ((str[cursor + len] != separator) && str[cursor + len])
++len;
next_str = malloc((size_t)len * sizeof(char) + 1);
if (NULL == next_str)
return (NULL);
while ((str[cursor] != separator) && str[cursor])
next_str[i++] = str[cursor++];
next_str[i] = '\0';
return (next_str);
}
char **ft_split(char *str, char separator)
{
int words_number;
char **splitted;
int i;
i = 0;
words_number = count_words(str, separator);
if (!words_number)
exit(1);
splitted = malloc(sizeof(char *) * (size_t)(words_number + 2));
if (NULL == splitted)
return (NULL);
while (words_number-- >= 0)
{
if (0 == i)
{
splitted[i] = malloc(sizeof(char));
if (NULL == splitted[i])
return (NULL);
splitted[i++][0] = '\0';
continue ;
}
splitted[i++] = get_next_word(str, separator);
}
splitted[i] = NULL;
return (splitted);
}
char *join_args(int argc, char **argv)
{
int i;
int total_length;
char *joined;
int current_pos;
i = 1;
total_length = 0;
current_pos = 0;
while (i < argc)
total_length += ft_strlen(argv[i++]) + 1;
joined = malloc(total_length * sizeof(char));
if (!joined)
return (NULL);
i = 1;
while (i < argc)
{
ft_strcpy(&joined[current_pos], argv[i]);
current_pos += ft_strlen(argv[i]);
if (i < argc - 1)
joined[current_pos++] = ' ';
i++;
}
joined[current_pos] = '\0';
return (joined);
}