-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_array.c
More file actions
43 lines (38 loc) · 1.1 KB
/
token_array.c
File metadata and controls
43 lines (38 loc) · 1.1 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
#include "shell.h"
/**
* _token_array - produces tokens from a string
* @strn: the input string
* @delim_type: delimiter type used to produce tokens
* Return: An array of tokens
*/
char **_token_array(char *strn, char *delim_type)
{
char **token_array = NULL;
int len_of_tok = 0, tok_index = 0, str_index = 0, inp_str_index = 0;
int count = 0;
count = total_delim(strn, delim_type[0]);
token_array = malloc(sizeof(char *) * (count + 1));
if (token_array == NULL)
return (NULL);
inp_str_index = 0;
while (strn[inp_str_index] != '\0')
{
while (strn[inp_str_index] == delim_type[0])
inp_str_index++;
len_of_tok = tok_strnlen(strn, inp_str_index, delim_type[0]);
token_array[tok_index] = malloc(sizeof(char) * (len_of_tok + 1));
if (token_array[tok_index] == NULL)
{
for (str_index = 0; str_index < tok_index; str_index++)
free(token_array[str_index]);
free(token_array);
return (NULL);
}
for (str_index = 0; str_index < len_of_tok; str_index++)
token_array[tok_index][str_index] = strn[inp_str_index + str_index];
token_array[tok_index][len_of_tok] = '\0';
tok_index++;
inp_str_index += len_of_tok;
}
return (token_array);
}