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

OpenTTD Source 14.0-beta3

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef STRGEN_H
11 #define STRGEN_H
12
13 #include "../language.h"
14 #include "../3rdparty/fmt/format.h"
15
16 #include <unordered_map>
17 #include <array>
18
20 struct Case {
21 int caseidx;
22 std::string string;
23
24 Case(int caseidx, const std::string &string);
25 };
26
28 struct LangString {
29 std::string name;
30 std::string english;
31 std::string translated;
32 size_t index;
33 int line;
34 std::vector<Case> translated_cases;
35
36 LangString(const std::string &name, const std::string &english, size_t index, int line);
37 void FreeTranslation();
38 };
39
41 struct StringData {
42 std::vector<std::unique_ptr<LangString>> strings;
43 std::unordered_map<std::string_view, LangString *> name_to_string;
44 size_t tabs;
45 size_t max_strings;
46 size_t next_string_id;
47
48 StringData(size_t tabs);
49 void FreeTranslation();
50 void Add(std::unique_ptr<LangString> ls);
51 LangString *Find(const std::string_view s);
52 uint VersionHashStr(uint hash, const char *s) const;
53 uint Version() const;
54 uint CountInUse(uint tab) const;
55 };
56
58 struct StringReader {
59 StringData &data;
60 const std::string file;
61 bool master;
62 bool translation;
63
64 StringReader(StringData &data, const std::string &file, bool master, bool translation);
65 virtual ~StringReader() {}
66 void HandleString(char *str);
67
72 virtual std::optional<std::string> ReadLine() = 0;
73
78 virtual void HandlePragma(char *str);
79
83 virtual void ParseFile();
84 };
85
87 struct HeaderWriter {
93 virtual void WriteStringID(const std::string &name, int stringid) = 0;
94
99 virtual void Finalise(const StringData &data) = 0;
100
102 virtual ~HeaderWriter() = default;
103
104 void WriteHeader(const StringData &data);
105 };
106
108 struct LanguageWriter {
114 virtual void WriteHeader(const LanguagePackHeader *header) = 0;
115
121 virtual void Write(const byte *buffer, size_t length) = 0;
122
126 virtual void Finalise() = 0;
127
129 virtual ~LanguageWriter() = default;
130
131 virtual void WriteLength(uint length);
132 virtual void WriteLang(const StringData &data);
133 };
134
135 struct CmdStruct;
136
137 struct CmdPair {
138 const CmdStruct *cmd;
139 std::string param;
140 };
141
142 struct ParsedCommandStruct {
143 std::vector<CmdPair> non_consuming_commands;
144 std::array<const CmdStruct*, 32> consuming_commands{ nullptr }; // ordered by param #
145 };
146
147 const CmdStruct *TranslateCmdForCompare(const CmdStruct *a);
148 ParsedCommandStruct ExtractCommandString(const char *s, bool warnings);
149
150 void StrgenWarningI(const std::string &msg);
151 void StrgenErrorI(const std::string &msg);
152 [[noreturn]] void StrgenFatalI(const std::string &msg);
153 #define StrgenWarning(format_string, ...) StrgenWarningI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
154 #define StrgenError(format_string, ...) StrgenErrorI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
155 #define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
156 char *ParseWord(char **buf);
157
158 extern const char *_file;
159 extern int _cur_line;
160 extern int _errors, _warnings, _show_todo;
161 extern LanguagePackHeader _lang;
162
163 #endif /* STRGEN_H */
LanguageWriter::Finalise
virtual void Finalise()=0
Finalise writing the file.
_file
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
Definition: strgen_base.cpp:29
StringReader::HandlePragma
virtual void HandlePragma(char *str)
Handle the pragma of the file.
Definition: strgen_base.cpp:729
LangString::name
std::string name
Name of the string.
Definition: strgen.h:29
StringData::Add
void Add(std::unique_ptr< LangString > ls)
Add a newly created LangString.
Definition: strgen_base.cpp:90
StringData::name_to_string
std::unordered_map< std::string_view, LangString * > name_to_string
Lookup table for the strings.
Definition: strgen.h:43
StringData::max_strings
size_t max_strings
The maximum number of strings.
Definition: strgen.h:45
LangString::translated_cases
std::vector< Case > translated_cases
Cases of the translation.
Definition: strgen.h:34
StringData::VersionHashStr
uint VersionHashStr(uint hash, const char *s) const
Create a compound hash.
Definition: strgen_base.cpp:115
HeaderWriter::~HeaderWriter
virtual ~HeaderWriter()=default
Especially destroy the subclasses.
Case::Case
Case(int caseidx, const std::string &string)
Create a new case.
Definition: strgen_base.cpp:42
LanguageWriter::Write
virtual void Write(const byte *buffer, size_t length)=0
Write a number of bytes.
LanguageWriter::~LanguageWriter
virtual ~LanguageWriter()=default
Especially destroy the subclasses.
HeaderWriter
Base class for writing the header, i.e.
Definition: strgen.h:87
StringData::StringData
StringData(size_t tabs)
Create a new string data container.
Definition: strgen_base.cpp:70
_lang
LanguagePackHeader _lang
Header information about a language.
Definition: strgen_base.cpp:32
Case::caseidx
int caseidx
The index of the case.
Definition: strgen.h:21
LangString::LangString
LangString(const std::string &name, const std::string &english, size_t index, int line)
Create a new string.
Definition: strgen_base.cpp:54
StringData::strings
std::vector< std::unique_ptr< LangString > > strings
List of all known strings.
Definition: strgen.h:42
_cur_line
int _cur_line
The current line we're parsing in the input file.
Definition: strgen_base.cpp:30
LanguageWriter::WriteLang
virtual void WriteLang(const StringData &data)
Actually write the language.
Definition: strgen_base.cpp:891
StringReader::master
bool master
Are we reading the master file?
Definition: strgen.h:61
StringData::next_string_id
size_t next_string_id
The next string ID to allocate.
Definition: strgen.h:46
Case::string
std::string string
The translation of the case.
Definition: strgen.h:22
LangString
Information about a single string.
Definition: strgen.h:28
StringData::Version
uint Version() const
Make a hash of the file to get a unique "version number".
Definition: strgen_base.cpp:128
HeaderWriter::Finalise
virtual void Finalise(const StringData &data)=0
Finalise writing the file.
StringReader::StringReader
StringReader(StringData &data, const std::string &file, bool master, bool translation)
Prepare reading.
Definition: strgen_base.cpp:528
StringData
Information about the currently known strings.
Definition: strgen.h:41
StringData::CountInUse
uint CountInUse(uint tab) const
Count the number of tab elements that are in use.
Definition: strgen_base.cpp:164
StringData::FreeTranslation
void FreeTranslation()
Free all data related to the translation.
Definition: strgen_base.cpp:77
CmdStruct
Definition: strgen_tables.h:23
LanguageWriter::WriteHeader
virtual void WriteHeader(const LanguagePackHeader *header)=0
Write the header metadata.
LangString::english
std::string english
English text.
Definition: strgen.h:30
LangString::translated
std::string translated
Translated text.
Definition: strgen.h:31
StringReader::ReadLine
virtual std::optional< std::string > ReadLine()=0
Read a single line from the source of strings.
CmdPair
Definition: strgen.h:137
LangString::index
size_t index
The index in the language file.
Definition: strgen.h:32
StringData::tabs
size_t tabs
The number of 'tabs' of strings.
Definition: strgen.h:44
StringReader::data
StringData & data
The data to fill during reading.
Definition: strgen.h:59
HeaderWriter::WriteStringID
virtual void WriteStringID(const std::string &name, int stringid)=0
Write the string ID.
LangString::line
int line
Line of string in source-file.
Definition: strgen.h:33
StringReader::translation
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false.
Definition: strgen.h:62
StringReader::ParseFile
virtual void ParseFile()
Start parsing the file.
Definition: strgen_base.cpp:746
StringData::Find
LangString * Find(const std::string_view s)
Find a LangString based on the string name.
Definition: strgen_base.cpp:101
LanguageWriter
Base class for all language writers.
Definition: strgen.h:108
ParsedCommandStruct
Definition: strgen.h:142
HeaderWriter::WriteHeader
void WriteHeader(const StringData &data)
Write the header information.
Definition: strgen_base.cpp:782
Case
Container for the different cases of a string.
Definition: strgen.h:20
LangString::FreeTranslation
void FreeTranslation()
Free all data related to the translation.
Definition: strgen_base.cpp:60
LanguageWriter::WriteLength
virtual void WriteLength(uint length)
Write the length as a simple gamma.
Definition: strgen_base.cpp:872
StringReader
Helper for reading strings.
Definition: strgen.h:58
StringReader::file
const std::string file
The file we are reading.
Definition: strgen.h:60
LanguagePackHeader
Header of a language file.
Definition: language.h:24