diff options
author | Michael Lutz <michi@icosahedron.de> | 2019-04-02 21:31:17 +0200 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2019-04-09 22:45:15 +0200 |
commit | d3e113eb5f618ce0174fa0dfa2591cb96e999350 (patch) | |
tree | c26b96f07fa2f5f20c0eefc4b7116fa65bb51ae0 /src/game | |
parent | baf9229931e4d5a3479892007e9bcc875bc9930b (diff) | |
download | openttd-d3e113eb5f618ce0174fa0dfa2591cb96e999350.tar.xz |
Codechange: Use std::vector instead of AutoDeleteSmallVector in GS text handling.
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/game_text.cpp | 21 | ||||
-rw-r--r-- | src/game/game_text.hpp | 10 |
2 files changed, 15 insertions, 16 deletions
diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp index 5016f967d..26498338c 100644 --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -23,6 +23,7 @@ #include "table/strings.h" #include <stdarg.h> +#include <memory> #include "../safeguards.h" @@ -80,9 +81,8 @@ LanguageStrings::~LanguageStrings() * @param file The file to read from. * @return The raw strings, or NULL upon error. */ -LanguageStrings *ReadRawLanguageStrings(const char *file) +std::unique_ptr<LanguageStrings> ReadRawLanguageStrings(const char *file) { - LanguageStrings *ret = NULL; try { size_t to_read; FILE *fh = FioFOpenFile(file, "rb", GAME_DIR, &to_read); @@ -100,7 +100,7 @@ LanguageStrings *ReadRawLanguageStrings(const char *file) /* Check for invalid empty filename */ if (*langname == '.' || *langname == 0) return NULL; - ret = new LanguageStrings(langname, strchr(langname, '.')); + std::unique_ptr<LanguageStrings> ret(new LanguageStrings(langname, strchr(langname, '.'))); char buffer[2048]; while (to_read != 0 && fgets(buffer, sizeof(buffer), fh) != NULL) { @@ -122,7 +122,6 @@ LanguageStrings *ReadRawLanguageStrings(const char *file) return ret; } catch (...) { - delete ret; return NULL; } } @@ -140,8 +139,8 @@ struct StringListReader : StringReader { * @param master Are we reading the master file? * @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()) + 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()) { } @@ -309,7 +308,7 @@ GameStrings *LoadTranslations() void GameStrings::Compile() { StringData data(1); - StringListReader master_reader(data, this->raw_strings[0], true, false); + StringListReader master_reader(data, *this->raw_strings[0], true, false); master_reader.ParseFile(); if (_errors != 0) throw std::exception(); @@ -318,13 +317,13 @@ void GameStrings::Compile() StringNameWriter id_writer(&this->string_names); id_writer.WriteHeader(data); - for (LanguageStrings *p : this->raw_strings) { + for (const auto &p : this->raw_strings) { data.FreeTranslation(); - StringListReader translation_reader(data, p, false, strcmp(p->language, "english") != 0); + StringListReader translation_reader(data, *p, false, strcmp(p->language, "english") != 0); translation_reader.ParseFile(); if (_errors != 0) throw std::exception(); - this->compiled_strings.push_back(new LanguageStrings(p->language)); + this->compiled_strings.emplace_back(new LanguageStrings(p->language)); TranslationWriter writer(&this->compiled_strings.back()->lines); writer.WriteLang(data); } @@ -392,7 +391,7 @@ void ReconsiderGameScriptLanguage() assert(language != NULL); language++; - for (LanguageStrings *p : _current_data->compiled_strings) { + for (auto &p : _current_data->compiled_strings) { if (strcmp(p->language, language) == 0) { _current_data->cur_language = p; return; diff --git a/src/game/game_text.hpp b/src/game/game_text.hpp index 11c63b5ab..d199a5281 100644 --- a/src/game/game_text.hpp +++ b/src/game/game_text.hpp @@ -29,12 +29,12 @@ struct LanguageStrings { /** Container for all the game strings. */ struct GameStrings { - uint version; ///< The version of the language strings. - LanguageStrings *cur_language; ///< The current (compiled) language. + uint version; ///< The version of the language strings. + std::shared_ptr<LanguageStrings> cur_language; ///< The current (compiled) language. - AutoDeleteSmallVector<LanguageStrings *> raw_strings; ///< The raw strings per language, first must be English/the master language!. - AutoDeleteSmallVector<LanguageStrings *> compiled_strings; ///< The compiled strings per language, first must be English/the master language!. - StringList string_names; ///< The names of the compiled strings. + std::vector<std::unique_ptr<LanguageStrings>> raw_strings; ///< The raw strings per language, first must be English/the master language!. + std::vector<std::shared_ptr<LanguageStrings>> compiled_strings; ///< The compiled strings per language, first must be English/the master language!. + StringList string_names; ///< The names of the compiled strings. void Compile(); }; |