10 #include "../stdafx.h"
11 #include "../core/endian_func.hpp"
12 #include "../core/mem_func.hpp"
13 #include "../error_func.h"
14 #include "../string_func.h"
15 #include "../strings_type.h"
16 #include "../misc/getoptdata.h"
17 #include "../table/control_codes.h"
18 #include "../3rdparty/fmt/std.h"
25 #include "../table/strgen_tables.h"
27 #include "../safeguards.h"
31 # define LINE_NUM_FMT(s) "{} ({}): warning: {} (" s ")\n"
33 # define LINE_NUM_FMT(s) "{}:{}: " s ": {}\n"
36 void StrgenWarningI(
const std::string &msg)
39 fmt::print(stderr, LINE_NUM_FMT(
"warning"),
_file,
_cur_line, msg);
46 void StrgenErrorI(
const std::string &msg)
52 [[noreturn]]
void StrgenFatalI(
const std::string &msg)
56 fmt::print(stderr, LINE_NUM_FMT(
"warning"),
_file,
_cur_line,
"language is not compiled");
58 throw std::exception();
65 fmt::print(stderr, LINE_NUM_FMT(
"warning"),
_file,
_cur_line,
"language is not compiled");
72 std::ifstream input_stream;
84 this->input_stream.open(
file, std::ifstream::binary);
90 if (!std::getline(this->input_stream, result))
return std::nullopt;
101 FatalError(
"Language must include ##name, ##ownname and ##isocode");
108 if (!memcmp(str,
"id ", 3)) {
110 }
else if (!memcmp(str,
"name ", 5)) {
112 }
else if (!memcmp(str,
"ownname ", 8)) {
114 }
else if (!memcmp(str,
"isocode ", 8)) {
116 }
else if (!memcmp(str,
"textdir ", 8)) {
117 if (!memcmp(str + 8,
"ltr", 3)) {
119 }
else if (!memcmp(str + 8,
"rtl", 3)) {
122 FatalError(
"Invalid textdir {}", str + 8);
124 }
else if (!memcmp(str,
"digitsep ", 9)) {
127 }
else if (!memcmp(str,
"digitsepcur ", 12)) {
130 }
else if (!memcmp(str,
"decimalsep ", 11)) {
133 }
else if (!memcmp(str,
"winlangid ", 10)) {
134 const char *buf = str + 10;
135 long langid = std::strtol(buf,
nullptr, 16);
136 if (langid > (
long)UINT16_MAX || langid < 0) {
137 FatalError(
"Invalid winlangid {}", buf);
140 }
else if (!memcmp(str,
"grflangid ", 10)) {
141 const char *buf = str + 10;
142 long langid = std::strtol(buf,
nullptr, 16);
143 if (langid >= 0x7F || langid < 0) {
144 FatalError(
"Invalid grflangid {}", buf);
147 }
else if (!memcmp(str,
"gender ", 7)) {
148 if (this->
master) FatalError(
"Genders are not allowed in the base translation.");
152 const char *s = ParseWord(&buf);
154 if (s ==
nullptr)
break;
159 }
else if (!memcmp(str,
"case ", 5)) {
160 if (this->
master) FatalError(
"Cases are not allowed in the base translation.");
164 const char *s = ParseWord(&buf);
166 if (s ==
nullptr)
break;
176 bool CompareFiles(
const std::filesystem::path &path1,
const std::filesystem::path &path2)
179 std::error_code error_code;
180 if (std::filesystem::file_size(path1, error_code) != std::filesystem::file_size(path2, error_code))
return false;
182 std::ifstream stream1(path1, std::ifstream::binary);
183 std::ifstream stream2(path2, std::ifstream::binary);
185 return std::equal(std::istreambuf_iterator<char>(stream1.rdbuf()),
186 std::istreambuf_iterator<char>(),
187 std::istreambuf_iterator<char>(stream2.rdbuf()));
193 const std::filesystem::path
path;
202 this->output_stream.open(
path, openmode);
208 this->output_stream.close();
215 if (this->output_stream.is_open()) {
216 this->output_stream.close();
217 std::filesystem::remove(this->path);
236 this->
output_stream <<
"/* This file is automatically generated. Do not modify */\n\n";
244 fmt::print(this->
output_stream,
"static const StringID {} = 0x{:X};\n", name, stringid);
252 int max_plural_forms = 0;
254 max_plural_forms = std::max(max_plural_forms,
_plural_forms[i].plural_count);
259 "static const uint LANGUAGE_PACK_VERSION = 0x{:X};\n"
260 "static const uint LANGUAGE_MAX_PLURAL = {};\n"
261 "static const uint LANGUAGE_MAX_PLURAL_FORMS = {};\n"
262 "static const uint LANGUAGE_TOTAL_STRINGS = {};\n"
271 std::error_code error_code;
274 std::filesystem::remove(this->
path, error_code);
278 std::filesystem::remove(this->real_path, error_code);
280 std::filesystem::rename(this->
path, this->real_path, error_code);
281 if (error_code) FatalError(
"rename({}, {}) failed: {}", this->
path, this->real_path, error_code.message());
298 this->
Write((
const byte *)header,
sizeof(*header));
307 void Write(
const byte *buffer,
size_t length)
override
327 int CDECL
main(
int argc,
char *argv[])
329 std::filesystem::path src_dir(
".");
330 std::filesystem::path dest_dir;
334 int i = mgo.GetOpt();
339 fmt::print(
"args\tflags\tcommand\treplacement\n");
340 for (
const CmdStruct *cs = _cmd_structs; cs <
endof(_cmd_structs); cs++) {
342 if (cs->proc == EmitGender) {
344 }
else if (cs->proc == EmitPlural) {
351 fmt::print(
"{}\t{:c}\t\"{}\"\t\"{}\"\n", cs->consumes, flags, cs->cmd, strstr(cs->cmd,
"STRING") ?
"STRING" : cs->cmd);
356 fmt::print(
"count\tdescription\tnames\n");
358 fmt::print(
"{}\t\"{}\"\t{}\n", pf->plural_count, pf->description, pf->names);
363 fmt::print(
"name\tflags\tdefault\tdescription\n");
365 fmt::print(
"\"{}\"\t{}\t\"{}\"\t\"{}\"\n",
381 " -t | --todo replace any untranslated strings with '<TODO>'\n"
382 " -w | --warning print a warning for any untranslated strings\n"
383 " -h | -? | --help print this help message and exit\n"
384 " -s | --source_dir search for english.txt in the specified directory\n"
385 " -d | --dest_dir put output file in the specified directory, create if needed\n"
386 " -export-commands export all commands and exit\n"
387 " -export-plurals export all plural forms and exit\n"
388 " -export-pragmas export all pragmas and exit\n"
389 " Run without parameters and strgen will search for english.txt and parse it,\n"
390 " creating strings.h. Passing an argument, strgen will translate that language\n"
391 " file using english.txt as a reference and output <language>.lng.\n"
404 fmt::print(stderr,
"Invalid arguments\n");
409 if (dest_dir.empty()) dest_dir = src_dir;
416 if (mgo.numleft == 0) {
417 std::filesystem::path input_path = src_dir;
418 input_path /=
"english.txt";
423 master_reader.ParseFile();
424 if (_errors != 0)
return 1;
427 std::filesystem::path output_path = dest_dir;
428 std::filesystem::create_directories(dest_dir);
429 output_path /=
"strings.h";
432 writer.WriteHeader(data);
433 writer.Finalise(data);
434 if (_errors != 0)
return 1;
435 }
else if (mgo.numleft >= 1) {
436 std::filesystem::path input_path = src_dir;
437 input_path /=
"english.txt";
442 master_reader.ParseFile();
444 for (
int i = 0; i < mgo.numleft; i++) {
445 data.FreeTranslation();
447 std::filesystem::path lang_file = mgo.argv[i];
448 FileStringReader translation_reader(data, lang_file,
false, lang_file.filename() !=
"english.txt");
449 translation_reader.ParseFile();
450 if (_errors != 0)
return 1;
453 std::filesystem::path output_file = dest_dir;
454 output_file /= lang_file.filename();
455 output_file.replace_extension(
"lng");
458 writer.WriteLang(data);
462 if ((_show_todo & 2) != 0) {
463 fmt::print(
"{} warnings and {} errors for {}\n", _warnings, _errors, output_file);