From 74ea6dc86646cee9915292d73d8c7afef01ef3e0 Mon Sep 17 00:00:00 2001 From: Sopár Adrián Date: Thu, 20 Jun 2024 09:28:14 +0200 Subject: First commit. This is mostly the state of the project as I left it around the end of 2019. --- src/game/commands.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/game/commands.c (limited to 'src/game/commands.c') diff --git a/src/game/commands.c b/src/game/commands.c new file mode 100644 index 0000000..7e0e865 --- /dev/null +++ b/src/game/commands.c @@ -0,0 +1,61 @@ +#include "commands.h" +#include + +static struct cmd_res command_switch(struct game_state *state, int argc, char **argv); + +struct cmd_res run_command(struct game_state *state, char *command) +{ + int argc = 0; + char *argv[MAX_ARGS_COUNT]; + char *arg = command; + char *space; + int arg_length; + while ((space = strchr(arg, ' ')) != NULL) + { + if (*arg != ' ') + { + arg_length = space - arg; + argv[argc] = malloc(arg_length + 1); + strncpy(argv[argc], arg, arg_length); + argv[argc][arg_length] = 0; + arg = space + 1; + argc++; + } + } + //Last arg + arg_length = strlen(arg); + argv[argc] = malloc(arg_length + 1); + strncpy(argv[argc], arg, arg_length); + argv[argc][arg_length] = 0; + argc++; + + struct cmd_res response = command_switch(state, argc, argv); + for (int i = 0; i < argc; i++) + free(argv[i]); + return response; +} + +static struct cmd_res nothing(struct game_state *state, int argc, char **argv) +{ + struct cmd_res response; + response.type = CRT_FAIL; + strncpy(response.text, "There is no command you entered!", MAX_CR_TEXT_LENGTH); + return response; +} + +static struct cmd_res quit(struct game_state *state, int argc, char **argv) +{ + state->result = GR_QUIT; + struct cmd_res response; + response.type = CRT_QUIT; + *(response.text) = 0; + return response; +} + +static struct cmd_res command_switch(struct game_state *state, int argc, char **argv) +{ + if (!strcmp("", *argv)) + return nothing(state, argc, argv); + else if (!strcmp("q", *argv)) + return quit(state, argc, argv); +} \ No newline at end of file -- cgit v1.2.3