-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.c
425 lines (409 loc) · 12.3 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "Parser.h"
#include "Game.h"
#include "FilesAux.h"
#include "MainAux.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int str_equals(char *str1, char *str2) {
return strcmp(str1, str2);
}
bool is_digit(char c){
return c>='0'&&c<='9';
}
bool is_dot(char c){
return c==46;
}
bool is_valid_path(char* path){
int i;
for(i=0;i<(int) strlen(path);i++){
if(path[i]=='|'||path[i]=='<'||path[i]=='>'||path[i]=='"'||path[i]=='?'||path[i]==':'||path[i]=='\\'){
return false;
}
}
return true;
}
int my_pow(int a, int b){
int i;
int ret = 1;
for (i=0;i<b;i++){
ret = ret*a;
}
return ret;
}
/*gets a string, and returns the number the string represents.
* if the string does not represent a number, returns -1*/
int is_integer(char* s){
int i;
int coordinate = 0;
for (i = 0; i<(int) strlen(s) ; i++){
if (is_digit(s[i]) == false)
return -1;
else{
coordinate+=(s[i]-'0')*my_pow(10, strlen(s) - i -1);
}
}
return coordinate;
}
bool is_double(char* s){
int i;
int dot_counter = 0;
int digit_counter = 0;
for (i = 0; i < (int)strlen(s); i++){
if (is_digit(s[i]) == false && !is_dot(s[i]))
return false;
if(is_dot(s[i]))
dot_counter++;
else
digit_counter++;
}
if(dot_counter==0)
return true;
if (dot_counter==1&&digit_counter>=1)
return true;
return false;
}
bool is_valid_param(int command_code, char command_data[4][258]){
if(command_code==5){
if(strlen(command_data[3])>0){
printf("Error: To many parameters given\n");
return false;
}
}
if(command_code==8||command_code==12||command_code==13){
if(strlen(command_data[2])>0){
printf("Error: To many parameters given\n");
return false;
}
}
if (command_code==1||command_code==2||command_code==3||command_code==7||command_code==11){
if(strlen(command_data[1])>0){
printf("Error: To many parameters given\n");
return false;
}
}
if (command_code==4||command_code==6||command_code==9||command_code==10||command_code==14||command_code==15||command_code==16||command_code==17){
if(strlen(command_data[0])>0){
printf("Error: To many parameters given\n");
return false;
}
}
return true;
}
int command_to_code(char* input) {
if (str_equals(input, "solve") == 0)
return 1;
else
if (str_equals(input, "edit") == 0)
return 2;
else
if (str_equals(input, "mark_errors") == 0)
return 3;
else
if (str_equals(input, "print_board") == 0)
return 4;
else
if (str_equals(input, "set") == 0)
return 5;
else
if (str_equals(input, "validate") == 0)
return 6;
else
if (str_equals(input, "guess") == 0)
return 7;
else
if (str_equals(input, "generate") == 0)
return 8;
else
if (str_equals(input, "undo") == 0)
return 9;
else
if (str_equals(input, "redo") == 0)
return 10;
else
if (str_equals(input, "save") == 0)
return 11;
else
if (str_equals(input, "hint") == 0)
return 12;
else
if (str_equals(input, "guess_hint") == 0)
return 13;
else
if (str_equals(input, "num_solutions") == 0)
return 14;
else
if (str_equals(input, "autofill") == 0)
return 15;
else
if (str_equals(input, "reset") == 0)
return 16;
else
if (str_equals(input, "exit") == 0)
return 17;
return 0;
}
void interpret_command(){
char c;
int input_len = 257;
char input[257];
char *token;
char* ptr;
int command_code = 0;
int j = 0;
char command_data[4][258];
for (; j < 4; ++j) {
command_data[j][0]='\0';
}
j=0;
if(fgets(input, input_len, stdin)!=NULL){
size_t len = strlen(input);
/*flush*/
if((int)len==(input_len-1)){
printf("Error: command is to long\n");
while((c = getchar()) != '\n' && c != EOF);
if (c == EOF){
my_exit();
}
return;
}
/* fix input*/
if (len > 0 && input[len - 1] == '\n') input[--len] = '\0';
token = strtok_r(input, " \t\r", &ptr);
if(token!=NULL){
command_code = command_to_code(token);
} else{
return;
}
/*this way the parser ignores empty commands*/
token = strtok_r(NULL, " \t\r", &ptr);
while (token!=NULL&& j<4){
strcpy(command_data[j], token);
token = strtok_r(NULL, " \t\r", &ptr);
j++;
}
j=0;
}
else{
my_exit();
}
execute_command(command_code, command_data);
}
void execute_command(int command_code, char command_data[4][258]) {
int x=0,y=0,z=0;
double d;
char* stop_string;
if(!is_valid_param(command_code, command_data))
return;
switch(command_code)
{
case 1:
if(command_data[0][0]=='\0'){
printf("Error: No path given\n");
break;
}
if(!is_valid_path(command_data[0])){
printf("Error: The parameter isn't legal (invalid path)\n");
break;
}
solve(command_data[0]);
break;
case 2:
edit(command_data[0]);
break;
case 3:
if (state != Solve){
printf("Error: Mark errors only available in solve mode\n");
break;
}
if(command_data[0][0]=='\0'){
printf("Error: No parameter given\n");
break;
}
x = is_integer(command_data[0]);
if(x == -1){
printf("Error: The parameter isn't legal\n");
break;
}
mark_errors(x);
break;
case 4:
if(state != Solve && state != Edit) {
printf("Error: Print board only available in solve or edit mode\n");
break;
}
print_board();
break;
case 5:
if (state != Solve && state != Edit) {
printf("Error: Set only available in solve or edit mode\n");
break;
}
if(command_data[0][0]=='\0'||command_data[1][0]=='\0'||command_data[2][0]=='\0'){
printf("Error: Missing coordinates\n");
break;
}
/* translate strings of numbers to integers */
x = is_integer(command_data[0]);
y = is_integer(command_data[1]);
z = is_integer(command_data[2]);
if(x == -1 || y == -1 || z == -1){
if (x == -1){
printf("Error: The first parameter isn't legal (not a positive number)\n");
break;
} else if (y == -1){
printf("Error: The second parameter isn't legal (not a positive number)\n");
break;
} else {
printf("Error: The third parameter isn't legal (not a positive number)\n");
break;
}
}
board_set(x,y,z);
break;
case 6:
if(state!=Solve && state != Edit){
printf("Error: validate only available in solve or edit mode\n");
break;
}
validate();
break;
case 7:
if(state != Solve){
printf("Error: Guess command is only available in Solve mode\n");
break;
}
if(command_data[0][0]=='\0'){
printf("Error: No parameter given\n");
break;
}
if (!is_double(command_data[0])){
printf("Error: The parameter isn't legal (not a positive double)\n");
break;
}
d = strtod(command_data[0], &stop_string);
guess(d);
break;
case 8:
if (state != Edit){
printf("Error: Generate command is available only in Edit mode!\n");
break;
}
if(command_data[0][0]=='\0'||command_data[1][0]=='\0'){
printf("Error: No parameters given\n");
break;
}
x = is_integer(command_data[0]);
y = is_integer(command_data[1]);
if(x == -1 || y == -1){
if (x == -1){
printf("Error: The first parameter isn't legal (not a positive number)\n");
break;
} else {
printf("Error: The second parameter isn't legal (not a positive number)\n");
break;
}
}
generate(x,y);
break;
case 9:
if(state != Solve && state != Edit){
printf("Error: Undo only available in solve or edit mode\n");
break;
}
undo();
break;
case 10:
if (state != Solve && state != Edit) {
printf("Error: Redo only available in solve or edit mode\n");
break;
}
redo();
break;
case 11:
if (state != Solve && state != Edit){
printf("Error: Save command is only available in Solve or Edit mode\n");
break;
}
if(command_data[0][0]=='\0'){
printf("Error: No path given\n");
break;
}
if(!is_valid_path(command_data[0])){
printf("Error: The parameter isn't legal (invalid path)\n");
break;
}
save(command_data[0]);
break;
case 12:
if(state != Solve) {
printf("Error: Hint command is only available in Solve mode\n");
break;
}
if(command_data[0][0]=='\0'||command_data[1][0]=='\0'){
printf("Error: No coordinates given\n");
break;
}
x = is_integer(command_data[0]);
y = is_integer(command_data[1]);
if(x == -1 || y == -1){
if (x == -1){
printf("Error: The first parameter isn't legal (not a positive number)\n");
break;
} else {
printf("Error: The second parameter isn't legal (not a positive number)\n");
break;
}
}
hint(x,y);
break;
case 13:
if(state != Solve) {
printf("Error: Guess_hint command is only available in Solve mode\n");
break;
}
if(command_data[0][0]=='\0'||command_data[1][0]=='\0'){
printf("Error: No coordinates given\n");
break;
}
x = is_integer(command_data[0]);
y = is_integer(command_data[1]);
if (x == -1 || y == -1){
if (x == -1){
printf("Error: The first parameter isn't legal (not a positive number)\n");
break;
} else {
printf("Error: The second parameter isn't legal (not a positive number)\n");
break;
}
}
guess_hint(x,y);
break;
case 14:
if (state != Solve && state != Edit){
printf("Error: Num_solutions command is only available in Solve or Edit mode\n");
break;
}
num_solutions();
break;
case 15:
if (state != Solve){
printf("Error: Autofill command is only available in Solve mode\n");
break;
}
autofill();
break;
case 16:
if (state != Solve && state != Edit) {
printf("Error: Reset only available in solve or edit mode\n");
break;
}
reset();
break;
case 17:
my_exit();
break;
default:
printf("Error: invalid command\n");
}
}