X-Git-Url: https://jxself.org/git/?p=open-adventure.git;a=blobdiff_plain;f=misc.c;h=2fe1363826eef89a4b85a76c0667a210cb6d936e;hp=120a503351a0d899349d98c162dd7c0f35900866;hb=5337e00725f17e08836fb2a2f59ab178ece47000;hpb=ef236aea3beef846b38245aa4ce3a753862c84f0 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;