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,
"numberformat ", 13)) {
129 if (result.has_value()) FatalError(
"Invalid number format: {}", *result);
132 }
else if (!memcmp(str,
"numberabbreviations ", 20)) {
137 if (result.has_value()) FatalError(
"Invalid number abbreviations: {}", *result);
140 }
else if (!memcmp(str,
"decimalsep ", 11)) {
143 }
else if (!memcmp(str,
"winlangid ", 10)) {
144 const char *buf = str + 10;
145 long langid = std::strtol(buf,
nullptr, 16);
146 if (langid > (
long)UINT16_MAX || langid < 0) {
147 FatalError(
"Invalid winlangid {}", buf);
150 }
else if (!memcmp(str,
"grflangid ", 10)) {
151 const char *buf = str + 10;
152 long langid = std::strtol(buf,
nullptr, 16);
153 if (langid >= 0x7F || langid < 0) {
154 FatalError(
"Invalid grflangid {}", buf);
157 }
else if (!memcmp(str,
"gender ", 7)) {
158 if (this->
master) FatalError(
"Genders are not allowed in the base translation.");
162 const char *s = ParseWord(&buf);
164 if (s ==
nullptr)
break;
169 }
else if (!memcmp(str,
"case ", 5)) {
170 if (this->
master) FatalError(
"Cases are not allowed in the base translation.");
174 const char *s = ParseWord(&buf);
176 if (s ==
nullptr)
break;
186 bool CompareFiles(
const std::filesystem::path &path1,
const std::filesystem::path &path2)
189 std::error_code error_code;
190 if (std::filesystem::file_size(path1, error_code) != std::filesystem::file_size(path2, error_code))
return false;
192 std::ifstream stream1(path1, std::ifstream::binary);
193 std::ifstream stream2(path2, std::ifstream::binary);
195 return std::equal(std::istreambuf_iterator<char>(stream1.rdbuf()),
196 std::istreambuf_iterator<char>(),
197 std::istreambuf_iterator<char>(stream2.rdbuf()));
203 const std::filesystem::path
path;
212 this->output_stream.open(
path, openmode);
218 this->output_stream.close();
225 if (this->output_stream.is_open()) {
226 this->output_stream.close();
227 std::filesystem::remove(this->path);
246 this->
output_stream <<
"/* This file is automatically generated. Do not modify */\n\n";
254 fmt::print(this->
output_stream,
"static const StringID {} = 0x{:X};\n", name, stringid);
262 int max_plural_forms = 0;
264 max_plural_forms = std::max(max_plural_forms,
_plural_forms[i].plural_count);
269 "static const uint LANGUAGE_PACK_VERSION = 0x{:X};\n"
270 "static const uint LANGUAGE_MAX_PLURAL = {};\n"
271 "static const uint LANGUAGE_MAX_PLURAL_FORMS = {};\n"
272 "static const uint LANGUAGE_TOTAL_STRINGS = {};\n"
281 std::error_code error_code;
284 std::filesystem::remove(this->
path, error_code);
288 std::filesystem::remove(this->real_path, error_code);
290 std::filesystem::rename(this->
path, this->real_path, error_code);
291 if (error_code) FatalError(
"rename({}, {}) failed: {}", this->
path, this->real_path, error_code.message());
308 this->
Write((
const byte *)header,
sizeof(*header));
317 void Write(
const byte *buffer,
size_t length)
override
337 int CDECL
main(
int argc,
char *argv[])
339 std::filesystem::path src_dir(
".");
340 std::filesystem::path dest_dir;
344 int i = mgo.GetOpt();
349 fmt::print(
"args\tflags\tcommand\treplacement\n");
350 for (
const CmdStruct *cs = _cmd_structs; cs <
endof(_cmd_structs); cs++) {
352 if (cs->proc == EmitGender) {
354 }
else if (cs->proc == EmitPlural) {
361 fmt::print(
"{}\t{:c}\t\"{}\"\t\"{}\"\n", cs->consumes, flags, cs->cmd, strstr(cs->cmd,
"STRING") ?
"STRING" : cs->cmd);
366 fmt::print(
"count\tdescription\tnames\n");
368 fmt::print(
"{}\t\"{}\"\t{}\n", pf->plural_count, pf->description, pf->names);
373 fmt::print(
"name\tflags\tdefault\tdescription\n");
375 fmt::print(
"\"{}\"\t{}\t\"{}\"\t\"{}\"\n",
391 " -t | --todo replace any untranslated strings with '<TODO>'\n"
392 " -w | --warning print a warning for any untranslated strings\n"
393 " -h | -? | --help print this help message and exit\n"
394 " -s | --source_dir search for english.txt in the specified directory\n"
395 " -d | --dest_dir put output file in the specified directory, create if needed\n"
396 " -export-commands export all commands and exit\n"
397 " -export-plurals export all plural forms and exit\n"
398 " -export-pragmas export all pragmas and exit\n"
399 " Run without parameters and strgen will search for english.txt and parse it,\n"
400 " creating strings.h. Passing an argument, strgen will translate that language\n"
401 " file using english.txt as a reference and output <language>.lng.\n"
414 fmt::print(stderr,
"Invalid arguments\n");
419 if (dest_dir.empty()) dest_dir = src_dir;
426 if (mgo.numleft == 0) {
427 std::filesystem::path input_path = src_dir;
428 input_path /=
"english.txt";
433 master_reader.ParseFile();
434 if (_errors != 0)
return 1;
437 std::filesystem::path output_path = dest_dir;
438 std::filesystem::create_directories(dest_dir);
439 output_path /=
"strings.h";
442 writer.WriteHeader(data);
443 writer.Finalise(data);
444 if (_errors != 0)
return 1;
445 }
else if (mgo.numleft >= 1) {
446 std::filesystem::path input_path = src_dir;
447 input_path /=
"english.txt";
452 master_reader.ParseFile();
454 for (
int i = 0; i < mgo.numleft; i++) {
455 data.FreeTranslation();
457 std::filesystem::path lang_file = mgo.argv[i];
458 FileStringReader translation_reader(data, lang_file,
false, lang_file.filename() !=
"english.txt");
459 translation_reader.ParseFile();
460 if (_errors != 0)
return 1;
463 std::filesystem::path output_file = dest_dir;
464 output_file /= lang_file.filename();
465 output_file.replace_extension(
"lng");
468 writer.WriteLang(data);
472 if ((_show_todo & 2) != 0) {
473 fmt::print(
"{} warnings and {} errors for {}\n", _warnings, _errors, output_file);