summaryrefslogtreecommitdiff
path: root/src/game/game_text.cpp
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2019-04-02 21:31:33 +0200
committerMichael Lutz <michi@icosahedron.de>2019-04-09 22:45:15 +0200
commite804173595d49a537503ea08bec4663117bae047 (patch)
treeaca1af9b44daefab8ded671615d87e08a3a96059 /src/game/game_text.cpp
parentc7b9987d081ae4e0103309b18c93deecc395dec9 (diff)
downloadopenttd-e804173595d49a537503ea08bec4663117bae047.tar.xz
Codechange: If something is a vector of strings, use a vector of strings instead of an AutoFreeSmallVector.
Diffstat (limited to 'src/game/game_text.cpp')
-rw-r--r--src/game/game_text.cpp35
1 files changed, 16 insertions, 19 deletions
diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp
index 26498338c..944bf82d1 100644
--- a/src/game/game_text.cpp
+++ b/src/game/game_text.cpp
@@ -111,7 +111,7 @@ std::unique_ptr<LanguageStrings> ReadRawLanguageStrings(const char *file)
while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--;
buffer[i] = '\0';
- ret->lines.push_back(stredup(buffer, buffer + to_read - 1));
+ ret->lines.emplace_back(buffer, buffer + to_read - 1);
if (len > to_read) {
to_read = 0;
@@ -129,8 +129,8 @@ std::unique_ptr<LanguageStrings> ReadRawLanguageStrings(const char *file)
/** A reader that simply reads using fopen. */
struct StringListReader : StringReader {
- const char * const *p; ///< The current location of the iteration.
- const char * const *end; ///< The end of the iteration.
+ StringList::const_iterator p; ///< The current location of the iteration.
+ StringList::const_iterator end; ///< The end of the iteration.
/**
* Create the reader.
@@ -140,7 +140,7 @@ struct StringListReader : StringReader {
* @param translation Are we reading a translation?
*/
StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation) :
- StringReader(data, strings.language, master, translation), p(strings.lines.data()), end(p + strings.lines.size())
+ StringReader(data, strings.language, master, translation), p(strings.lines.begin()), end(strings.lines.end())
{
}
@@ -148,7 +148,7 @@ struct StringListReader : StringReader {
{
if (this->p == this->end) return NULL;
- strecpy(buffer, *this->p, last);
+ strecpy(buffer, this->p->c_str(), last);
this->p++;
return buffer;
@@ -157,13 +157,13 @@ struct StringListReader : StringReader {
/** Class for writing an encoded language. */
struct TranslationWriter : LanguageWriter {
- StringList *strings; ///< The encoded strings.
+ StringList &strings; ///< The encoded strings.
/**
* Writer for the encoded data.
* @param strings The string table to add the strings to.
*/
- TranslationWriter(StringList *strings) : strings(strings)
+ TranslationWriter(StringList &strings) : strings(strings)
{
}
@@ -184,28 +184,25 @@ struct TranslationWriter : LanguageWriter {
void Write(const byte *buffer, size_t length)
{
- char *dest = MallocT<char>(length + 1);
- memcpy(dest, buffer, length);
- dest[length] = '\0';
- this->strings->push_back(dest);
+ this->strings.emplace_back((const char *)buffer, length);
}
};
/** Class for writing the string IDs. */
struct StringNameWriter : HeaderWriter {
- StringList *strings; ///< The string names.
+ StringList &strings; ///< The string names.
/**
* Writer for the string names.
* @param strings The string table to add the strings to.
*/
- StringNameWriter(StringList *strings) : strings(strings)
+ StringNameWriter(StringList &strings) : strings(strings)
{
}
void WriteStringID(const char *name, int stringid)
{
- if (stringid == (int)this->strings->size()) this->strings->push_back(stredup(name));
+ if (stringid == (int)this->strings.size()) this->strings.emplace_back(name);
}
void Finalise(const StringData &data)
@@ -314,7 +311,7 @@ void GameStrings::Compile()
this->version = data.Version();
- StringNameWriter id_writer(&this->string_names);
+ StringNameWriter id_writer(this->string_names);
id_writer.WriteHeader(data);
for (const auto &p : this->raw_strings) {
@@ -324,7 +321,7 @@ void GameStrings::Compile()
if (_errors != 0) throw std::exception();
this->compiled_strings.emplace_back(new LanguageStrings(p->language));
- TranslationWriter writer(&this->compiled_strings.back()->lines);
+ TranslationWriter writer(this->compiled_strings.back()->lines);
writer.WriteLang(data);
}
}
@@ -340,7 +337,7 @@ GameStrings *_current_data = NULL;
const char *GetGameStringPtr(uint id)
{
if (id >= _current_data->cur_language->lines.size()) return GetStringPtr(STR_UNDEFINED);
- return _current_data->cur_language->lines[id];
+ return _current_data->cur_language->lines[id].c_str();
}
/**
@@ -359,8 +356,8 @@ void RegisterGameTranslation(Squirrel *engine)
if (SQ_FAILED(sq_get(vm, -2))) return;
int idx = 0;
- for (const char * const p : _current_data->string_names) {
- sq_pushstring(vm, p, -1);
+ for (const auto &p : _current_data->string_names) {
+ sq_pushstring(vm, p.c_str(), -1);
sq_pushinteger(vm, idx);
sq_rawset(vm, -3);
idx++;