diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-21 15:06:52 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-21 15:07:38 +0300 |
commit | d773b499e595a43b9b1ae449262dcf13cabf2d02 (patch) | |
tree | b1baeb12025a04f8316636b5d0ab18e30ceedb2c /src/ui/command.c |
Initial commit
Borrowing the app skeleton from Bitwise Harmony.
Diffstat (limited to 'src/ui/command.c')
-rw-r--r-- | src/ui/command.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/ui/command.c b/src/ui/command.c new file mode 100644 index 00000000..16a0d948 --- /dev/null +++ b/src/ui/command.c | |||
@@ -0,0 +1,89 @@ | |||
1 | #include "command.h" | ||
2 | #include "app.h" | ||
3 | |||
4 | #include <the_Foundation/string.h> | ||
5 | #include <ctype.h> | ||
6 | |||
7 | iBool equal_Command(const char *cmdWithArgs, const char *cmd) { | ||
8 | if (strchr(cmdWithArgs, ':')) { | ||
9 | return beginsWith_CStr(cmdWithArgs, cmd) && cmdWithArgs[strlen(cmd)] == ' '; | ||
10 | } | ||
11 | return equal_CStr(cmdWithArgs, cmd); | ||
12 | } | ||
13 | |||
14 | static const iString *tokenString_(const char *label) { | ||
15 | return collectNewFormat_String(" %s:", label); | ||
16 | } | ||
17 | |||
18 | int argLabel_Command(const char *cmd, const char *label) { | ||
19 | const iString *tok = tokenString_(label); | ||
20 | const char *ptr = strstr(cmd, cstr_String(tok)); | ||
21 | if (ptr) { | ||
22 | return atoi(ptr + size_String(tok)); | ||
23 | } | ||
24 | return 0; | ||
25 | } | ||
26 | |||
27 | int arg_Command(const char *cmd) { | ||
28 | return argLabel_Command(cmd, "arg"); | ||
29 | } | ||
30 | |||
31 | float argf_Command(const char *cmd) { | ||
32 | const char *ptr = strstr(cmd, " arg:"); | ||
33 | if (ptr) { | ||
34 | return strtof(ptr + 5, NULL); | ||
35 | } | ||
36 | return 0; | ||
37 | } | ||
38 | |||
39 | void *pointerLabel_Command(const char *cmd, const char *label) { | ||
40 | const iString *tok = tokenString_(label); | ||
41 | const char *ptr = strstr(cmd, cstr_String(tok)); | ||
42 | if (ptr) { | ||
43 | void *val = NULL; | ||
44 | sscanf(ptr + size_String(tok), "%p", &val); | ||
45 | return val; | ||
46 | } | ||
47 | return NULL; | ||
48 | } | ||
49 | |||
50 | void *pointer_Command(const char *cmd) { | ||
51 | return pointerLabel_Command(cmd, "ptr"); | ||
52 | } | ||
53 | |||
54 | const char *valuePtr_Command(const char *cmd, const char *label) { | ||
55 | const iString *tok = tokenString_(label); | ||
56 | const char *ptr = strstr(cmd, cstr_String(tok)); | ||
57 | if (ptr) { | ||
58 | return ptr + size_String(tok); | ||
59 | } | ||
60 | return NULL; | ||
61 | } | ||
62 | |||
63 | const iString *string_Command(const char *cmd, const char *label) { | ||
64 | iRangecc val = { valuePtr_Command(cmd, label), NULL }; | ||
65 | if (val.start) { | ||
66 | for (val.end = val.start; *val.end && !isspace(*val.end); val.end++) {} | ||
67 | return collect_String(newRange_String(&val)); | ||
68 | } | ||
69 | return collectNew_String(); | ||
70 | } | ||
71 | |||
72 | iInt2 dir_Command(const char *cmd) { | ||
73 | const char *ptr = strstr(cmd, " dir:"); | ||
74 | if (ptr) { | ||
75 | iInt2 dir; | ||
76 | sscanf(ptr + 5, "%d%d", &dir.x, &dir.y); | ||
77 | return dir; | ||
78 | } | ||
79 | return zero_I2(); | ||
80 | } | ||
81 | |||
82 | iInt2 coord_Command(const char *cmd) { | ||
83 | iInt2 coord = zero_I2(); | ||
84 | const char *ptr = strstr(cmd, " coord:"); | ||
85 | if (ptr) { | ||
86 | sscanf(ptr + 7, "%d%d", &coord.x, &coord.y); | ||
87 | } | ||
88 | return coord; | ||
89 | } | ||