-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.c
168 lines (139 loc) · 3.19 KB
/
tokens.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "tokens.h"
/**
* tokenize - split a string into words (tokens) and dequote
* @str: the string to tokenize
* Return: If malloc fails or if str is 0 or contains no tokens, return NULL.
* Otherwise, return an array containing the tokens in str, terminated by NULL.
*/
char **tokenize(const char *str)
{
char **tokens;
const char *tok;
size_t count;
quote_state_t state;
if (!str)
return (NULL);
tokens = malloc(sizeof(char *) * (count_tokens(str) + 1));
if (!tokens)
return (NULL);
for (count = 0; *(str += quote_state_len(str, QUOTE_NONE)); ++count)
{
tok = str;
while (*str && (state = quote_state(*str)) != QUOTE_NONE)
{
if (state & (QUOTE_DOUBLE | QUOTE_SINGLE | QUOTE_ESCAPE))
str += quote_state_len(str + 1, state) + 1;
else
str += quote_state_len(str, state);
if (*str && (state & (QUOTE_DOUBLE | QUOTE_SINGLE)))
++str;
}
tokens[count] = _memdup(tok, str - tok + 1);
if (!tokens[count])
{
free_tokens(&tokens);
return (NULL);
}
tokens[count][str - tok] = '\0';
}
tokens[count] = NULL;
return (tokens);
}
/**
* count_tokens - compute the length of a string after dequoting
* @str: the string to evaluate
* Return: Return the length of str after dequoting
*/
size_t count_tokens(const char *str)
{
size_t count;
quote_state_t state;
for (count = 0; *(str += quote_state_len(str, QUOTE_NONE)); ++count)
{
while (*str && (state = quote_state(*str)) != QUOTE_NONE)
{
if (state & (QUOTE_DOUBLE | QUOTE_SINGLE | QUOTE_ESCAPE))
str += quote_state_len(str + 1, state) + 1;
else
str += quote_state_len(str, state);
if (*str && (state & (QUOTE_DOUBLE | QUOTE_SINGLE)))
++str;
}
}
return (count);
}
/**
* tokenize_noquote - split a string into words (tokens)
* @str: the string to tokenize
* Return: If malloc fails or if str is 0 or contains no tokens, return NULL.
* Otherwise, return an array containing the tokens in str, terminated by NULL.
*/
char **tokenize_noquote(const char *str)
{
char **tokens;
const char *tok;
size_t count;
if (!str)
return (NULL);
tokens = malloc(sizeof(char *) * (count_tokens_noquote(str) + 1));
if (!tokens)
return (NULL);
for (count = 0; *str; ++count)
{
while (_isspace(*str))
++str;
if (!*str)
break;
tok = str;
do {
++str;
} while (*str && !_isspace(*str));
tokens[count] = _memdup(tok, str - tok + 1);
if (!tokens[count])
{
free_tokens(&tokens);
return (NULL);
}
tokens[count][str - tok] = '\0';
}
tokens[count] = NULL;
return (tokens);
}
/**
* count_tokens_noquote - count the words in a string
* @str: the string to evaluate
* Return: If str is NULL, return -1.
* Otherwise, return the number of words in str.
*/
size_t count_tokens_noquote(const char *str)
{
size_t tok_count;
for (tok_count = 0; *str; ++tok_count)
{
while (_isspace(*str))
++str;
if (!*str)
break;
do {
++str;
} while (*str && !_isspace(*str));
}
return (tok_count);
}
/**
* free_tokens - free & nullify an array of strings
* @tokens: pointer to an array of tokens
*/
void free_tokens(char ***tokens)
{
char **tok;
if (!tokens)
return;
tok = *tokens;
if (!tok)
return;
while (*tok)
free(*tok++);
free(*tokens);
*tokens = NULL;
}