X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=misc.c;h=2fe1363826eef89a4b85a76c0667a210cb6d936e;hb=5f28f9524472ef6784191470747d708262b31386;hp=120a503351a0d899349d98c162dd7c0f35900866;hpb=a3c159660bb03f220b7de52ba0ac4977098f389e;p=open-adventure.git diff --git a/misc.c b/misc.c index 120a503..2fe1363 100644 --- a/misc.c +++ b/misc.c @@ -399,6 +399,30 @@ static int get_special_vocab_id(const char* word) return (WORD_NOT_FOUND); } +static bool is_valid_int(const char *str) +/* Returns true if the string passed in is represents a valid integer, + * that could then be parsed by atoi() */ +{ + // Handle negative number + if (*str == '-') + ++str; + + // Handle empty string or just "-" + if (!*str) + return false; + + // Check for non-digit chars in the rest of the stirng. + while (*str) + { + if (!isdigit(*str)) + return false; + else + ++str; + } + + return true; +} + static void get_vocab_metadata(const char* word, vocab_t* id, enum wordtype* type) { /* Check for an empty string */ @@ -445,6 +469,13 @@ static void get_vocab_metadata(const char* word, vocab_t* id, enum wordtype* typ return; } + // Check words that are actually numbers. + if (is_valid_int(word)) { + *id = WORD_EMPTY; + *type = NUMERIC; + return; + } + *id = WORD_NOT_FOUND; *type = NO_WORD_TYPE; return;