commit c0c6ae4a59ce0ecc749f0507f9ee44f8df7dfafc from: the xhr date: Thu Sep 16 10:13:07 2021 UTC Add function to get a stat and a bonus from the arguments commit - 319d405008bb0034368a710f5a2c4028fef919ef commit + c0c6ae4a59ce0ecc749f0507f9ee44f8df7dfafc blob - 6f26e84ee9a5f11d669e3571eb67acfa8e889bdf blob + 6bea602f9d9fddd05276de1ce90261117e41e24b --- isscrolls.h +++ isscrolls.h @@ -33,6 +33,7 @@ #define MAX_PTP_LEN 201 #define MAX_CHAR_LEN 100 #define MAX_PROGRESS 10 +#define MAX_STAT_LEN 20 #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" @@ -97,6 +98,7 @@ int action_roll(int[]); int progress_roll(double[]); void ask_for_journey_difficulty(void); int get_int_from_cmd(const char *); +int get_args_from_cmd(char *, char *, int*); /* isscrolls.c */ void cmd_quit(char *); blob - daa4ae40e2be8e142ef0ecfa369b21b5c12a1481 blob + ae0708741b8923c117fe7baafa41581ed261f4e8 --- rolls.c +++ rolls.c @@ -739,4 +739,53 @@ get_int_from_cmd(const char *cmd) return ival; } + +int +get_args_from_cmd(char *cmd, char *stat, int *ival) +{ + char *tokens[MAXTOKENS]; + char *ep; + char *p, *last; + int i = 0; + long lval = -1; + + /* Parse the argument line into max tokens, separated by space */ + for ((p = strtok_r(cmd, " ", &last)); p; + (p = strtok_r(NULL, " ", &last))) { + if (i < MAXTOKENS - 1) + tokens[i++] = p; + } + tokens[i] = NULL; + /* First token is a stat */ + i = 0; + if (tokens[i] == NULL) + return -10; + else if (strlen(tokens[i]) == 0) + return -11; + + log_debug("stat token: %s\n", tokens[i]); + snprintf(stat, MAX_STAT_LEN, "%s", tokens[i]); + + /* Second token is a bonus value*/ + errno = 0; + i++; + /* It's OK to have no bonus, so the token can be NULL and we can return */ + if (tokens[i] == NULL) + return 0; + else + log_debug("bonus token: %s\n", tokens[i]); + + lval = strtol(tokens[i], &ep, 10); + if (cmd[0] == '\0' || *ep != '\0') { + printf("Please provide a number as argument\n"); + return -20; + } + if ((errno == ERANGE || lval <= 0 || lval > 10)) { + printf("Please provide a number between 1 and 10\n"); + return -22; + } + *ival = lval; + + return 0; +}