From 76b3632d13bbf57c6354bc10f67ea849f1d47279 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Sat, 17 Sep 2016 09:58:14 +0200 Subject: Rework player_race_mod::{title,desc} to std::string --- src/birth.cc | 27 ++++++++++---------- src/corrupt.cc | 10 ++------ src/dungeon.cc | 6 ++--- src/files.cc | 2 +- src/init1.cc | 15 ++++------- src/loadsave.cc | 66 ++++++++++++++++++++++++++++++++----------------- src/player_race_mod.hpp | 5 ++-- src/xtra2.cc | 34 +++---------------------- src/xtra2.hpp | 2 -- 9 files changed, 76 insertions(+), 91 deletions(-) (limited to 'src') diff --git a/src/birth.cc b/src/birth.cc index 8d14da00..27b7ee89 100644 --- a/src/birth.cc +++ b/src/birth.cc @@ -981,8 +981,9 @@ static void player_outfit_spellbook(cptr spell_name) */ static void player_outfit(void) { + // Shorthand names for convenience cptr class_name = spp_ptr->title; - cptr subrace_name = rmp_ptr->title; + auto const &subrace_name = rmp_ptr->title; /* * Get an adventurer guide describing a bit of the @@ -1048,37 +1049,37 @@ static void player_outfit(void) } /* Dragons */ - if (streq(subrace_name, "Red")) + if (subrace_name == "Red") { player_outfit_spellbook("Globe of Light"); } - if (streq(subrace_name, "Black")) + if (subrace_name == "Black") { player_outfit_spellbook("Geyser"); } - if (streq(subrace_name, "Green")) + if (subrace_name == "Green") { player_outfit_spellbook("Noxious Cloud"); } - if (streq(subrace_name, "Blue")) + if (subrace_name == "Blue") { player_outfit_spellbook("Stone Skin"); } - if (streq(subrace_name, "White")) + if (subrace_name == "White") { player_outfit_spellbook("Sense Monsters"); } - if (streq(subrace_name, "Ethereal")) + if (subrace_name == "Ethereal") { player_outfit_spellbook("Recharge"); } /* Demons */ - if (streq(subrace_name, "(Aewrog)")) + if (subrace_name == "(Aewrog)") { player_outfit_spellbook("Charm"); } - if (streq(subrace_name, "(Narrog)")) + if (subrace_name == "(Narrog)") { player_outfit_spellbook("Phase Door"); } @@ -1116,7 +1117,7 @@ static void player_outfit(void) identify_pack_fully(); } - if (streq(rmp_ptr->title, "Vampire")) + if (rmp_ptr->title == "Vampire") { player_gain_corruption(CORRUPT_VAMPIRE_TEETH); player_gain_corruption(CORRUPT_VAMPIRE_STRENGTH); @@ -1382,7 +1383,7 @@ static int dump_rmods(int sel, int *racem, int max) /* Display */ if (racem[n]) - strnfmt(buf, 80, "%c%c%c %s", p1, I2A(n), p2, rmp_ptr->title); + strnfmt(buf, 80, "%c%c%c %s", p1, I2A(n), p2, rmp_ptr->title.c_str()); else strnfmt(buf, 80, "%c%c%c Classical", p1, I2A(n), p2); @@ -1391,7 +1392,7 @@ static int dump_rmods(int sel, int *racem, int max) { std::string desc; - desc += rmp_ptr->desc; + desc += rmp_ptr->description; if (rmp_ptr->flags & PR_EXPERIMENTAL) { desc += "\nEXPERIMENTAL"; @@ -1716,7 +1717,7 @@ static bool_ player_birth_aux_ask() } else if (c == '?') { - help_subrace(race_mod_info[racem[sel]].title); + help_subrace(race_mod_info[racem[sel]].title.c_str()); } k = (islower(c) ? A2I(c) : -1); diff --git a/src/corrupt.cc b/src/corrupt.cc index 8ad22ea9..bc9f3a8b 100644 --- a/src/corrupt.cc +++ b/src/corrupt.cc @@ -90,19 +90,13 @@ static void player_gain_vampire() { player_race_mod *rmp_ptr = &race_mod_info[SUBRACE_SAVE]; - /* Be a Vampire and be proud of it */ - cptr title = rmp_ptr->title; - if (streq(title, "Vampire")) + if (rmp_ptr->title == "Vampire") { - title = "Vampire"; rmp_ptr->place = FALSE; - set_subrace_title(rmp_ptr, title); } else { - char buf[512]; - sprintf(buf, "Vampire %s", title); - set_subrace_title(rmp_ptr, buf); + rmp_ptr->title = fmt::format("Vampire {}", rmp_ptr->title); } /* Bonus/and .. not bonus :) */ diff --git a/src/dungeon.cc b/src/dungeon.cc index bc5ec69c..824831cf 100644 --- a/src/dungeon.cc +++ b/src/dungeon.cc @@ -1016,7 +1016,7 @@ static bool_ grace_delay_trigger() static void process_world_gods() { const char *race_name = rp_ptr->title; - const char *subrace_name = rmp_ptr->title; + auto const &subrace_name = rmp_ptr->title; if (p_ptr->pgod == GOD_VARDA) { @@ -1177,7 +1177,7 @@ static void process_world_gods() if (grace_delay_trigger()) { /* He loves astral beings */ - if (streq(subrace_name, "LostSoul")) + if (subrace_name == "LostSoul") { inc_piety(GOD_ALL, 1); } @@ -1189,7 +1189,7 @@ static void process_world_gods() } /* Really hates vampires and demons */ - if (streq(subrace_name, "Vampire") || + if ((subrace_name == "Vampire") || streq(race_name, "Demon")) { inc_piety(GOD_ALL, -10); diff --git a/src/files.cc b/src/files.cc index ec569e73..8d489d0c 100644 --- a/src/files.cc +++ b/src/files.cc @@ -900,7 +900,7 @@ static cptr process_pref_file_expr(char **sp, char *fp) /* Race */ else if (streq(b + 1, "RACEMOD")) { - v = rmp_ptr->title; + v = rmp_ptr->title.c_str(); // The string SHOULD be stable enough for this } /* Class */ diff --git a/src/init1.cc b/src/init1.cc index 0654155e..de92ae90 100644 --- a/src/init1.cc +++ b/src/init1.cc @@ -1241,8 +1241,7 @@ errr init_player_info_txt(FILE *fp) rmp_ptr = &race_mod_info[i]; /* Copy title */ - assert(!rmp_ptr->title); - rmp_ptr->title = my_strdup(s); + rmp_ptr->title = s; /* Initialize */ rmp_ptr->powers[0] = rmp_ptr->powers[1] = rmp_ptr->powers[2] = rmp_ptr->powers[3] = -1; @@ -1270,15 +1269,11 @@ errr init_player_info_txt(FILE *fp) } /* Description */ - if (!rmp_ptr->desc) + if (!rmp_ptr->description.empty()) { - rmp_ptr->desc = my_strdup(s); - } - else - { - /* Append chars to the name */ - strappend(&rmp_ptr->desc, format("\n%s", s)); + rmp_ptr->description += '\n'; } + rmp_ptr->description += s; /* Next... */ continue; @@ -7331,7 +7326,7 @@ static cptr process_dungeon_file_expr(char **sp, char *fp) /* Race Mod */ else if (streq(b + 1, "RACEMOD")) { - v = rmp_ptr->title; + v = rmp_ptr->title.c_str(); // The string SHOULD be stable enough for this } /* Class */ diff --git a/src/loadsave.cc b/src/loadsave.cc index 8166ab65..066e6a6e 100644 --- a/src/loadsave.cc +++ b/src/loadsave.cc @@ -221,6 +221,48 @@ static void do_string(char *str, int max, ls_flag_t flag) } } +static void load_std_string(std::string &s) +{ + s.clear(); + while (true) + { + byte u8 = sf_get(); + if (u8) + { + s += u8; + } + else + { + break; + } + } +} + +static void save_std_string(std::string &s) +{ + for (std::size_t i = 0; i < s.length(); i++) + { + assert(s[i] != 0); // Sanity check; we cannot handle NULs in strings + sf_put(s[i]); + } + sf_put(0); // NUL terminator +} + +static void do_std_string(std::string &s, ls_flag_t flag) +{ + switch(flag) + { + case ls_flag_t::LOAD: + { + load_std_string(s); + break; + } + case ls_flag_t::SAVE: + save_std_string(s); + break; + } +} + /* * Load/save flag set */ @@ -291,29 +333,9 @@ static void do_subrace(ls_flag_t flag) { player_race_mod *sr_ptr = &race_mod_info[SUBRACE_SAVE]; int i; - char buf[81]; - buf[80] = '\0'; // Make sure string is always NUL terminated - - if (flag == ls_flag_t::SAVE) - { - strncpy(buf, sr_ptr->title, 80); - } - do_string(buf, 80, flag); - if (flag == ls_flag_t::LOAD) - { - set_subrace_title(sr_ptr, buf); - } - - if (flag == ls_flag_t::SAVE) - { - strncpy(buf, sr_ptr->desc, 80); - } - do_string(buf, 80, flag); - if (flag == ls_flag_t::LOAD) - { - set_subrace_description(sr_ptr, buf); - } + do_std_string(sr_ptr->title, flag); + do_std_string(sr_ptr->description, flag); do_bool(&sr_ptr->place, flag); diff --git a/src/player_race_mod.hpp b/src/player_race_mod.hpp index da3ae0ca..60558d24 100644 --- a/src/player_race_mod.hpp +++ b/src/player_race_mod.hpp @@ -12,12 +12,13 @@ #include "skills_defs.hpp" #include +#include #include struct player_race_mod { - char *title = nullptr; /* Type of race mod */ - char *desc = nullptr; /* Desc */ + std::string title; + std::string description; bool_ place = FALSE; /* TRUE = race race modifier, FALSE = Race modifier race */ diff --git a/src/xtra2.cc b/src/xtra2.cc index 580d55a9..03c04587 100644 --- a/src/xtra2.cc +++ b/src/xtra2.cc @@ -5359,44 +5359,18 @@ void switch_subrace(int racem, bool_ copy_old) /* If we switch to the saved subrace, we copy over the old subrace data */ if (copy_old && (racem == SUBRACE_SAVE)) { - // Keep references to owned pointers. - auto old_title = race_mod_info[SUBRACE_SAVE].title; - auto old_desc = race_mod_info[SUBRACE_SAVE].desc; + // Keep old description + auto old_desc = race_mod_info[SUBRACE_SAVE].description; // Copy everything race_mod_info[SUBRACE_SAVE] = race_mod_info[p_ptr->pracem]; - // "Undo" copy of title and description (since they're *owned* pointers) - race_mod_info[SUBRACE_SAVE].title = old_title; - race_mod_info[SUBRACE_SAVE].desc = old_desc; - // Replace subrace title with the title currently held by player. - set_subrace_title(&race_mod_info[SUBRACE_SAVE], race_mod_info[p_ptr->pracem].title); + // Reinstate description + race_mod_info[SUBRACE_SAVE].description = old_desc; } p_ptr->pracem = racem; rmp_ptr = &race_mod_info[p_ptr->pracem]; } -void set_subrace_title(player_race_mod *rmp_ptr, cptr name) -{ - // Free old title. - free(rmp_ptr->title); - // Allocate copy of new title. - rmp_ptr->title = strdup(name); - if (!rmp_ptr->title) { - abort(); - } -} - -void set_subrace_description(player_race_mod *rmp_ptr, cptr desc) -{ - // Free old description - free(rmp_ptr->desc); - // Allocate copy of new description. - rmp_ptr->desc = strdup(desc); - if (!rmp_ptr->desc) { - abort(); - } -} - /* * Rebirth, recalc hp & exp/level */ diff --git a/src/xtra2.hpp b/src/xtra2.hpp index 10d752a2..5d9d26f5 100644 --- a/src/xtra2.hpp +++ b/src/xtra2.hpp @@ -8,8 +8,6 @@ #include extern void do_rebirth(void); -extern void set_subrace_title(player_race_mod *rmp_ptr, cptr name); -extern void set_subrace_description(player_race_mod *rmp_ptr, cptr desc); extern void switch_subrace(int racem, bool_ copy_old); extern void drop_from_wild(void); extern bool_ set_roots(int v, s16b ac, s16b dam); -- cgit v1.2.3