-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.c
47 lines (44 loc) · 903 Bytes
/
token.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
#include "main.h"
/**
* tokenizer - creates tokens from given input
* @line: to be tokenized
* Return: array of strings
*/
char **tokenizer(char *line)
{
char *buf = NULL, *bufp = NULL, *token = NULL, *delim = " :\t\r\n";
char **tokens = NULL;
int tokensize = 1;
size_t index = 0, flag = 0;
buf = _strdup(line);
if (!buf)
return (NULL);
bufp = buf;
while (*bufp)
{
if (_strchr(delim, *bufp) != NULL && flag == 0)
{
tokensize++;
flag = 1;
}
else if (_strchr(delim, *bufp) == NULL && flag == 1)
flag = 0;
bufp++;
}
tokens = malloc(sizeof(char *) * (tokensize + 1));
token = strtok(buf, delim);
while (token)
{
tokens[index] = _strdup(token);
if (tokens[index] == NULL)
{
free(tokens);
return (NULL);
}
token = strtok(NULL, delim);
index++;
}
tokens[index] = '\0';
free(buf);
return (tokens);
}