OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../error_func.h"
12#include "../string_func.h"
13#include "../strings_type.h"
14#include "../misc/getoptdata.h"
16#include "../3rdparty/fmt/std.h"
17
18#include "strgen.h"
19
20#include <filesystem>
21#include <fstream>
22
24
25#include "../safeguards.h"
26
27
28#ifdef _MSC_VER
29# define LINE_NUM_FMT(s) "{} ({}): warning: {} (" s ")\n"
30#else
31# define LINE_NUM_FMT(s) "{}:{}: " s ": {}\n"
32#endif
33
34void StrgenWarningI(const std::string &msg)
35{
36 if (_strgen.translation) {
37 fmt::print(stderr, LINE_NUM_FMT("info"), _strgen.file, _strgen.cur_line, msg);
38 } else {
39 fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, msg);
40 }
41 _strgen.warnings++;
42}
43
44void StrgenErrorI(const std::string &msg)
45{
46 fmt::print(stderr, LINE_NUM_FMT("error"), _strgen.file, _strgen.cur_line, msg);
47 _strgen.errors++;
48}
49
50[[noreturn]] void StrgenFatalI(const std::string &msg)
51{
52 fmt::print(stderr, LINE_NUM_FMT("FATAL"), _strgen.file, _strgen.cur_line, msg);
53#ifdef _MSC_VER
54 fmt::print(stderr, LINE_NUM_FMT("warning"), _strgen.file, _strgen.cur_line, "language is not compiled");
55#endif
56 throw std::exception();
57}
58
59/* Doxygen in error_func.h */
60[[noreturn]] void FatalErrorI(const std::string &str)
61{
62 fmt::print(stderr, LINE_NUM_FMT("FATAL"), _strgen.file, _strgen.cur_line, str);
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 << "/**\n";
233 this->output_stream << " * @file strings.h This file contains IDs for all registered strings.\n";
234 this->output_stream << " * @attention This file is automatically generated. Do not modify.\n";
235 this->output_stream << " */\n\n";
236 this->output_stream << "#ifndef TABLE_STRINGS_H\n";
237 this->output_stream << "#define TABLE_STRINGS_H\n";
238 }
239
240 void WriteStringID(const std::string &name, size_t stringid) override
241 {
242 if (prev + 1 != stringid) this->output_stream << "\n";
243 fmt::print(this->output_stream, "static const StringID {} = 0x{:X};\n", name, stringid);
244 prev = stringid;
245 total_strings++;
246 }
247
248 void Finalise(const StringData &data) override
249 {
250 /* Find the plural form with the most amount of cases. */
251 size_t max_plural_forms = 0;
252 for (const auto &pf : _plural_forms) {
253 max_plural_forms = std::max(max_plural_forms, pf.plural_count);
254 }
255
256 fmt::print(this->output_stream,
257 "\n"
258 "static const uint LANGUAGE_PACK_VERSION = 0x{:X};\n"
259 "static const uint LANGUAGE_MAX_PLURAL = {};\n"
260 "static const uint LANGUAGE_MAX_PLURAL_FORMS = {};\n"
261 "static const uint LANGUAGE_TOTAL_STRINGS = {};\n"
262 "\n",
263 data.Version(), std::size(_plural_forms), max_plural_forms, total_strings
264 );
265
266 this->output_stream << "#endif /* TABLE_STRINGS_H */\n";
267
268 this->FileWriter::Finalise();
269
270 std::error_code error_code;
271 if (CompareFiles(this->path, this->real_path)) {
272 /* files are equal. tmp.xxx is not needed */
273 std::filesystem::remove(this->path, error_code); // Just ignore the error
274 } else {
275 /* else rename tmp.xxx into filename */
276 std::filesystem::rename(this->path, this->real_path, error_code);
277 if (error_code) FatalError("rename({}, {}) failed: {}", this->path, this->real_path, error_code.message());
278 }
279 }
280};
281
288 LanguageFileWriter(const std::filesystem::path &path) : FileWriter(path, std::ofstream::binary | std::ofstream::out)
289 {
290 }
291
292 void WriteHeader(const LanguagePackHeader *header) override
293 {
294 this->Write({reinterpret_cast<const char *>(header), sizeof(*header)});
295 }
296
297 void Finalise() override
298 {
299 this->output_stream.put(0);
300 this->FileWriter::Finalise();
301 }
302
303 void Write(std::string_view buffer) override
304 {
305 this->output_stream.write(buffer.data(), buffer.size());
306 }
307};
308
310static const OptionData _opts[] = {
311 { .type = ODF_NO_VALUE, .id = 'C', .longname = "-export-commands" },
312 { .type = ODF_NO_VALUE, .id = 'L', .longname = "-export-plurals" },
313 { .type = ODF_NO_VALUE, .id = 'P', .longname = "-export-pragmas" },
314 { .type = ODF_NO_VALUE, .id = 't', .shortname = 't', .longname = "--todo" },
315 { .type = ODF_NO_VALUE, .id = 'w', .shortname = 'w', .longname = "--warning" },
316 { .type = ODF_NO_VALUE, .id = 'h', .shortname = 'h', .longname = "--help" },
317 { .type = ODF_NO_VALUE, .id = 'h', .shortname = '?' },
318 { .type = ODF_HAS_VALUE, .id = 's', .shortname = 's', .longname = "--source_dir" },
319 { .type = ODF_HAS_VALUE, .id = 'd', .shortname = 'd', .longname = "--dest_dir" },
320};
321
322int CDECL main(int argc, char *argv[])
323{
324 std::filesystem::path src_dir(".");
325 std::filesystem::path dest_dir;
326
327 std::vector<std::string_view> params;
328 for (int i = 1; i < argc; ++i) params.emplace_back(argv[i]);
329 GetOptData mgo(params, _opts);
330 for (;;) {
331 int i = mgo.GetOpt();
332 if (i == -1) break;
333
334 switch (i) {
335 case 'C':
336 fmt::print("args\tflags\tcommand\treplacement\n");
337 for (const auto &cs : _cmd_structs) {
338 char flags;
339 if (cs.proc == EmitGender) {
340 flags = 'g'; // Command needs number of parameters defined by number of genders
341 } else if (cs.proc == EmitPlural) {
342 flags = 'p'; // Command needs number of parameters defined by plural value
343 } else if (cs.flags.Test(CmdFlag::DontCount)) {
344 flags = 'i'; // Command may be in the translation when it is not in base
345 } else {
346 flags = '0'; // Command needs no parameters
347 }
348 fmt::print("{}\t{:c}\t\"{}\"\t\"{}\"\n", cs.consumes, flags, cs.cmd, cs.cmd.find("STRING") != std::string::npos ? "STRING" : cs.cmd);
349 }
350 return 0;
351
352 case 'L':
353 fmt::print("count\tdescription\tnames\n");
354 for (const auto &pf : _plural_forms) {
355 fmt::print("{}\t\"{}\"\t{}\n", pf.plural_count, pf.description, pf.names);
356 }
357 return 0;
358
359 case 'P':
360 fmt::print("name\tflags\tdefault\tdescription\n");
361 for (const auto &pragma : _pragmas) {
362 fmt::print("\"{}\"\t{}\t\"{}\"\t\"{}\"\n",
363 pragma[0], pragma[1], pragma[2], pragma[3]);
364 }
365 return 0;
366
367 case 't':
368 _strgen.annotate_todos = true;
369 break;
370
371 case 'w':
372 _strgen.show_warnings = true;
373 break;
374
375 case 'h':
376 fmt::print(
377 "strgen\n"
378 " -t | --todo replace any untranslated strings with '<TODO>'\n"
379 " -w | --warning print a warning for any untranslated strings\n"
380 " -h | -? | --help print this help message and exit\n"
381 " -s | --source_dir search for english.txt in the specified directory\n"
382 " -d | --dest_dir put output file in the specified directory, create if needed\n"
383 " -export-commands export all commands and exit\n"
384 " -export-plurals export all plural forms and exit\n"
385 " -export-pragmas export all pragmas and exit\n"
386 " Run without parameters and strgen will search for english.txt and parse it,\n"
387 " creating strings.h. Passing an argument, strgen will translate that language\n"
388 " file using english.txt as a reference and output <language>.lng.\n"
389 );
390 return 0;
391
392 case 's':
393 src_dir = mgo.opt;
394 break;
395
396 case 'd':
397 dest_dir = mgo.opt;
398 break;
399
400 case -2:
401 fmt::print(stderr, "Invalid arguments\n");
402 return 0;
403 }
404 }
405
406 if (dest_dir.empty()) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
407
408 try {
409 /* strgen has two modes of operation. If no (free) arguments are passed
410 * strgen generates strings.h to the destination directory. If it is supplied
411 * with a (free) parameter the program will translate that language to destination
412 * directory. As input english.txt is parsed from the source directory */
413 if (mgo.arguments.empty()) {
414 std::filesystem::path input_path = std::move(src_dir);
415 input_path /= "english.txt";
416
417 /* parse master file */
419 FileStringReader master_reader(data, input_path, true, false);
420 master_reader.ParseFile();
421 if (_strgen.errors != 0) return 1;
422
423 /* write strings.h */
424 std::filesystem::path output_path = dest_dir;
425 std::filesystem::create_directories(dest_dir);
426 output_path /= "strings.h";
427
428 HeaderFileWriter writer(output_path);
429 writer.WriteHeader(data);
430 writer.Finalise(data);
431 if (_strgen.errors != 0) return 1;
432 } else {
433 std::filesystem::path input_path = std::move(src_dir);
434 input_path /= "english.txt";
435
437 /* parse master file and check if target file is correct */
438 FileStringReader master_reader(data, input_path, true, false);
439 master_reader.ParseFile();
440
441 for (auto &argument: mgo.arguments) {
442 data.FreeTranslation();
443
444 std::filesystem::path lang_file = argument;
445 FileStringReader translation_reader(data, lang_file, false, lang_file.filename() != "english.txt");
446 translation_reader.ParseFile(); // target file
447 if (_strgen.errors != 0) return 1;
448
449 /* get the targetfile, strip any directories and append to destination path */
450 std::filesystem::path output_file = dest_dir;
451 output_file /= lang_file.filename();
452 output_file.replace_extension("lng");
453
454 LanguageFileWriter writer(output_file);
455 writer.WriteLang(data);
456 writer.Finalise();
457
458 /* if showing warnings, print a summary of the language */
459 if (_strgen.show_warnings) {
460 fmt::print("{} warnings and {} errors for {}\n", _strgen.warnings, _strgen.errors, output_file);
461 }
462 }
463 }
464 } catch (...) {
465 return 2;
466 }
467
468 return 0;
469}
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".
Control codes that are embedded in the translation strings.
Error reporting related functions.
Library for parsing command-line options.
@ 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
A number of safeguards to prevent using unsafe methods.
static const OptionData _opts[]
Options of settingsgen.
static bool CompareFiles(std::filesystem::path path1, std::filesystem::path path2)
Compare two files for identity.
Definition of base types and functions in a cross-platform compatible way.
void FatalErrorI(const std::string &str)
Error handling for fatal non-user errors.
Definition strgen.cpp:60
Structures related to strgen.
Tables of commands for 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:56
Functions related to low-level strings.
#define NBSP
A non-breaking space.
Definition string_type.h:16
Types related to strings.
@ 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
FileWriter(FileHandle &&file)
Create the file writer, so it writes to a specific file.
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:248
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:240
Base class for writing the header, i.e.
Definition strgen.h:89
Class for writing a language to disk.
Definition strgen.cpp:283
LanguageFileWriter(const std::filesystem::path &path)
Open a file to write to.
Definition strgen.cpp:288
void Finalise() override
Finalise writing the file.
Definition strgen.cpp:297
void WriteHeader(const LanguagePackHeader *header) override
Write the header metadata.
Definition strgen.cpp:292
void Write(std::string_view buffer) override
Write a number of bytes.
Definition strgen.cpp:303
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:110
Data of an option.
Definition getoptdata.h:21
Information about the currently known strings.
Definition strgen.h:43
uint32_t Version() const
Make a hash of the file to get a unique "version number".
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.
bool master
Are we reading the master file?
Definition strgen.h:62