OpenTTD Source 20250523-master-g321f7e8683
strgen.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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../core/endian_func.hpp"
12#include "../error_func.h"
13#include "../string_func.h"
14#include "../strings_type.h"
15#include "../misc/getoptdata.h"
16#include "../table/control_codes.h"
17#include "../3rdparty/fmt/std.h"
18
19#include "strgen.h"
20
21#include <filesystem>
22#include <fstream>
23
24#include "../table/strgen_tables.h"
25
26#include "../safeguards.h"
27
28
29#ifdef _MSC_VER
30# define LINE_NUM_FMT(s) "{} ({}): warning: {} (" s ")\n"
31#else
32# define LINE_NUM_FMT(s) "{}:{}: " s ": {}\n"
33#endif
34
35void StrgenWarningI(const std::string &msg)
36{
37 if (_strgen.translation) {
38 fmt::print(stderr, LINE_NUM_FMT("info"), _strgen.file, _strgen.cur_line, msg);
39 } else {
40 fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, msg);
41 }
42 _strgen.warnings++;
43}
44
45void StrgenErrorI(const std::string &msg)
46{
47 fmt::print(stderr, LINE_NUM_FMT("error"), _strgen.file, _strgen.cur_line, msg);
48 _strgen.errors++;
49}
50
51[[noreturn]] void StrgenFatalI(const std::string &msg)
52{
53 fmt::print(stderr, LINE_NUM_FMT("FATAL"), _strgen.file, _strgen.cur_line, msg);
54#ifdef _MSC_VER
55 fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, "language is not compiled");
56#endif
57 throw std::exception();
58}
59
60[[noreturn]] void FatalErrorI(const std::string &msg)
61{
62 fmt::print(stderr, LINE_NUM_FMT("FATAL"), _strgen.file, _strgen.cur_line, msg);
63#ifdef _MSC_VER
64 fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, "language is not compiled");
65#endif
66 exit(2);
67}
68
71 std::ifstream input_stream;
72
80 FileStringReader(StringData &data, const std::filesystem::path &file, bool master, bool translation) :
81 StringReader(data, file.generic_string(), master, translation)
82 {
83 this->input_stream.open(file, std::ifstream::binary);
84 }
85
86 std::optional<std::string> ReadLine() override
87 {
88 std::string result;
89 if (!std::getline(this->input_stream, result)) return std::nullopt;
90 return result;
91 }
92
93 void HandlePragma(std::string_view str, LanguagePackHeader &lang) override;
94
95 void ParseFile() override
96 {
98
99 if (*_strgen.lang.name == '\0' || *_strgen.lang.own_name == '\0' || *_strgen.lang.isocode == '\0') {
100 FatalError("Language must include ##name, ##ownname and ##isocode");
101 }
102 }
103};
104
105void FileStringReader::HandlePragma(std::string_view str, LanguagePackHeader &lang)
106{
107 StringConsumer consumer(str);
108 auto name = consumer.ReadUntilChar(' ', StringConsumer::SKIP_ALL_SEPARATORS);
109 if (name == "id") {
110 this->data.next_string_id = consumer.ReadIntegerBase<uint32_t>(0);
111 } else if (name == "name") {
112 strecpy(lang.name, consumer.Read(StringConsumer::npos));
113 } else if (name == "ownname") {
114 strecpy(lang.own_name, consumer.Read(StringConsumer::npos));
115 } else if (name == "isocode") {
116 strecpy(lang.isocode, consumer.Read(StringConsumer::npos));
117 } else if (name == "textdir") {
118 auto dir = consumer.Read(StringConsumer::npos);
119 if (dir == "ltr") {
120 lang.text_dir = TD_LTR;
121 } else if (dir == "rtl") {
122 lang.text_dir = TD_RTL;
123 } else {
124 FatalError("Invalid textdir {}", dir);
125 }
126 } else if (name == "digitsep") {
127 auto sep = consumer.Read(StringConsumer::npos);
128 strecpy(lang.digit_group_separator, sep == "{NBSP}" ? NBSP : sep);
129 } else if (name == "digitsepcur") {
130 auto sep = consumer.Read(StringConsumer::npos);
131 strecpy(lang.digit_group_separator_currency, sep == "{NBSP}" ? NBSP : sep);
132 } else if (name == "decimalsep") {
133 auto sep = consumer.Read(StringConsumer::npos);
134 strecpy(lang.digit_decimal_separator, sep == "{NBSP}" ? NBSP : sep);
135 } else if (name == "winlangid") {
136 auto langid = consumer.ReadIntegerBase<int32_t>(0);
137 if (langid > UINT16_MAX || langid < 0) {
138 FatalError("Invalid winlangid {}", langid);
139 }
140 lang.winlangid = static_cast<uint16_t>(langid);
141 } else if (name == "grflangid") {
142 auto langid = consumer.ReadIntegerBase<int32_t>(0);
143 if (langid >= 0x7F || langid < 0) {
144 FatalError("Invalid grflangid {}", langid);
145 }
146 lang.newgrflangid = static_cast<uint8_t>(langid);
147 } else if (name == "gender") {
148 if (this->master) FatalError("Genders are not allowed in the base translation.");
149 for (;;) {
150 auto s = ParseWord(consumer);
151
152 if (!s.has_value()) break;
153 if (lang.num_genders >= MAX_NUM_GENDERS) FatalError("Too many genders, max {}", MAX_NUM_GENDERS);
154 s->copy(lang.genders[lang.num_genders], CASE_GENDER_LEN - 1);
155 lang.num_genders++;
156 }
157 } else if (name == "case") {
158 if (this->master) FatalError("Cases are not allowed in the base translation.");
159 for (;;) {
160 auto s = ParseWord(consumer);
161
162 if (!s.has_value()) break;
163 if (lang.num_cases >= MAX_NUM_CASES) FatalError("Too many cases, max {}", MAX_NUM_CASES);
164 s->copy(lang.cases[lang.num_cases], CASE_GENDER_LEN - 1);
165 lang.num_cases++;
166 }
167 } else {
169 }
170}
171
172static bool CompareFiles(const std::filesystem::path &path1, const std::filesystem::path &path2)
173{
174 /* Check for equal size, but ignore the error code for cases when a file does not exist. */
175 std::error_code error_code;
176 if (std::filesystem::file_size(path1, error_code) != std::filesystem::file_size(path2, error_code)) return false;
177
178 std::ifstream stream1(path1, std::ifstream::binary);
179 std::ifstream stream2(path2, std::ifstream::binary);
180
181 return std::equal(std::istreambuf_iterator<char>(stream1.rdbuf()),
182 std::istreambuf_iterator<char>(),
183 std::istreambuf_iterator<char>(stream2.rdbuf()));
184}
185
187struct FileWriter {
188 std::ofstream output_stream;
189 const std::filesystem::path path;
190
196 FileWriter(const std::filesystem::path &path, std::ios_base::openmode openmode) : path(path)
197 {
198 this->output_stream.open(path, openmode);
199 }
200
202 void Finalise()
203 {
204 this->output_stream.close();
205 }
206
208 virtual ~FileWriter()
209 {
210 /* If we weren't closed an exception was thrown, so remove the temporary file. */
211 if (this->output_stream.is_open()) {
212 this->output_stream.close();
213 std::filesystem::remove(this->path);
214 }
215 }
216};
217
220 const std::filesystem::path real_path;
222 size_t prev;
223 size_t total_strings;
224
229 HeaderFileWriter(const std::filesystem::path &path) : FileWriter("tmp.xxx", std::ofstream::out),
230 real_path(path), prev(0), total_strings(0)
231 {
232 this->output_stream << "/* This file is automatically generated. Do not modify */\n\n";
233 this->output_stream << "#ifndef TABLE_STRINGS_H\n";
234 this->output_stream << "#define TABLE_STRINGS_H\n";
235 }
236
237 void WriteStringID(const std::string &name, size_t stringid) override
238 {
239 if (prev + 1 != stringid) this->output_stream << "\n";
240 fmt::print(this->output_stream, "static const StringID {} = 0x{:X};\n", name, stringid);
241 prev = stringid;
242 total_strings++;
243 }
244
245 void Finalise(const StringData &data) override
246 {
247 /* Find the plural form with the most amount of cases. */
248 size_t max_plural_forms = 0;
249 for (const auto &pf : _plural_forms) {
250 max_plural_forms = std::max(max_plural_forms, pf.plural_count);
251 }
252
253 fmt::print(this->output_stream,
254 "\n"
255 "static const uint LANGUAGE_PACK_VERSION = 0x{:X};\n"
256 "static const uint LANGUAGE_MAX_PLURAL = {};\n"
257 "static const uint LANGUAGE_MAX_PLURAL_FORMS = {};\n"
258 "static const uint LANGUAGE_TOTAL_STRINGS = {};\n"
259 "\n",
260 data.Version(), std::size(_plural_forms), max_plural_forms, total_strings
261 );
262
263 this->output_stream << "#endif /* TABLE_STRINGS_H */\n";
264
265 this->FileWriter::Finalise();
266
267 std::error_code error_code;
268 if (CompareFiles(this->path, this->real_path)) {
269 /* files are equal. tmp.xxx is not needed */
270 std::filesystem::remove(this->path, error_code); // Just ignore the error
271 } else {
272 /* else rename tmp.xxx into filename */
273 std::filesystem::rename(this->path, this->real_path, error_code);
274 if (error_code) FatalError("rename({}, {}) failed: {}", this->path, this->real_path, error_code.message());
275 }
276 }
277};
278
285 LanguageFileWriter(const std::filesystem::path &path) : FileWriter(path, std::ofstream::binary | std::ofstream::out)
286 {
287 }
288
289 void WriteHeader(const LanguagePackHeader *header) override
290 {
291 this->Write({reinterpret_cast<const char *>(header), sizeof(*header)});
292 }
293
294 void Finalise() override
295 {
296 this->output_stream.put(0);
297 this->FileWriter::Finalise();
298 }
299
300 void Write(std::string_view buffer) override
301 {
302 this->output_stream.write(buffer.data(), buffer.size());
303 }
304};
305
307static const OptionData _opts[] = {
308 { .type = ODF_NO_VALUE, .id = 'C', .longname = "-export-commands" },
309 { .type = ODF_NO_VALUE, .id = 'L', .longname = "-export-plurals" },
310 { .type = ODF_NO_VALUE, .id = 'P', .longname = "-export-pragmas" },
311 { .type = ODF_NO_VALUE, .id = 't', .shortname = 't', .longname = "--todo" },
312 { .type = ODF_NO_VALUE, .id = 'w', .shortname = 'w', .longname = "--warning" },
313 { .type = ODF_NO_VALUE, .id = 'h', .shortname = 'h', .longname = "--help" },
314 { .type = ODF_NO_VALUE, .id = 'h', .shortname = '?' },
315 { .type = ODF_HAS_VALUE, .id = 's', .shortname = 's', .longname = "--source_dir" },
316 { .type = ODF_HAS_VALUE, .id = 'd', .shortname = 'd', .longname = "--dest_dir" },
317};
318
319int CDECL main(int argc, char *argv[])
320{
321 std::filesystem::path src_dir(".");
322 std::filesystem::path dest_dir;
323
324 std::vector<std::string_view> params;
325 for (int i = 1; i < argc; ++i) params.emplace_back(argv[i]);
326 GetOptData mgo(params, _opts);
327 for (;;) {
328 int i = mgo.GetOpt();
329 if (i == -1) break;
330
331 switch (i) {
332 case 'C':
333 fmt::print("args\tflags\tcommand\treplacement\n");
334 for (const auto &cs : _cmd_structs) {
335 char flags;
336 if (cs.proc == EmitGender) {
337 flags = 'g'; // Command needs number of parameters defined by number of genders
338 } else if (cs.proc == EmitPlural) {
339 flags = 'p'; // Command needs number of parameters defined by plural value
340 } else if (cs.flags.Test(CmdFlag::DontCount)) {
341 flags = 'i'; // Command may be in the translation when it is not in base
342 } else {
343 flags = '0'; // Command needs no parameters
344 }
345 fmt::print("{}\t{:c}\t\"{}\"\t\"{}\"\n", cs.consumes, flags, cs.cmd, cs.cmd.find("STRING") != std::string::npos ? "STRING" : cs.cmd);
346 }
347 return 0;
348
349 case 'L':
350 fmt::print("count\tdescription\tnames\n");
351 for (const auto &pf : _plural_forms) {
352 fmt::print("{}\t\"{}\"\t{}\n", pf.plural_count, pf.description, pf.names);
353 }
354 return 0;
355
356 case 'P':
357 fmt::print("name\tflags\tdefault\tdescription\n");
358 for (const auto &pragma : _pragmas) {
359 fmt::print("\"{}\"\t{}\t\"{}\"\t\"{}\"\n",
360 pragma[0], pragma[1], pragma[2], pragma[3]);
361 }
362 return 0;
363
364 case 't':
365 _strgen.annotate_todos = true;
366 break;
367
368 case 'w':
369 _strgen.show_warnings = true;
370 break;
371
372 case 'h':
373 fmt::print(
374 "strgen\n"
375 " -t | --todo replace any untranslated strings with '<TODO>'\n"
376 " -w | --warning print a warning for any untranslated strings\n"
377 " -h | -? | --help print this help message and exit\n"
378 " -s | --source_dir search for english.txt in the specified directory\n"
379 " -d | --dest_dir put output file in the specified directory, create if needed\n"
380 " -export-commands export all commands and exit\n"
381 " -export-plurals export all plural forms and exit\n"
382 " -export-pragmas export all pragmas and exit\n"
383 " Run without parameters and strgen will search for english.txt and parse it,\n"
384 " creating strings.h. Passing an argument, strgen will translate that language\n"
385 " file using english.txt as a reference and output <language>.lng.\n"
386 );
387 return 0;
388
389 case 's':
390 src_dir = mgo.opt;
391 break;
392
393 case 'd':
394 dest_dir = mgo.opt;
395 break;
396
397 case -2:
398 fmt::print(stderr, "Invalid arguments\n");
399 return 0;
400 }
401 }
402
403 if (dest_dir.empty()) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
404
405 try {
406 /* strgen has two modes of operation. If no (free) arguments are passed
407 * strgen generates strings.h to the destination directory. If it is supplied
408 * with a (free) parameter the program will translate that language to destination
409 * directory. As input english.txt is parsed from the source directory */
410 if (mgo.arguments.empty()) {
411 std::filesystem::path input_path = std::move(src_dir);
412 input_path /= "english.txt";
413
414 /* parse master file */
416 FileStringReader master_reader(data, input_path, true, false);
417 master_reader.ParseFile();
418 if (_strgen.errors != 0) return 1;
419
420 /* write strings.h */
421 std::filesystem::path output_path = dest_dir;
422 std::filesystem::create_directories(dest_dir);
423 output_path /= "strings.h";
424
425 HeaderFileWriter writer(output_path);
426 writer.WriteHeader(data);
427 writer.Finalise(data);
428 if (_strgen.errors != 0) return 1;
429 } else {
430 std::filesystem::path input_path = std::move(src_dir);
431 input_path /= "english.txt";
432
434 /* parse master file and check if target file is correct */
435 FileStringReader master_reader(data, input_path, true, false);
436 master_reader.ParseFile();
437
438 for (auto &argument: mgo.arguments) {
439 data.FreeTranslation();
440
441 std::filesystem::path lang_file = argument;
442 FileStringReader translation_reader(data, lang_file, false, lang_file.filename() != "english.txt");
443 translation_reader.ParseFile(); // target file
444 if (_strgen.errors != 0) return 1;
445
446 /* get the targetfile, strip any directories and append to destination path */
447 std::filesystem::path output_file = dest_dir;
448 output_file /= lang_file.filename();
449 output_file.replace_extension("lng");
450
451 LanguageFileWriter writer(output_file);
452 writer.WriteLang(data);
453 writer.Finalise();
454
455 /* if showing warnings, print a summary of the language */
456 if (_strgen.show_warnings) {
457 fmt::print("{} warnings and {} errors for {}\n", _strgen.warnings, _strgen.errors, output_file);
458 }
459 }
460 }
461 } catch (...) {
462 return 2;
463 }
464
465 return 0;
466}
Parse data from a string / buffer.
std::string_view ReadUntilChar(char c, SeparatorUsage sep)
Read data until the first occurrence of 8-bit char 'c', and advance reader.
@ SKIP_ALL_SEPARATORS
Read and discard all consecutive separators, do not include any in the result.
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".
@ ODF_NO_VALUE
A plain option (no value attached to it).
Definition getoptdata.h:15
@ ODF_HAS_VALUE
An option with a value.
Definition getoptdata.h:16
static const uint8_t MAX_NUM_GENDERS
Maximum number of supported genders.
Definition language.h:20
static const uint8_t CASE_GENDER_LEN
The (maximum) length of a case/gender string.
Definition language.h:19
static const uint8_t MAX_NUM_CASES
Maximum number of supported cases.
Definition language.h:21
static const OptionData _opts[]
Options of strgen.
Definition strgen.cpp:307
void FatalErrorI(const std::string &msg)
Error handling for fatal non-user errors.
Definition strgen.cpp:60
Structures related to strgen.
static const std::string_view _pragmas[][4]
All pragmas used.
static const PluralForm _plural_forms[]
All plural forms used.
@ DontCount
These commands aren't counted for comparison.
void strecpy(std::span< char > dst, std::string_view src)
Copies characters from one buffer to another.
Definition string.cpp:54
#define NBSP
A non-breaking space.
Definition string_type.h:16
@ TEXT_TAB_END
End of language files.
@ TD_LTR
Text is written left-to-right by default.
@ TD_RTL
Text is written right-to-left by default.
A reader that simply reads using fopen.
Definition strgen.cpp:70
FileStringReader(StringData &data, const std::filesystem::path &file, bool master, bool translation)
Create the reader.
Definition strgen.cpp:80
void ParseFile() override
Start parsing the file.
Definition strgen.cpp:95
std::optional< std::string > ReadLine() override
Read a single line from the source of strings.
Definition strgen.cpp:86
void HandlePragma(std::string_view str, LanguagePackHeader &lang) override
Handle the pragma of the file.
Definition strgen.cpp:105
Yes, simply writing to a file.
std::ofstream output_stream
The stream to write all the output to.
Definition strgen.cpp:188
virtual ~FileWriter()
Make sure the file is closed.
Definition strgen.cpp:208
const std::filesystem::path path
The file name we're writing to.
Definition strgen.cpp:189
FileWriter(const std::filesystem::path &path, std::ios_base::openmode openmode)
Open a file to write to.
Definition strgen.cpp:196
void Finalise()
Finalise the writing.
Definition strgen.cpp:202
Data storage for parsing command line options.
Definition getoptdata.h:29
const std::filesystem::path real_path
The real path we eventually want to write to.
Definition strgen.cpp:220
void Finalise(const StringData &data) override
Finalise writing the file.
Definition strgen.cpp:245
HeaderFileWriter(const std::filesystem::path &path)
Open a file to write to.
Definition strgen.cpp:229
size_t prev
The previous string ID that was printed.
Definition strgen.cpp:222
void WriteStringID(const std::string &name, size_t stringid) override
Write the string ID.
Definition strgen.cpp:237
Base class for writing the header, i.e.
Definition strgen.h:88
Class for writing a language to disk.
Definition strgen.cpp:280
LanguageFileWriter(const std::filesystem::path &path)
Open a file to write to.
Definition strgen.cpp:285
void Finalise() override
Finalise writing the file.
Definition strgen.cpp:294
void WriteHeader(const LanguagePackHeader *header) override
Write the header metadata.
Definition strgen.cpp:289
void Write(std::string_view buffer) override
Write a number of bytes.
Definition strgen.cpp:300
Header of a language file.
Definition language.h:24
char isocode[16]
the ISO code for the language (not country code)
Definition language.h:31
char own_name[32]
the localized name of this language
Definition language.h:30
char name[32]
the international name of this language
Definition language.h:29
uint16_t winlangid
Windows language ID: Windows cannot and will not convert isocodes to something it can use to determin...
Definition language.h:51
uint8_t num_genders
the number of genders of this language
Definition language.h:53
char cases[MAX_NUM_CASES][CASE_GENDER_LEN]
the cases used by this translation
Definition language.h:58
char digit_group_separator[8]
Thousand separator used for anything not currencies.
Definition language.h:35
uint8_t newgrflangid
newgrf language id
Definition language.h:52
uint8_t text_dir
default direction of the text
Definition language.h:42
char genders[MAX_NUM_GENDERS][CASE_GENDER_LEN]
the genders used by this translation
Definition language.h:57
uint8_t num_cases
the number of cases of this language
Definition language.h:54
char digit_decimal_separator[8]
Decimal separator.
Definition language.h:39
char digit_group_separator_currency[8]
Thousand separator used for currencies.
Definition language.h:37
Base class for all language writers.
Definition strgen.h:109
Data of an option.
Definition getoptdata.h:21
OptionDataType type
The type of option.
Definition getoptdata.h:22
std::string file
The filename of the input, so we can refer to it in errors/warnings.
Definition strgen.h:160
bool translation
Is the current file actually a translation or not.
Definition strgen.h:166
LanguagePackHeader lang
Header information about a language.
Definition strgen.h:167
size_t cur_line
The current line we're parsing in the input file.
Definition strgen.h:161
Information about the currently known strings.
Definition strgen.h:43
size_t next_string_id
The next string ID to allocate.
Definition strgen.h:48
uint32_t Version() const
Make a hash of the file to get a unique "version number".
Helper for reading strings.
Definition strgen.h:59
const std::string file
The file we are reading.
Definition strgen.h:61
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.
bool master
Are we reading the master file?
Definition strgen.h:62