16#include "../3rdparty/fmt/std.h"
29# define LINE_NUM_FMT(s) "{} ({}): warning: {} (" s ")\n"
31# define LINE_NUM_FMT(s) "{}:{}: " s ": {}\n"
34void StrgenWarningI(
const std::string &msg)
36 if (_strgen.translation) {
37 fmt::print(stderr, LINE_NUM_FMT(
"info"), _strgen.file, _strgen.cur_line, msg);
39 fmt::print(stderr, LINE_NUM_FMT(
"warning"), _strgen.file, _strgen.cur_line, msg);
44void StrgenErrorI(
const std::string &msg)
46 fmt::print(stderr, LINE_NUM_FMT(
"error"), _strgen.file, _strgen.cur_line, msg);
50[[noreturn]]
void StrgenFatalI(
const std::string &msg)
52 fmt::print(stderr, LINE_NUM_FMT(
"FATAL"), _strgen.file, _strgen.cur_line, msg);
54 fmt::print(stderr, LINE_NUM_FMT(
"warning"), _strgen.file, _strgen.cur_line,
"language is not compiled");
56 throw std::exception();
62 fmt::print(stderr, LINE_NUM_FMT(
"FATAL"), _strgen.file, _strgen.cur_line, str);
64 fmt::print(stderr, LINE_NUM_FMT(
"warning"), _strgen.file, _strgen.cur_line,
"language is not compiled");
71 std::ifstream input_stream;
83 this->input_stream.open(
file, std::ifstream::binary);
89 if (!std::getline(this->input_stream, result))
return std::nullopt;
99 if (*_strgen.lang.name ==
'\0' || *_strgen.lang.own_name ==
'\0' || *_strgen.lang.isocode ==
'\0') {
100 FatalError(
"Language must include ##name, ##ownname and ##isocode");
111 }
else if (name ==
"name") {
113 }
else if (name ==
"ownname") {
115 }
else if (name ==
"isocode") {
117 }
else if (name ==
"textdir") {
121 }
else if (dir ==
"rtl") {
124 FatalError(
"Invalid textdir {}", dir);
126 }
else if (name ==
"digitsep") {
129 }
else if (name ==
"digitsepcur") {
132 }
else if (name ==
"decimalsep") {
135 }
else if (name ==
"winlangid") {
137 if (langid > UINT16_MAX || langid < 0) {
138 FatalError(
"Invalid winlangid {}", langid);
140 lang.
winlangid =
static_cast<uint16_t
>(langid);
141 }
else if (name ==
"grflangid") {
143 if (langid >= 0x7F || langid < 0) {
144 FatalError(
"Invalid grflangid {}", langid);
147 }
else if (name ==
"gender") {
148 if (this->
master) FatalError(
"Genders are not allowed in the base translation.");
150 auto s = ParseWord(consumer);
152 if (!s.has_value())
break;
157 }
else if (name ==
"case") {
158 if (this->
master) FatalError(
"Cases are not allowed in the base translation.");
160 auto s = ParseWord(consumer);
162 if (!s.has_value())
break;
172static bool CompareFiles(
const std::filesystem::path &path1,
const std::filesystem::path &path2)
175 std::error_code error_code;
176 if (std::filesystem::file_size(path1, error_code) != std::filesystem::file_size(path2, error_code))
return false;
178 std::ifstream stream1(path1, std::ifstream::binary);
179 std::ifstream stream2(path2, std::ifstream::binary);
181 return std::equal(std::istreambuf_iterator<char>(stream1.rdbuf()),
182 std::istreambuf_iterator<char>(),
183 std::istreambuf_iterator<char>(stream2.rdbuf()));
189 const std::filesystem::path
path;
198 this->output_stream.open(
path, openmode);
204 this->output_stream.close();
211 if (this->output_stream.is_open()) {
212 this->output_stream.close();
213 std::filesystem::remove(this->path);
223 size_t total_strings;
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";
243 fmt::print(this->
output_stream,
"static const StringID {} = 0x{:X};\n", name, stringid);
251 size_t max_plural_forms = 0;
253 max_plural_forms = std::max(max_plural_forms, pf.plural_count);
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"
270 std::error_code error_code;
271 if (CompareFiles(this->
path, this->real_path)) {
273 std::filesystem::remove(this->
path, error_code);
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());
294 this->
Write({
reinterpret_cast<const char *
>(header),
sizeof(*header)});
303 void Write(std::string_view buffer)
override
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" },
318 { .type =
ODF_HAS_VALUE, .id =
's', .shortname =
's', .longname =
"--source_dir" },
319 { .type =
ODF_HAS_VALUE, .id =
'd', .shortname =
'd', .longname =
"--dest_dir" },
322int CDECL main(
int argc,
char *argv[])
324 std::filesystem::path src_dir(
".");
325 std::filesystem::path dest_dir;
327 std::vector<std::string_view> params;
328 for (
int i = 1; i < argc; ++i) params.emplace_back(argv[i]);
331 int i = mgo.GetOpt();
336 fmt::print(
"args\tflags\tcommand\treplacement\n");
337 for (
const auto &cs : _cmd_structs) {
339 if (cs.proc == EmitGender) {
341 }
else if (cs.proc == EmitPlural) {
348 fmt::print(
"{}\t{:c}\t\"{}\"\t\"{}\"\n", cs.consumes, flags, cs.cmd, cs.cmd.find(
"STRING") != std::string::npos ?
"STRING" : cs.cmd);
353 fmt::print(
"count\tdescription\tnames\n");
355 fmt::print(
"{}\t\"{}\"\t{}\n", pf.plural_count, pf.description, pf.names);
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]);
368 _strgen.annotate_todos =
true;
372 _strgen.show_warnings =
true;
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"
401 fmt::print(stderr,
"Invalid arguments\n");
406 if (dest_dir.empty()) dest_dir = src_dir;
413 if (mgo.arguments.empty()) {
414 std::filesystem::path input_path = std::move(src_dir);
415 input_path /=
"english.txt";
420 master_reader.ParseFile();
421 if (_strgen.errors != 0)
return 1;
424 std::filesystem::path output_path = dest_dir;
425 std::filesystem::create_directories(dest_dir);
426 output_path /=
"strings.h";
429 writer.WriteHeader(data);
430 writer.Finalise(data);
431 if (_strgen.errors != 0)
return 1;
433 std::filesystem::path input_path = std::move(src_dir);
434 input_path /=
"english.txt";
439 master_reader.ParseFile();
441 for (
auto &argument: mgo.arguments) {
442 data.FreeTranslation();
444 std::filesystem::path lang_file = argument;
445 FileStringReader translation_reader(data, lang_file,
false, lang_file.filename() !=
"english.txt");
446 translation_reader.ParseFile();
447 if (_strgen.errors != 0)
return 1;
450 std::filesystem::path output_file = dest_dir;
451 output_file /= lang_file.filename();
452 output_file.replace_extension(
"lng");
455 writer.WriteLang(data);
459 if (_strgen.show_warnings) {
460 fmt::print(
"{} warnings and {} errors for {}\n", _strgen.warnings, _strgen.errors, output_file);
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).
@ ODF_HAS_VALUE
An option with a value.
static const uint8_t MAX_NUM_GENDERS
Maximum number of supported genders.
static const uint8_t CASE_GENDER_LEN
The (maximum) length of a case/gender string.
static const uint8_t MAX_NUM_CASES
Maximum number of supported cases.
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.
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.
Functions related to low-level strings.
#define NBSP
A non-breaking space.
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.
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.
void HandlePragma(std::string_view str, LanguagePackHeader &lang) override
Handle the pragma of the file.
Yes, simply writing to a file.
std::ofstream output_stream
The stream to write all the output to.
FileWriter(FileHandle &&file)
Create the file writer, so it writes to a specific file.
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(std::string_view buffer) override
Write a number of bytes.
Base class for all language writers.
Information about the currently known strings.
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.
StringReader(StringData &data, const std::string &file, bool master, bool translation)
Prepare 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(std::string_view str, LanguagePackHeader &lang)
Handle the pragma of the file.
bool master
Are we reading the master file?