X-Git-Url: https://jxself.org/git/?p=open-adventure.git;a=blobdiff_plain;f=misc.c;h=36deef454c2c332f075c30558f313c8c42fb4aa1;hp=0b00992b6e0b1b2342a93258aa7a4db680bcbf41;hb=f3dae6ff83cad7d5bf3cbf5db523af7ca6629941;hpb=df36b62c7436bdec84f29c14b3542cdcdaaa3784 diff --git a/misc.c b/misc.c index 0b00992..36deef4 100644 --- a/misc.c +++ b/misc.c @@ -23,7 +23,7 @@ static void* xmalloc(size_t size) return (ptr); } -void packed_to_token(long packed, char token[TOKLEN+1]) +void packed_to_token(long packed, char token[TOKLEN + 1]) { // The advent->ascii mapping. const char advent_to_ascii[] = { @@ -81,7 +81,7 @@ long token_to_packed(const char token[]) size_t t_len = strlen(token); if (t_len > TOKLEN) - t_len = TOKLEN; + t_len = TOKLEN; long packed = 0; for (size_t i = 0; i < t_len; ++i) { char mapped = ascii_to_advent[(int) toupper(token[i])]; @@ -94,8 +94,10 @@ void tokenize(char* raw, struct command_t *cmd) { memset(cmd, '\0', sizeof(struct command_t)); - /* FIXME: put a bound prefix on the %s to prevent buffer overflow */ - int word_count = sscanf(raw, "%s%s", cmd->raw1, cmd->raw2); + /* Bound prefix on the %s would be needed to prevent buffer + * overflow. but we shortstop this more simply by making each + * raw-input buffer as long as the enrire inout buffer. */ + sscanf(raw, "%s%s", cmd->raw1, cmd->raw2); // pack the substrings cmd->wd1 = token_to_packed(cmd->raw1); @@ -190,20 +192,6 @@ void vspeak(const char* msg, bool blank, va_list ap) size -= len; } - // All-lowercase specifier. - if (msg[i] == 'L' || - msg[i] == 'C') { - packed_to_token(arg, renderp); /* unpack directly to destination */ - int len = strlen(renderp); - for (int j = 0; j < len; ++j) { - renderp[j] = tolower(renderp[j]); - } - if (msg[i] == 'C') // First char uppercase, rest lowercase. - renderp[0] = toupper(renderp[0]); - renderp += len; - size -= len; - } - previous_arg = arg; } } @@ -288,16 +276,16 @@ int word_count(char* str) int inblanks = true; for (char *s = str; *s; s++) - if (inblanks) { - if (strchr(delims, *s) == 0) { - ++count; - inblanks = false; - } - } else { - if (strchr(delims, *s) != 0) { - inblanks = true; - } - } + if (inblanks) { + if (strchr(delims, *s) == 0) { + ++count; + inblanks = false; + } + } else { + if (strchr(delims, *s) != 0) { + inblanks = true; + } + } return (count); } @@ -497,6 +485,10 @@ int get_special_vocab_id(const char* word) long get_vocab_id(const char* word) // Search the vocab categories in order for the supplied word. { + /* Check for an empty string */ + if (strncmp(word, "", sizeof("")) == 0) + return (WORD_EMPTY); + long ref_num; /* FIXME: Magic numbers related to vocabulary */ @@ -659,7 +651,7 @@ long randrange(long range) return range * get_next_lcg_value() / game.lcg_m; } -void make_zzword(char zzword[TOKLEN+1]) +void make_zzword(char zzword[TOKLEN + 1]) { for (int i = 0; i < 5; ++i) { zzword[i] = 'A' + randrange(26);