From 65cbde4b30f8fdf6d4cf1196f6a596a5550c9aee Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Wed, 28 Apr 2021 16:46:24 +0200 Subject: Codechange: move currency settings to std::string --- src/newgrf.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/newgrf.cpp') diff --git a/src/newgrf.cpp b/src/newgrf.cpp index c372f033a..3473cbdc2 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -2587,6 +2587,22 @@ static ChangeInfoResult LoadTranslationTable(uint gvid, int numinfo, ByteReader return CIR_SUCCESS; } +/** + * Helper to read a DWord worth of bytes from the reader + * and to return it as a valid string. + * @param reader The source of the DWord. + * @return The read DWord as string. + */ +static std::string ReadDWordAsString(ByteReader *reader) +{ + char output[5]; + for (int i = 0; i < 4; i++) output[i] = reader->ReadByte(); + output[4] = '\0'; + str_validate(output, lastof(output)); + + return std::string(output); +} + /** * Define properties for global variables * @param gvid ID of the global variable. @@ -2674,11 +2690,10 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By case 0x0D: { // Currency prefix symbol uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); - uint32 tempfix = buf->ReadDWord(); + std::string prefix = ReadDWordAsString(buf); if (curidx < CURRENCY_END) { - memcpy(_currency_specs[curidx].prefix, &tempfix, 4); - _currency_specs[curidx].prefix[4] = 0; + _currency_specs[curidx].prefix = prefix; } else { grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx); } @@ -2687,11 +2702,10 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By case 0x0E: { // Currency suffix symbol uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); - uint32 tempfix = buf->ReadDWord(); + std::string suffix = ReadDWordAsString(buf); if (curidx < CURRENCY_END) { - memcpy(&_currency_specs[curidx].suffix, &tempfix, 4); - _currency_specs[curidx].suffix[4] = 0; + _currency_specs[curidx].suffix = suffix; } else { grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx); } -- cgit v1.2.3-54-g00ecf