-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_whitespacesquote.c
119 lines (110 loc) · 2.59 KB
/
ft_whitespacesquote.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_whitespacesquote.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbopp <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/07 11:15:36 by lbopp #+# #+# */
/* Updated: 2017/02/07 12:31:50 by lbopp ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_word(char *s, int i, int word)
{
int tmp;
char quote;
while (s[i] != '\0')
{
if (!ft_isspace(s[i]) && s[i] != '\0')
word++;
if (s[i++] == 34 || s[i++] == 39)
{
quote = s[i - 1];
tmp = i;
while (s[i] != quote && s[i])
i++;
if (s[i] == '\0')
{
word--;
i = tmp;
}
}
else
while (!ft_isspace(s[i]) && s[i] != '\0' && s[i])
i++;
s[i] != '\0' ? i++ : 0;
}
return (word);
}
void ft_fill(char const *s, char ***array)
{
int i;
int letter;
int word;
char quote;
i = 0;
word = 0;
while (s[i])
{
letter = 0;
if (s[i] == 34 || s[i] == 39)
{
quote = s[i++];
while (s[i] && s[i] != quote)
(*array)[word][letter++] = s[i++];
if (s[i] == quote)
i++;
}
else
while (!ft_isspace(s[i]) && s[i])
(*array)[word][letter++] = s[i++];
word++;
while (ft_isspace(s[i]) && s[i])
i++;
}
}
void ft_letter(char const *s, char ***array, int i, int word)
{
int letter;
char quote;
while (s[i])
{
letter = 0;
if (s[i] == 34 || s[i] == 39)
{
quote = s[i++];
while (s[i++] != quote && s[i])
letter++;
(s[i] == quote) ? i++ : 0;
}
else
while (!ft_isspace(s[i]) && s[i])
{
letter++;
i++;
}
while (ft_isspace(s[i]) && s[i])
i++;
if (!((*array)[word++] =
(char*)ft_memalloc(sizeof(char) * (letter + 1))))
return ;
}
}
char **ft_whitespacesquote(char const *s)
{
int word;
char **array;
char *string;
if (!(s))
return (NULL);
if (!(string = ft_strtrim(s)))
return (0);
word = ft_word(string, 0, 0);
if (!(array = (char**)ft_memalloc(sizeof(char*) * (word + 1))))
return (0);
ft_letter(string, &array, 0, 0);
ft_fill(string, &array);
free(string);
return (array);
}