X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=misc.c;h=dde006340525739807e6e4c8f9362ce02083726c;hb=80c0c10221604322425ef9d138fad0da32b8bd65;hp=801331bdd06b14b3264ff1fbe5818daba780a44c;hpb=a2e0c55fb4b435c3aff243421114364e164f0fc9;p=open-adventure.git diff --git a/misc.c b/misc.c index 801331b..dde0063 100644 --- a/misc.c +++ b/misc.c @@ -23,12 +23,24 @@ void* xmalloc(size_t size) void packed_to_token(long packed, char token[6]) { + // Unpack and map back to ASCII. for (int i = 0; i < 5; ++i) { char advent = (packed >> i * 6) & 63; - token[i] = advent_to_ascii[advent]; + token[4 - i] = advent_to_ascii[advent]; } + + // Ensure the last character is \0. token[5] = '\0'; + + // Replace trailing whitespace with \0. + for (int i = 4; i >= 0; --i) + { + if (token[i] == ' ' || token[i] == '\t') + token[i] = '\0'; + else + break; + } } /* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */ @@ -66,7 +78,7 @@ void newspeak(char* msg) if (msg[i + 1] == 'd') { copy[i + 1] = 's'; - sprintf(parameters[pi], "%d", PARMS[pi]); + sprintf(parameters[pi], "%ld", PARMS[pi]); } // Unmodified string specifier. @@ -116,7 +128,7 @@ void newspeak(char* msg) // Render the final string. char rendered[2000]; // FIXME: to be replaced with dynamic allocation - sprintf(&rendered, copy, parameters[1], parameters[2], parameters[3], parameters[4]); // FIXME: to be replaced with vsprintf() + sprintf((char *)&rendered, copy, parameters[1], parameters[2], parameters[3], parameters[4]); // FIXME: to be replaced with vsprintf() // Print the message. printf("%s\n", rendered);