-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit_white.c
More file actions
86 lines (77 loc) · 2.2 KB
/
ft_strsplit_white.c
File metadata and controls
86 lines (77 loc) · 2.2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit_white.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yyefimov <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/11 11:45:05 by yyefimov #+# #+# */
/* Updated: 2016/12/12 14:30:28 by yyefimov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static size_t ft_count_words(char const *str)
{
size_t i;
i = 0;
while (*str != '\0')
{
if (*str == ' ' || *str == '\t' || *str == '\n')
while ((*str == ' ' || *str == '\t' || *str == '\n') && *str)
str++;
if (*str == '\0')
break ;
while (*str && (*str != ' ' || *str != '\t' || *str != '\n'))
str++;
i++;
}
return (i);
}
static size_t ft_len(char const *str)
{
size_t len;
len = 0;
while ((*str != ' ' || *str != '\t' || *str != '\n') && *str != '\0')
{
len++;
str++;
}
return (len);
}
static char **ft_makearr(char **res, const char *str)
{
char **tmp;
size_t words;
size_t index;
index = 0;
words = ft_count_words(str);
tmp = res;
while (words--)
{
while ((*str == ' ' || *str == '\t' || *str == '\n') && *str != '\0')
str++;
tmp[index] = ft_strsub(str, 0, ft_len(str));
if (tmp[index] == NULL)
return (NULL);
str = str + ft_len(str);
index++;
}
return (res);
}
char **ft_strsplit_white(char const *str)
{
char **res;
size_t words;
words = ft_count_words(str);
if (!str || *str == '\0')
return (ft_memalloc(sizeof(res)));
if (words == 0)
return (ft_memalloc(sizeof(res)));
res = (char **)malloc(sizeof(res) * words + 1);
if (res == 0)
return (ft_memalloc(sizeof(res)));
res = ft_makearr(res, str);
res[words] = NULL;
return (res);
}