2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file strgen.cpp Tool to create computer readable (stand-alone) translation files. */
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "../stdafx.h"
|
2007-12-25 15:08:56 +01:00
|
|
|
#include "../core/endian_func.hpp"
|
2023-05-18 11:20:35 +02:00
|
|
|
#include "../core/mem_func.hpp"
|
2023-04-19 22:47:36 +02:00
|
|
|
#include "../error_func.h"
|
2008-01-07 15:23:25 +01:00
|
|
|
#include "../string_func.h"
|
2008-10-17 19:42:51 +02:00
|
|
|
#include "../strings_type.h"
|
2011-02-18 21:52:42 +01:00
|
|
|
#include "../misc/getoptdata.h"
|
2008-01-13 02:21:35 +01:00
|
|
|
#include "../table/control_codes.h"
|
2023-04-29 21:40:06 +02:00
|
|
|
#include "../3rdparty/fmt/std.h"
|
2008-01-13 02:21:35 +01:00
|
|
|
|
2011-12-17 18:25:50 +01:00
|
|
|
#include "strgen.h"
|
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
2006-08-24 14:08:25 +02:00
|
|
|
|
2009-04-23 16:06:57 +02:00
|
|
|
#include "../table/strgen_tables.h"
|
2005-07-17 12:18:23 +02:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2005-07-17 12:18:23 +02:00
|
|
|
|
2006-05-27 18:12:16 +02:00
|
|
|
#ifdef _MSC_VER
|
2023-05-21 08:13:28 +02:00
|
|
|
# define LINE_NUM_FMT(s) "{} ({}): warning: {} (" s ")\n"
|
2006-05-27 18:12:16 +02:00
|
|
|
#else
|
2023-05-21 08:13:28 +02:00
|
|
|
# define LINE_NUM_FMT(s) "{}:{}: " s ": {}\n"
|
2006-05-27 18:12:16 +02:00
|
|
|
#endif
|
2005-07-15 16:53:44 +02:00
|
|
|
|
2023-04-18 20:24:21 +02:00
|
|
|
void StrgenWarningI(const std::string &msg)
|
2005-07-15 09:48:17 +02:00
|
|
|
{
|
2021-06-29 22:19:24 +02:00
|
|
|
if (_show_todo > 0) {
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, msg);
|
2021-06-29 22:19:24 +02:00
|
|
|
} else {
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, LINE_NUM_FMT("info"), _file, _cur_line, msg);
|
2021-06-29 22:19:24 +02:00
|
|
|
}
|
2005-07-15 16:53:44 +02:00
|
|
|
_warnings++;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 20:24:21 +02:00
|
|
|
void StrgenErrorI(const std::string &msg)
|
2005-07-15 16:53:44 +02:00
|
|
|
{
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, LINE_NUM_FMT("error"), _file, _cur_line, msg);
|
2005-07-15 16:53:44 +02:00
|
|
|
_errors++;
|
|
|
|
}
|
|
|
|
|
2024-01-31 21:03:17 +01:00
|
|
|
[[noreturn]] void StrgenFatalI(const std::string &msg)
|
2005-07-15 09:48:17 +02:00
|
|
|
{
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
|
2010-02-13 00:47:50 +01:00
|
|
|
#ifdef _MSC_VER
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
2010-02-13 00:47:50 +01:00
|
|
|
#endif
|
2011-12-17 13:15:19 +01:00
|
|
|
throw std::exception();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2024-01-31 21:03:17 +01:00
|
|
|
[[noreturn]] void FatalErrorI(const std::string &msg)
|
2011-12-17 18:25:50 +01:00
|
|
|
{
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
|
2011-12-17 19:34:03 +01:00
|
|
|
#ifdef _MSC_VER
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
2011-12-17 19:34:03 +01:00
|
|
|
#endif
|
|
|
|
exit(2);
|
2011-12-17 18:25:50 +01:00
|
|
|
}
|
2011-12-17 17:56:32 +01:00
|
|
|
|
|
|
|
/** A reader that simply reads using fopen. */
|
|
|
|
struct FileStringReader : StringReader {
|
2023-04-29 21:40:06 +02:00
|
|
|
std::ifstream input_stream;
|
2011-12-17 17:56:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the reader.
|
|
|
|
* @param data The data to fill during reading.
|
|
|
|
* @param file The file we are reading.
|
|
|
|
* @param master Are we reading the master file?
|
|
|
|
* @param translation Are we reading a translation?
|
|
|
|
*/
|
2023-04-29 21:40:06 +02:00
|
|
|
FileStringReader(StringData &data, const std::filesystem::path &file, bool master, bool translation) :
|
2023-11-10 01:17:36 +01:00
|
|
|
StringReader(data, file.generic_string(), master, translation)
|
2011-12-17 17:56:32 +01:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
this->input_stream.open(file, std::ifstream::binary);
|
2011-12-17 17:56:32 +01:00
|
|
|
}
|
|
|
|
|
2023-06-09 18:10:24 +02:00
|
|
|
std::optional<std::string> ReadLine() override
|
2011-12-17 17:56:32 +01:00
|
|
|
{
|
2023-06-09 18:10:24 +02:00
|
|
|
std::string result;
|
|
|
|
if (!std::getline(this->input_stream, result)) return std::nullopt;
|
|
|
|
return result;
|
2011-12-17 17:56:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 23:25:13 +01:00
|
|
|
void HandlePragma(char *str) override;
|
2011-12-17 17:56:32 +01:00
|
|
|
|
2019-03-03 23:25:13 +01:00
|
|
|
void ParseFile() override
|
2011-12-17 17:56:32 +01:00
|
|
|
{
|
|
|
|
this->StringReader::ParseFile();
|
|
|
|
|
|
|
|
if (StrEmpty(_lang.name) || StrEmpty(_lang.own_name) || StrEmpty(_lang.isocode)) {
|
2023-04-19 22:47:36 +02:00
|
|
|
FatalError("Language must include ##name, ##ownname and ##isocode");
|
2011-12-17 17:56:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void FileStringReader::HandlePragma(char *str)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2005-07-15 16:53:44 +02:00
|
|
|
if (!memcmp(str, "id ", 3)) {
|
2023-04-26 13:56:14 +02:00
|
|
|
this->data.next_string_id = std::strtoul(str + 3, nullptr, 0);
|
2005-07-15 16:53:44 +02:00
|
|
|
} else if (!memcmp(str, "name ", 5)) {
|
2010-11-07 19:20:18 +01:00
|
|
|
strecpy(_lang.name, str + 5, lastof(_lang.name));
|
2005-07-15 16:53:44 +02:00
|
|
|
} else if (!memcmp(str, "ownname ", 8)) {
|
2010-11-07 19:20:18 +01:00
|
|
|
strecpy(_lang.own_name, str + 8, lastof(_lang.own_name));
|
2005-07-15 16:53:44 +02:00
|
|
|
} else if (!memcmp(str, "isocode ", 8)) {
|
2010-11-07 19:20:18 +01:00
|
|
|
strecpy(_lang.isocode, str + 8, lastof(_lang.isocode));
|
2008-10-17 19:42:51 +02:00
|
|
|
} else if (!memcmp(str, "textdir ", 8)) {
|
|
|
|
if (!memcmp(str + 8, "ltr", 3)) {
|
2010-11-07 19:20:18 +01:00
|
|
|
_lang.text_dir = TD_LTR;
|
2008-10-17 19:42:51 +02:00
|
|
|
} else if (!memcmp(str + 8, "rtl", 3)) {
|
2010-11-07 19:20:18 +01:00
|
|
|
_lang.text_dir = TD_RTL;
|
2008-10-17 19:42:51 +02:00
|
|
|
} else {
|
2023-04-19 22:47:36 +02:00
|
|
|
FatalError("Invalid textdir {}", str + 8);
|
2008-10-17 19:42:51 +02:00
|
|
|
}
|
2024-02-22 18:08:33 +01:00
|
|
|
} else if (!memcmp(str, "digitsep ", 9)) {
|
|
|
|
str += 9;
|
|
|
|
strecpy(_lang.digit_group_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_group_separator));
|
|
|
|
} else if (!memcmp(str, "digitsepcur ", 12)) {
|
|
|
|
str += 12;
|
|
|
|
strecpy(_lang.digit_group_separator_currency, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_group_separator_currency));
|
2009-08-12 03:28:11 +02:00
|
|
|
} else if (!memcmp(str, "decimalsep ", 11)) {
|
|
|
|
str += 11;
|
2010-11-07 19:20:18 +01:00
|
|
|
strecpy(_lang.digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_decimal_separator));
|
2008-11-24 19:53:17 +01:00
|
|
|
} else if (!memcmp(str, "winlangid ", 10)) {
|
2009-03-13 01:30:26 +01:00
|
|
|
const char *buf = str + 10;
|
2023-04-26 13:56:14 +02:00
|
|
|
long langid = std::strtol(buf, nullptr, 16);
|
2009-06-23 00:04:48 +02:00
|
|
|
if (langid > (long)UINT16_MAX || langid < 0) {
|
2023-04-19 22:47:36 +02:00
|
|
|
FatalError("Invalid winlangid {}", buf);
|
2008-11-24 19:53:17 +01:00
|
|
|
}
|
2023-05-08 19:01:06 +02:00
|
|
|
_lang.winlangid = (uint16_t)langid;
|
2009-03-13 01:30:26 +01:00
|
|
|
} else if (!memcmp(str, "grflangid ", 10)) {
|
|
|
|
const char *buf = str + 10;
|
2023-04-26 13:56:14 +02:00
|
|
|
long langid = std::strtol(buf, nullptr, 16);
|
2009-03-13 01:30:26 +01:00
|
|
|
if (langid >= 0x7F || langid < 0) {
|
2023-04-19 22:47:36 +02:00
|
|
|
FatalError("Invalid grflangid {}", buf);
|
2009-03-13 01:30:26 +01:00
|
|
|
}
|
2023-05-08 19:01:06 +02:00
|
|
|
_lang.newgrflangid = (uint8_t)langid;
|
2005-07-16 22:58:04 +02:00
|
|
|
} else if (!memcmp(str, "gender ", 7)) {
|
2023-04-19 22:47:36 +02:00
|
|
|
if (this->master) FatalError("Genders are not allowed in the base translation.");
|
2009-01-10 01:31:47 +01:00
|
|
|
char *buf = str + 7;
|
2006-02-12 11:44:52 +01:00
|
|
|
|
2006-02-01 08:36:15 +01:00
|
|
|
for (;;) {
|
2009-01-10 01:31:47 +01:00
|
|
|
const char *s = ParseWord(&buf);
|
2006-02-12 11:44:52 +01:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
if (s == nullptr) break;
|
2023-04-19 22:47:36 +02:00
|
|
|
if (_lang.num_genders >= MAX_NUM_GENDERS) FatalError("Too many genders, max {}", MAX_NUM_GENDERS);
|
2010-11-13 15:36:43 +01:00
|
|
|
strecpy(_lang.genders[_lang.num_genders], s, lastof(_lang.genders[_lang.num_genders]));
|
|
|
|
_lang.num_genders++;
|
2005-07-16 22:58:04 +02:00
|
|
|
}
|
2005-07-17 12:18:23 +02:00
|
|
|
} else if (!memcmp(str, "case ", 5)) {
|
2023-04-19 22:47:36 +02:00
|
|
|
if (this->master) FatalError("Cases are not allowed in the base translation.");
|
2009-01-10 01:31:47 +01:00
|
|
|
char *buf = str + 5;
|
2006-02-12 11:44:52 +01:00
|
|
|
|
2006-02-01 08:36:15 +01:00
|
|
|
for (;;) {
|
2009-01-10 01:31:47 +01:00
|
|
|
const char *s = ParseWord(&buf);
|
2006-02-12 11:44:52 +01:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
if (s == nullptr) break;
|
2023-04-19 22:47:36 +02:00
|
|
|
if (_lang.num_cases >= MAX_NUM_CASES) FatalError("Too many cases, max {}", MAX_NUM_CASES);
|
2010-11-13 15:36:43 +01:00
|
|
|
strecpy(_lang.cases[_lang.num_cases], s, lastof(_lang.cases[_lang.num_cases]));
|
|
|
|
_lang.num_cases++;
|
2005-07-17 12:18:23 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
} else {
|
2012-09-09 17:55:20 +02:00
|
|
|
StringReader::HandlePragma(str);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
bool CompareFiles(const std::filesystem::path &path1, const std::filesystem::path &path2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
/* Check for equal size, but ignore the error code for cases when a file does not exist. */
|
|
|
|
std::error_code error_code;
|
|
|
|
if (std::filesystem::file_size(path1, error_code) != std::filesystem::file_size(path2, error_code)) return false;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
std::ifstream stream1(path1, std::ifstream::binary);
|
|
|
|
std::ifstream stream2(path2, std::ifstream::binary);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
return std::equal(std::istreambuf_iterator<char>(stream1.rdbuf()),
|
|
|
|
std::istreambuf_iterator<char>(),
|
|
|
|
std::istreambuf_iterator<char>(stream2.rdbuf()));
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
/** Base class for writing data to disk. */
|
|
|
|
struct FileWriter {
|
2023-04-29 21:40:06 +02:00
|
|
|
std::ofstream output_stream; ///< The stream to write all the output to.
|
|
|
|
const std::filesystem::path path; ///< The file name we're writing to.
|
2005-07-15 16:53:44 +02:00
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
/**
|
|
|
|
* Open a file to write to.
|
2023-04-29 21:40:06 +02:00
|
|
|
* @param path The path to the file to open.
|
|
|
|
* @param openmode The openmode flags for opening the file.
|
2011-12-17 13:15:19 +01:00
|
|
|
*/
|
2023-04-29 21:40:06 +02:00
|
|
|
FileWriter(const std::filesystem::path &path, std::ios_base::openmode openmode) : path(path)
|
2011-12-17 13:15:19 +01:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
this->output_stream.open(path, openmode);
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
/** Finalise the writing. */
|
|
|
|
void Finalise()
|
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
this->output_stream.close();
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
/** Make sure the file is closed. */
|
|
|
|
virtual ~FileWriter()
|
|
|
|
{
|
2013-01-08 23:46:42 +01:00
|
|
|
/* If we weren't closed an exception was thrown, so remove the temporary file. */
|
2023-04-29 21:40:06 +02:00
|
|
|
if (this->output_stream.is_open()) {
|
|
|
|
this->output_stream.close();
|
|
|
|
std::filesystem::remove(this->path);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-17 13:15:19 +01:00
|
|
|
};
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
struct HeaderFileWriter : HeaderWriter, FileWriter {
|
2023-04-29 21:40:06 +02:00
|
|
|
/** The real path we eventually want to write to. */
|
|
|
|
const std::filesystem::path real_path;
|
2011-12-17 13:15:19 +01:00
|
|
|
/** The previous string ID that was printed. */
|
|
|
|
int prev;
|
2021-04-11 13:56:24 +02:00
|
|
|
uint total_strings;
|
2005-07-15 16:53:44 +02:00
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
/**
|
|
|
|
* Open a file to write to.
|
2023-04-29 21:40:06 +02:00
|
|
|
* @param path The path to the file to open.
|
2011-12-17 13:15:19 +01:00
|
|
|
*/
|
2023-04-29 21:40:06 +02:00
|
|
|
HeaderFileWriter(const std::filesystem::path &path) : FileWriter("tmp.xxx", std::ofstream::out),
|
|
|
|
real_path(path), prev(0), total_strings(0)
|
2013-11-22 22:45:57 +01:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
this->output_stream << "/* This file is automatically generated. Do not modify */\n\n";
|
|
|
|
this->output_stream << "#ifndef TABLE_STRINGS_H\n";
|
|
|
|
this->output_stream << "#define TABLE_STRINGS_H\n";
|
2013-11-22 22:45:57 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 08:23:18 +02:00
|
|
|
void WriteStringID(const std::string &name, int stringid) override
|
2011-12-17 13:15:19 +01:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
if (prev + 1 != stringid) this->output_stream << "\n";
|
|
|
|
fmt::print(this->output_stream, "static const StringID {} = 0x{:X};\n", name, stringid);
|
2011-12-17 13:15:19 +01:00
|
|
|
prev = stringid;
|
2021-04-11 13:56:24 +02:00
|
|
|
total_strings++;
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2023-04-13 08:23:18 +02:00
|
|
|
void Finalise(const StringData &data) override
|
2011-12-17 13:15:19 +01:00
|
|
|
{
|
|
|
|
/* Find the plural form with the most amount of cases. */
|
|
|
|
int max_plural_forms = 0;
|
|
|
|
for (uint i = 0; i < lengthof(_plural_forms); i++) {
|
2021-01-08 11:16:18 +01:00
|
|
|
max_plural_forms = std::max(max_plural_forms, _plural_forms[i].plural_count);
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
fmt::print(this->output_stream,
|
2011-12-17 13:15:19 +01:00
|
|
|
"\n"
|
2023-05-21 08:13:28 +02:00
|
|
|
"static const uint LANGUAGE_PACK_VERSION = 0x{:X};\n"
|
|
|
|
"static const uint LANGUAGE_MAX_PLURAL = {};\n"
|
|
|
|
"static const uint LANGUAGE_MAX_PLURAL_FORMS = {};\n"
|
|
|
|
"static const uint LANGUAGE_TOTAL_STRINGS = {};\n"
|
2021-04-11 13:56:24 +02:00
|
|
|
"\n",
|
2023-05-21 08:13:28 +02:00
|
|
|
data.Version(), lengthof(_plural_forms), max_plural_forms, total_strings
|
2011-12-17 13:15:19 +01:00
|
|
|
);
|
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
this->output_stream << "#endif /* TABLE_STRINGS_H */\n";
|
2011-12-17 13:15:19 +01:00
|
|
|
|
|
|
|
this->FileWriter::Finalise();
|
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
std::error_code error_code;
|
|
|
|
if (CompareFiles(this->path, this->real_path)) {
|
2011-12-17 13:15:19 +01:00
|
|
|
/* files are equal. tmp.xxx is not needed */
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::remove(this->path, error_code); // Just ignore the error
|
2011-12-17 13:15:19 +01:00
|
|
|
} else {
|
|
|
|
/* else rename tmp.xxx into filename */
|
2018-12-09 02:28:14 +01:00
|
|
|
# if defined(_WIN32)
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::remove(this->real_path, error_code); // Just ignore the error, file probably doesn't exist
|
2018-12-09 02:28:14 +01:00
|
|
|
# endif
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::rename(this->path, this->real_path, error_code);
|
|
|
|
if (error_code) FatalError("rename({}, {}) failed: {}", this->path, this->real_path, error_code.message());
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
2005-07-15 16:53:44 +02:00
|
|
|
}
|
2011-12-17 13:15:19 +01:00
|
|
|
};
|
2005-07-15 16:53:44 +02:00
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
/** Class for writing a language to disk. */
|
|
|
|
struct LanguageFileWriter : LanguageWriter, FileWriter {
|
|
|
|
/**
|
|
|
|
* Open a file to write to.
|
2023-04-29 21:40:06 +02:00
|
|
|
* @param path The path to the file to open.
|
2011-12-17 13:15:19 +01:00
|
|
|
*/
|
2023-04-29 21:40:06 +02:00
|
|
|
LanguageFileWriter(const std::filesystem::path &path) : FileWriter(path, std::ofstream::binary | std::ofstream::out)
|
2011-12-17 13:15:19 +01:00
|
|
|
{
|
|
|
|
}
|
2010-02-13 00:45:25 +01:00
|
|
|
|
2023-04-13 08:23:18 +02:00
|
|
|
void WriteHeader(const LanguagePackHeader *header) override
|
2011-12-17 13:15:19 +01:00
|
|
|
{
|
2024-03-16 23:59:32 +01:00
|
|
|
this->Write((const uint8_t *)header, sizeof(*header));
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 08:23:18 +02:00
|
|
|
void Finalise() override
|
2011-12-17 13:15:19 +01:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
this->output_stream.put(0);
|
2011-12-17 13:15:19 +01:00
|
|
|
this->FileWriter::Finalise();
|
|
|
|
}
|
|
|
|
|
2024-03-16 23:59:32 +01:00
|
|
|
void Write(const uint8_t *buffer, size_t length) override
|
2011-12-17 13:15:19 +01:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
this->output_stream.write((const char *)buffer, length);
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
|
|
|
};
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
/** Options of strgen. */
|
|
|
|
static const OptionData _opts[] = {
|
|
|
|
GETOPT_GENERAL('C', '\0', "-export-commands", ODF_NO_VALUE),
|
|
|
|
GETOPT_GENERAL('L', '\0', "-export-plurals", ODF_NO_VALUE),
|
|
|
|
GETOPT_GENERAL('P', '\0', "-export-pragmas", ODF_NO_VALUE),
|
|
|
|
GETOPT_NOVAL( 't', "--todo"),
|
|
|
|
GETOPT_NOVAL( 'w', "--warning"),
|
|
|
|
GETOPT_NOVAL( 'h', "--help"),
|
2019-04-13 21:18:31 +02:00
|
|
|
GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE),
|
2011-02-18 21:52:42 +01:00
|
|
|
GETOPT_VALUE( 's', "--source_dir"),
|
|
|
|
GETOPT_VALUE( 'd', "--dest_dir"),
|
|
|
|
GETOPT_END(),
|
|
|
|
};
|
|
|
|
|
2009-01-10 01:31:47 +01:00
|
|
|
int CDECL main(int argc, char *argv[])
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::path src_dir(".");
|
|
|
|
std::filesystem::path dest_dir;
|
2006-08-24 14:08:25 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
GetOptData mgo(argc - 1, argv + 1, _opts);
|
|
|
|
for (;;) {
|
|
|
|
int i = mgo.GetOpt();
|
|
|
|
if (i == -1) break;
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 'C':
|
2023-05-21 08:04:33 +02:00
|
|
|
fmt::print("args\tflags\tcommand\treplacement\n");
|
2024-04-07 22:18:39 +02:00
|
|
|
for (const auto &cs : _cmd_structs) {
|
2011-02-18 21:52:42 +01:00
|
|
|
char flags;
|
2024-04-07 22:18:39 +02:00
|
|
|
if (cs.proc == EmitGender) {
|
2013-04-17 20:21:43 +02:00
|
|
|
flags = 'g'; // Command needs number of parameters defined by number of genders
|
2024-04-07 22:18:39 +02:00
|
|
|
} else if (cs.proc == EmitPlural) {
|
2013-04-17 20:21:43 +02:00
|
|
|
flags = 'p'; // Command needs number of parameters defined by plural value
|
2024-04-07 22:18:39 +02:00
|
|
|
} else if (cs.flags & C_DONTCOUNT) {
|
2013-04-17 20:21:43 +02:00
|
|
|
flags = 'i'; // Command may be in the translation when it is not in base
|
|
|
|
} else {
|
|
|
|
flags = '0'; // Command needs no parameters
|
2011-02-18 21:52:42 +01:00
|
|
|
}
|
2024-04-07 22:18:39 +02:00
|
|
|
fmt::print("{}\t{:c}\t\"{}\"\t\"{}\"\n", cs.consumes, flags, cs.cmd, strstr(cs.cmd, "STRING") ? "STRING" : cs.cmd);
|
2009-04-23 15:32:13 +02:00
|
|
|
}
|
2011-02-18 21:52:42 +01:00
|
|
|
return 0;
|
2009-04-21 23:34:26 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
case 'L':
|
2023-05-21 08:04:33 +02:00
|
|
|
fmt::print("count\tdescription\tnames\n");
|
2024-04-07 22:18:39 +02:00
|
|
|
for (const auto &pf : _plural_forms) {
|
|
|
|
fmt::print("{}\t\"{}\"\t{}\n", pf.plural_count, pf.description, pf.names);
|
2011-02-18 21:52:42 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2009-04-21 23:34:26 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
case 'P':
|
2023-05-21 08:04:33 +02:00
|
|
|
fmt::print("name\tflags\tdefault\tdescription\n");
|
2024-04-09 17:18:35 +02:00
|
|
|
for (const auto &pragma : _pragmas) {
|
2023-05-21 08:04:33 +02:00
|
|
|
fmt::print("\"{}\"\t{}\t\"{}\"\t\"{}\"\n",
|
2024-04-09 17:18:35 +02:00
|
|
|
pragma[0], pragma[1], pragma[2], pragma[3]);
|
2011-02-18 21:52:42 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2009-04-25 23:09:42 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
case 't':
|
|
|
|
_show_todo |= 1;
|
|
|
|
break;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
case 'w':
|
|
|
|
_show_todo |= 2;
|
|
|
|
break;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
case 'h':
|
2023-05-21 08:21:40 +02:00
|
|
|
fmt::print(
|
2023-05-21 08:33:04 +02:00
|
|
|
"strgen\n"
|
2011-02-18 21:52:42 +01:00
|
|
|
" -t | --todo replace any untranslated strings with '<TODO>'\n"
|
|
|
|
" -w | --warning print a warning for any untranslated strings\n"
|
|
|
|
" -h | -? | --help print this help message and exit\n"
|
|
|
|
" -s | --source_dir search for english.txt in the specified directory\n"
|
|
|
|
" -d | --dest_dir put output file in the specified directory, create if needed\n"
|
|
|
|
" -export-commands export all commands and exit\n"
|
|
|
|
" -export-plurals export all plural forms and exit\n"
|
|
|
|
" -export-pragmas export all pragmas and exit\n"
|
|
|
|
" Run without parameters and strgen will search for english.txt and parse it,\n"
|
|
|
|
" creating strings.h. Passing an argument, strgen will translate that language\n"
|
2023-05-21 08:21:40 +02:00
|
|
|
" file using english.txt as a reference and output <language>.lng.\n"
|
2011-02-18 21:52:42 +01:00
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 's':
|
2023-04-29 21:40:06 +02:00
|
|
|
src_dir = mgo.opt;
|
2011-02-18 21:52:42 +01:00
|
|
|
break;
|
2006-01-28 12:10:52 +01:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
case 'd':
|
2023-04-29 21:40:06 +02:00
|
|
|
dest_dir = mgo.opt;
|
2011-02-18 21:52:42 +01:00
|
|
|
break;
|
2006-08-24 14:08:25 +02:00
|
|
|
|
2011-02-18 21:52:42 +01:00
|
|
|
case -2:
|
2023-05-21 08:13:28 +02:00
|
|
|
fmt::print(stderr, "Invalid arguments\n");
|
2011-02-18 21:52:42 +01:00
|
|
|
return 0;
|
2006-08-30 22:08:58 +02:00
|
|
|
}
|
2006-08-24 14:08:25 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
if (dest_dir.empty()) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
|
2006-08-30 22:08:58 +02:00
|
|
|
|
2011-12-17 13:15:19 +01:00
|
|
|
try {
|
|
|
|
/* strgen has two modes of operation. If no (free) arguments are passed
|
2011-12-17 17:56:32 +01:00
|
|
|
* strgen generates strings.h to the destination directory. If it is supplied
|
|
|
|
* with a (free) parameter the program will translate that language to destination
|
|
|
|
* directory. As input english.txt is parsed from the source directory */
|
2011-12-17 13:15:19 +01:00
|
|
|
if (mgo.numleft == 0) {
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::path input_path = src_dir;
|
|
|
|
input_path /= "english.txt";
|
2011-12-17 13:15:19 +01:00
|
|
|
|
|
|
|
/* parse master file */
|
2017-02-26 21:10:41 +01:00
|
|
|
StringData data(TEXT_TAB_END);
|
2023-04-29 21:40:06 +02:00
|
|
|
FileStringReader master_reader(data, input_path, true, false);
|
2011-12-17 17:56:32 +01:00
|
|
|
master_reader.ParseFile();
|
2011-12-17 13:15:19 +01:00
|
|
|
if (_errors != 0) return 1;
|
|
|
|
|
|
|
|
/* write strings.h */
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::path output_path = dest_dir;
|
|
|
|
std::filesystem::create_directories(dest_dir);
|
|
|
|
output_path /= "strings.h";
|
2011-12-17 13:15:19 +01:00
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
HeaderFileWriter writer(output_path);
|
2011-12-17 15:41:10 +01:00
|
|
|
writer.WriteHeader(data);
|
2011-12-17 15:50:35 +01:00
|
|
|
writer.Finalise(data);
|
2019-01-29 22:11:18 +01:00
|
|
|
if (_errors != 0) return 1;
|
2011-12-17 18:03:38 +01:00
|
|
|
} else if (mgo.numleft >= 1) {
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::path input_path = src_dir;
|
|
|
|
input_path /= "english.txt";
|
2011-12-17 13:15:19 +01:00
|
|
|
|
2017-02-26 21:10:41 +01:00
|
|
|
StringData data(TEXT_TAB_END);
|
2011-12-17 13:15:19 +01:00
|
|
|
/* parse master file and check if target file is correct */
|
2023-04-29 21:40:06 +02:00
|
|
|
FileStringReader master_reader(data, input_path, true, false);
|
2011-12-17 17:56:32 +01:00
|
|
|
master_reader.ParseFile();
|
|
|
|
|
2011-12-17 18:03:38 +01:00
|
|
|
for (int i = 0; i < mgo.numleft; i++) {
|
|
|
|
data.FreeTranslation();
|
2011-12-17 13:15:19 +01:00
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::path lang_file = mgo.argv[i];
|
|
|
|
FileStringReader translation_reader(data, lang_file, false, lang_file.filename() != "english.txt");
|
2011-12-17 18:03:38 +01:00
|
|
|
translation_reader.ParseFile(); // target file
|
|
|
|
if (_errors != 0) return 1;
|
2011-12-17 13:15:19 +01:00
|
|
|
|
2011-12-17 18:03:38 +01:00
|
|
|
/* get the targetfile, strip any directories and append to destination path */
|
2023-04-29 21:40:06 +02:00
|
|
|
std::filesystem::path output_file = dest_dir;
|
|
|
|
output_file /= lang_file.filename();
|
|
|
|
output_file.replace_extension("lng");
|
2011-12-17 13:15:19 +01:00
|
|
|
|
2023-04-29 21:40:06 +02:00
|
|
|
LanguageFileWriter writer(output_file);
|
2011-12-17 18:03:38 +01:00
|
|
|
writer.WriteLang(data);
|
|
|
|
writer.Finalise();
|
|
|
|
|
|
|
|
/* if showing warnings, print a summary of the language */
|
|
|
|
if ((_show_todo & 2) != 0) {
|
2023-04-29 21:40:06 +02:00
|
|
|
fmt::print("{} warnings and {} errors for {}\n", _warnings, _errors, output_file);
|
2011-12-17 18:03:38 +01:00
|
|
|
}
|
2011-12-17 13:15:19 +01:00
|
|
|
}
|
2006-12-22 02:18:56 +01:00
|
|
|
}
|
2011-12-17 13:15:19 +01:00
|
|
|
} catch (...) {
|
|
|
|
return 2;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|