mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Move global strgen state into a single struct.
parent
786893a844
commit
e2ebf3a0f3
|
@ -25,19 +25,19 @@
|
||||||
|
|
||||||
void CDECL StrgenWarningI(const std::string &msg)
|
void CDECL StrgenWarningI(const std::string &msg)
|
||||||
{
|
{
|
||||||
Debug(script, 0, "{}:{}: warning: {}", _file, _cur_line, msg);
|
Debug(script, 0, "{}:{}: warning: {}", _strgen.file, _strgen.cur_line, msg);
|
||||||
_warnings++;
|
_strgen.warnings++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CDECL StrgenErrorI(const std::string &msg)
|
void CDECL StrgenErrorI(const std::string &msg)
|
||||||
{
|
{
|
||||||
Debug(script, 0, "{}:{}: error: {}", _file, _cur_line, msg);
|
Debug(script, 0, "{}:{}: error: {}", _strgen.file, _strgen.cur_line, msg);
|
||||||
_errors++;
|
_strgen.errors++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CDECL StrgenFatalI(const std::string &msg)
|
void CDECL StrgenFatalI(const std::string &msg)
|
||||||
{
|
{
|
||||||
Debug(script, 0, "{}:{}: FATAL: {}", _file, _cur_line, msg);
|
Debug(script, 0, "{}:{}: FATAL: {}", _strgen.file, _strgen.cur_line, msg);
|
||||||
throw std::exception();
|
throw std::exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -289,7 +289,7 @@ void GameStrings::Compile()
|
||||||
StringData data(32);
|
StringData data(32);
|
||||||
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 (_strgen.errors != 0) throw std::exception();
|
||||||
|
|
||||||
this->version = data.Version();
|
this->version = data.Version();
|
||||||
|
|
||||||
|
@ -302,7 +302,7 @@ void GameStrings::Compile()
|
||||||
data.FreeTranslation();
|
data.FreeTranslation();
|
||||||
StringListReader translation_reader(data, p, false, p.language != "english");
|
StringListReader translation_reader(data, p, false, p.language != "english");
|
||||||
translation_reader.ParseFile();
|
translation_reader.ParseFile();
|
||||||
if (_errors != 0) throw std::exception();
|
if (_strgen.errors != 0) throw std::exception();
|
||||||
|
|
||||||
auto &strings = this->compiled_strings.emplace_back(p.language);
|
auto &strings = this->compiled_strings.emplace_back(p.language);
|
||||||
TranslationWriter writer(strings.lines);
|
TranslationWriter writer(strings.lines);
|
||||||
|
|
|
@ -35,34 +35,34 @@
|
||||||
|
|
||||||
void StrgenWarningI(const std::string &msg)
|
void StrgenWarningI(const std::string &msg)
|
||||||
{
|
{
|
||||||
if (_translation) {
|
if (_strgen.translation) {
|
||||||
fmt::print(stderr, LINE_NUM_FMT("info"), _file, _cur_line, msg);
|
fmt::print(stderr, LINE_NUM_FMT("info"), _strgen.file, _strgen.cur_line, msg);
|
||||||
} else {
|
} else {
|
||||||
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, msg);
|
fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, msg);
|
||||||
}
|
}
|
||||||
_warnings++;
|
_strgen.warnings++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StrgenErrorI(const std::string &msg)
|
void StrgenErrorI(const std::string &msg)
|
||||||
{
|
{
|
||||||
fmt::print(stderr, LINE_NUM_FMT("error"), _file, _cur_line, msg);
|
fmt::print(stderr, LINE_NUM_FMT("error"), _strgen.file, _strgen.cur_line, msg);
|
||||||
_errors++;
|
_strgen.errors++;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] void StrgenFatalI(const std::string &msg)
|
[[noreturn]] void StrgenFatalI(const std::string &msg)
|
||||||
{
|
{
|
||||||
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
|
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _strgen.file, _strgen.cur_line, msg);
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, "language is not compiled");
|
||||||
#endif
|
#endif
|
||||||
throw std::exception();
|
throw std::exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] void FatalErrorI(const std::string &msg)
|
[[noreturn]] void FatalErrorI(const std::string &msg)
|
||||||
{
|
{
|
||||||
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
|
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _strgen.file, _strgen.cur_line, msg);
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, "language is not compiled");
|
||||||
#endif
|
#endif
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
@ -91,59 +91,59 @@ struct FileStringReader : StringReader {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandlePragma(char *str) override;
|
void HandlePragma(char *str, LanguagePackHeader &lang) override;
|
||||||
|
|
||||||
void ParseFile() override
|
void ParseFile() override
|
||||||
{
|
{
|
||||||
this->StringReader::ParseFile();
|
this->StringReader::ParseFile();
|
||||||
|
|
||||||
if (StrEmpty(_lang.name) || StrEmpty(_lang.own_name) || StrEmpty(_lang.isocode)) {
|
if (StrEmpty(_strgen.lang.name) || StrEmpty(_strgen.lang.own_name) || StrEmpty(_strgen.lang.isocode)) {
|
||||||
FatalError("Language must include ##name, ##ownname and ##isocode");
|
FatalError("Language must include ##name, ##ownname and ##isocode");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void FileStringReader::HandlePragma(char *str)
|
void FileStringReader::HandlePragma(char *str, LanguagePackHeader &lang)
|
||||||
{
|
{
|
||||||
if (!memcmp(str, "id ", 3)) {
|
if (!memcmp(str, "id ", 3)) {
|
||||||
this->data.next_string_id = std::strtoul(str + 3, nullptr, 0);
|
this->data.next_string_id = std::strtoul(str + 3, nullptr, 0);
|
||||||
} else if (!memcmp(str, "name ", 5)) {
|
} else if (!memcmp(str, "name ", 5)) {
|
||||||
strecpy(_lang.name, str + 5);
|
strecpy(lang.name, str + 5);
|
||||||
} else if (!memcmp(str, "ownname ", 8)) {
|
} else if (!memcmp(str, "ownname ", 8)) {
|
||||||
strecpy(_lang.own_name, str + 8);
|
strecpy(lang.own_name, str + 8);
|
||||||
} else if (!memcmp(str, "isocode ", 8)) {
|
} else if (!memcmp(str, "isocode ", 8)) {
|
||||||
strecpy(_lang.isocode, str + 8);
|
strecpy(lang.isocode, str + 8);
|
||||||
} else if (!memcmp(str, "textdir ", 8)) {
|
} else if (!memcmp(str, "textdir ", 8)) {
|
||||||
if (!memcmp(str + 8, "ltr", 3)) {
|
if (!memcmp(str + 8, "ltr", 3)) {
|
||||||
_lang.text_dir = TD_LTR;
|
lang.text_dir = TD_LTR;
|
||||||
} else if (!memcmp(str + 8, "rtl", 3)) {
|
} else if (!memcmp(str + 8, "rtl", 3)) {
|
||||||
_lang.text_dir = TD_RTL;
|
lang.text_dir = TD_RTL;
|
||||||
} else {
|
} else {
|
||||||
FatalError("Invalid textdir {}", str + 8);
|
FatalError("Invalid textdir {}", str + 8);
|
||||||
}
|
}
|
||||||
} else if (!memcmp(str, "digitsep ", 9)) {
|
} else if (!memcmp(str, "digitsep ", 9)) {
|
||||||
str += 9;
|
str += 9;
|
||||||
strecpy(_lang.digit_group_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str);
|
strecpy(lang.digit_group_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str);
|
||||||
} else if (!memcmp(str, "digitsepcur ", 12)) {
|
} else if (!memcmp(str, "digitsepcur ", 12)) {
|
||||||
str += 12;
|
str += 12;
|
||||||
strecpy(_lang.digit_group_separator_currency, strcmp(str, "{NBSP}") == 0 ? NBSP : str);
|
strecpy(lang.digit_group_separator_currency, strcmp(str, "{NBSP}") == 0 ? NBSP : str);
|
||||||
} else if (!memcmp(str, "decimalsep ", 11)) {
|
} else if (!memcmp(str, "decimalsep ", 11)) {
|
||||||
str += 11;
|
str += 11;
|
||||||
strecpy(_lang.digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str);
|
strecpy(lang.digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str);
|
||||||
} else if (!memcmp(str, "winlangid ", 10)) {
|
} else if (!memcmp(str, "winlangid ", 10)) {
|
||||||
const char *buf = str + 10;
|
const char *buf = str + 10;
|
||||||
long langid = std::strtol(buf, nullptr, 16);
|
long langid = std::strtol(buf, nullptr, 16);
|
||||||
if (langid > UINT16_MAX || langid < 0) {
|
if (langid > UINT16_MAX || langid < 0) {
|
||||||
FatalError("Invalid winlangid {}", buf);
|
FatalError("Invalid winlangid {}", buf);
|
||||||
}
|
}
|
||||||
_lang.winlangid = static_cast<uint16_t>(langid);
|
lang.winlangid = static_cast<uint16_t>(langid);
|
||||||
} else if (!memcmp(str, "grflangid ", 10)) {
|
} else if (!memcmp(str, "grflangid ", 10)) {
|
||||||
const char *buf = str + 10;
|
const char *buf = str + 10;
|
||||||
long langid = std::strtol(buf, nullptr, 16);
|
long langid = std::strtol(buf, nullptr, 16);
|
||||||
if (langid >= 0x7F || langid < 0) {
|
if (langid >= 0x7F || langid < 0) {
|
||||||
FatalError("Invalid grflangid {}", buf);
|
FatalError("Invalid grflangid {}", buf);
|
||||||
}
|
}
|
||||||
_lang.newgrflangid = static_cast<uint8_t>(langid);
|
lang.newgrflangid = static_cast<uint8_t>(langid);
|
||||||
} else if (!memcmp(str, "gender ", 7)) {
|
} else if (!memcmp(str, "gender ", 7)) {
|
||||||
if (this->master) FatalError("Genders are not allowed in the base translation.");
|
if (this->master) FatalError("Genders are not allowed in the base translation.");
|
||||||
const char *buf = str + 7;
|
const char *buf = str + 7;
|
||||||
|
@ -152,9 +152,9 @@ void FileStringReader::HandlePragma(char *str)
|
||||||
auto s = ParseWord(&buf);
|
auto s = ParseWord(&buf);
|
||||||
|
|
||||||
if (!s.has_value()) break;
|
if (!s.has_value()) break;
|
||||||
if (_lang.num_genders >= MAX_NUM_GENDERS) FatalError("Too many genders, max {}", MAX_NUM_GENDERS);
|
if (lang.num_genders >= MAX_NUM_GENDERS) FatalError("Too many genders, max {}", MAX_NUM_GENDERS);
|
||||||
s->copy(_lang.genders[_lang.num_genders], CASE_GENDER_LEN - 1);
|
s->copy(lang.genders[lang.num_genders], CASE_GENDER_LEN - 1);
|
||||||
_lang.num_genders++;
|
lang.num_genders++;
|
||||||
}
|
}
|
||||||
} else if (!memcmp(str, "case ", 5)) {
|
} else if (!memcmp(str, "case ", 5)) {
|
||||||
if (this->master) FatalError("Cases are not allowed in the base translation.");
|
if (this->master) FatalError("Cases are not allowed in the base translation.");
|
||||||
|
@ -164,12 +164,12 @@ void FileStringReader::HandlePragma(char *str)
|
||||||
auto s = ParseWord(&buf);
|
auto s = ParseWord(&buf);
|
||||||
|
|
||||||
if (!s.has_value()) break;
|
if (!s.has_value()) break;
|
||||||
if (_lang.num_cases >= MAX_NUM_CASES) FatalError("Too many cases, max {}", MAX_NUM_CASES);
|
if (lang.num_cases >= MAX_NUM_CASES) FatalError("Too many cases, max {}", MAX_NUM_CASES);
|
||||||
s->copy(_lang.cases[_lang.num_cases], CASE_GENDER_LEN - 1);
|
s->copy(lang.cases[lang.num_cases], CASE_GENDER_LEN - 1);
|
||||||
_lang.num_cases++;
|
lang.num_cases++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
StringReader::HandlePragma(str);
|
StringReader::HandlePragma(str, lang);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,11 +364,11 @@ int CDECL main(int argc, char *argv[])
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
_annotate_todos = true;
|
_strgen.annotate_todos = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'w':
|
case 'w':
|
||||||
_show_warnings = true;
|
_strgen.show_warnings = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
|
@ -417,7 +417,7 @@ int CDECL main(int argc, char *argv[])
|
||||||
StringData data(TEXT_TAB_END);
|
StringData data(TEXT_TAB_END);
|
||||||
FileStringReader master_reader(data, input_path, true, false);
|
FileStringReader master_reader(data, input_path, true, false);
|
||||||
master_reader.ParseFile();
|
master_reader.ParseFile();
|
||||||
if (_errors != 0) return 1;
|
if (_strgen.errors != 0) return 1;
|
||||||
|
|
||||||
/* write strings.h */
|
/* write strings.h */
|
||||||
std::filesystem::path output_path = dest_dir;
|
std::filesystem::path output_path = dest_dir;
|
||||||
|
@ -427,7 +427,7 @@ int CDECL main(int argc, char *argv[])
|
||||||
HeaderFileWriter writer(output_path);
|
HeaderFileWriter writer(output_path);
|
||||||
writer.WriteHeader(data);
|
writer.WriteHeader(data);
|
||||||
writer.Finalise(data);
|
writer.Finalise(data);
|
||||||
if (_errors != 0) return 1;
|
if (_strgen.errors != 0) return 1;
|
||||||
} else {
|
} else {
|
||||||
std::filesystem::path input_path = std::move(src_dir);
|
std::filesystem::path input_path = std::move(src_dir);
|
||||||
input_path /= "english.txt";
|
input_path /= "english.txt";
|
||||||
|
@ -443,7 +443,7 @@ int CDECL main(int argc, char *argv[])
|
||||||
std::filesystem::path lang_file = argument;
|
std::filesystem::path lang_file = argument;
|
||||||
FileStringReader translation_reader(data, lang_file, false, lang_file.filename() != "english.txt");
|
FileStringReader translation_reader(data, lang_file, false, lang_file.filename() != "english.txt");
|
||||||
translation_reader.ParseFile(); // target file
|
translation_reader.ParseFile(); // target file
|
||||||
if (_errors != 0) return 1;
|
if (_strgen.errors != 0) return 1;
|
||||||
|
|
||||||
/* get the targetfile, strip any directories and append to destination path */
|
/* get the targetfile, strip any directories and append to destination path */
|
||||||
std::filesystem::path output_file = dest_dir;
|
std::filesystem::path output_file = dest_dir;
|
||||||
|
@ -455,8 +455,8 @@ int CDECL main(int argc, char *argv[])
|
||||||
writer.Finalise();
|
writer.Finalise();
|
||||||
|
|
||||||
/* if showing warnings, print a summary of the language */
|
/* if showing warnings, print a summary of the language */
|
||||||
if (_show_warnings) {
|
if (_strgen.show_warnings) {
|
||||||
fmt::print("{} warnings and {} errors for {}\n", _warnings, _errors, output_file);
|
fmt::print("{} warnings and {} errors for {}\n", _strgen.warnings, _strgen.errors, output_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,7 +74,7 @@ struct StringReader {
|
||||||
* Handle the pragma of the file.
|
* Handle the pragma of the file.
|
||||||
* @param str The pragma string to parse.
|
* @param str The pragma string to parse.
|
||||||
*/
|
*/
|
||||||
virtual void HandlePragma(char *str);
|
virtual void HandlePragma(char *str, LanguagePackHeader &lang);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start parsing the file.
|
* Start parsing the file.
|
||||||
|
@ -154,10 +154,17 @@ void StrgenErrorI(const std::string &msg);
|
||||||
#define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
|
#define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
|
||||||
std::optional<std::string_view> ParseWord(const char **buf);
|
std::optional<std::string_view> ParseWord(const char **buf);
|
||||||
|
|
||||||
extern const char *_file;
|
/** Global state shared between strgen.cpp, game_text.cpp and strgen_base.cpp */
|
||||||
extern size_t _cur_line;
|
struct StrgenState {
|
||||||
extern size_t _errors, _warnings;
|
std::string file = "(unknown file)"; ///< The filename of the input, so we can refer to it in errors/warnings
|
||||||
extern bool _show_warnings, _annotate_todos, _translation;
|
size_t cur_line = 0; ///< The current line we're parsing in the input file
|
||||||
extern LanguagePackHeader _lang;
|
size_t errors = 0;
|
||||||
|
size_t warnings = 0;
|
||||||
|
bool show_warnings = false;
|
||||||
|
bool annotate_todos = false;
|
||||||
|
bool translation = false; ///< Is the current file actually a translation or not
|
||||||
|
LanguagePackHeader lang; ///< Header information about a language.
|
||||||
|
};
|
||||||
|
extern StrgenState _strgen;
|
||||||
|
|
||||||
#endif /* STRGEN_H */
|
#endif /* STRGEN_H */
|
||||||
|
|
|
@ -21,13 +21,8 @@
|
||||||
|
|
||||||
#include "../safeguards.h"
|
#include "../safeguards.h"
|
||||||
|
|
||||||
|
StrgenState _strgen;
|
||||||
static bool _translated; ///< Whether the current language is not the master language
|
static bool _translated; ///< Whether the current language is not the master language
|
||||||
bool _translation; ///< Is the current file actually a translation or not
|
|
||||||
const char *_file = "(unknown file)"; ///< The filename of the input, so we can refer to it in errors/warnings
|
|
||||||
size_t _cur_line; ///< The current line we're parsing in the input file
|
|
||||||
size_t _errors, _warnings;
|
|
||||||
bool _show_warnings = false, _annotate_todos = false;
|
|
||||||
LanguagePackHeader _lang; ///< Header information about a language.
|
|
||||||
static const char *_cur_ident;
|
static const char *_cur_ident;
|
||||||
static ParsedCommandStruct _cur_pcs;
|
static ParsedCommandStruct _cur_pcs;
|
||||||
static size_t _cur_argidx;
|
static size_t _cur_argidx;
|
||||||
|
@ -297,13 +292,13 @@ void EmitPlural(StringBuilder &builder, const char *buf, char32_t)
|
||||||
StrgenFatal("{}: No plural words", _cur_ident);
|
StrgenFatal("{}: No plural words", _cur_ident);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t expected = _plural_forms[_lang.plural_form].plural_count;
|
size_t expected = _plural_forms[_strgen.lang.plural_form].plural_count;
|
||||||
if (expected != words.size()) {
|
if (expected != words.size()) {
|
||||||
if (_translated) {
|
if (_translated) {
|
||||||
StrgenFatal("{}: Invalid number of plural forms. Expecting {}, found {}.", _cur_ident,
|
StrgenFatal("{}: Invalid number of plural forms. Expecting {}, found {}.", _cur_ident,
|
||||||
expected, words.size());
|
expected, words.size());
|
||||||
} else {
|
} else {
|
||||||
if (_show_warnings) StrgenWarning("'{}' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
|
if (_strgen.show_warnings) StrgenWarning("'{}' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
|
||||||
if (words.size() > expected) {
|
if (words.size() > expected) {
|
||||||
words.resize(expected);
|
words.resize(expected);
|
||||||
} else {
|
} else {
|
||||||
|
@ -315,7 +310,7 @@ void EmitPlural(StringBuilder &builder, const char *buf, char32_t)
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.PutUtf8(SCC_PLURAL_LIST);
|
builder.PutUtf8(SCC_PLURAL_LIST);
|
||||||
builder.PutUint8(_lang.plural_form);
|
builder.PutUint8(_strgen.lang.plural_form);
|
||||||
builder.PutUint8(static_cast<uint8_t>(TranslateArgumentIdx(*argidx, *offset)));
|
builder.PutUint8(static_cast<uint8_t>(TranslateArgumentIdx(*argidx, *offset)));
|
||||||
EmitWordList(builder, words);
|
EmitWordList(builder, words);
|
||||||
}
|
}
|
||||||
|
@ -326,7 +321,7 @@ void EmitGender(StringBuilder &builder, const char *buf, char32_t)
|
||||||
buf++;
|
buf++;
|
||||||
|
|
||||||
/* This is a {G=DER} command */
|
/* This is a {G=DER} command */
|
||||||
auto nw = _lang.GetGenderIndex(buf);
|
auto nw = _strgen.lang.GetGenderIndex(buf);
|
||||||
if (nw >= MAX_NUM_GENDERS) StrgenFatal("G argument '{}' invalid", buf);
|
if (nw >= MAX_NUM_GENDERS) StrgenFatal("G argument '{}' invalid", buf);
|
||||||
|
|
||||||
/* now nw contains the gender index */
|
/* now nw contains the gender index */
|
||||||
|
@ -350,7 +345,7 @@ void EmitGender(StringBuilder &builder, const char *buf, char32_t)
|
||||||
if (!word.has_value()) break;
|
if (!word.has_value()) break;
|
||||||
words.emplace_back(*word);
|
words.emplace_back(*word);
|
||||||
}
|
}
|
||||||
if (words.size() != _lang.num_genders) StrgenFatal("Bad # of arguments for gender command");
|
if (words.size() != _strgen.lang.num_genders) StrgenFatal("Bad # of arguments for gender command");
|
||||||
|
|
||||||
assert(IsInsideBS(cmd->value, SCC_CONTROL_START, UINT8_MAX));
|
assert(IsInsideBS(cmd->value, SCC_CONTROL_START, UINT8_MAX));
|
||||||
builder.PutUtf8(SCC_GENDER_LIST);
|
builder.PutUtf8(SCC_GENDER_LIST);
|
||||||
|
@ -369,7 +364,7 @@ static const CmdStruct *FindCmd(std::string_view s)
|
||||||
|
|
||||||
static uint8_t ResolveCaseName(std::string_view str)
|
static uint8_t ResolveCaseName(std::string_view str)
|
||||||
{
|
{
|
||||||
uint8_t case_idx = _lang.GetCaseIndex(str);
|
uint8_t case_idx = _strgen.lang.GetCaseIndex(str);
|
||||||
if (case_idx >= MAX_NUM_CASES) StrgenFatal("Invalid case-name '{}'", str);
|
if (case_idx >= MAX_NUM_CASES) StrgenFatal("Invalid case-name '{}'", str);
|
||||||
return case_idx + 1;
|
return case_idx + 1;
|
||||||
}
|
}
|
||||||
|
@ -510,7 +505,7 @@ static bool CheckCommandsMatch(const char *a, const char *b, const char *name)
|
||||||
* it is pointless to do all these checks as it'll always be correct.
|
* it is pointless to do all these checks as it'll always be correct.
|
||||||
* After all, all checks are based on the base language.
|
* After all, all checks are based on the base language.
|
||||||
*/
|
*/
|
||||||
if (!_translation) return true;
|
if (!_strgen.translation) return true;
|
||||||
|
|
||||||
bool result = true;
|
bool result = true;
|
||||||
|
|
||||||
|
@ -558,7 +553,7 @@ static bool CheckCommandsMatch(const char *a, const char *b, const char *name)
|
||||||
void StringReader::HandleString(char *str)
|
void StringReader::HandleString(char *str)
|
||||||
{
|
{
|
||||||
if (*str == '#') {
|
if (*str == '#') {
|
||||||
if (str[1] == '#' && str[2] != '#') this->HandlePragma(str + 2);
|
if (str[1] == '#' && str[2] != '#') this->HandlePragma(str + 2, _strgen.lang);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -621,7 +616,7 @@ void StringReader::HandleString(char *str)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate a new LangString */
|
/* Allocate a new LangString */
|
||||||
this->data.Add(std::make_unique<LangString>(str, s, this->data.next_string_id++, _cur_line));
|
this->data.Add(std::make_unique<LangString>(str, s, this->data.next_string_id++, _strgen.cur_line));
|
||||||
} else {
|
} else {
|
||||||
if (ent == nullptr) {
|
if (ent == nullptr) {
|
||||||
StrgenWarning("String name '{}' does not exist in master file", str);
|
StrgenWarning("String name '{}' does not exist in master file", str);
|
||||||
|
@ -643,17 +638,17 @@ void StringReader::HandleString(char *str)
|
||||||
/* If the string was translated, use the line from the
|
/* If the string was translated, use the line from the
|
||||||
* translated language so errors in the translated file
|
* translated language so errors in the translated file
|
||||||
* are properly referenced to. */
|
* are properly referenced to. */
|
||||||
ent->line = _cur_line;
|
ent->line = _strgen.cur_line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringReader::HandlePragma(char *str)
|
void StringReader::HandlePragma(char *str, LanguagePackHeader &lang)
|
||||||
{
|
{
|
||||||
if (!memcmp(str, "plural ", 7)) {
|
if (!memcmp(str, "plural ", 7)) {
|
||||||
_lang.plural_form = atoi(str + 7);
|
lang.plural_form = atoi(str + 7);
|
||||||
if (_lang.plural_form >= lengthof(_plural_forms)) {
|
if (lang.plural_form >= lengthof(_plural_forms)) {
|
||||||
StrgenFatal("Invalid pluralform {}", _lang.plural_form);
|
StrgenFatal("Invalid pluralform {}", lang.plural_form);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
StrgenFatal("unknown pragma '{}'", str);
|
StrgenFatal("unknown pragma '{}'", str);
|
||||||
|
@ -667,25 +662,25 @@ static void StripTrailingWhitespace(std::string &str)
|
||||||
|
|
||||||
void StringReader::ParseFile()
|
void StringReader::ParseFile()
|
||||||
{
|
{
|
||||||
_warnings = _errors = 0;
|
_strgen.warnings = _strgen.errors = 0;
|
||||||
|
|
||||||
_translation = this->translation;
|
_strgen.translation = this->translation;
|
||||||
_file = this->file.c_str();
|
_strgen.file = this->file;
|
||||||
|
|
||||||
/* For each new file we parse, reset the genders, and language codes. */
|
/* For each new file we parse, reset the genders, and language codes. */
|
||||||
MemSetT(&_lang, 0);
|
MemSetT(&_strgen.lang, 0);
|
||||||
strecpy(_lang.digit_group_separator, ",");
|
strecpy(_strgen.lang.digit_group_separator, ",");
|
||||||
strecpy(_lang.digit_group_separator_currency, ",");
|
strecpy(_strgen.lang.digit_group_separator_currency, ",");
|
||||||
strecpy(_lang.digit_decimal_separator, ".");
|
strecpy(_strgen.lang.digit_decimal_separator, ".");
|
||||||
|
|
||||||
_cur_line = 1;
|
_strgen.cur_line = 1;
|
||||||
while (this->data.next_string_id < this->data.max_strings) {
|
while (this->data.next_string_id < this->data.max_strings) {
|
||||||
std::optional<std::string> line = this->ReadLine();
|
std::optional<std::string> line = this->ReadLine();
|
||||||
if (!line.has_value()) return;
|
if (!line.has_value()) return;
|
||||||
|
|
||||||
StripTrailingWhitespace(line.value());
|
StripTrailingWhitespace(line.value());
|
||||||
this->HandleString(line.value().data());
|
this->HandleString(line.value().data());
|
||||||
_cur_line++;
|
_strgen.cur_line++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->data.next_string_id == this->data.max_strings) {
|
if (this->data.next_string_id == this->data.max_strings) {
|
||||||
|
@ -812,20 +807,20 @@ void LanguageWriter::WriteLang(const StringData &data)
|
||||||
size_t n = data.CountInUse(tab);
|
size_t n = data.CountInUse(tab);
|
||||||
|
|
||||||
in_use.push_back(n);
|
in_use.push_back(n);
|
||||||
_lang.offsets[tab] = TO_LE16(static_cast<uint16_t>(n));
|
_strgen.lang.offsets[tab] = TO_LE16(static_cast<uint16_t>(n));
|
||||||
|
|
||||||
for (size_t j = 0; j != in_use[tab]; j++) {
|
for (size_t j = 0; j != in_use[tab]; j++) {
|
||||||
const LangString *ls = data.strings[(tab * TAB_SIZE) + j].get();
|
const LangString *ls = data.strings[(tab * TAB_SIZE) + j].get();
|
||||||
if (ls != nullptr && ls->translated.empty()) _lang.missing++;
|
if (ls != nullptr && ls->translated.empty()) _strgen.lang.missing++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_lang.ident = TO_LE32(LanguagePackHeader::IDENT);
|
_strgen.lang.ident = TO_LE32(LanguagePackHeader::IDENT);
|
||||||
_lang.version = TO_LE32(data.Version());
|
_strgen.lang.version = TO_LE32(data.Version());
|
||||||
_lang.missing = TO_LE16(_lang.missing);
|
_strgen.lang.missing = TO_LE16(_strgen.lang.missing);
|
||||||
_lang.winlangid = TO_LE16(_lang.winlangid);
|
_strgen.lang.winlangid = TO_LE16(_strgen.lang.winlangid);
|
||||||
|
|
||||||
this->WriteHeader(&_lang);
|
this->WriteHeader(&_strgen.lang);
|
||||||
|
|
||||||
for (size_t tab = 0; tab < data.tabs; tab++) {
|
for (size_t tab = 0; tab < data.tabs; tab++) {
|
||||||
for (size_t j = 0; j != in_use[tab]; j++) {
|
for (size_t j = 0; j != in_use[tab]; j++) {
|
||||||
|
@ -840,14 +835,14 @@ void LanguageWriter::WriteLang(const StringData &data)
|
||||||
std::string output;
|
std::string output;
|
||||||
StringBuilder builder(output);
|
StringBuilder builder(output);
|
||||||
_cur_ident = ls->name.c_str();
|
_cur_ident = ls->name.c_str();
|
||||||
_cur_line = ls->line;
|
_strgen.cur_line = ls->line;
|
||||||
|
|
||||||
/* Produce a message if a string doesn't have a translation. */
|
/* Produce a message if a string doesn't have a translation. */
|
||||||
if (ls->translated.empty()) {
|
if (ls->translated.empty()) {
|
||||||
if (_show_warnings) {
|
if (_strgen.show_warnings) {
|
||||||
StrgenWarning("'{}' is untranslated", ls->name);
|
StrgenWarning("'{}' is untranslated", ls->name);
|
||||||
}
|
}
|
||||||
if (_annotate_todos) {
|
if (_strgen.annotate_todos) {
|
||||||
builder.Put("<TODO> ");
|
builder.Put("<TODO> ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue