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, pf.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);
277 std::filesystem::rename(this->
path, this->real_path, error_code);
278 if (error_code) FatalError(
"rename({}, {}) failed: {}", this->
path, this->real_path, error_code.message());
295 this->
Write((
const uint8_t *)header,
sizeof(*header));
304 void Write(
const uint8_t *buffer,
size_t length)
override
313 { .type =
ODF_NO_VALUE, .id =
'L', .longname =
"-export-plurals" },
314 { .type =
ODF_NO_VALUE, .id =
'P', .longname =
"-export-pragmas" },
315 { .type =
ODF_NO_VALUE, .id =
't', .shortname =
't', .longname =
"--todo" },
316 { .type =
ODF_NO_VALUE, .id =
'w', .shortname =
'w', .longname =
"--warning" },
317 { .type =
ODF_NO_VALUE, .id =
'h', .shortname =
'h', .longname =
"--help" },
319 { .type =
ODF_HAS_VALUE, .id =
's', .shortname =
's', .longname =
"--source_dir" },
320 { .type =
ODF_HAS_VALUE, .id =
'd', .shortname =
'd', .longname =
"--dest_dir" },
323 int CDECL
main(
int argc,
char *argv[])
325 std::filesystem::path src_dir(
".");
326 std::filesystem::path dest_dir;
330 int i = mgo.GetOpt();
335 fmt::print(
"args\tflags\tcommand\treplacement\n");
336 for (
const auto &cs : _cmd_structs) {
338 if (cs.proc == EmitGender) {
340 }
else if (cs.proc == EmitPlural) {
347 fmt::print(
"{}\t{:c}\t\"{}\"\t\"{}\"\n", cs.consumes, flags, cs.cmd, strstr(cs.cmd,
"STRING") ?
"STRING" : cs.cmd);
352 fmt::print(
"count\tdescription\tnames\n");
354 fmt::print(
"{}\t\"{}\"\t{}\n", pf.plural_count, pf.description, pf.names);
359 fmt::print(
"name\tflags\tdefault\tdescription\n");
360 for (
const auto &pragma :
_pragmas) {
361 fmt::print(
"\"{}\"\t{}\t\"{}\"\t\"{}\"\n",
362 pragma[0], pragma[1], pragma[2], pragma[3]);
377 " -t | --todo replace any untranslated strings with '<TODO>'\n"
378 " -w | --warning print a warning for any untranslated strings\n"
379 " -h | -? | --help print this help message and exit\n"
380 " -s | --source_dir search for english.txt in the specified directory\n"
381 " -d | --dest_dir put output file in the specified directory, create if needed\n"
382 " -export-commands export all commands and exit\n"
383 " -export-plurals export all plural forms and exit\n"
384 " -export-pragmas export all pragmas and exit\n"
385 " Run without parameters and strgen will search for english.txt and parse it,\n"
386 " creating strings.h. Passing an argument, strgen will translate that language\n"
387 " file using english.txt as a reference and output <language>.lng.\n"
400 fmt::print(stderr,
"Invalid arguments\n");
405 if (dest_dir.empty()) dest_dir = src_dir;
412 if (mgo.arguments.empty()) {
413 std::filesystem::path input_path = src_dir;
414 input_path /=
"english.txt";
419 master_reader.ParseFile();
420 if (_errors != 0)
return 1;
423 std::filesystem::path output_path = dest_dir;
424 std::filesystem::create_directories(dest_dir);
425 output_path /=
"strings.h";
428 writer.WriteHeader(data);
429 writer.Finalise(data);
430 if (_errors != 0)
return 1;
432 std::filesystem::path input_path = src_dir;
433 input_path /=
"english.txt";
438 master_reader.ParseFile();
440 for (
auto &argument: mgo.arguments) {
441 data.FreeTranslation();
443 std::filesystem::path lang_file = argument;
444 FileStringReader translation_reader(data, lang_file,
false, lang_file.filename() !=
"english.txt");
445 translation_reader.ParseFile();
446 if (_errors != 0)
return 1;
449 std::filesystem::path output_file = dest_dir;
450 output_file /= lang_file.filename();
451 output_file.replace_extension(
"lng");
454 writer.WriteLang(data);
458 if ((_show_todo & 2) != 0) {
459 fmt::print(
"{} warnings and {} errors for {}\n", _warnings, _errors, output_file);