summaryrefslogtreecommitdiff
path: root/src/init1.cc
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2015-02-23 09:11:57 +0100
committerBardur Arantsson <bardur@scientician.net>2015-02-23 09:11:57 +0100
commitc4746c0f6d328baa429c96e54d86d2d7f97ccbcc (patch)
tree3f0b3a2f98298ab823662e0096bc7ba857793b98 /src/init1.cc
parentb84ac3e1b505624e2af18a4283e7ec379c00b0dc (diff)
Remove r_name, r_text, r_head in favor of simple strings
Diffstat (limited to 'src/init1.cc')
-rw-r--r--src/init1.cc66
1 files changed, 34 insertions, 32 deletions
diff --git a/src/init1.cc b/src/init1.cc
index 8696e18..4fc3513 100644
--- a/src/init1.cc
+++ b/src/init1.cc
@@ -1498,6 +1498,33 @@ static byte monster_ego_modify(char c)
}
}
+/**
+ * Append one string to the end of another, reallocating if
+ * necessary.
+ */
+static void strappend(char **s, char *t)
+{
+ // Do we need to initialize the destination string?
+ if (*s == nullptr)
+ {
+ // Costs an extra allocation which could be avoided
+ // but this leads to simpler code.
+ *s = strdup("");
+ }
+ // We should really be preserving the original pointer and
+ // do something else in case of failure to realloc(), but
+ // instead we just do the lazy thing and call abort() if
+ // reallocation fails. In practice it won't.
+ *s = static_cast<char *>(realloc(*s, strlen(*s) + strlen(t) + 1));
+ if (*s == nullptr)
+ {
+ abort(); // Cannot handle failure to reallocate
+ }
+
+ /* Append 't' to the destination string */
+ strcat(*s, t);
+}
+
/*
* Implements fp stacks, for included files
*/
@@ -7485,10 +7512,6 @@ errr init_r_info_txt(FILE *fp, char *buf)
error_line = -1;
- /* Start the "fake" stuff */
- r_head->name_size = 0;
- r_head->text_size = 0;
-
/* Parse */
fp_stack_init(fp);
while (0 == my_fgets_dostack(buf, 1024))
@@ -7550,7 +7573,7 @@ errr init_r_info_txt(FILE *fp, char *buf)
if (i < error_idx) return (4);
/* Verify information */
- if (i >= r_head->info_num) return (2);
+ if (i >= max_r_idx) return (2);
/* Save the index */
error_idx = i;
@@ -7558,17 +7581,9 @@ errr init_r_info_txt(FILE *fp, char *buf)
/* Point at the "info" */
r_ptr = &r_info[i];
- /* Hack -- Verify space */
- if (r_head->name_size + strlen(s) + 8 > FAKE_NAME_SIZE) return (7);
-
- /* Advance and Save the name index */
- if (!r_ptr->name) r_ptr->name = ++r_head->name_size;
-
- /* Append chars to the name */
- strcpy(r_name + r_head->name_size, s);
-
- /* Advance the index */
- r_head->name_size += strlen(s);
+ /* Allocate name string. */
+ assert(!r_ptr->name); // Sanity check that we aren't overwriting anything
+ r_ptr->name = strdup(s);
/* HACK -- Those ones HAVE to have a set default value */
r_ptr->drops.treasure = OBJ_GENE_TREASURE;
@@ -7591,17 +7606,8 @@ errr init_r_info_txt(FILE *fp, char *buf)
/* Acquire the text */
s = buf + 2;
- /* Hack -- Verify space */
- if (r_head->text_size + strlen(s) + 8 > FAKE_TEXT_SIZE) return (7);
-
- /* Advance and Save the text index */
- if (!r_ptr->text) r_ptr->text = ++r_head->text_size;
-
- /* Append chars to the name */
- strcpy(r_text + r_head->text_size, s);
-
- /* Advance the index */
- r_head->text_size += strlen(s);
+ /* Append to description */
+ strappend(&r_ptr->text, s);
/* Next... */
continue;
@@ -7853,11 +7859,7 @@ errr init_r_info_txt(FILE *fp, char *buf)
return (6);
}
-
- /* Complete the "name" and "text" sizes */
- ++r_head->name_size;
- ++r_head->text_size;
-
+ /* Postprocessing */
for (i = 1; i < max_r_idx; i++)
{
/* Invert flag WILD_ONLY <-> RF8_DUNGEON */