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"
36void StrgenWarningI(
const std::string &msg)
39 fmt::print(stderr, LINE_NUM_FMT(
"warning"),
_file,
_cur_line, msg);
46void 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;
176bool 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;
272 if (CompareFiles(this->
path, this->real_path)) {
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" },
323int 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);
@ ODF_NO_VALUE
A plain option (no value attached to it).
@ ODF_HAS_VALUE
An option with a value.
static const uint8_t MAX_NUM_GENDERS
Maximum number of supported genders.
static const uint8_t MAX_NUM_CASES
Maximum number of supported cases.
static const OptionData _opts[]
Options of strgen.
void FatalErrorI(const std::string &msg)
Error handling for fatal non-user errors.
Structures related to strgen.
LanguagePackHeader _lang
Header information about a language.
int _cur_line
The current line we're parsing in the input file.
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
static const char *const _pragmas[][4]
All pragmas used.
static const PluralForm _plural_forms[]
All plural forms used.
@ C_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.
bool StrEmpty(const char *s)
Check if a string buffer is empty.
#define NBSP
A non-breaking space.
@ TD_LTR
Text is written left-to-right by default.
@ TD_RTL
Text is written right-to-left by default.
@ TEXT_TAB_END
End of language files.
A reader that simply reads using fopen.
void HandlePragma(char *str) override
Handle the pragma of the file.
FileStringReader(StringData &data, const std::filesystem::path &file, bool master, bool translation)
Create the reader.
void ParseFile() override
Start parsing the file.
std::optional< std::string > ReadLine() override
Read a single line from the source of strings.
Yes, simply writing to a file.
std::ofstream output_stream
The stream to write all the output to.
virtual ~FileWriter()
Make sure the file is closed.
const std::filesystem::path path
The file name we're writing to.
FileWriter(const std::filesystem::path &path, std::ios_base::openmode openmode)
Open a file to write to.
void Finalise()
Finalise the writing.
Data storage for parsing command line options.
Class for writing a language to disk.
LanguageFileWriter(const std::filesystem::path &path)
Open a file to write to.
void Finalise() override
Finalise writing the file.
void WriteHeader(const LanguagePackHeader *header) override
Write the header metadata.
void Write(const uint8_t *buffer, size_t length) override
Write a number of bytes.
Base class for all language writers.
OptionDataType type
The type of option.
Information about the currently known strings.
size_t next_string_id
The next string ID to allocate.
uint Version() const
Make a hash of the file to get a unique "version number".
Helper for reading strings.
const std::string file
The file we are reading.
StringData & data
The data to fill during reading.
virtual void ParseFile()
Start parsing the file.
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false.
virtual void HandlePragma(char *str)
Handle the pragma of the file.
bool master
Are we reading the master file?