From 63ccb36ef3ecd53a503c0e9fea87f0784802af0f Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Sun, 17 May 2020 23:31:59 +0200 Subject: Codechange: Use std::string for most of the user-settable custom names. --- src/saveload/strings_sl.cpp | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'src/saveload/strings_sl.cpp') diff --git a/src/saveload/strings_sl.cpp b/src/saveload/strings_sl.cpp index dce8fdedb..dad7eb1ab 100644 --- a/src/saveload/strings_sl.cpp +++ b/src/saveload/strings_sl.cpp @@ -11,6 +11,7 @@ #include "../string_func.h" #include "../strings_func.h" #include "saveload_internal.h" +#include #include "table/strings.h" @@ -56,18 +57,17 @@ char *_old_name_array = nullptr; * @param id the StringID of the custom name to clone. * @return the clones custom name. */ -char *CopyFromOldName(StringID id) +std::string CopyFromOldName(StringID id) { /* Is this name an (old) custom name? */ - if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return nullptr; + if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return std::string(); if (IsSavegameVersionBefore(SLV_37)) { - /* Allow for expansion when converted to UTF-8. */ - char tmp[LEN_OLD_STRINGS * MAX_CHAR_LENGTH]; uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9); const char *strfrom = &_old_name_array[offs]; - char *strto = tmp; + std::ostringstream tmp; + std::ostreambuf_iterator strto(tmp); for (; *strfrom != '\0'; strfrom++) { WChar c = (byte)*strfrom; @@ -84,19 +84,13 @@ char *CopyFromOldName(StringID id) default: break; } - /* Check character will fit into our buffer. */ - if (strto + Utf8CharLen(c) > lastof(tmp)) break; - - strto += Utf8Encode(strto, c); + Utf8Encode(strto, c); } - /* Terminate the new string and copy it back to the name array */ - *strto = '\0'; - - return stredup(tmp); + return tmp.str(); } else { /* Name will already be in UTF-8. */ - return stredup(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]); + return std::string(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]); } } -- cgit v1.2.3-70-g09d2