Codechange: Use std::vector instead of AutoDeleteSmallVector in GS text handling.

This commit is contained in:
Michael Lutz 2019-04-02 21:31:17 +02:00
parent baf9229931
commit d3e113eb5f
3 changed files with 18 additions and 19 deletions

View File

@ -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;

View File

@ -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();
};

View File

@ -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<LanguageStrings> 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());
}
}