-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
73 lines (71 loc) · 1.37 KB
/
parser.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
#include "sshell.h"
/**
* Counter - counts the number of lim found in the input
* @C: the input string;
* @lim: character to find inside the C string
* Return: number of characters found
*/
int Counter(char *C, char *lim)
{
int i = 0, num = 0;
if (lim != NULL)
{
while (C && C[i] != '\0')
{
if (C[i] != lim[0])
{
if (C[i + 1] == lim[0] || C[i + 1] == '\0')
num++;
}
i++;
}
}
return (num);
}
/**
* parsing - create an array of pointers depending of the delimit characters
* @line: input of the user
* Return: an array of pointers of n size
*/
char **parsing(char *line)
{
char *token = NULL, **p = NULL;
int length = 0, j = 0, i = 0, buffsize = 0;
if (line == NULL)
return (NULL);
buffsize = Counter(line, " ");
p = _calloc((buffsize + 1), sizeof(char *));
if (!p)
{
perror("No memory");
return (NULL);
}
/*store the token partition inside **p */
token = _strtok(line, " \t\n");
if (!token)
{
free(p);
return (NULL);
}
while (token)
{
while (token[length] != '\0')
length++;
p[j] = _calloc((length + 1), sizeof(char));
if (p[j] == NULL)
{
gridfree(p, j);
perror("No memory");
return (NULL);
}
/*fill the pointer with the content of token*/
for (i = 0; i < length; i++)
p[j][i] = token[i];
length = 0;
j++;
/*get the next element*/
token = _strtok(NULL, " \t\n");
}
p[j] = NULL;
return (p);
}