OpenTTD Source 20241224-master-gee860a5c8e
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
20struct Case {
21 int caseidx;
22 std::string string;
23
24 Case(int caseidx, const std::string &string);
25};
26
28struct 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
41struct 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;
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
60 const std::string file;
61 bool master;
63
64 StringReader(StringData &data, const std::string &file, bool master, bool translation);
65 virtual ~StringReader() = default;
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
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
114 virtual void WriteHeader(const LanguagePackHeader *header) = 0;
115
121 virtual void Write(const uint8_t *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
135struct CmdStruct;
136
137struct CmdPair {
138 const CmdStruct *cmd;
139 std::string param;
140};
141
143 std::vector<CmdPair> non_consuming_commands;
144 std::array<const CmdStruct*, 32> consuming_commands{ nullptr }; // ordered by param #
145};
146
147const CmdStruct *TranslateCmdForCompare(const CmdStruct *a);
148ParsedCommandStruct ExtractCommandString(const char *s, bool warnings);
149
150void StrgenWarningI(const std::string &msg);
151void 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_OPT__(,) __VA_ARGS__))
154#define StrgenError(format_string, ...) StrgenErrorI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
155#define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__))
156char *ParseWord(char **buf);
157
158extern const char *_file;
159extern int _cur_line;
160extern int _errors, _warnings, _show_todo;
162
163#endif /* STRGEN_H */
LanguagePackHeader _lang
Header information about a language.
int _cur_line
The current line we're parsing in the input file.
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
Container for the different cases of a string.
Definition strgen.h:20
int caseidx
The index of the case.
Definition strgen.h:21
std::string string
The translation of the case.
Definition strgen.h:22
Base class for writing the header, i.e.
Definition strgen.h:87
virtual ~HeaderWriter()=default
Especially destroy the subclasses.
virtual void WriteStringID(const std::string &name, int stringid)=0
Write the string ID.
virtual void Finalise(const StringData &data)=0
Finalise writing the file.
void WriteHeader(const StringData &data)
Write the header information.
Information about a single string.
Definition strgen.h:28
int line
Line of string in source-file.
Definition strgen.h:33
std::string english
English text.
Definition strgen.h:30
std::vector< Case > translated_cases
Cases of the translation.
Definition strgen.h:34
std::string translated
Translated text.
Definition strgen.h:31
void FreeTranslation()
Free all data related to the translation.
std::string name
Name of the string.
Definition strgen.h:29
size_t index
The index in the language file.
Definition strgen.h:32
Header of a language file.
Definition language.h:24
Base class for all language writers.
Definition strgen.h:108
virtual void WriteHeader(const LanguagePackHeader *header)=0
Write the header metadata.
virtual void Finalise()=0
Finalise writing the file.
virtual void WriteLength(uint length)
Write the length as a simple gamma.
virtual ~LanguageWriter()=default
Especially destroy the subclasses.
virtual void Write(const uint8_t *buffer, size_t length)=0
Write a number of bytes.
virtual void WriteLang(const StringData &data)
Actually write the language.
Information about the currently known strings.
Definition strgen.h:41
size_t tabs
The number of 'tabs' of strings.
Definition strgen.h:44
uint CountInUse(uint tab) const
Count the number of tab elements that are in use.
uint VersionHashStr(uint hash, const char *s) const
Create a compound hash.
std::vector< std::unique_ptr< LangString > > strings
List of all known strings.
Definition strgen.h:42
size_t max_strings
The maximum number of strings.
Definition strgen.h:45
void Add(std::unique_ptr< LangString > ls)
Add a newly created LangString.
size_t next_string_id
The next string ID to allocate.
Definition strgen.h:46
uint Version() const
Make a hash of the file to get a unique "version number".
LangString * Find(const std::string_view s)
Find a LangString based on the string name.
void FreeTranslation()
Free all data related to the translation.
std::unordered_map< std::string_view, LangString * > name_to_string
Lookup table for the strings.
Definition strgen.h:43
Helper for reading strings.
Definition strgen.h:58
const std::string file
The file we are reading.
Definition strgen.h:60
StringData & data
The data to fill during reading.
Definition strgen.h:59
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:62
virtual std::optional< std::string > ReadLine()=0
Read a single line from the source of strings.
virtual void HandlePragma(char *str)
Handle the pragma of the file.
bool master
Are we reading the master file?
Definition strgen.h:61