# HG changeset patch # User Michael Lutz # Date 2019-04-02 19:31:17 # Node ID 674884acc47e39f44d904914cf4c979797a22703 # Parent b4bf61c1f7870b8469c1da317eafcefea76d8066 Codechange: Use std::vector instead of AutoDeleteSmallVector in GS text handling. diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -23,6 +23,7 @@ #include "table/strings.h" #include +#include #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 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( /* Check for invalid empty filename */ if (*langname == '.' || *langname == 0) return NULL; - ret = new LanguageStrings(langname, strchr(langname, '.')); + std::unique_ptr 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( 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 --- 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 cur_language; ///< The current (compiled) language. - AutoDeleteSmallVector raw_strings; ///< The raw strings per language, first must be English/the master language!. - AutoDeleteSmallVector 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> raw_strings; ///< The raw strings per language, first must be English/the master language!. + std::vector> 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(); }; diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -150,13 +150,13 @@ static void Load_GSTR() _game_saveload_string = NULL; SlObject(NULL, _game_language_header); - LanguageStrings *ls = new LanguageStrings(_game_saveload_string != NULL ? _game_saveload_string : ""); + std::unique_ptr ls(new LanguageStrings(_game_saveload_string != NULL ? _game_saveload_string : "")); for (uint i = 0; i < _game_saveload_strings; i++) { SlObject(NULL, _game_language_string); ls->lines.push_back(stredup(_game_saveload_string != NULL ? _game_saveload_string : "")); } - _current_data->raw_strings.push_back(ls); + _current_data->raw_strings.push_back(std::move(ls)); } /* If there were no strings in the savegame, set GameStrings to NULL */ @@ -176,7 +176,7 @@ static void Save_GSTR() for (uint i = 0; i < _current_data->raw_strings.size(); i++) { SlSetArrayIndex(i); - SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i]); + SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i].get()); } }