This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.c
416 lines (391 loc) · 15.5 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
#include "parser.h"
#define COMMAND_LEN 257
void printInvalidCommand() {
printf("ERROR: invalid command\n");
}
/* @param: low and high - inclusive */
void printErrorInRange(int low, int high) {
printf("Error: value not in range %d-%d\n", low, high);
}
void printNotZeroOrOne() {
printf("Error: the value should be 0 or 1\n");
}
/* reads input's chars one by one and checks that all are valid digits
* uses "ctype.h" library function isdigit
* returns TRUE (1) or FALSE (0) */
int checkIfNumericString(char *string) {
int i = 0, isNumeric = TRUE;
if (string == NULL) return FALSE;
while (string[i] != '\0') {
if (isdigit(string[i]) == FALSE) { /* isdigit returns 0 = FALSE if the passed char isn't an int */
isNumeric = FALSE;
break;
}
i++;
}
return isNumeric;
}
/* checks if string represents a non-negative integer in the range of from-to (inclusive)
* return value: if string represents a valid int - its int value, otherwise -1 (error indicator) */
int checkIfInRange(char *string, int low, int high) {
int number, isInRange;
if (checkIfNumericString(string)) {
number = atoi(string); /* string is a numeric string - safe to use atoi for conversion */
isInRange = (number >= low && number <= high) ? (number) : (-1);
return isInRange;
}
return -1;
}
int checkIfZeroOrOne(char *string) {
int number, isZeroOrOne;
if (checkIfNumericString(string)) {
number = atoi(string);
isZeroOrOne = (number == 0 || number == 1);
return (isZeroOrOne == TRUE) ? (number) : (-1);
}
return (-1);
}
int commandAvailable(commandType type, enum gameMode mode) {
switch (type) {
case SET:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case HINT:
return (mode == SOLVE_MODE);
case VALIDATE:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case RESET:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case EXIT:
case SOLVE:
case EDIT:
return TRUE;
case MARK_ERRORS:
return (mode == SOLVE_MODE);
case PRINT_BOARD:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case GENERATE:
return (mode == EDIT_MODE);
case UNDO:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case REDO:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case SAVE:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case NUM_SOLS:
return ((mode == SOLVE_MODE) || (mode == EDIT_MODE));
case AUTO_FILL:
return (mode == SOLVE_MODE);
default:
printErrorInCodeFlow("commandAvailable", "parser.c");
return TRUE;
}
}
/* @params: command - pointer to the command to which this function gets parameters
* typeOfCommand - enum representing the command type
* copyOfInput - a copy of the original string that the user entered as a command (including the first token, such as "set")
* N - the N parameter of the sudoku board (sudoku has 1-N digits) */
void getParams(commandStruct *command, commandType typeOfCommand, char *copyOfInput, int N) {
int number;
char *firstToken;
char *secondToken;
char *thirdToken;
strtok(copyOfInput, " \t\r\n"); /* gets rid of first word (already read before, in getCommandFromUser) */
switch (typeOfCommand) {
case SET: /* read 3 space-separated integers */
firstToken = strtok(NULL, " \t\r\n");
secondToken = strtok(NULL, " \t\r\n");
thirdToken = strtok(NULL, " \t\r\n");
if (firstToken == NULL || secondToken == NULL ||
thirdToken == NULL) { /* If one of them is NULL the command is invalid */
printInvalidCommand();
return;
}
/* SET command is in the right format, check the values: */
/* first argument */
number = checkIfInRange(firstToken, 1, N);
if (number != -1) {
command->x = number;
} else {
printErrorInRange(0, N);
command->isValid = FALSE;
return;
}
/* second argument */
number = checkIfInRange(secondToken, 1, N);
if (number != -1) {
command->y = number;
} else {
printErrorInRange(0, N);
command->isValid = FALSE;
return;
}
/* third argument */
number = checkIfInRange(thirdToken, 0, N);
if (number != -1) {
command->z = number;
} else {
printErrorInRange(0, N);
command->isValid = FALSE;
return;
}
command->isValid = TRUE; /* successfully read 3 valid integers from user - SET command is valid */
break;
case HINT:
firstToken = strtok(NULL, " \t\r\n");
secondToken = strtok(NULL, " \t\r\n");
if (firstToken == NULL || secondToken == NULL) {
printInvalidCommand();
return;
}
/* HINT command is in the right format, check the values: */
/* first argument */
number = checkIfInRange(firstToken, 1, N);
if (number != -1) {
command->x = number;
} else {
printErrorInRange(1, N);
command->isValid = FALSE;
return;
}
/* second argument */
number = checkIfInRange(secondToken, 1, N);
if (number != -1) {
command->y = number;
} else {
printErrorInRange(1, N);
command->isValid = FALSE;
return;
}
command->isValid = TRUE; /* successfully read 2 valid integers from user - HINT command is valid */
break;
case GENERATE:
firstToken = strtok(NULL, " \t\r\n");
secondToken = strtok(NULL, " \t\r\n");
if (firstToken == NULL || secondToken == NULL) {
printInvalidCommand();
return;
}
/* GENERATE command is in the right format, check the values: */
/* first argument - must be between 0 to N*N */
number = checkIfInRange(firstToken, 0, N * N);
if (number != -1) {
command->x = number;
} else {
printErrorInRange(0, N * N);
command->isValid = FALSE;
return;
}
/* second argument - must be between 0 to N*N */
number = checkIfInRange(secondToken, 0, N * N);
if (number != -1) {
command->y = number;
} else {
printErrorInRange(0, N * N);
command->isValid = FALSE;
return;
}
command->isValid = TRUE; /* successfully read 2 valid integers from user - GENERATE command is valid */
break;
/* deal with SOLVE and SAVE identically - read file path */
case SOLVE:
case SAVE:
firstToken = strtok(NULL, " \t\r\n");
if (firstToken == NULL) { /* Not enough arguments (file path wasn't provided) invalid command */
printInvalidCommand();
return;
}
command->fileName = (char *) malloc(strlen(firstToken) + 1); /* allocate memory for fileName */
if (command->fileName == NULL) {
printMallocFailed();
exit(0);
}
strcpy(command->fileName, firstToken);
command->isValid = TRUE;
break;
case EDIT: /* The user may enter "edit" without a file path */
firstToken = strtok(NULL, " \t\r\n");
if (firstToken == NULL) { /* If no file path provided temp will point to NULL */
command->fileName = NULL;
} else { /* Path was provided by the user */
command->fileName = (char *) malloc(strlen(firstToken) + 1);
if (command->fileName == NULL) {
printMallocFailed();
exit(0);
}
strcpy(command->fileName, firstToken);
}
command->isValid = TRUE;
break;
case MARK_ERRORS:
firstToken = strtok(NULL, " \t\r\n");
if (firstToken == NULL) { /* Not enough arguments - invalid command */
printInvalidCommand();
return;
}
number = checkIfZeroOrOne(firstToken);
if (number != -1) {
command->markErrors = number;
command->isValid = TRUE;
} else {
printNotZeroOrOne();
command->isValid = FALSE;
return;
}
break;
default:
printErrorInCodeFlow("getParams", "parser.c");
}
}
/* parses command from user console input into a userCommand struct and then invokes relevant function from "commands.h"
* returns a commandStruct with a "isValid" indication. Calling function must check the "isValid" field of the returned value
* important: this function allocates memory for the commandStruct it returns
* therefore after resolving the command (i.e. calling SET function with the right args)
* the resolveCommand function should free this memory*/
commandStruct *getCommandFromUser(gameParams *game) {
commandStruct *command;
char input[COMMAND_LEN];
char copyOfInput[COMMAND_LEN]; /* holds a copy of input string, to be passed to getParams function (since strtok alters input) */
char *token, *zeroToken;
int valid;
command = initializeCommand(); /* memory allocation - Freed in the end of playSudoku() */
token = (char *) malloc(COMMAND_LEN * sizeof(char) +
1); /* allocate memory using the COMMAND_LEN upper bound (memory freed in the end of the function)*/
if (token == NULL) {
printMallocFailed();
exit(EXIT_FAILURE);
}
do {
/* read command from console (at most 256 chars) */
printf("Enter your command:\n");
if (fgets(input, COMMAND_LEN, stdin) == NULL) {
/* exitCleanly(); */
freeCommand(command);
free(token);
exitGame(game);
exit(0);
}
strcpy(copyOfInput, input);
zeroToken = strtok(input, " \t\r\n"); /* Read first token */
if (zeroToken != NULL) {
strcpy(token, zeroToken);
valid = TRUE;
}
} while (valid == FALSE); /* while valid == FALSE user entered only newline */
/* classify command according to first token */
if (strcmp(token, "set") == 0) {
if (commandAvailable(SET, game->mode)) {
command->type = SET;
getParams(command, SET, copyOfInput,
game->N); /* getParams parses x,y and z and sets isValid tu TRUE if all are valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "hint") == 0) { /* hint command */
if (commandAvailable(HINT, game->mode)) {
command->type = HINT;
getParams(command, HINT, copyOfInput,
game->N); /* getParams parses x,y and sets isValid tu TRUE if all are valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "validate") == 0) { /* validate command */
if (commandAvailable(VALIDATE, game->mode)) {
command->type = VALIDATE;
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "reset") == 0) { /* restart command */
if (commandAvailable(RESET, game->mode)) {
command->type = RESET;
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "exit") == 0) { /* exit command */
if (commandAvailable(EXIT, game->mode)) {
command->type = EXIT;
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "solve") == 0) { /* available modes : ALL */
if (commandAvailable(SOLVE, game->mode)) {
command->type = SOLVE;
getParams(command, SOLVE, copyOfInput, game->N);
} else {
printInvalidCommand();
}
} else if (strcmp(token, "edit") == 0) { /* available modes : ALL */
if (commandAvailable(EDIT, game->mode)) {
command->type = EDIT;
getParams(command, EDIT, copyOfInput, game->N);
} else {
printInvalidCommand();
}
} else if (strcmp(token, "mark_errors") == 0) { /* available modes: SOLVE */
if (commandAvailable(MARK_ERRORS, game->mode)) {
command->type = MARK_ERRORS;
getParams(command, MARK_ERRORS, copyOfInput, game->N);
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "print_board") == 0) {
if (commandAvailable(PRINT_BOARD, game->mode)) {
command->type = PRINT_BOARD;
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "generate") == 0) {
if (commandAvailable(GENERATE, game->mode)) {
command->type = GENERATE;
getParams(command, GENERATE, copyOfInput, game->N);
} else {
printInvalidCommand();
}
} else if (strcmp(token, "undo") == 0) {
if (commandAvailable(UNDO, game->mode)) {
command->type = UNDO;
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "redo") == 0) {
if (commandAvailable(REDO, game->mode)) {
command->type = REDO;
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "save") == 0) {
if (commandAvailable(SAVE, game->mode)) {
command->type = SAVE;
getParams(command, SAVE, copyOfInput, game->N);
} else {
printInvalidCommand();
}
} else if (strcmp(token, "num_solutions") == 0) {
if (commandAvailable(NUM_SOLS, game->mode)) {
command->type = NUM_SOLS;
command->isValid = TRUE; /* no need for further parameters, therefore the command is valid */
} else {
printInvalidCommand();
}
} else if (strcmp(token, "autofill") == 0) {
if (commandAvailable(AUTO_FILL, game->mode)) {
command->type = AUTO_FILL;
command->isValid = TRUE;
} else {
printInvalidCommand();
}
}
/* anything else is an invalid command: */
else {
command->isValid = FALSE;
printInvalidCommand();
}
free(token);
return command;
}