OpenTTD Source 20260206-master-g4d4e37dbf1
strgen_base.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
12#include "../core/math_func.hpp"
13#include "../error_func.h"
14#include "../string_func.h"
17
18#include "strgen.h"
19
21
22#include "../safeguards.h"
23
24StrgenState _strgen;
25static bool _translated;
26static std::string_view _cur_ident;
27static ParsedCommandStruct _cur_pcs;
28static size_t _cur_argidx;
29
31 const CmdStruct *cmd = nullptr;
32 std::string param;
33 std::optional<size_t> argno;
34 std::optional<uint8_t> casei;
35};
36static ParsedCommandString ParseCommandString(StringConsumer &consumer);
37static size_t TranslateArgumentIdx(size_t arg, size_t offset = 0);
38
44Case::Case(uint8_t caseidx, std::string_view string) :
46{
47}
48
56LangString::LangString(std::string_view name, std::string_view english, size_t index, size_t line) :
58{
59}
60
63{
64 this->translated.clear();
65 this->translated_cases.clear();
66}
67
73{
74 this->strings.resize(max_strings);
75 this->next_string_id = 0;
76}
77
80{
81 for (size_t i = 0; i < this->max_strings; i++) {
82 LangString *ls = this->strings[i].get();
83 if (ls != nullptr) ls->FreeTranslation();
84 }
85}
86
91void StringData::Add(std::shared_ptr<LangString> ls)
92{
93 this->name_to_string[ls->name] = ls;
94 this->strings[ls->index] = std::move(ls);
95}
96
102LangString *StringData::Find(std::string_view s)
103{
104 auto it = this->name_to_string.find(s);
105 if (it == this->name_to_string.end()) return nullptr;
106
107 return it->second.get();
108}
109
116static uint32_t VersionHashStr(uint32_t hash, std::string_view s)
117{
118 for (auto c : s) {
119 hash = std::rotl(hash, 3) ^ c;
120 hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
121 }
122 return hash;
123}
124
129uint32_t StringData::Version() const
130{
131 uint32_t hash = 0;
132
133 for (size_t i = 0; i < this->max_strings; i++) {
134 const LangString *ls = this->strings[i].get();
135
136 if (ls != nullptr) {
137 hash ^= i * 0x717239;
138 hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
139 hash = VersionHashStr(hash, ls->name);
140
141 StringConsumer consumer(ls->english);
143 while ((cs = ParseCommandString(consumer)).cmd != nullptr) {
144 if (cs.cmd->flags.Test(CmdFlag::DontCount)) continue;
145
146 hash ^= (cs.cmd - _cmd_structs) * 0x1234567;
147 hash = (hash & 1 ? hash >> 1 ^ 0xF00BAA4 : hash >> 1);
148 }
149 }
150 }
151
152 return hash;
153}
154
159size_t StringData::CountInUse(size_t tab) const
160{
161 size_t count = TAB_SIZE;
162 while (count > 0 && this->strings[(tab * TAB_SIZE) + count - 1] == nullptr) --count;
163 return count;
164}
165
166void EmitSingleChar(StringBuilder &builder, std::string_view param, char32_t value)
167{
168 if (!param.empty()) StrgenWarning("Ignoring trailing letters in command");
169 builder.PutUtf8(value);
170}
171
172/* The plural specifier looks like
173 * {NUM} {PLURAL <ARG#> passenger passengers} then it picks either passenger/passengers depending on the count in NUM */
174static std::pair<std::optional<size_t>, std::optional<size_t>> ParseRelNum(StringConsumer &consumer)
175{
177 std::optional<size_t> v = consumer.TryReadIntegerBase<size_t>(10);
178 std::optional<size_t> offset;
179 if (v.has_value() && consumer.ReadCharIf(':')) {
180 /* Take the Nth within */
181 offset = consumer.TryReadIntegerBase<size_t>(10);
182 if (!offset.has_value()) StrgenFatal("Expected number for substring parameter");
183 }
184 return {v, offset};
185}
186
187/* Parse out the next word, or nullptr */
188std::optional<std::string_view> ParseWord(StringConsumer &consumer)
189{
191 if (!consumer.AnyBytesLeft()) return {};
192
193 if (consumer.ReadCharIf('"')) {
194 /* parse until next " or NUL */
195 auto result = consumer.ReadUntilChar('"', StringConsumer::KEEP_SEPARATOR);
196 if (!consumer.ReadCharIf('"')) StrgenFatal("Unterminated quotes");
197 return result;
198 } else {
199 /* proceed until whitespace or NUL */
201 }
202}
203
204/* This is encoded like
205 * CommandByte <ARG#> <NUM> {Length of each string} {each string} */
206static void EmitWordList(StringBuilder &builder, const std::vector<std::string> &words)
207{
208 builder.PutUint8(static_cast<uint8_t>(words.size()));
209 for (size_t i = 0; i < words.size(); i++) {
210 size_t len = words[i].size();
211 if (len > UINT8_MAX) StrgenFatal("WordList {}/{} string '{}' too long, max bytes {}", i + 1, words.size(), words[i], UINT8_MAX);
212 builder.PutUint8(static_cast<uint8_t>(len));
213 }
214 for (size_t i = 0; i < words.size(); i++) {
215 builder.Put(words[i]);
216 }
217}
218
219void EmitPlural(StringBuilder &builder, std::string_view param, char32_t)
220{
221 StringConsumer consumer(param);
222
223 /* Parse out the number, if one exists. Otherwise default to prev arg. */
224 auto [argidx, offset] = ParseRelNum(consumer);
225 if (!argidx.has_value()) {
226 if (_cur_argidx == 0) StrgenFatal("Plural choice needs positional reference");
227 argidx = _cur_argidx - 1;
228 }
229
230 const CmdStruct *cmd = _cur_pcs.consuming_commands[*argidx];
231 if (!offset.has_value()) {
232 /* Use default offset */
233 if (cmd == nullptr || !cmd->default_plural_offset.has_value()) {
234 StrgenFatal("Command '{}' has no (default) plural position", cmd == nullptr ? "<empty>" : cmd->cmd);
235 }
236 offset = cmd->default_plural_offset;
237 }
238
239 /* Parse each string */
240 std::vector<std::string> words;
241 for (;;) {
242 auto word = ParseWord(consumer);
243 if (!word.has_value()) break;
244 words.emplace_back(*word);
245 }
246
247 if (words.empty()) {
248 StrgenFatal("{}: No plural words", _cur_ident);
249 }
250
251 size_t expected = _plural_forms[_strgen.lang.plural_form].plural_count;
252 if (expected != words.size()) {
253 if (_translated) {
254 StrgenFatal("{}: Invalid number of plural forms. Expecting {}, found {}.", _cur_ident,
255 expected, words.size());
256 } else {
257 if (_strgen.show_warnings) StrgenWarning("'{}' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
258 if (words.size() > expected) {
259 words.resize(expected);
260 } else {
261 while (words.size() < expected) {
262 words.push_back(words.back());
263 }
264 }
265 }
266 }
267
268 builder.PutUtf8(SCC_PLURAL_LIST);
269 builder.PutUint8(_strgen.lang.plural_form);
270 builder.PutUint8(static_cast<uint8_t>(TranslateArgumentIdx(*argidx, *offset)));
271 EmitWordList(builder, words);
272}
273
274void EmitGender(StringBuilder &builder, std::string_view param, char32_t)
275{
276 StringConsumer consumer(param);
277 if (consumer.ReadCharIf('=')) {
278 /* This is a {G=DER} command */
279 auto gender = consumer.Read(StringConsumer::npos);
280 auto nw = _strgen.lang.GetGenderIndex(gender);
281 if (nw >= MAX_NUM_GENDERS) StrgenFatal("G argument '{}' invalid", gender);
282
283 /* now nw contains the gender index */
284 builder.PutUtf8(SCC_GENDER_INDEX);
285 builder.PutUint8(nw);
286 } else {
287 /* This is a {G 0 foo bar two} command.
288 * If no relative number exists, default to +0 */
289 auto [argidx, offset] = ParseRelNum(consumer);
290 if (!argidx.has_value()) argidx = _cur_argidx;
291 if (!offset.has_value()) offset = 0;
292
293 const CmdStruct *cmd = _cur_pcs.consuming_commands[*argidx];
294 if (cmd == nullptr || !cmd->flags.Test(CmdFlag::Gender)) {
295 StrgenFatal("Command '{}' can't have a gender", cmd == nullptr ? "<empty>" : cmd->cmd);
296 }
297
298 std::vector<std::string> words;
299 for (;;) {
300 auto word = ParseWord(consumer);
301 if (!word.has_value()) break;
302 words.emplace_back(*word);
303 }
304 if (words.size() != _strgen.lang.num_genders) StrgenFatal("Bad # of arguments for gender command");
305
306 assert(IsInsideBS(cmd->value, SCC_CONTROL_START, UINT8_MAX));
307 builder.PutUtf8(SCC_GENDER_LIST);
308 builder.PutUint8(static_cast<uint8_t>(TranslateArgumentIdx(*argidx, *offset)));
309 EmitWordList(builder, words);
310 }
311}
312
313static const CmdStruct *FindCmd(std::string_view s)
314{
315 auto it = std::ranges::find(_cmd_structs, s, &CmdStruct::cmd);
316 if (it != std::end(_cmd_structs)) return &*it;
317 return nullptr;
318}
319
320static uint8_t ResolveCaseName(std::string_view str)
321{
322 uint8_t case_idx = _strgen.lang.GetCaseIndex(str);
323 if (case_idx >= MAX_NUM_CASES) StrgenFatal("Invalid case-name '{}'", str);
324 return case_idx + 1;
325}
326
327/* returns cmd == nullptr on eof */
328static ParsedCommandString ParseCommandString(StringConsumer &consumer)
329{
330 ParsedCommandString result;
331
332 /* Scan to the next command, exit if there's no next command. */
334 if (!consumer.ReadCharIf('{')) return {};
335
336 if (auto argno = consumer.TryReadIntegerBase<uint32_t>(10); argno.has_value()) {
337 result.argno = argno;
338 if (!consumer.ReadCharIf(':')) StrgenFatal("missing arg #");
339 }
340
341 /* parse command name */
342 auto command = consumer.ReadUntilCharIn("} =.");
343 result.cmd = FindCmd(command);
344 if (result.cmd == nullptr) {
345 StrgenError("Undefined command '{}'", command);
346 return {};
347 }
348
349 /* parse case */
350 if (consumer.ReadCharIf('.')) {
351 if (!result.cmd->flags.Test(CmdFlag::Case)) {
352 StrgenFatal("Command '{}' can't have a case", result.cmd->cmd);
353 }
354
355 auto casep = consumer.ReadUntilCharIn("} ");
356 result.casei = ResolveCaseName(casep);
357 }
358
359 /* parse params */
360 result.param = consumer.ReadUntilChar('}', StringConsumer::KEEP_SEPARATOR);
361
362 if (!consumer.ReadCharIf('}')) {
363 StrgenError("Missing }} from command '{}'", result.cmd->cmd);
364 return {};
365 }
366
367 return result;
368}
369
381
382ParsedCommandStruct ExtractCommandString(std::string_view s, bool)
383{
385 StringConsumer consumer(s);
386
387 size_t argidx = 0;
388 for (;;) {
389 /* read until next command from a. */
390 auto cs = ParseCommandString(consumer);
391
392 if (cs.cmd == nullptr) break;
393
394 /* Sanity checking */
395 if (cs.argno.has_value() && cs.cmd->consumes == 0) StrgenFatal("Non consumer param can't have a paramindex");
396
397 if (cs.cmd->consumes > 0) {
398 if (cs.argno.has_value()) argidx = *cs.argno;
399 if (argidx >= p.consuming_commands.max_size()) StrgenFatal("invalid param idx {}", argidx);
400 if (p.consuming_commands[argidx] != nullptr && p.consuming_commands[argidx] != cs.cmd) StrgenFatal("duplicate param idx {}", argidx);
401
402 p.consuming_commands[argidx++] = cs.cmd;
403 } else if (!cs.cmd->flags.Test(CmdFlag::DontCount)) { // Ignore some of them
404 p.non_consuming_commands.emplace_back(cs.cmd, std::move(cs.param));
405 }
406 }
407
408 return p;
409}
410
411const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
412{
413 if (a == nullptr) return nullptr;
414
415 if (a->cmd == "STRING1" ||
416 a->cmd == "STRING2" ||
417 a->cmd == "STRING3" ||
418 a->cmd == "STRING4" ||
419 a->cmd == "STRING5" ||
420 a->cmd == "STRING6" ||
421 a->cmd == "STRING7" ||
422 a->cmd == "RAW_STRING") {
423 return FindCmd("STRING");
424 }
425
426 return a;
427}
428
429static bool CheckCommandsMatch(std::string_view a, std::string_view b, std::string_view name)
430{
431 /* If we're not translating, i.e. we're compiling the base language,
432 * it is pointless to do all these checks as it'll always be correct.
433 * After all, all checks are based on the base language.
434 */
435 if (!_strgen.translation) return true;
436
437 bool result = true;
438
439 ParsedCommandStruct templ = ExtractCommandString(b, true);
440 ParsedCommandStruct lang = ExtractCommandString(a, true);
441
442 /* For each string in templ, see if we find it in lang */
443 if (templ.non_consuming_commands.max_size() != lang.non_consuming_commands.max_size()) {
444 StrgenWarning("{}: template string and language string have a different # of commands", name);
445 result = false;
446 }
447
448 for (auto &templ_nc : templ.non_consuming_commands) {
449 /* see if we find it in lang, and zero it out */
450 auto it = std::ranges::find(lang.non_consuming_commands, templ_nc);
451 if (it != std::end(lang.non_consuming_commands)) {
452 /* it was found in both. zero it out from lang so we don't find it again */
453 it->cmd = nullptr;
454 } else {
455 StrgenWarning("{}: command '{}' exists in template file but not in language file", name, templ_nc.cmd->cmd);
456 result = false;
457 }
458 }
459
460 /* if we reach here, all non consumer commands match up.
461 * Check if the non consumer commands match up also. */
462 for (size_t i = 0; i < templ.consuming_commands.max_size(); i++) {
463 if (TranslateCmdForCompare(templ.consuming_commands[i]) != lang.consuming_commands[i]) {
464 StrgenWarning("{}: Param idx #{} '{}' doesn't match with template command '{}'", name, i,
465 lang.consuming_commands[i] == nullptr ? "<empty>" : TranslateCmdForCompare(lang.consuming_commands[i])->cmd,
466 templ.consuming_commands[i] == nullptr ? "<empty>" : templ.consuming_commands[i]->cmd);
467 result = false;
468 }
469 }
470
471 return result;
472}
473
474void StringReader::HandleString(std::string_view src)
475{
476 /* Ignore blank lines */
477 if (src.empty()) return;
478
479 StringConsumer consumer(src);
480 if (consumer.ReadCharIf('#')) {
481 if (consumer.ReadCharIf('#') && !consumer.ReadCharIf('#')) this->HandlePragma(consumer.Read(StringConsumer::npos), _strgen.lang);
482 return; // ignore comments
483 }
484
485 /* Read string name */
486 std::string_view str_name = StrTrimView(consumer.ReadUntilChar(':', StringConsumer::KEEP_SEPARATOR), StringConsumer::WHITESPACE_NO_NEWLINE);
487 if (!consumer.ReadCharIf(':')) {
488 StrgenError("Line has no ':' delimiter");
489 return;
490 }
491
492 /* Read string case */
493 std::optional<std::string_view> casep;
494 if (auto index = str_name.find("."); index != std::string_view::npos) {
495 casep = str_name.substr(index + 1);
496 str_name = str_name.substr(0, index);
497 }
498
499 /* Read string data */
500 std::string_view value = consumer.Read(StringConsumer::npos);
501
502 /* Check string is valid UTF-8 */
503 for (StringConsumer validation_consumer(value); validation_consumer.AnyBytesLeft(); ) {
504 auto c = validation_consumer.TryReadUtf8();
505 if (!c.has_value()) StrgenFatal("Invalid UTF-8 sequence in '{}'", value);
506 if (*c <= 0x001F || // ASCII control character range
507 *c == 0x200B || // Zero width space
508 (*c >= 0xE000 && *c <= 0xF8FF) || // Private range
509 (*c >= 0xFFF0 && *c <= 0xFFFF)) { // Specials range
510 StrgenFatal("Unwanted UTF-8 character U+{:04X} in sequence '{}'", static_cast<uint32_t>(*c), value);
511 }
512 }
513
514 /* Check if this string already exists.. */
515 LangString *ent = this->data.Find(str_name);
516
517 if (this->master) {
518 if (casep.has_value()) {
519 StrgenError("Cases in the base translation are not supported.");
520 return;
521 }
522
523 if (ent != nullptr) {
524 StrgenError("String name '{}' is used multiple times", str_name);
525 return;
526 }
527
528 if (this->data.strings[this->data.next_string_id] != nullptr) {
529 StrgenError("String ID 0x{:X} for '{}' already in use by '{}'", this->data.next_string_id, str_name, this->data.strings[this->data.next_string_id]->name);
530 return;
531 }
532
533 /* Allocate a new LangString */
534 this->data.Add(std::make_unique<LangString>(str_name, value, this->data.next_string_id++, _strgen.cur_line));
535 } else {
536 if (ent == nullptr) {
537 StrgenWarning("String name '{}' does not exist in master file", str_name);
538 return;
539 }
540
541 if (!ent->translated.empty() && !casep.has_value()) {
542 StrgenError("String name '{}' is used multiple times", str_name);
543 return;
544 }
545
546 /* make sure that the commands match */
547 if (!CheckCommandsMatch(value, ent->english, str_name)) return;
548
549 if (casep.has_value()) {
550 ent->translated_cases.emplace_back(ResolveCaseName(*casep), value);
551 } else {
552 ent->translated = value;
553 /* If the string was translated, use the line from the
554 * translated language so errors in the translated file
555 * are properly referenced to. */
556 ent->line = _strgen.cur_line;
557 }
558 }
559}
560
561void StringReader::HandlePragma(std::string_view str, LanguagePackHeader &lang)
562{
563 StringConsumer consumer(str);
564 auto name = consumer.ReadUntilChar(' ', StringConsumer::SKIP_ALL_SEPARATORS);
565 if (name == "plural") {
566 lang.plural_form = consumer.ReadIntegerBase<uint32_t>(10);
567 if (lang.plural_form >= lengthof(_plural_forms)) {
568 StrgenFatal("Invalid pluralform {}", lang.plural_form);
569 }
570 } else {
571 StrgenFatal("unknown pragma '{}'", name);
572 }
573}
574
576{
577 _strgen.warnings = _strgen.errors = 0;
578
579 _strgen.translation = this->translation;
580 _strgen.file = this->file;
581
582 /* For each new file we parse, reset the genders, and language codes. */
583 _strgen.lang = {};
584
585 _strgen.cur_line = 1;
586 while (this->data.next_string_id < this->data.max_strings) {
587 std::optional<std::string> line = this->ReadLine();
588 if (!line.has_value()) return;
589
590 this->HandleString(StrTrimView(line.value(), StringConsumer::WHITESPACE_OR_NEWLINE));
591 _strgen.cur_line++;
592 }
593
594 if (this->data.next_string_id == this->data.max_strings) {
595 StrgenError("Too many strings, maximum allowed is {}", this->data.max_strings);
596 }
597}
598
604{
605 size_t last = 0;
606 for (size_t i = 0; i < data.max_strings; i++) {
607 if (data.strings[i] != nullptr) {
608 this->WriteStringID(data.strings[i]->name, i);
609 last = i;
610 }
611 }
612
613 this->WriteStringID("STR_LAST_STRINGID", last);
614}
615
616static size_t TranslateArgumentIdx(size_t argidx, size_t offset)
617{
618 if (argidx >= _cur_pcs.consuming_commands.max_size()) {
619 StrgenFatal("invalid argidx {}", argidx);
620 }
621 const CmdStruct *cs = _cur_pcs.consuming_commands[argidx];
622 if (cs != nullptr && cs->consumes <= offset) {
623 StrgenFatal("invalid argidx offset {}:{}", argidx, offset);
624 }
625
626 if (_cur_pcs.consuming_commands[argidx] == nullptr) {
627 StrgenFatal("no command for this argidx {}", argidx);
628 }
629
630 size_t sum = 0;
631 for (size_t i = 0; i < argidx; i++) {
632 cs = _cur_pcs.consuming_commands[i];
633
634 if (cs == nullptr && sum > i) continue;
635
636 sum += (cs != nullptr) ? cs->consumes : 1;
637 }
638
639 return sum + offset;
640}
641
642static void PutArgidxCommand(StringBuilder &builder)
643{
644 builder.PutUtf8(SCC_ARG_INDEX);
645 builder.PutUint8(static_cast<uint8_t>(TranslateArgumentIdx(_cur_argidx)));
646}
647
648static std::string PutCommandString(std::string_view str)
649{
650 std::string result;
651 StringBuilder builder(result);
652 StringConsumer consumer(str);
653 _cur_argidx = 0;
654
655 for (;;) {
656 /* Process characters as they are until we encounter a { */
657 builder.Put(consumer.ReadUntilChar('{', StringConsumer::KEEP_SEPARATOR));
658 if (!consumer.AnyBytesLeft()) break;
659
660 auto cs = ParseCommandString(consumer);
661 auto *cmd = cs.cmd;
662 if (cmd == nullptr) break;
663
664 if (cs.casei.has_value()) {
665 builder.PutUtf8(SCC_SET_CASE); // {SET_CASE}
666 builder.PutUint8(*cs.casei);
667 }
668
669 /* For params that consume values, we need to handle the argindex properly */
670 if (cmd->consumes > 0) {
671 /* Check if we need to output a move-param command */
672 if (cs.argno.has_value() && *cs.argno != _cur_argidx) {
673 _cur_argidx = *cs.argno;
674 PutArgidxCommand(builder);
675 }
676
677 /* Output the one from the master string... it's always accurate. */
678 cmd = _cur_pcs.consuming_commands[_cur_argidx++];
679 if (cmd == nullptr) {
680 StrgenFatal("{}: No argument exists at position {}", _cur_ident, _cur_argidx - 1);
681 }
682 }
683
684 cmd->proc(builder, cs.param, cmd->value);
685 }
686 return result;
687}
688
694{
695 char buffer[2];
696 size_t offs = 0;
697 if (length >= 0x4000) {
698 StrgenFatal("string too long");
699 }
700
701 if (length >= 0xC0) {
702 buffer[offs++] = static_cast<char>(static_cast<uint8_t>((length >> 8) | 0xC0));
703 }
704 buffer[offs++] = static_cast<char>(static_cast<uint8_t>(length & 0xFF));
705 this->Write({buffer, offs});
706}
707
713{
714 std::vector<size_t> in_use;
715 for (size_t tab = 0; tab < data.tabs; tab++) {
716 size_t n = data.CountInUse(tab);
717
718 in_use.push_back(n);
719 _strgen.lang.offsets[tab] = TO_LE16(static_cast<uint16_t>(n));
720
721 for (size_t j = 0; j != in_use[tab]; j++) {
722 const LangString *ls = data.strings[(tab * TAB_SIZE) + j].get();
723 if (ls != nullptr && ls->translated.empty()) _strgen.lang.missing++;
724 }
725 }
726
727 _strgen.lang.ident = TO_LE32(LanguagePackHeader::IDENT);
728 _strgen.lang.version = TO_LE32(data.Version());
729 _strgen.lang.missing = TO_LE16(_strgen.lang.missing);
730 _strgen.lang.winlangid = TO_LE16(_strgen.lang.winlangid);
731
732 this->WriteHeader(&_strgen.lang);
733
734 for (size_t tab = 0; tab < data.tabs; tab++) {
735 for (size_t j = 0; j != in_use[tab]; j++) {
736 const LangString *ls = data.strings[(tab * TAB_SIZE) + j].get();
737
738 /* For undefined strings, just set that it's an empty string */
739 if (ls == nullptr) {
740 this->WriteLength(0);
741 continue;
742 }
743
744 std::string output;
745 StringBuilder builder(output);
746 _cur_ident = ls->name;
747 _strgen.cur_line = ls->line;
748
749 /* Produce a message if a string doesn't have a translation. */
750 if (ls->translated.empty()) {
751 if (_strgen.show_warnings) {
752 StrgenWarning("'{}' is untranslated", ls->name);
753 }
754 if (_strgen.annotate_todos) {
755 builder.Put("<TODO> ");
756 }
757 }
758
759 /* Extract the strings and stuff from the english command string */
760 _cur_pcs = ExtractCommandString(ls->english, false);
761
762 _translated = !ls->translated_cases.empty() || !ls->translated.empty();
763 const std::string &cmdp = _translated ? ls->translated : ls->english;
764
765 if (!ls->translated_cases.empty()) {
766 /* Need to output a case-switch.
767 * It has this format
768 * <0x9E> <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <LENDEFAULT> <STRINGDEFAULT>
769 * Each LEN is printed using 2 bytes in little endian order. */
770 builder.PutUtf8(SCC_SWITCH_CASE);
771 builder.PutUint8(static_cast<uint8_t>(ls->translated_cases.size()));
772
773 /* Write each case */
774 for (const Case &c : ls->translated_cases) {
775 auto case_str = PutCommandString(c.string);
776 builder.PutUint8(c.caseidx);
777 builder.PutUint16LE(static_cast<uint16_t>(case_str.size()));
778 builder.Put(case_str);
779 }
780 }
781
782 std::string def_str;
783 if (!cmdp.empty()) def_str = PutCommandString(cmdp);
784 if (!ls->translated_cases.empty()) {
785 builder.PutUint16LE(static_cast<uint16_t>(def_str.size()));
786 }
787 builder.Put(def_str);
788
789 this->WriteLength(output.size());
790 this->Write(output);
791 }
792 }
793}
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
void PutUtf8(char32_t c)
Append UTF.8 char.
void PutUint16LE(uint16_t value)
Append binary uint16 using little endian.
void Put(std::string_view str)
Append string.
void PutUint8(uint8_t value)
Append binary uint8.
Compose data into a growing std::string.
Parse data from a string / buffer.
bool ReadCharIf(char c)
Check whether the next 8-bit char matches 'c', and skip it.
std::optional< T > TryReadIntegerBase(int base, bool clamp=false)
Try to read and parse an integer in number 'base', and then advance the reader.
std::string_view ReadUntilChar(char c, SeparatorUsage sep)
Read data until the first occurrence of 8-bit char 'c', and advance reader.
void SkipUntilChar(char c, SeparatorUsage sep)
Skip data until the first occurrence of 8-bit char 'c'.
@ SKIP_ALL_SEPARATORS
Read and discard all consecutive separators, do not include any in the result.
@ KEEP_SEPARATOR
Keep the separator in the data as next value to be read.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
static const std::string_view WHITESPACE_OR_NEWLINE
ASCII whitespace characters, including new-line.
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
void SkipUntilCharNotIn(std::string_view chars)
Skip 8-bit chars, while they are in 'chars', until they are not.
std::string_view ReadUntilCharIn(std::string_view chars)
Read 8-bit chars, while they are not in 'chars', until they are; and advance reader.
T ReadIntegerBase(int base, T def=0, bool clamp=false)
Read and parse an integer in number 'base', and advance the reader.
std::string_view Read(size_type len)
Read the next 'len' bytes, and advance reader.
static constexpr size_type npos
Special value for "end of data".
Control codes that are embedded in the translation strings.
Function to handling different endian machines.
Error reporting related functions.
static const uint8_t MAX_NUM_GENDERS
Maximum number of supported genders.
Definition language.h:20
static const uint8_t MAX_NUM_CASES
Maximum number of supported cases.
Definition language.h:21
Integer math functions.
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
Structures related to strgen.
static bool _translated
Whether the current language is not the master language.
static uint32_t VersionHashStr(uint32_t hash, std::string_view s)
Create a compound hash.
Tables of commands for strgen.
static const PluralForm _plural_forms[]
All plural forms used.
@ Gender
These commands support genders.
@ Case
These commands support cases.
@ DontCount
These commands aren't counted for comparison.
Compose strings from textual and binary data.
Functions related to low-level strings.
static const uint TAB_SIZE
Number of strings per StringTab.
Container for the different cases of a string.
Definition strgen.h:22
Case(uint8_t caseidx, std::string_view string)
Create a new case.
uint8_t caseidx
The index of the case.
Definition strgen.h:23
std::string string
The translation of the case.
Definition strgen.h:24
virtual void WriteStringID(const std::string &name, size_t stringid)=0
Write the string ID.
void WriteHeader(const StringData &data)
Write the header information.
Information about a single string.
Definition strgen.h:30
size_t line
Line of string in source-file.
Definition strgen.h:35
std::string english
English text.
Definition strgen.h:32
std::vector< Case > translated_cases
Cases of the translation.
Definition strgen.h:36
std::string translated
Translated text.
Definition strgen.h:33
void FreeTranslation()
Free all data related to the translation.
std::string name
Name of the string.
Definition strgen.h:31
size_t index
The index in the language file.
Definition strgen.h:34
LangString(std::string_view name, std::string_view english, size_t index, size_t line)
Create a new string.
Header of a language file.
Definition language.h:24
uint8_t plural_form
plural form index
Definition language.h:41
static const uint32_t IDENT
Identifier for OpenTTD language files, big endian for "LANG".
Definition language.h:25
virtual void WriteHeader(const LanguagePackHeader *header)=0
Write the header metadata.
virtual void WriteLength(size_t length)
Write the length as a simple gamma.
virtual void Write(std::string_view buffer)=0
Write a number of bytes.
virtual void WriteLang(const StringData &data)
Actually write the language.
Global state shared between strgen.cpp, game_text.cpp and strgen_base.cpp.
Definition strgen.h:162
LanguagePackHeader lang
Header information about a language.
Definition strgen.h:170
size_t cur_line
The current line we're parsing in the input file.
Definition strgen.h:164
Information about the currently known strings.
Definition strgen.h:43
size_t tabs
The number of 'tabs' of strings.
Definition strgen.h:46
void Add(std::shared_ptr< LangString > ls)
Add a newly created LangString.
size_t max_strings
The maximum number of strings.
Definition strgen.h:47
size_t next_string_id
The next string ID to allocate.
Definition strgen.h:48
void FreeTranslation()
Free all data related to the translation.
LangString * Find(std::string_view s)
Find a LangString based on the string name.
StringData(size_t tabs)
Create a new string data container.
std::unordered_map< std::string, std::shared_ptr< LangString >, StringHash, std::equal_to<> > name_to_string
Lookup table for the strings.
Definition strgen.h:45
std::vector< std::shared_ptr< LangString > > strings
List of all known strings.
Definition strgen.h:44
uint32_t Version() const
Make a hash of the file to get a unique "version number".
size_t CountInUse(size_t tab) const
Count the number of tab elements that are in use.
const std::string file
The file we are reading.
Definition strgen.h:61
StringReader(StringData &data, const std::string &file, bool master, bool translation)
Prepare reading.
StringData & data
The data to fill during reading.
Definition strgen.h:60
virtual void ParseFile()
Start parsing the file.
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false.
Definition strgen.h:63
virtual void HandlePragma(std::string_view str, LanguagePackHeader &lang)
Handle the pragma of the file.
virtual std::optional< std::string > ReadLine()=0
Read a single line from the source of strings.
bool master
Are we reading the master file?
Definition strgen.h:62