mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use std::vector instead of AutoDeleteSmallVector in GS text handling.
parent
baf9229931
commit
d3e113eb5f
|
@ -23,6 +23,7 @@
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "../safeguards.h"
|
#include "../safeguards.h"
|
||||||
|
|
||||||
|
@ -80,9 +81,8 @@ LanguageStrings::~LanguageStrings()
|
||||||
* @param file The file to read from.
|
* @param file The file to read from.
|
||||||
* @return The raw strings, or NULL upon error.
|
* @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 {
|
try {
|
||||||
size_t to_read;
|
size_t to_read;
|
||||||
FILE *fh = FioFOpenFile(file, "rb", GAME_DIR, &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 */
|
/* Check for invalid empty filename */
|
||||||
if (*langname == '.' || *langname == 0) return NULL;
|
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];
|
char buffer[2048];
|
||||||
while (to_read != 0 && fgets(buffer, sizeof(buffer), fh) != NULL) {
|
while (to_read != 0 && fgets(buffer, sizeof(buffer), fh) != NULL) {
|
||||||
|
@ -122,7 +122,6 @@ LanguageStrings *ReadRawLanguageStrings(const char *file)
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
delete ret;
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,8 +139,8 @@ struct StringListReader : StringReader {
|
||||||
* @param master Are we reading the master file?
|
* @param master Are we reading the master file?
|
||||||
* @param translation Are we reading a translation?
|
* @param translation Are we reading a translation?
|
||||||
*/
|
*/
|
||||||
StringListReader(StringData &data, const LanguageStrings *strings, bool master, bool 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.data()), end(p + strings.lines.size())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,7 +308,7 @@ GameStrings *LoadTranslations()
|
||||||
void GameStrings::Compile()
|
void GameStrings::Compile()
|
||||||
{
|
{
|
||||||
StringData data(1);
|
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();
|
master_reader.ParseFile();
|
||||||
if (_errors != 0) throw std::exception();
|
if (_errors != 0) throw std::exception();
|
||||||
|
|
||||||
|
@ -318,13 +317,13 @@ void GameStrings::Compile()
|
||||||
StringNameWriter id_writer(&this->string_names);
|
StringNameWriter id_writer(&this->string_names);
|
||||||
id_writer.WriteHeader(data);
|
id_writer.WriteHeader(data);
|
||||||
|
|
||||||
for (LanguageStrings *p : this->raw_strings) {
|
for (const auto &p : this->raw_strings) {
|
||||||
data.FreeTranslation();
|
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();
|
translation_reader.ParseFile();
|
||||||
if (_errors != 0) throw std::exception();
|
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);
|
TranslationWriter writer(&this->compiled_strings.back()->lines);
|
||||||
writer.WriteLang(data);
|
writer.WriteLang(data);
|
||||||
}
|
}
|
||||||
|
@ -392,7 +391,7 @@ void ReconsiderGameScriptLanguage()
|
||||||
assert(language != NULL);
|
assert(language != NULL);
|
||||||
language++;
|
language++;
|
||||||
|
|
||||||
for (LanguageStrings *p : _current_data->compiled_strings) {
|
for (auto &p : _current_data->compiled_strings) {
|
||||||
if (strcmp(p->language, language) == 0) {
|
if (strcmp(p->language, language) == 0) {
|
||||||
_current_data->cur_language = p;
|
_current_data->cur_language = p;
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -30,10 +30,10 @@ struct LanguageStrings {
|
||||||
/** Container for all the game strings. */
|
/** Container for all the game strings. */
|
||||||
struct GameStrings {
|
struct GameStrings {
|
||||||
uint version; ///< The version of the language strings.
|
uint version; ///< The version of the language strings.
|
||||||
LanguageStrings *cur_language; ///< The current (compiled) language.
|
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!.
|
std::vector<std::unique_ptr<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!.
|
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.
|
StringList string_names; ///< The names of the compiled strings.
|
||||||
|
|
||||||
void Compile();
|
void Compile();
|
||||||
|
|
|
@ -150,13 +150,13 @@ static void Load_GSTR()
|
||||||
_game_saveload_string = NULL;
|
_game_saveload_string = NULL;
|
||||||
SlObject(NULL, _game_language_header);
|
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++) {
|
for (uint i = 0; i < _game_saveload_strings; i++) {
|
||||||
SlObject(NULL, _game_language_string);
|
SlObject(NULL, _game_language_string);
|
||||||
ls->lines.push_back(stredup(_game_saveload_string != NULL ? _game_saveload_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 */
|
/* 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++) {
|
for (uint i = 0; i < _current_data->raw_strings.size(); i++) {
|
||||||
SlSetArrayIndex(i);
|
SlSetArrayIndex(i);
|
||||||
SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i]);
|
SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i].get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue