OpenTTD Source: src/strgen/strgen.h Source File

OpenTTD Source 15.3

strgen.h
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
10#ifndef STRGEN_H
11#define STRGEN_H
12
13#include "../core/string_consumer.hpp"
14#include "../language.h"
15#include "../string_type.h"
16#include "../3rdparty/fmt/format.h"
17
18#include <unordered_map>
19#include <array>
20
22 struct Case {
23 uint8_t caseidx;
24 std::string string;
25
26 Case(uint8_t caseidx, std::string_view string);
27};
28
30 struct LangString {
31 std::string name;
32 std::string english;
33 std::string translated;
34 size_t index;
35 size_t line;
36 std::vector<Case> translated_cases;
37
38 LangString(std::string_view name, std::string_view english, size_t index, size_t line);
39 void FreeTranslation();
40};
41
43 struct StringData {
44 std::vector<std::shared_ptr<LangString>> strings;
45 std::unordered_map<std::string, std::shared_ptr<LangString>, StringHash, std::equal_to<>> name_to_string;
46 size_t tabs;
47 size_t max_strings;
48 size_t next_string_id;
49
50 StringData(size_t tabs);
51 void FreeTranslation();
52 void Add(std::shared_ptr<LangString> ls);
53 LangString *Find(std::string_view s);
54 uint32_t Version() const;
55 size_t CountInUse(size_t tab) const;
56};
57
59 struct StringReader {
60 StringData &data;
61 const std::string file;
62 bool master;
63 bool translation;
64
65 StringReader(StringData &data, const std::string &file, bool master, bool translation);
66 virtual ~StringReader() = default;
67 void HandleString(std::string_view str);
68
73 virtual std::optional<std::string> ReadLine() = 0;
74
79 virtual void HandlePragma(std::string_view str, LanguagePackHeader &lang);
80
84 virtual void ParseFile();
85};
86
88 struct HeaderWriter {
94 virtual void WriteStringID(const std::string &name, size_t stringid) = 0;
95
100 virtual void Finalise(const StringData &data) = 0;
101
103 virtual ~HeaderWriter() = default;
104
105 void WriteHeader(const StringData &data);
106};
107
109 struct LanguageWriter {
115 virtual void WriteHeader(const LanguagePackHeader *header) = 0;
116
121 virtual void Write(std::string_view buffer) = 0;
122
126 virtual void Finalise() = 0;
127
129 virtual ~LanguageWriter() = default;
130
131 virtual void WriteLength(size_t length);
132 virtual void WriteLang(const StringData &data);
133};
134
135struct CmdStruct;
136
137 struct CmdPair {
138 const CmdStruct *cmd;
139 std::string param;
140
141 auto operator<=>(const CmdPair &other) const = default;
142};
143
144 struct ParsedCommandStruct {
145 std::vector<CmdPair> non_consuming_commands;
146 std::array<const CmdStruct*, 32> consuming_commands{ nullptr }; // ordered by param #
147};
148
149const CmdStruct *TranslateCmdForCompare(const CmdStruct *a);
150 ParsedCommandStruct ExtractCommandString(std::string_view s, bool warnings);
151
152void StrgenWarningI(const std::string &msg);
153void StrgenErrorI(const std::string &msg);
154[[noreturn]] void StrgenFatalI(const std::string &msg);
155#define StrgenWarning(format_string, ...) StrgenWarningI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
156#define StrgenError(format_string, ...) StrgenErrorI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
157#define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
158std::optional<std::string_view> ParseWord(StringConsumer &consumer);
159
161 struct StrgenState {
162 std::string file = "(unknown file)";
163 size_t cur_line = 0;
164 size_t errors = 0;
165 size_t warnings = 0;
166 bool show_warnings = false;
167 bool annotate_todos = false;
168 bool translation = false;
169 LanguagePackHeader lang;
170};
171extern StrgenState _strgen;
172
173#endif /* STRGEN_H */
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
Case
Container for the different cases of a string.
Definition strgen.h:22
Case::caseidx
uint8_t caseidx
The index of the case.
Definition strgen.h:23
Case::string
std::string string
The translation of the case.
Definition strgen.h:24
CmdPair
Definition strgen.h:137
CmdStruct
Definition strgen_tables.h:22
HeaderWriter
Base class for writing the header, i.e.
Definition strgen.h:88
HeaderWriter::~HeaderWriter
virtual ~HeaderWriter()=default
Especially destroy the subclasses.
HeaderWriter::Finalise
virtual void Finalise(const StringData &data)=0
Finalise writing the file.
HeaderWriter::WriteStringID
virtual void WriteStringID(const std::string &name, size_t stringid)=0
Write the string ID.
HeaderWriter::WriteHeader
void WriteHeader(const StringData &data)
Write the header information.
Definition strgen_base.cpp:604
LangString
Information about a single string.
Definition strgen.h:30
LangString::line
size_t line
Line of string in source-file.
Definition strgen.h:35
LangString::english
std::string english
English text.
Definition strgen.h:32
LangString::translated_cases
std::vector< Case > translated_cases
Cases of the translation.
Definition strgen.h:36
LangString::translated
std::string translated
Translated text.
Definition strgen.h:33
LangString::FreeTranslation
void FreeTranslation()
Free all data related to the translation.
Definition strgen_base.cpp:62
LangString::name
std::string name
Name of the string.
Definition strgen.h:31
LangString::index
size_t index
The index in the language file.
Definition strgen.h:34
LanguagePackHeader
Header of a language file.
Definition language.h:24
LanguageWriter
Base class for all language writers.
Definition strgen.h:109
LanguageWriter::WriteHeader
virtual void WriteHeader(const LanguagePackHeader *header)=0
Write the header metadata.
LanguageWriter::Finalise
virtual void Finalise()=0
Finalise writing the file.
LanguageWriter::WriteLength
virtual void WriteLength(size_t length)
Write the length as a simple gamma.
Definition strgen_base.cpp:694
LanguageWriter::Write
virtual void Write(std::string_view buffer)=0
Write a number of bytes.
LanguageWriter::~LanguageWriter
virtual ~LanguageWriter()=default
Especially destroy the subclasses.
LanguageWriter::WriteLang
virtual void WriteLang(const StringData &data)
Actually write the language.
Definition strgen_base.cpp:713
ParsedCommandStruct
Definition strgen.h:144
StrgenState
Global state shared between strgen.cpp, game_text.cpp and strgen_base.cpp.
Definition strgen.h:161
StrgenState::file
std::string file
The filename of the input, so we can refer to it in errors/warnings.
Definition strgen.h:162
StrgenState::translation
bool translation
Is the current file actually a translation or not.
Definition strgen.h:168
StrgenState::lang
LanguagePackHeader lang
Header information about a language.
Definition strgen.h:169
StrgenState::cur_line
size_t cur_line
The current line we're parsing in the input file.
Definition strgen.h:163
StringData
Information about the currently known strings.
Definition strgen.h:43
StringData::tabs
size_t tabs
The number of 'tabs' of strings.
Definition strgen.h:46
StringData::Add
void Add(std::shared_ptr< LangString > ls)
Add a newly created LangString.
Definition strgen_base.cpp:92
StringData::max_strings
size_t max_strings
The maximum number of strings.
Definition strgen.h:47
StringData::next_string_id
size_t next_string_id
The next string ID to allocate.
Definition strgen.h:48
StringData::FreeTranslation
void FreeTranslation()
Free all data related to the translation.
Definition strgen_base.cpp:79
StringData::Find
LangString * Find(std::string_view s)
Find a LangString based on the string name.
Definition strgen_base.cpp:103
StringData::name_to_string
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
StringData::strings
std::vector< std::shared_ptr< LangString > > strings
List of all known strings.
Definition strgen.h:44
StringData::Version
uint32_t Version() const
Make a hash of the file to get a unique "version number".
Definition strgen_base.cpp:130
StringData::CountInUse
size_t CountInUse(size_t tab) const
Count the number of tab elements that are in use.
Definition strgen_base.cpp:160
StringHash
Helper to provide transparent hashing for string types in e.g.
Definition string_type.h:63
StringReader
Helper for reading strings.
Definition strgen.h:59
StringReader::file
const std::string file
The file we are reading.
Definition strgen.h:61
StringReader::data
StringData & data
The data to fill during reading.
Definition strgen.h:60
StringReader::ParseFile
virtual void ParseFile()
Start parsing the file.
Definition strgen_base.cpp:576
StringReader::translation
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false.
Definition strgen.h:63
StringReader::HandlePragma
virtual void HandlePragma(std::string_view str, LanguagePackHeader &lang)
Handle the pragma of the file.
Definition strgen_base.cpp:562
StringReader::ReadLine
virtual std::optional< std::string > ReadLine()=0
Read a single line from the source of strings.
StringReader::master
bool master
Are we reading the master file?
Definition strgen.h:62