-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split_bonus.c
145 lines (123 loc) · 2.62 KB
/
ft_split_bonus.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanglee2 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/21 16:31:16 by sanglee2 #+# #+# */
/* Updated: 2023/05/30 18:42:02 by sanglee2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap_bonus.h"
#include <stddef.h>
static size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
static int word_count(char *str, char c)
{
int word_cnt;
int flag;
word_cnt = 0;
flag = 1;
while (*str)
{
if (flag == 1 && *str != '\0' && *str != c)
{
word_cnt++;
flag = 0;
}
else if (*str == '\0' || *str == c)
flag = 1;
str++;
}
return (word_cnt);
}
static char *str_word_print(char *s, char c)
{
char *word;
int loc;
int i;
loc = 0;
i = 0;
while (s[loc] != '\0' && s[loc] != c)
loc++;
word = (char *)malloc(sizeof(char) * (loc + 1));
if (!word)
return (0);
while (s[i] != '\0' && s[i] != c)
{
word[i] = s[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
char **result;
int i;
int len;
len = word_count((char *)s, c);
result = (char **)malloc(sizeof(char *) * (len + 1));
if (!result)
return (0);
i = 0;
while (i < len)
{
while (*s != '\0' && *s == c)
s++;
result[i] = str_word_print((char *)s, c);
if (result[i] == 0)
{
while (--i >= 0)
free(result[i]);
free(result);
return (0);
}
s = s + ft_strlen(result[i++]);
}
result[i] = NULL;
return (result);
}
t_deq *malloc_deq_a(void)
{
t_deq* deq_a;
deq_a = create();
init_deq(deq_a);
return(deq_a);
}
t_deq *malloc_deq_b(void)
{
t_deq* deq_b;
deq_b = create();
init_deq(deq_b);
return(deq_b);
}
t_deq *parse(int ac, char **av, int i)
{
char **temp;
char **arr;
t_node* node;
t_deq* deq_a;
deq_a = malloc_deq_a();
while(i < ac)
{
arr = ft_split(av[i], ' ');
temp = arr;
while(*temp)
{
node = init_node(ft_atoi(*temp));
push_bot_a(deq_a, node);
temp++;
}
ft_free(arr);
i++;
}
return(deq_a);
}