-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
99 lines (89 loc) · 2.14 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yaalaoui <yaalaoui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/24 21:49:18 by yaalaoui #+# #+# */
/* Updated: 2020/11/21 20:23:22 by yaalaoui ### ########.fr */
/* */
/* ************************************************************************** */
#include "lib.h"
void fix_res(t_mapdata *map)
{
int width;
int height;
mlx_get_screen_size(MLX, &width, &height);
HT = (HT > height) ? height : HT;
(HT < 100) ? ft_error("height too small") : 0;
WH = (WH > width) ? width : WH;
(WH < 100) ? ft_error("width too small") : 0;
}
int count_words(const char *str, char c)
{
int i;
int count;
int words;
i = 0;
count = 0;
words = 0;
if (!str)
return (0);
while (str[i] != '\0')
{
if (str[i] == c)
words = 0;
else if (words == 0)
{
words = 1;
count++;
}
i++;
}
return (count);
}
static int ft_strlem(const char *s, char c, int d)
{
int i;
i = 0;
while (s[d] != c && s[d] != '\0')
{
i++;
d++;
}
return (i);
}
static char **ft_frees(char **w, int j)
{
while (--j)
free(w[j]);
free(w);
return (0);
}
char **ft_split(char const *s, char c)
{
char **w;
int i;
int k;
int j;
i = 0;
j = 0;
if (!s)
return (0);
if (!(w = (char**)malloc(sizeof(char*) * (count_words(s, c) + 1))))
return (0);
while (s[i] != '\0' && j < count_words(s, c))
{
k = 0;
while (s[i] == c)
i++;
if (!(w[j] = (char*)malloc(sizeof(char) * ft_strlem(s, c, i) + 1)))
return (ft_frees(w, j));
while (s[i] != '\0' && s[i] != c)
w[j][k++] = s[i++];
w[j++][k] = '\0';
}
w[j] = 0;
return (w);
}