-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.c
More file actions
237 lines (197 loc) · 4.31 KB
/
parse.c
File metadata and controls
237 lines (197 loc) · 4.31 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <stdbool.h>
#include "parse.h"
// Helper functions
// Checks if pre is prefix of str
bool prefix(const char *pre, const char *str)
{
return strncmp(pre, str, strlen(pre)) == 0;
}
// Check is a char is space (and space only)
inline int is_sp(char a) { return a == ' '; }
// Parsing functions
// Parses number into an int
// Returns -1 in case of error, number otherwise
int parse_number(char *input)
{
int i = 0;
if (input[0] == '\n')
return -1;
// Accept '0', but not '000', etc.
if (input[0] == '0') {
i++;
while (input[i] == ' ')
i++;
return input[i] == '\n' ? 0 : -1;
}
int parse_res = 0;
int len = strlen(input);
for (i = 0; i < len; ++i) {
if (!isdigit(input[i]))
break;
parse_res *= 10;
parse_res += input[i] - '0';
}
if (i > MAX_ARG_LEN)
return -1;
// Check rest of the string
while (input[i] == ' ')
i++;
if (input[i] != '\n')
return -1;
return parse_res;
}
// Parses single number into result structure args parameter,
// at a given index
int parse_single_number(char *input, int index, ParseResult *result)
{
// Accept '0', but not '000', etc.
int i = 0;
if (input[0] == '0') {
if (isspace(input[1])) {
result->args[index] = 0;
return 1;
}
return -1;
}
if (!isdigit(input[i]))
return -1;
int parse_res = 0;
int len = strlen(input);
for (; i < len; ++i) {
if (!isdigit(input[i]))
break;
parse_res *= 10;
parse_res += input[i] - '0';
}
if (i > MAX_ARG_LEN)
return -1;
result->args[index] = parse_res;
return i;
}
int parse_number_args(char *input, ParseResult *result)
{
int i = 0;
if (input[0] == '\n')
return -1;
// Try to parse all 3 args
for (int j = 0; j < 3; j++) {
input += i;
i = parse_single_number(input, j, result);
if (i == -1)
return -1;
// Check rest of the string
while (input[i] == ' ')
i++;
}
if (input[i] != '\n')
return -1;
return 1;
}
// Returns 0 if the input was ill-formed,
// end position of the insert/find word otherwise
int parse_word(char *input)
{
if (input[0] == '\n') // Empty string
return 0;
// Check if word contains space
int p = 0;
while (islower(input[p])) {
p++;
}
int rest = p;
while (is_sp(input[rest]))
rest++;
if (input[rest] != '\n')
return 0;
return p;
}
// Parses command together with its arguments, returns ParseResult enum
ParseResult parse_command(FILE *stream)
{
ParseResult command = {QUIT, NULL, {-1, -1, -1}};
char *input = NULL;
char line[BUF_LEN];
if (stream == NULL)
return command;
char *readline = fgets(line, BUF_LEN, stream);
if (readline == NULL)
return command;
command.result = IGNORE;
// Skip spaces on the beginning
input = &line[0];
while (input[0] == ' ')
input++;
if (!isalpha(input[0]))
return command; // No command supplied
switch (input[0]) {
case 'i': // insert
if (prefix("insert", input)) {
input += (strlen("insert"));
if (!is_sp(input[0]))
return command;
while (input[0] == ' ')
input++;
int end_pos = parse_word(input);
if (!end_pos)
return command;
command.result = INSERT;
command.arg =
(char *)malloc(end_pos + 1 * sizeof(char));
strncpy(command.arg, input, end_pos);
command.arg[end_pos] = 0;
}
break;
case 'f': // find
if (prefix("find", input)) {
input += (strlen("find"));
if (!is_sp(input[0]))
return command;
while (input[0] == ' ')
input++;
int end_pos = parse_word(input);
if (!end_pos)
return command;
command.result = FIND;
command.arg =
(char *)malloc(end_pos + 1 * sizeof(char));
strncpy(command.arg, input, end_pos);
command.arg[end_pos] = 0;
}
break;
case 'c': // clear
if (prefix("clear", input)) {
input += (strlen("clear"));
while (is_sp(input[0]))
input++;
if (input[0] == '\n')
command.result = CLEAR;
}
break;
case 'd': // delete
if (prefix("delete", input)) {
input += (strlen("delete"));
if (!is_sp(input[0]))
return command; // Check for space delimiter
while (is_sp(input[0]))
input++;
int arg = parse_number(input);
command.result = DELETE;
command.args[0] = arg;
}
break;
case 'p': // prev
if (prefix("prev", input)) {
input += (strlen("prev"));
while (is_sp(input[0]))
input++;
int res = parse_number_args(input, &command);
if (res != -1) {
command.result = PREV;
}
}
break;
default:
break;
}
return command;
}