-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_processing.c
More file actions
121 lines (104 loc) · 1.87 KB
/
input_processing.c
File metadata and controls
121 lines (104 loc) · 1.87 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
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
#include "main.h"
/**
*escapeChars - change \n with \0
*@data: shell data
*/
void escapeChars(shell_data *data)
{
int len, i = 0;
char *str = data->input;
len = strlen(str);
for (; i < len; i++)
{
if (data->input[i] == '\n')
data->input[i] = '\0';
}
}
/**
*removeExtraSpace - removes extra space
*@data: shell data
*/
void removeExtraSpace(shell_data *data)
{
int i, j;
int spaceFound = 0;
int leadingSpaces = 0;
for (i = 0; data->input[i] != '\0'; i++)
{
if (data->input[i] != ' ')
break;
leadingSpaces++;
}
for (i = leadingSpaces, j = 0; data->input[i] != '\0'; i++)
{
if (data->input[i] == ' ')
{
if (!spaceFound)
{
data->input[j++] = ' ';
spaceFound = 1;
}
}
else
{
data->input[j++] = data->input[i];
spaceFound = 0;
}
}
while (j > leadingSpaces && data->input[j - 1] == ' ')
j--;
data->input[j] = '\0';
}
/**
*fixDataInput - handles input
*@data: shell data
*/
void fixDataInput(shell_data *data)
{
int i = 1, j = 1;
const int MAX_ARGS = 25;
char *token;
data->input = _strtok(data->input, " ");
data->args = (char **)malloc(sizeof(char *) * MAX_ARGS);
for (; j < MAX_ARGS; j++)
{
data->args[j] = NULL;
}
token = _strtok(NULL, " ");
while (token != NULL)
{
data->args[i] = token;
i++;
token = _strtok(NULL, " ");
}
}
/**
*isOnlyNull - checks if input empty string
*@data: relevent data
*Return:must return 0 if not NULL
*/
int isOnlyNull(shell_data *data)
{
if (_strcmp(data->input, "") == 0)
return (1);
return (0);
}
/**
*countTokens - counts tokens in string
*@str: string to be examened
*@delimiter: how to count
*Return: count;
*/
int countTokens(char *str, char *delimiter)
{
int count = 0;
char *strCopy = _strdup(str);
char *token = _strtok(strCopy, delimiter);
while (token != NULL)
{
count++;
token = _strtok(NULL, delimiter);
}
free(strCopy);
return (count);
}