Magic-number elimination.
authorEric S. Raymond <esr@thyrsus.com>
Mon, 3 Jul 2017 11:14:15 +0000 (07:14 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 3 Jul 2017 11:14:15 +0000 (07:14 -0400)
actions.c
advent.h
main.c
misc.c

index 5de7cb001e4a7b276ccc19c8f0e96bd05539e470..9ddbb56bde703c52e54c2a378b852c98775d785e 100644 (file)
--- a/actions.c
+++ b/actions.c
@@ -171,7 +171,7 @@ static int bigwords(token_t foo)
  *  Look up foo in special section of vocab to determine which word we've got.
  *  Last word zips the eggs back to the giant room (unless already there). */
 {
  *  Look up foo in special section of vocab to determine which word we've got.
  *  Last word zips the eggs back to the giant room (unless already there). */
 {
-    char word[6];
+    char word[TOKLEN+1];
     packed_to_token(foo, word);
     int k = (int) get_special_vocab_id(word);
     int spk = NOTHING_HAPPENS;
     packed_to_token(foo, word);
     int k = (int) get_special_vocab_id(word);
     int spk = NOTHING_HAPPENS;
@@ -1043,7 +1043,7 @@ static int say(struct command_t *command)
         b = command->wd2x;
         command->wd1 = command->wd2;
     }
         b = command->wd2x;
         command->wd1 = command->wd2;
     }
-    char word1[6];
+    char word1[TOKLEN+1];
     packed_to_token(command->wd1, word1);
     int wd = (int) get_vocab_id(word1);
     /* FIXME: magic numbers */
     packed_to_token(command->wd1, word1);
     int wd = (int) get_vocab_id(word1);
     /* FIXME: magic numbers */
index 2ef6a59bdf59ea88f26d5138a72a6a26f3011768..085244067441c5de988a35ef3c3d57df88142b8b 100644 (file)
--- a/advent.h
+++ b/advent.h
@@ -6,6 +6,7 @@
 #include "dungeon.h"
 
 #define LINESIZE       1024
 #include "dungeon.h"
 
 #define LINESIZE       1024
+#define TOKLEN        5          // # sigificant character sin a token */
 #define NDWARVES       6          // number of dwarves
 #define PIRATE         NDWARVES   // must be NDWARVES-1 when zero-origin
 #define DALTLC         LOC_NUGGET // alternate dwarf location
 #define NDWARVES       6          // number of dwarves
 #define PIRATE         NDWARVES   // must be NDWARVES-1 when zero-origin
 #define DALTLC         LOC_NUGGET // alternate dwarf location
@@ -151,7 +152,7 @@ struct game_t {
     long trnluz;                 // # points lost so far due to number of turns used
     long turns;                  // how many commands he's given (ignores yes/no)
     bool wzdark;                 // whether the loc he's leaving was dark
     long trnluz;                 // # points lost so far due to number of turns used
     long turns;                  // how many commands he's given (ignores yes/no)
     bool wzdark;                 // whether the loc he's leaving was dark
-    char zzword[6];              // randomly generated magic word from bird
+    char zzword[TOKLEN+1];       // randomly generated magic word from bird
     bool blooded;                // has player drunk of dragon's blood?
     long abbrev[NLOCATIONS + 1];
     long atloc[NLOCATIONS + 1];
     bool blooded;                // has player drunk of dragon's blood?
     long abbrev[NLOCATIONS + 1];
     long atloc[NLOCATIONS + 1];
@@ -190,7 +191,7 @@ extern struct settings_t settings;
 extern char* xstrdup(const char* s);
 extern void* xmalloc(size_t size);
 extern void packed_to_token(long, char token[]);
 extern char* xstrdup(const char* s);
 extern void* xmalloc(size_t size);
 extern void packed_to_token(long, char token[]);
-extern long token_to_packed(const char token[6]);
+extern long token_to_packed(const char token[TOKLEN+1]);
 extern void tokenize(char*, long tokens[4]);
 extern void vspeak(const char*, bool, va_list);
 extern bool wordeq(token_t, token_t);
 extern void tokenize(char*, long tokens[4]);
 extern void vspeak(const char*, bool, va_list);
 extern bool wordeq(token_t, token_t);
diff --git a/main.c b/main.c
index 4f46c0ac8a81dc3c9f6ffda2269b1f5a963efde9..3b48af3cf596e6700ff358a7859e141cce7f3b3c 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1089,8 +1089,8 @@ L2607:
         } else
             lampcheck();
 
         } else
             lampcheck();
 
-        char word1[6];
-        char word2[6];
+        char word1[TOKLEN+1];
+        char word2[TOKLEN+1];
         packed_to_token(command.wd1, word1);
         packed_to_token(command.wd2, word2);
         V1 = get_vocab_id(word1);
         packed_to_token(command.wd1, word1);
         packed_to_token(command.wd2, word2);
         V1 = get_vocab_id(word1);
diff --git a/misc.c b/misc.c
index b1c50d18ee76e6b4425fff5fd6943f0159cfdfef..6f80c9d861a89bb7c8c73b9ff963ba45b1bea245 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -35,7 +35,7 @@ void* xmalloc(size_t size)
     return (ptr);
 }
 
     return (ptr);
 }
 
-void packed_to_token(long packed, char token[6])
+void packed_to_token(long packed, char token[TOKLEN+1])
 {
     // The advent->ascii mapping.
     const char advent_to_ascii[] = {
 {
     // The advent->ascii mapping.
     const char advent_to_ascii[] = {
@@ -68,7 +68,7 @@ void packed_to_token(long packed, char token[6])
     }
 }
 
     }
 }
 
-long token_to_packed(const char token[6])
+long token_to_packed(const char token[TOKLEN+1])
 {
     const char ascii_to_advent[] = {
         63, 63, 63, 63, 63, 63, 63, 63,
 {
     const char ascii_to_advent[] = {
         63, 63, 63, 63, 63, 63, 63, 63,
@@ -113,7 +113,7 @@ void tokenize(char* raw, long tokens[4])
     int word_count = sscanf(raw, "%s%s", words[0], words[1]);
 
     // make space for substrings and zero it out
     int word_count = sscanf(raw, "%s%s", words[0], words[1]);
 
     // make space for substrings and zero it out
-    char chunk_data[][6] = {
+    char chunk_data[][TOKLEN+1] = {
         {"\0\0\0\0\0"},
         {"\0\0\0\0\0"},
         {"\0\0\0\0\0"},
         {"\0\0\0\0\0"},
         {"\0\0\0\0\0"},
         {"\0\0\0\0\0"},
@@ -679,7 +679,7 @@ long randrange(long range)
     return range * get_next_lcg_value() / game.lcg_m;
 }
 
     return range * get_next_lcg_value() / game.lcg_m;
 }
 
-void make_zzword(char zzword[6])
+void make_zzword(char zzword[TOKLEN+1])
 {
     for (int i = 0; i < 5; ++i) {
         zzword[i] = 'A' + randrange(26);
 {
     for (int i = 0; i < 5; ++i) {
         zzword[i] = 'A' + randrange(26);