-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandparser.c
82 lines (63 loc) · 1.54 KB
/
commandparser.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
#include "commandparser.h"
void commandparser_parse (const cmd_table *command_list, char *command)
{
command[strlen(command) - 1] = '\0';
char *token = strtok(command, " ");
cmd *c = cmd_table_get(command_list, token);
if (!c)
return;
cmd_arg *args = cmdparser_get_args(NULL, c->args);
c->func(args);
}
void cmd_arg_init (cmd_arg *arg)
{
arg->i = 0;
arg->f = 0.0f;
arg->c = 0;
arg->s = NULL;
arg->v = NULL;
}
cmd_arg *cmdparser_get_args (char *command, const char *args_s)
{
char delim;
if (strcmp(args_s, "vs") == 0)
delim = '\0';
else
delim = ' ';
int argc = strlen(args_s);
cmd_arg *args = NULL;
if (argc)
{
args = malloc(sizeof(cmd_arg) * argc);
for (int i = 0; i < argc; i++)
cmd_arg_init(&args[i]);
}
char *token = strtok(NULL, &delim);
for (int i = 0; i < argc && token; i++)
{
switch (args_s[i])
{
case 'i':
args[i].i = atoi(token);
break;
case 'f':
if (scanf(token, "%f", &args[i].f) < 0)
return NULL;
break;
case 'c':
args[i].c = token[0];
break;
case 's':
args[i].s = token;
break;
case 'v':
continue;
break;
default:
continue;
break;
}
token = strtok(NULL, &delim);
}
return args;
}