From 5928204e69432be76b618bfa08c03e9bee6b3b09 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Wed, 5 Oct 2016 18:45:08 +0200 Subject: Use std::string for describe_player_location() --- src/files.cc | 32 ++++++++++++++++++-------------- src/files.hpp | 2 +- src/spells2.cc | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/files.cc b/src/files.cc index 183ae0af..a2c11846 100644 --- a/src/files.cc +++ b/src/files.cc @@ -60,8 +60,10 @@ #include "xtra1.hpp" #include "z-rand.hpp" +#include #include #include +#include #include #include #include @@ -2322,28 +2324,28 @@ void display_player(int mode) * Describe the player's location -- either by dungeon level, town, or in * wilderness with landmark reference. */ -cptr describe_player_location() +std::string describe_player_location() { auto const &wilderness = game->wilderness; auto const &d_info = game->edit_data.d_info; - int i; - static char desc[80]; + std::string desc; + int pwx = (p_ptr->wild_mode ? p_ptr->px : p_ptr->wilderness_x); int pwy = (p_ptr->wild_mode ? p_ptr->py : p_ptr->wilderness_y); int feat = wilderness(pwx, pwy).feat; if (dungeon_type != DUNGEON_WILDERNESS && dun_level > 0) { - sprintf(desc, "on level %d of %s", dun_level, d_info[dungeon_type].name.c_str()); + desc += fmt::format("on level {:d} of {}", dun_level, d_info[dungeon_type].name); } else if (wf_info[feat].terrain_idx == TERRAIN_TOWN) { - sprintf(desc, "in the town of %s", wf_info[feat].name); + desc += fmt::format("in the town of {}", wf_info[feat].name); } else if (wf_info[feat].entrance) { - sprintf(desc, "near %s", wf_info[feat].name); + desc += fmt::format("near {}", wf_info[feat].name); } else { @@ -2382,12 +2384,12 @@ cptr describe_player_location() if (!landmark) { - sprintf(desc, "in %s", wf_info[feat].text); + desc += fmt::format("in {}", wf_info[feat].text); } else if (pwx == lwx && pwy == lwy) { /* Paranoia; this should have been caught above */ - sprintf(desc, "near %s", wf_info[feat].name); + desc += fmt::format("near {}", wf_info[feat].name); } else { @@ -2409,7 +2411,8 @@ cptr describe_player_location() if (dy * 81 < dx * 31) ns = ""; if (dx * 81 < dy * 31) ew = ""; - sprintf(desc, "in %s %s%s of %s", + desc += fmt::format( + "in {} {}{} of {}", wf_info[feat].text, ns, ew, @@ -2417,10 +2420,8 @@ cptr describe_player_location() } } - /* strip trailing whitespace */ - for (i = 0; desc[i]; ++i); - while (desc[--i] == ' ') - desc[i] = 0; + /* Strip trailing whitespace */ + boost::trim_right(desc); return desc; } @@ -2724,7 +2725,10 @@ errr file_character(cptr name, bool_ full) } /* Where we are, if we're alive */ - if (!death) fprintf(fff, "\n You are currently %s.", describe_player_location()); + if (!death) + { + fprintf(fff, "\n You are currently %s.", describe_player_location().c_str()); + } /* Monsters slain */ { diff --git a/src/files.hpp b/src/files.hpp index 35a10079..bd39c759 100644 --- a/src/files.hpp +++ b/src/files.hpp @@ -13,7 +13,7 @@ extern object_flag_set player_flags(); extern void wipe_saved(void); extern s16b tokenize(char *buf, s16b num, char **tokens, char delim1, char delim2); extern void display_player(int mode); -extern cptr describe_player_location(void); +extern std::string describe_player_location(); extern errr file_character(cptr name, bool_ full); extern errr process_pref_file_aux(char *buf); extern errr process_pref_file(cptr name); diff --git a/src/spells2.cc b/src/spells2.cc index ae7f8aa1..02b51056 100644 --- a/src/spells2.cc +++ b/src/spells2.cc @@ -831,7 +831,7 @@ void self_knowledge(FILE *fff) static char buf[250]; sprintf(buf, "You are dead, killed by %s %s.", - died_from, describe_player_location()); + died_from, describe_player_location().c_str()); info[i++] = buf; } -- cgit v1.2.3