-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
109 lines (93 loc) · 2.44 KB
/
parser.c
File metadata and controls
109 lines (93 loc) · 2.44 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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "main_aux.h"
#include "parser.h"
#include "game.h"
#define DELIMITER " \t\r\n"
#define MALLOC_ERROR "Error: malloc has failed\n"
/*
* Scans input from user for number of wanted fixed cells in the board.
* returns it when the input is legal.
*/
int get_fixed_cells(Board* board) {
int fixedCells;
if (scanf("%d", &fixedCells) != 1) {
checkEOF(board);
}
while (fixedCells < 0 || fixedCells > 80){
printf("Error: invalid number of cells to fill (should be between 0 and 80)\n");
printf("Please enter the number of cells to fill [0-80]:\n");
if (scanf("%d", &fixedCells) != 1) {
checkEOF(board);
}
}
return fixedCells;
}
const char* get_command_name(int cmd_id) {
static char* names[] = { "invalid_command", "set", "hint", "validate", "restart", "exit" };
if (cmd_id < INVALID_COMMAND || cmd_id > EXIT) {
return 0;
} else {
return names[cmd_id];
}
}
int get_command_id(char *type) {
int cmd_id;
if (!type || type == '\0')
return -1;
for (cmd_id = INVALID_COMMAND; cmd_id <= EXIT; cmd_id++) {
if (!strcmp(type, get_command_name(cmd_id)))
return cmd_id;
}
return -1;
}
/*
*Creates a Command struct out of the user input.
*/
Command* create_new_command_object(int cmd_id, int params[3], int param_counter) {
int i;
Command* cmd = (Command*) malloc(sizeof(Command));
if (cmd == NULL) {
printf(MALLOC_ERROR);
exit(EXIT_FAILURE);
}
cmd->id = cmd_id;
cmd->param_counter = param_counter;
for (i = 0; i < 3; i++) {
cmd->params[i] = params[i];
}
return cmd;
}
/*
* Gets given input from the user.
* Parses it to a specific command, including the paramaters.
*/
Command* parse_command(char userInput[]) {
int cmd_id;
int param_counter = 0;
int params[3] = { 0 };
char *token = strtok(userInput, DELIMITER);
if (feof(stdin) && !token) {
cmd_id = EXIT;
return create_new_command_object(cmd_id, params, 0);
}
if (!token) {
/*
* Failed to parse any input from the user, hence continuing.
*/
return NULL;
}
cmd_id = get_command_id(token);
/*
* Populating the param list.(No need to check more then 3 params)
*/
token = strtok(NULL, DELIMITER);
while(token && param_counter < 3) {
params[param_counter] = atoi(token);
/* We were directed to assume that X Y Z are valid integers so no need to check validity. */
param_counter++;
token = strtok(NULL, DELIMITER);
}
return create_new_command_object(cmd_id, params, param_counter);
}