summaryrefslogtreecommitdiff
path: root/src/newgrf.cpp
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-04-28 16:46:24 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-13 23:13:17 +0200
commit65cbde4b30f8fdf6d4cf1196f6a596a5550c9aee (patch)
tree180f29871d8401d18dce5ae92f196832b34fe0bc /src/newgrf.cpp
parent2022e3482417eceeb1045d01c2aa64db42f03f08 (diff)
downloadopenttd-65cbde4b30f8fdf6d4cf1196f6a596a5550c9aee.tar.xz
Codechange: move currency settings to std::string
Diffstat (limited to 'src/newgrf.cpp')
-rw-r--r--src/newgrf.cpp26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index c372f033a..3473cbdc2 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -2588,6 +2588,22 @@ static ChangeInfoResult LoadTranslationTable(uint gvid, int numinfo, ByteReader
}
/**
+ * 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.
* @param numinfo Number of subsequent IDs to change the property for.
@@ -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);
}