-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokenize.c
84 lines (82 loc) · 2.22 KB
/
tokenize.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
#include "header.h"
#include "tokenize.h"
#include "helper.h"
char **getList(char *input)
{
// input contains the string of input
// assuming a max of command_capacity == 512 number of commands
char **list = (char **)malloc(100 * sizeof(char *));
num_command = 0; // initialising the number of commands to 0
char *temp = strtok(input, ";");
while (temp != NULL)
{
list[num_command] = temp;
if (temp[0] != '\n')
num_command++;
temp = strtok(NULL, ";");
}
// we'll get a space filled list of commands
// printf("%d\n", num_command); check
// replace tabs with spaces
replaceTabs(list);
// replace extra spaces to single places
// for (int i = 0; i < num_command; i++)
// remove_char_front_and_back(list[i], ' ');
RemoveExtraSpaces(list);
return list;
}
void replaceTabs(char **list)
{
for (int i = 0; i < num_command; i++)
{
for (int j = 0; j < strlen(list[i]); j++)
{
if (list[i][j] == '\t')
list[i][j] = ' ';
}
}
}
void RemoveExtraSpaces(char **list)
{
for (int itr = 0; itr < num_command; itr++)
{
char *string = list[itr];
char cpyCommand[name_len];
// firstly remove the spaces in the front before the start
int j = 0;
while (string[j] == ' ')
j++;
bool prevspace = false;
int i = 0;
while (1)
{
if (string[j] != ' ')
{
cpyCommand[i] = string[j];
j++;
i++;
prevspace = false;
}
else if (string[j] == ' ' && prevspace == false)
{
cpyCommand[i] = string[j];
j++;
i++;
prevspace = true;
}
else
{
j++;
}
if (j == strlen(string))
break;
}
// append the null character , look for last mein space
if (cpyCommand[i - 1] == ' ')
cpyCommand[i - 1] = '\0';
else
cpyCommand[i] = '\0';
// copy the string to original store placeholder
strcpy(list[itr], cpyCommand);
}
}