Update to 14.0-beta1
This commit is contained in:
@@ -10,6 +10,7 @@ if (NOT HOST_BINARY_DIR)
|
||||
strgen_base.cpp
|
||||
../core/alloc_func.cpp
|
||||
../misc/getoptdata.cpp
|
||||
../error.cpp
|
||||
../string.cpp
|
||||
)
|
||||
add_definitions(-DSTRGEN)
|
||||
|
||||
+124
-240
@@ -9,24 +9,18 @@
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../core/endian_func.hpp"
|
||||
#include "../core/mem_func.hpp"
|
||||
#include "../error_func.h"
|
||||
#include "../string_func.h"
|
||||
#include "../strings_type.h"
|
||||
#include "../misc/getoptdata.h"
|
||||
#include "../table/control_codes.h"
|
||||
#include "../3rdparty/fmt/std.h"
|
||||
|
||||
#include "strgen.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <exception>
|
||||
|
||||
#if !defined(_WIN32) || defined(__CYGWIN__)
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(__WATCOMC__)
|
||||
#include <direct.h>
|
||||
#endif /* _WIN32 || __WATCOMC__ */
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "../table/strgen_tables.h"
|
||||
|
||||
@@ -34,68 +28,48 @@
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define LINE_NUM_FMT(s) "%s (%d): warning: %s (" s ")\n"
|
||||
# define LINE_NUM_FMT(s) "{} ({}): warning: {} (" s ")\n"
|
||||
#else
|
||||
# define LINE_NUM_FMT(s) "%s:%d: " s ": %s\n"
|
||||
# define LINE_NUM_FMT(s) "{}:{}: " s ": {}\n"
|
||||
#endif
|
||||
|
||||
void CDECL strgen_warning(const char *s, ...)
|
||||
void StrgenWarningI(const std::string &msg)
|
||||
{
|
||||
char buf[1024];
|
||||
va_list va;
|
||||
va_start(va, s);
|
||||
vseprintf(buf, lastof(buf), s, va);
|
||||
va_end(va);
|
||||
if (_show_todo > 0) {
|
||||
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, buf);
|
||||
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, msg);
|
||||
} else {
|
||||
fprintf(stderr, LINE_NUM_FMT("info"), _file, _cur_line, buf);
|
||||
fmt::print(stderr, LINE_NUM_FMT("info"), _file, _cur_line, msg);
|
||||
}
|
||||
_warnings++;
|
||||
}
|
||||
|
||||
void CDECL strgen_error(const char *s, ...)
|
||||
void StrgenErrorI(const std::string &msg)
|
||||
{
|
||||
char buf[1024];
|
||||
va_list va;
|
||||
va_start(va, s);
|
||||
vseprintf(buf, lastof(buf), s, va);
|
||||
va_end(va);
|
||||
fprintf(stderr, LINE_NUM_FMT("error"), _file, _cur_line, buf);
|
||||
fmt::print(stderr, LINE_NUM_FMT("error"), _file, _cur_line, msg);
|
||||
_errors++;
|
||||
}
|
||||
|
||||
void NORETURN CDECL strgen_fatal(const char *s, ...)
|
||||
[[noreturn]] void StrgenFatalI(const std::string &msg)
|
||||
{
|
||||
char buf[1024];
|
||||
va_list va;
|
||||
va_start(va, s);
|
||||
vseprintf(buf, lastof(buf), s, va);
|
||||
va_end(va);
|
||||
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, buf);
|
||||
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
|
||||
#ifdef _MSC_VER
|
||||
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
||||
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
||||
#endif
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
void NORETURN CDECL error(const char *s, ...)
|
||||
[[noreturn]] void FatalErrorI(const std::string &msg)
|
||||
{
|
||||
char buf[1024];
|
||||
va_list va;
|
||||
va_start(va, s);
|
||||
vseprintf(buf, lastof(buf), s, va);
|
||||
va_end(va);
|
||||
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, buf);
|
||||
fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
|
||||
#ifdef _MSC_VER
|
||||
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
||||
fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
||||
#endif
|
||||
exit(2);
|
||||
}
|
||||
|
||||
/** A reader that simply reads using fopen. */
|
||||
struct FileStringReader : StringReader {
|
||||
FILE *fh; ///< The file we are reading.
|
||||
std::ifstream input_stream;
|
||||
|
||||
/**
|
||||
* Create the reader.
|
||||
@@ -104,22 +78,17 @@ struct FileStringReader : StringReader {
|
||||
* @param master Are we reading the master file?
|
||||
* @param translation Are we reading a translation?
|
||||
*/
|
||||
FileStringReader(StringData &data, const char *file, bool master, bool translation) :
|
||||
StringReader(data, file, master, translation)
|
||||
FileStringReader(StringData &data, const std::filesystem::path &file, bool master, bool translation) :
|
||||
StringReader(data, file.generic_string(), master, translation)
|
||||
{
|
||||
this->fh = fopen(file, "rb");
|
||||
if (this->fh == nullptr) error("Could not open %s", file);
|
||||
this->input_stream.open(file, std::ifstream::binary);
|
||||
}
|
||||
|
||||
/** Free/close the file. */
|
||||
virtual ~FileStringReader()
|
||||
std::optional<std::string> ReadLine() override
|
||||
{
|
||||
fclose(this->fh);
|
||||
}
|
||||
|
||||
char *ReadLine(char *buffer, const char *last) override
|
||||
{
|
||||
return fgets(buffer, ClampToU16(last - buffer + 1), this->fh);
|
||||
std::string result;
|
||||
if (!std::getline(this->input_stream, result)) return std::nullopt;
|
||||
return result;
|
||||
}
|
||||
|
||||
void HandlePragma(char *str) override;
|
||||
@@ -129,7 +98,7 @@ struct FileStringReader : StringReader {
|
||||
this->StringReader::ParseFile();
|
||||
|
||||
if (StrEmpty(_lang.name) || StrEmpty(_lang.own_name) || StrEmpty(_lang.isocode)) {
|
||||
error("Language must include ##name, ##ownname and ##isocode");
|
||||
FatalError("Language must include ##name, ##ownname and ##isocode");
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -137,7 +106,7 @@ struct FileStringReader : StringReader {
|
||||
void FileStringReader::HandlePragma(char *str)
|
||||
{
|
||||
if (!memcmp(str, "id ", 3)) {
|
||||
this->data.next_string_id = strtoul(str + 3, nullptr, 0);
|
||||
this->data.next_string_id = std::strtoul(str + 3, nullptr, 0);
|
||||
} else if (!memcmp(str, "name ", 5)) {
|
||||
strecpy(_lang.name, str + 5, lastof(_lang.name));
|
||||
} else if (!memcmp(str, "ownname ", 8)) {
|
||||
@@ -150,7 +119,7 @@ void FileStringReader::HandlePragma(char *str)
|
||||
} else if (!memcmp(str + 8, "rtl", 3)) {
|
||||
_lang.text_dir = TD_RTL;
|
||||
} else {
|
||||
error("Invalid textdir %s", str + 8);
|
||||
FatalError("Invalid textdir {}", str + 8);
|
||||
}
|
||||
} else if (!memcmp(str, "digitsep ", 9)) {
|
||||
str += 9;
|
||||
@@ -163,39 +132,39 @@ void FileStringReader::HandlePragma(char *str)
|
||||
strecpy(_lang.digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_decimal_separator));
|
||||
} else if (!memcmp(str, "winlangid ", 10)) {
|
||||
const char *buf = str + 10;
|
||||
long langid = strtol(buf, nullptr, 16);
|
||||
long langid = std::strtol(buf, nullptr, 16);
|
||||
if (langid > (long)UINT16_MAX || langid < 0) {
|
||||
error("Invalid winlangid %s", buf);
|
||||
FatalError("Invalid winlangid {}", buf);
|
||||
}
|
||||
_lang.winlangid = (uint16)langid;
|
||||
_lang.winlangid = (uint16_t)langid;
|
||||
} else if (!memcmp(str, "grflangid ", 10)) {
|
||||
const char *buf = str + 10;
|
||||
long langid = strtol(buf, nullptr, 16);
|
||||
long langid = std::strtol(buf, nullptr, 16);
|
||||
if (langid >= 0x7F || langid < 0) {
|
||||
error("Invalid grflangid %s", buf);
|
||||
FatalError("Invalid grflangid {}", buf);
|
||||
}
|
||||
_lang.newgrflangid = (uint8)langid;
|
||||
_lang.newgrflangid = (uint8_t)langid;
|
||||
} else if (!memcmp(str, "gender ", 7)) {
|
||||
if (this->master) error("Genders are not allowed in the base translation.");
|
||||
if (this->master) FatalError("Genders are not allowed in the base translation.");
|
||||
char *buf = str + 7;
|
||||
|
||||
for (;;) {
|
||||
const char *s = ParseWord(&buf);
|
||||
|
||||
if (s == nullptr) break;
|
||||
if (_lang.num_genders >= MAX_NUM_GENDERS) error("Too many genders, max %d", MAX_NUM_GENDERS);
|
||||
if (_lang.num_genders >= MAX_NUM_GENDERS) FatalError("Too many genders, max {}", MAX_NUM_GENDERS);
|
||||
strecpy(_lang.genders[_lang.num_genders], s, lastof(_lang.genders[_lang.num_genders]));
|
||||
_lang.num_genders++;
|
||||
}
|
||||
} else if (!memcmp(str, "case ", 5)) {
|
||||
if (this->master) error("Cases are not allowed in the base translation.");
|
||||
if (this->master) FatalError("Cases are not allowed in the base translation.");
|
||||
char *buf = str + 5;
|
||||
|
||||
for (;;) {
|
||||
const char *s = ParseWord(&buf);
|
||||
|
||||
if (s == nullptr) break;
|
||||
if (_lang.num_cases >= MAX_NUM_CASES) error("Too many cases, max %d", MAX_NUM_CASES);
|
||||
if (_lang.num_cases >= MAX_NUM_CASES) FatalError("Too many cases, max {}", MAX_NUM_CASES);
|
||||
strecpy(_lang.cases[_lang.num_cases], s, lastof(_lang.cases[_lang.num_cases]));
|
||||
_lang.num_cases++;
|
||||
}
|
||||
@@ -204,108 +173,80 @@ void FileStringReader::HandlePragma(char *str)
|
||||
}
|
||||
}
|
||||
|
||||
bool CompareFiles(const char *n1, const char *n2)
|
||||
bool CompareFiles(const std::filesystem::path &path1, const std::filesystem::path &path2)
|
||||
{
|
||||
FILE *f2 = fopen(n2, "rb");
|
||||
if (f2 == nullptr) return false;
|
||||
/* 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;
|
||||
|
||||
FILE *f1 = fopen(n1, "rb");
|
||||
if (f1 == nullptr) {
|
||||
fclose(f2);
|
||||
error("can't open %s", n1);
|
||||
}
|
||||
std::ifstream stream1(path1, std::ifstream::binary);
|
||||
std::ifstream stream2(path2, std::ifstream::binary);
|
||||
|
||||
size_t l1, l2;
|
||||
do {
|
||||
char b1[4096];
|
||||
char b2[4096];
|
||||
l1 = fread(b1, 1, sizeof(b1), f1);
|
||||
l2 = fread(b2, 1, sizeof(b2), f2);
|
||||
|
||||
if (l1 != l2 || memcmp(b1, b2, l1)) {
|
||||
fclose(f2);
|
||||
fclose(f1);
|
||||
return false;
|
||||
}
|
||||
} while (l1 != 0);
|
||||
|
||||
fclose(f2);
|
||||
fclose(f1);
|
||||
return true;
|
||||
return std::equal(std::istreambuf_iterator<char>(stream1.rdbuf()),
|
||||
std::istreambuf_iterator<char>(),
|
||||
std::istreambuf_iterator<char>(stream2.rdbuf()));
|
||||
}
|
||||
|
||||
/** Base class for writing data to disk. */
|
||||
struct FileWriter {
|
||||
FILE *fh; ///< The file handle we're writing to.
|
||||
const char *filename; ///< The file name we're writing to.
|
||||
std::ofstream output_stream; ///< The stream to write all the output to.
|
||||
const std::filesystem::path path; ///< The file name we're writing to.
|
||||
|
||||
/**
|
||||
* Open a file to write to.
|
||||
* @param filename The file to open.
|
||||
* @param path The path to the file to open.
|
||||
* @param openmode The openmode flags for opening the file.
|
||||
*/
|
||||
FileWriter(const char *filename)
|
||||
FileWriter(const std::filesystem::path &path, std::ios_base::openmode openmode) : path(path)
|
||||
{
|
||||
this->filename = stredup(filename);
|
||||
this->fh = fopen(this->filename, "wb");
|
||||
|
||||
if (this->fh == nullptr) {
|
||||
error("Could not open %s", this->filename);
|
||||
}
|
||||
this->output_stream.open(path, openmode);
|
||||
}
|
||||
|
||||
/** Finalise the writing. */
|
||||
void Finalise()
|
||||
{
|
||||
fclose(this->fh);
|
||||
this->fh = nullptr;
|
||||
this->output_stream.close();
|
||||
}
|
||||
|
||||
/** Make sure the file is closed. */
|
||||
virtual ~FileWriter()
|
||||
{
|
||||
/* If we weren't closed an exception was thrown, so remove the temporary file. */
|
||||
if (fh != nullptr) {
|
||||
fclose(this->fh);
|
||||
unlink(this->filename);
|
||||
if (this->output_stream.is_open()) {
|
||||
this->output_stream.close();
|
||||
std::filesystem::remove(this->path);
|
||||
}
|
||||
free(this->filename);
|
||||
}
|
||||
};
|
||||
|
||||
struct HeaderFileWriter : HeaderWriter, FileWriter {
|
||||
/** The real file name we eventually want to write to. */
|
||||
const char *real_filename;
|
||||
/** The real path we eventually want to write to. */
|
||||
const std::filesystem::path real_path;
|
||||
/** The previous string ID that was printed. */
|
||||
int prev;
|
||||
uint total_strings;
|
||||
|
||||
/**
|
||||
* Open a file to write to.
|
||||
* @param filename The file to open.
|
||||
* @param path The path to the file to open.
|
||||
*/
|
||||
HeaderFileWriter(const char *filename) : FileWriter("tmp.xxx"),
|
||||
real_filename(stredup(filename)), prev(0), total_strings(0)
|
||||
HeaderFileWriter(const std::filesystem::path &path) : FileWriter("tmp.xxx", std::ofstream::out),
|
||||
real_path(path), prev(0), total_strings(0)
|
||||
{
|
||||
fprintf(this->fh, "/* This file is automatically generated. Do not modify */\n\n");
|
||||
fprintf(this->fh, "#ifndef TABLE_STRINGS_H\n");
|
||||
fprintf(this->fh, "#define TABLE_STRINGS_H\n");
|
||||
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";
|
||||
}
|
||||
|
||||
/** Free the filename. */
|
||||
~HeaderFileWriter()
|
||||
void WriteStringID(const std::string &name, int stringid) override
|
||||
{
|
||||
free(real_filename);
|
||||
}
|
||||
|
||||
void WriteStringID(const char *name, int stringid)
|
||||
{
|
||||
if (prev + 1 != stringid) fprintf(this->fh, "\n");
|
||||
fprintf(this->fh, "static const StringID %s = 0x%X;\n", name, stringid);
|
||||
if (prev + 1 != stringid) this->output_stream << "\n";
|
||||
fmt::print(this->output_stream, "static const StringID {} = 0x{:X};\n", name, stringid);
|
||||
prev = stringid;
|
||||
total_strings++;
|
||||
}
|
||||
|
||||
void Finalise(const StringData &data)
|
||||
void Finalise(const StringData &data) override
|
||||
{
|
||||
/* Find the plural form with the most amount of cases. */
|
||||
int max_plural_forms = 0;
|
||||
@@ -313,29 +254,31 @@ struct HeaderFileWriter : HeaderWriter, FileWriter {
|
||||
max_plural_forms = std::max(max_plural_forms, _plural_forms[i].plural_count);
|
||||
}
|
||||
|
||||
fprintf(this->fh,
|
||||
fmt::print(this->output_stream,
|
||||
"\n"
|
||||
"static const uint LANGUAGE_PACK_VERSION = 0x%X;\n"
|
||||
"static const uint LANGUAGE_MAX_PLURAL = %u;\n"
|
||||
"static const uint LANGUAGE_MAX_PLURAL_FORMS = %d;\n"
|
||||
"static const uint LANGUAGE_TOTAL_STRINGS = %u;\n"
|
||||
"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"
|
||||
"\n",
|
||||
(uint)data.Version(), (uint)lengthof(_plural_forms), max_plural_forms, total_strings
|
||||
data.Version(), lengthof(_plural_forms), max_plural_forms, total_strings
|
||||
);
|
||||
|
||||
fprintf(this->fh, "#endif /* TABLE_STRINGS_H */\n");
|
||||
this->output_stream << "#endif /* TABLE_STRINGS_H */\n";
|
||||
|
||||
this->FileWriter::Finalise();
|
||||
|
||||
if (CompareFiles(this->filename, this->real_filename)) {
|
||||
std::error_code error_code;
|
||||
if (CompareFiles(this->path, this->real_path)) {
|
||||
/* files are equal. tmp.xxx is not needed */
|
||||
unlink(this->filename);
|
||||
std::filesystem::remove(this->path, error_code); // Just ignore the error
|
||||
} else {
|
||||
/* else rename tmp.xxx into filename */
|
||||
# if defined(_WIN32)
|
||||
unlink(this->real_filename);
|
||||
std::filesystem::remove(this->real_path, error_code); // Just ignore the error, file probably doesn't exist
|
||||
# endif
|
||||
if (rename(this->filename, this->real_filename) == -1) error("rename() failed");
|
||||
std::filesystem::rename(this->path, this->real_path, error_code);
|
||||
if (error_code) FatalError("rename({}, {}) failed: {}", this->path, this->real_path, error_code.message());
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -344,80 +287,31 @@ struct HeaderFileWriter : HeaderWriter, FileWriter {
|
||||
struct LanguageFileWriter : LanguageWriter, FileWriter {
|
||||
/**
|
||||
* Open a file to write to.
|
||||
* @param filename The file to open.
|
||||
* @param path The path to the file to open.
|
||||
*/
|
||||
LanguageFileWriter(const char *filename) : FileWriter(filename)
|
||||
LanguageFileWriter(const std::filesystem::path &path) : FileWriter(path, std::ofstream::binary | std::ofstream::out)
|
||||
{
|
||||
}
|
||||
|
||||
void WriteHeader(const LanguagePackHeader *header)
|
||||
void WriteHeader(const LanguagePackHeader *header) override
|
||||
{
|
||||
this->Write((const byte *)header, sizeof(*header));
|
||||
}
|
||||
|
||||
void Finalise()
|
||||
void Finalise() override
|
||||
{
|
||||
if (fputc(0, this->fh) == EOF) {
|
||||
error("Could not write to %s", this->filename);
|
||||
}
|
||||
this->output_stream.put(0);
|
||||
this->FileWriter::Finalise();
|
||||
}
|
||||
|
||||
void Write(const byte *buffer, size_t length)
|
||||
void Write(const byte *buffer, size_t length) override
|
||||
{
|
||||
if (fwrite(buffer, sizeof(*buffer), length, this->fh) != length) {
|
||||
error("Could not write to %s", this->filename);
|
||||
}
|
||||
this->output_stream.write((const char *)buffer, length);
|
||||
}
|
||||
};
|
||||
|
||||
/** Multi-OS mkdirectory function */
|
||||
static inline void ottd_mkdir(const char *directory)
|
||||
{
|
||||
/* Ignore directory creation errors; they'll surface later on, and most
|
||||
* of the time they are 'directory already exists' errors anyhow. */
|
||||
#if defined(_WIN32) || defined(__WATCOMC__)
|
||||
mkdir(directory);
|
||||
#else
|
||||
mkdir(directory, 0755);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a path consisting of an already existing path, a possible
|
||||
* path separator and the filename. The separator is only appended if the path
|
||||
* does not already end with a separator
|
||||
*/
|
||||
static inline char *mkpath(char *buf, const char *last, const char *path, const char *file)
|
||||
{
|
||||
strecpy(buf, path, last); // copy directory into buffer
|
||||
|
||||
char *p = strchr(buf, '\0'); // add path separator if necessary
|
||||
if (p[-1] != PATHSEPCHAR && p != last) *p++ = PATHSEPCHAR;
|
||||
strecpy(p, file, last); // concatenate filename at end of buffer
|
||||
return buf;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
/**
|
||||
* On MingW, it is common that both / as \ are accepted in the
|
||||
* params. To go with those flow, we rewrite all incoming /
|
||||
* simply to \, so internally we can safely assume \, and do
|
||||
* this for all Windows machines to keep identical behaviour,
|
||||
* no matter what your compiler was.
|
||||
*/
|
||||
static inline char *replace_pathsep(char *s)
|
||||
{
|
||||
for (char *c = s; *c != '\0'; c++) if (*c == '/') *c = '\\';
|
||||
return s;
|
||||
}
|
||||
#else
|
||||
static inline char *replace_pathsep(char *s) { return s; }
|
||||
#endif
|
||||
|
||||
/** Options of strgen. */
|
||||
static const OptionData _opts[] = {
|
||||
GETOPT_NOVAL( 'v', "--version"),
|
||||
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),
|
||||
@@ -432,9 +326,8 @@ static const OptionData _opts[] = {
|
||||
|
||||
int CDECL main(int argc, char *argv[])
|
||||
{
|
||||
char pathbuf[MAX_PATH];
|
||||
const char *src_dir = ".";
|
||||
const char *dest_dir = nullptr;
|
||||
std::filesystem::path src_dir(".");
|
||||
std::filesystem::path dest_dir;
|
||||
|
||||
GetOptData mgo(argc - 1, argv + 1, _opts);
|
||||
for (;;) {
|
||||
@@ -442,12 +335,8 @@ int CDECL main(int argc, char *argv[])
|
||||
if (i == -1) break;
|
||||
|
||||
switch (i) {
|
||||
case 'v':
|
||||
puts("$Revision$");
|
||||
return 0;
|
||||
|
||||
case 'C':
|
||||
printf("args\tflags\tcommand\treplacement\n");
|
||||
fmt::print("args\tflags\tcommand\treplacement\n");
|
||||
for (const CmdStruct *cs = _cmd_structs; cs < endof(_cmd_structs); cs++) {
|
||||
char flags;
|
||||
if (cs->proc == EmitGender) {
|
||||
@@ -459,22 +348,22 @@ int CDECL main(int argc, char *argv[])
|
||||
} else {
|
||||
flags = '0'; // Command needs no parameters
|
||||
}
|
||||
printf("%i\t%c\t\"%s\"\t\"%s\"\n", cs->consumes, flags, cs->cmd, strstr(cs->cmd, "STRING") ? "STRING" : cs->cmd);
|
||||
fmt::print("{}\t{:c}\t\"{}\"\t\"{}\"\n", cs->consumes, flags, cs->cmd, strstr(cs->cmd, "STRING") ? "STRING" : cs->cmd);
|
||||
}
|
||||
return 0;
|
||||
|
||||
case 'L':
|
||||
printf("count\tdescription\tnames\n");
|
||||
fmt::print("count\tdescription\tnames\n");
|
||||
for (const PluralForm *pf = _plural_forms; pf < endof(_plural_forms); pf++) {
|
||||
printf("%i\t\"%s\"\t%s\n", pf->plural_count, pf->description, pf->names);
|
||||
fmt::print("{}\t\"{}\"\t{}\n", pf->plural_count, pf->description, pf->names);
|
||||
}
|
||||
return 0;
|
||||
|
||||
case 'P':
|
||||
printf("name\tflags\tdefault\tdescription\n");
|
||||
for (size_t i = 0; i < lengthof(_pragmas); i++) {
|
||||
printf("\"%s\"\t%s\t\"%s\"\t\"%s\"\n",
|
||||
_pragmas[i][0], _pragmas[i][1], _pragmas[i][2], _pragmas[i][3]);
|
||||
fmt::print("name\tflags\tdefault\tdescription\n");
|
||||
for (size_t j = 0; j < lengthof(_pragmas); j++) {
|
||||
fmt::print("\"{}\"\t{}\t\"{}\"\t\"{}\"\n",
|
||||
_pragmas[j][0], _pragmas[j][1], _pragmas[j][2], _pragmas[j][3]);
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -487,9 +376,8 @@ int CDECL main(int argc, char *argv[])
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
puts(
|
||||
"strgen - $Revision$\n"
|
||||
" -v | --version print version information and exit\n"
|
||||
fmt::print(
|
||||
"strgen\n"
|
||||
" -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"
|
||||
@@ -500,25 +388,25 @@ int CDECL main(int argc, char *argv[])
|
||||
" -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"
|
||||
" file using english.txt as a reference and output <language>.lng."
|
||||
" file using english.txt as a reference and output <language>.lng.\n"
|
||||
);
|
||||
return 0;
|
||||
|
||||
case 's':
|
||||
src_dir = replace_pathsep(mgo.opt);
|
||||
src_dir = mgo.opt;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
dest_dir = replace_pathsep(mgo.opt);
|
||||
dest_dir = mgo.opt;
|
||||
break;
|
||||
|
||||
case -2:
|
||||
fprintf(stderr, "Invalid arguments\n");
|
||||
fmt::print(stderr, "Invalid arguments\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (dest_dir == nullptr) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
|
||||
if (dest_dir.empty()) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
|
||||
|
||||
try {
|
||||
/* strgen has two modes of operation. If no (free) arguments are passed
|
||||
@@ -526,57 +414,53 @@ int CDECL main(int argc, char *argv[])
|
||||
* with a (free) parameter the program will translate that language to destination
|
||||
* directory. As input english.txt is parsed from the source directory */
|
||||
if (mgo.numleft == 0) {
|
||||
mkpath(pathbuf, lastof(pathbuf), src_dir, "english.txt");
|
||||
std::filesystem::path input_path = src_dir;
|
||||
input_path /= "english.txt";
|
||||
|
||||
/* parse master file */
|
||||
StringData data(TEXT_TAB_END);
|
||||
FileStringReader master_reader(data, pathbuf, true, false);
|
||||
FileStringReader master_reader(data, input_path, true, false);
|
||||
master_reader.ParseFile();
|
||||
if (_errors != 0) return 1;
|
||||
|
||||
/* write strings.h */
|
||||
ottd_mkdir(dest_dir);
|
||||
mkpath(pathbuf, lastof(pathbuf), dest_dir, "strings.h");
|
||||
std::filesystem::path output_path = dest_dir;
|
||||
std::filesystem::create_directories(dest_dir);
|
||||
output_path /= "strings.h";
|
||||
|
||||
HeaderFileWriter writer(pathbuf);
|
||||
HeaderFileWriter writer(output_path);
|
||||
writer.WriteHeader(data);
|
||||
writer.Finalise(data);
|
||||
if (_errors != 0) return 1;
|
||||
} else if (mgo.numleft >= 1) {
|
||||
char *r;
|
||||
|
||||
mkpath(pathbuf, lastof(pathbuf), src_dir, "english.txt");
|
||||
std::filesystem::path input_path = src_dir;
|
||||
input_path /= "english.txt";
|
||||
|
||||
StringData data(TEXT_TAB_END);
|
||||
/* parse master file and check if target file is correct */
|
||||
FileStringReader master_reader(data, pathbuf, true, false);
|
||||
FileStringReader master_reader(data, input_path, true, false);
|
||||
master_reader.ParseFile();
|
||||
|
||||
for (int i = 0; i < mgo.numleft; i++) {
|
||||
data.FreeTranslation();
|
||||
|
||||
const char *translation = replace_pathsep(mgo.argv[i]);
|
||||
const char *file = strrchr(translation, PATHSEPCHAR);
|
||||
FileStringReader translation_reader(data, translation, false, file == nullptr || strcmp(file + 1, "english.txt") != 0);
|
||||
std::filesystem::path lang_file = mgo.argv[i];
|
||||
FileStringReader translation_reader(data, lang_file, false, lang_file.filename() != "english.txt");
|
||||
translation_reader.ParseFile(); // target file
|
||||
if (_errors != 0) return 1;
|
||||
|
||||
/* get the targetfile, strip any directories and append to destination path */
|
||||
r = strrchr(mgo.argv[i], PATHSEPCHAR);
|
||||
mkpath(pathbuf, lastof(pathbuf), dest_dir, (r != nullptr) ? &r[1] : mgo.argv[i]);
|
||||
std::filesystem::path output_file = dest_dir;
|
||||
output_file /= lang_file.filename();
|
||||
output_file.replace_extension("lng");
|
||||
|
||||
/* rename the .txt (input-extension) to .lng */
|
||||
r = strrchr(pathbuf, '.');
|
||||
if (r == nullptr || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
|
||||
strecpy(r, ".lng", lastof(pathbuf));
|
||||
|
||||
LanguageFileWriter writer(pathbuf);
|
||||
LanguageFileWriter writer(output_file);
|
||||
writer.WriteLang(data);
|
||||
writer.Finalise();
|
||||
|
||||
/* if showing warnings, print a summary of the language */
|
||||
if ((_show_todo & 2) != 0) {
|
||||
fprintf(stdout, "%d warnings and %d errors for %s\n", _warnings, _errors, pathbuf);
|
||||
fmt::print("{} warnings and {} errors for {}\n", _warnings, _errors, output_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+47
-33
@@ -11,46 +11,44 @@
|
||||
#define STRGEN_H
|
||||
|
||||
#include "../language.h"
|
||||
#include "../3rdparty/fmt/format.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <array>
|
||||
|
||||
/** Container for the different cases of a string. */
|
||||
struct Case {
|
||||
int caseidx; ///< The index of the case.
|
||||
char *string; ///< The translation of the case.
|
||||
Case *next; ///< The next, chained, case.
|
||||
int caseidx; ///< The index of the case.
|
||||
std::string string; ///< The translation of the case.
|
||||
|
||||
Case(int caseidx, const char *string, Case *next);
|
||||
~Case();
|
||||
Case(int caseidx, const std::string &string);
|
||||
};
|
||||
|
||||
/** Information about a single string. */
|
||||
struct LangString {
|
||||
char *name; ///< Name of the string.
|
||||
char *english; ///< English text.
|
||||
char *translated; ///< Translated text.
|
||||
size_t hash_next; ///< Next hash entry.
|
||||
size_t index; ///< The index in the language file.
|
||||
int line; ///< Line of string in source-file.
|
||||
Case *translated_case; ///< Cases of the translation.
|
||||
std::string name; ///< Name of the string.
|
||||
std::string english; ///< English text.
|
||||
std::string translated; ///< Translated text.
|
||||
size_t index; ///< The index in the language file.
|
||||
int line; ///< Line of string in source-file.
|
||||
std::vector<Case> translated_cases; ///< Cases of the translation.
|
||||
|
||||
LangString(const char *name, const char *english, size_t index, int line);
|
||||
~LangString();
|
||||
LangString(const std::string &name, const std::string &english, size_t index, int line);
|
||||
void FreeTranslation();
|
||||
};
|
||||
|
||||
/** Information about the currently known strings. */
|
||||
struct StringData {
|
||||
LangString **strings; ///< Array of all known strings.
|
||||
size_t *hash_heads; ///< Hash table for the strings.
|
||||
std::vector<std::unique_ptr<LangString>> strings; ///< List of all known strings.
|
||||
std::unordered_map<std::string_view, LangString *> name_to_string; ///< Lookup table for the strings.
|
||||
size_t tabs; ///< The number of 'tabs' of strings.
|
||||
size_t max_strings; ///< The maximum number of strings.
|
||||
size_t next_string_id;///< The next string ID to allocate.
|
||||
|
||||
StringData(size_t tabs);
|
||||
~StringData();
|
||||
void FreeTranslation();
|
||||
uint HashStr(const char *s) const;
|
||||
void Add(const char *s, LangString *ls);
|
||||
LangString *Find(const char *s);
|
||||
void Add(std::unique_ptr<LangString> ls);
|
||||
LangString *Find(const std::string_view s);
|
||||
uint VersionHashStr(uint hash, const char *s) const;
|
||||
uint Version() const;
|
||||
uint CountInUse(uint tab) const;
|
||||
@@ -59,21 +57,19 @@ struct StringData {
|
||||
/** Helper for reading strings. */
|
||||
struct StringReader {
|
||||
StringData &data; ///< The data to fill during reading.
|
||||
const char *file; ///< The file we are reading.
|
||||
const std::string file; ///< The file we are reading.
|
||||
bool master; ///< Are we reading the master file?
|
||||
bool translation; ///< Are we reading a translation, implies !master. However, the base translation will have this false.
|
||||
|
||||
StringReader(StringData &data, const char *file, bool master, bool translation);
|
||||
virtual ~StringReader();
|
||||
StringReader(StringData &data, const std::string &file, bool master, bool translation);
|
||||
virtual ~StringReader() {}
|
||||
void HandleString(char *str);
|
||||
|
||||
/**
|
||||
* Read a single line from the source of strings.
|
||||
* @param buffer The buffer to read the data in to.
|
||||
* @param last The last element in the buffer.
|
||||
* @return The buffer, or nullptr if at the end of the file.
|
||||
* @return The line, or std::nullopt if at the end of the file.
|
||||
*/
|
||||
virtual char *ReadLine(char *buffer, const char *last) = 0;
|
||||
virtual std::optional<std::string> ReadLine() = 0;
|
||||
|
||||
/**
|
||||
* Handle the pragma of the file.
|
||||
@@ -94,7 +90,7 @@ struct HeaderWriter {
|
||||
* @param name The name of the string.
|
||||
* @param stringid The ID of the string.
|
||||
*/
|
||||
virtual void WriteStringID(const char *name, int stringid) = 0;
|
||||
virtual void WriteStringID(const std::string &name, int stringid) = 0;
|
||||
|
||||
/**
|
||||
* Finalise writing the file.
|
||||
@@ -103,7 +99,7 @@ struct HeaderWriter {
|
||||
virtual void Finalise(const StringData &data) = 0;
|
||||
|
||||
/** Especially destroy the subclasses. */
|
||||
virtual ~HeaderWriter() {};
|
||||
virtual ~HeaderWriter() = default;
|
||||
|
||||
void WriteHeader(const StringData &data);
|
||||
};
|
||||
@@ -130,15 +126,33 @@ struct LanguageWriter {
|
||||
virtual void Finalise() = 0;
|
||||
|
||||
/** Especially destroy the subclasses. */
|
||||
virtual ~LanguageWriter() {}
|
||||
virtual ~LanguageWriter() = default;
|
||||
|
||||
virtual void WriteLength(uint length);
|
||||
virtual void WriteLang(const StringData &data);
|
||||
};
|
||||
|
||||
void CDECL strgen_warning(const char *s, ...) WARN_FORMAT(1, 2);
|
||||
void CDECL strgen_error(const char *s, ...) WARN_FORMAT(1, 2);
|
||||
void NORETURN CDECL strgen_fatal(const char *s, ...) WARN_FORMAT(1, 2);
|
||||
struct CmdStruct;
|
||||
|
||||
struct CmdPair {
|
||||
const CmdStruct *cmd;
|
||||
std::string param;
|
||||
};
|
||||
|
||||
struct ParsedCommandStruct {
|
||||
std::vector<CmdPair> non_consuming_commands;
|
||||
std::array<const CmdStruct*, 32> consuming_commands{ nullptr }; // ordered by param #
|
||||
};
|
||||
|
||||
const CmdStruct *TranslateCmdForCompare(const CmdStruct *a);
|
||||
ParsedCommandStruct ExtractCommandString(const char *s, bool warnings);
|
||||
|
||||
void StrgenWarningI(const std::string &msg);
|
||||
void StrgenErrorI(const std::string &msg);
|
||||
[[noreturn]] void StrgenFatalI(const std::string &msg);
|
||||
#define StrgenWarning(format_string, ...) StrgenWarningI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
|
||||
#define StrgenError(format_string, ...) StrgenErrorI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
|
||||
#define StrgenFatal(format_string, ...) StrgenFatalI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
|
||||
char *ParseWord(char **buf);
|
||||
|
||||
extern const char *_file;
|
||||
|
||||
+135
-211
@@ -8,7 +8,10 @@
|
||||
/** @file strgen_base.cpp Tool to create computer readable (stand-alone) translation files. */
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../core/alloc_func.hpp"
|
||||
#include "../core/endian_func.hpp"
|
||||
#include "../core/mem_func.hpp"
|
||||
#include "../error_func.h"
|
||||
#include "../string_func.h"
|
||||
#include "../table/control_codes.h"
|
||||
|
||||
@@ -35,20 +38,12 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
|
||||
* Create a new case.
|
||||
* @param caseidx The index of the case.
|
||||
* @param string The translation of the case.
|
||||
* @param next The next chained case.
|
||||
*/
|
||||
Case::Case(int caseidx, const char *string, Case *next) :
|
||||
caseidx(caseidx), string(stredup(string)), next(next)
|
||||
Case::Case(int caseidx, const std::string &string) :
|
||||
caseidx(caseidx), string(string)
|
||||
{
|
||||
}
|
||||
|
||||
/** Free everything we allocated. */
|
||||
Case::~Case()
|
||||
{
|
||||
free(this->string);
|
||||
delete this->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new string.
|
||||
* @param name The name of the string.
|
||||
@@ -56,29 +51,16 @@ Case::~Case()
|
||||
* @param index The index in the string table.
|
||||
* @param line The line this string was found on.
|
||||
*/
|
||||
LangString::LangString(const char *name, const char *english, size_t index, int line) :
|
||||
name(stredup(name)), english(stredup(english)), translated(nullptr),
|
||||
hash_next(0), index(index), line(line), translated_case(nullptr)
|
||||
LangString::LangString(const std::string &name, const std::string &english, size_t index, int line) :
|
||||
name(name), english(english), index(index), line(line)
|
||||
{
|
||||
}
|
||||
|
||||
/** Free everything we allocated. */
|
||||
LangString::~LangString()
|
||||
{
|
||||
free(this->name);
|
||||
free(this->english);
|
||||
free(this->translated);
|
||||
delete this->translated_case;
|
||||
}
|
||||
|
||||
/** Free all data related to the translation. */
|
||||
void LangString::FreeTranslation()
|
||||
{
|
||||
free(this->translated);
|
||||
this->translated = nullptr;
|
||||
|
||||
delete this->translated_case;
|
||||
this->translated_case = nullptr;
|
||||
this->translated.clear();
|
||||
this->translated_cases.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,52 +69,28 @@ void LangString::FreeTranslation()
|
||||
*/
|
||||
StringData::StringData(size_t tabs) : tabs(tabs), max_strings(tabs * TAB_SIZE)
|
||||
{
|
||||
this->strings = CallocT<LangString *>(max_strings);
|
||||
this->hash_heads = CallocT<size_t>(max_strings);
|
||||
this->strings.resize(max_strings);
|
||||
this->next_string_id = 0;
|
||||
}
|
||||
|
||||
/** Free everything we allocated. */
|
||||
StringData::~StringData()
|
||||
{
|
||||
for (size_t i = 0; i < this->max_strings; i++) delete this->strings[i];
|
||||
free(this->strings);
|
||||
free(this->hash_heads);
|
||||
}
|
||||
|
||||
/** Free all data related to the translation. */
|
||||
void StringData::FreeTranslation()
|
||||
{
|
||||
for (size_t i = 0; i < this->max_strings; i++) {
|
||||
LangString *ls = this->strings[i];
|
||||
LangString *ls = this->strings[i].get();
|
||||
if (ls != nullptr) ls->FreeTranslation();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a hash of the string for finding them back quickly.
|
||||
* @param s The string to hash.
|
||||
* @return The hashed string.
|
||||
*/
|
||||
uint StringData::HashStr(const char *s) const
|
||||
{
|
||||
uint hash = 0;
|
||||
for (; *s != '\0'; s++) hash = ROL(hash, 3) ^ *s;
|
||||
return hash % this->max_strings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a newly created LangString.
|
||||
* @param s The name of the string.
|
||||
* @param ls The string to add.
|
||||
*/
|
||||
void StringData::Add(const char *s, LangString *ls)
|
||||
void StringData::Add(std::unique_ptr<LangString> ls)
|
||||
{
|
||||
uint hash = this->HashStr(s);
|
||||
ls->hash_next = this->hash_heads[hash];
|
||||
/* Off-by-one for hash find. */
|
||||
this->hash_heads[hash] = ls->index + 1;
|
||||
this->strings[ls->index] = ls;
|
||||
this->name_to_string[ls->name] = ls.get();
|
||||
this->strings[ls->index].swap(ls);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,17 +98,12 @@ void StringData::Add(const char *s, LangString *ls)
|
||||
* @param s The string name to search on.
|
||||
* @return The LangString or nullptr if it is not known.
|
||||
*/
|
||||
LangString *StringData::Find(const char *s)
|
||||
LangString *StringData::Find(const std::string_view s)
|
||||
{
|
||||
size_t idx = this->hash_heads[this->HashStr(s)];
|
||||
auto it = this->name_to_string.find(s);
|
||||
if (it == this->name_to_string.end()) return nullptr;
|
||||
|
||||
while (idx-- > 0) {
|
||||
LangString *ls = this->strings[idx];
|
||||
|
||||
if (strcmp(ls->name, s) == 0) return ls;
|
||||
idx = ls->hash_next;
|
||||
}
|
||||
return nullptr;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,7 +115,7 @@ LangString *StringData::Find(const char *s)
|
||||
uint StringData::VersionHashStr(uint hash, const char *s) const
|
||||
{
|
||||
for (; *s != '\0'; s++) {
|
||||
hash = ROL(hash, 3) ^ *s;
|
||||
hash = std::rotl(hash, 3) ^ *s;
|
||||
hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
|
||||
}
|
||||
return hash;
|
||||
@@ -177,7 +130,7 @@ uint StringData::Version() const
|
||||
uint hash = 0;
|
||||
|
||||
for (size_t i = 0; i < this->max_strings; i++) {
|
||||
const LangString *ls = this->strings[i];
|
||||
const LangString *ls = this->strings[i].get();
|
||||
|
||||
if (ls != nullptr) {
|
||||
const CmdStruct *cs;
|
||||
@@ -186,12 +139,12 @@ uint StringData::Version() const
|
||||
int argno;
|
||||
int casei;
|
||||
|
||||
s = ls->name;
|
||||
s = ls->name.c_str();
|
||||
hash ^= i * 0x717239;
|
||||
hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
|
||||
hash = this->VersionHashStr(hash, s + 1);
|
||||
|
||||
s = ls->english;
|
||||
s = ls->english.c_str();
|
||||
while ((cs = ParseCommandString(&s, buf, &argno, &casei)) != nullptr) {
|
||||
if (cs->flags & C_DONTCOUNT) continue;
|
||||
|
||||
@@ -217,17 +170,6 @@ uint StringData::CountInUse(uint tab) const
|
||||
|
||||
static const char *_cur_ident;
|
||||
|
||||
struct CmdPair {
|
||||
const CmdStruct *a;
|
||||
const char *v;
|
||||
};
|
||||
|
||||
struct ParsedCommandStruct {
|
||||
uint np;
|
||||
CmdPair pairs[32];
|
||||
const CmdStruct *cmd[32]; // ordered by param #
|
||||
};
|
||||
|
||||
/* Used when generating some advanced commands. */
|
||||
static ParsedCommandStruct _cur_pcs;
|
||||
static int _cur_argidx;
|
||||
@@ -247,7 +189,7 @@ struct Buffer : std::vector<byte> {
|
||||
* Add an Unicode character encoded in UTF-8 to the buffer.
|
||||
* @param value The character to add.
|
||||
*/
|
||||
void AppendUtf8(uint32 value)
|
||||
void AppendUtf8(uint32_t value)
|
||||
{
|
||||
if (value < 0x80) {
|
||||
this->push_back(value);
|
||||
@@ -264,14 +206,14 @@ struct Buffer : std::vector<byte> {
|
||||
this->push_back(0x80 + GB(value, 6, 6));
|
||||
this->push_back(0x80 + GB(value, 0, 6));
|
||||
} else {
|
||||
strgen_warning("Invalid unicode value U+0x%X", value);
|
||||
StrgenWarning("Invalid unicode value U+0x{:X}", value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
size_t Utf8Validate(const char *s)
|
||||
{
|
||||
uint32 c;
|
||||
uint32_t c;
|
||||
|
||||
if (!HasBit(s[0], 7)) {
|
||||
/* 1 byte */
|
||||
@@ -296,7 +238,7 @@ size_t Utf8Validate(const char *s)
|
||||
|
||||
void EmitSingleChar(Buffer *buffer, char *buf, int value)
|
||||
{
|
||||
if (*buf != '\0') strgen_warning("Ignoring trailing letters in command");
|
||||
if (*buf != '\0') StrgenWarning("Ignoring trailing letters in command");
|
||||
buffer->AppendUtf8(value);
|
||||
}
|
||||
|
||||
@@ -318,7 +260,7 @@ bool ParseRelNum(char **buf, int *value, int *offset)
|
||||
rel = true;
|
||||
s++;
|
||||
}
|
||||
int v = strtol(s, &end, 0);
|
||||
int v = std::strtol(s, &end, 0);
|
||||
if (end == s) return false;
|
||||
if (rel || v < 0) {
|
||||
*value += v;
|
||||
@@ -328,7 +270,7 @@ bool ParseRelNum(char **buf, int *value, int *offset)
|
||||
if (offset != nullptr && *end == ':') {
|
||||
/* Take the Nth within */
|
||||
s = end + 1;
|
||||
*offset = strtol(s, &end, 0);
|
||||
*offset = std::strtol(s, &end, 0);
|
||||
if (end == s) return false;
|
||||
}
|
||||
*buf = end;
|
||||
@@ -373,7 +315,7 @@ char *ParseWord(char **buf)
|
||||
/* Forward declaration */
|
||||
static int TranslateArgumentIdx(int arg, int offset = 0);
|
||||
|
||||
static void EmitWordList(Buffer *buffer, const char * const *words, uint nw)
|
||||
static void EmitWordList(Buffer *buffer, const std::vector<const char *> &words, uint nw)
|
||||
{
|
||||
buffer->AppendByte(nw);
|
||||
for (uint i = 0; i < nw; i++) buffer->AppendByte((byte)strlen(words[i]) + 1);
|
||||
@@ -383,22 +325,22 @@ static void EmitWordList(Buffer *buffer, const char * const *words, uint nw)
|
||||
}
|
||||
}
|
||||
|
||||
void EmitPlural(Buffer *buffer, char *buf, int value)
|
||||
void EmitPlural(Buffer *buffer, char *buf, int)
|
||||
{
|
||||
int argidx = _cur_argidx;
|
||||
int offset = -1;
|
||||
int expected = _plural_forms[_lang.plural_form].plural_count;
|
||||
const char **words = AllocaM(const char *, std::max(expected, MAX_PLURALS));
|
||||
std::vector<const char *> words(std::max(expected, MAX_PLURALS), nullptr);
|
||||
int nw = 0;
|
||||
|
||||
/* Parse out the number, if one exists. Otherwise default to prev arg. */
|
||||
if (!ParseRelNum(&buf, &argidx, &offset)) argidx--;
|
||||
|
||||
const CmdStruct *cmd = _cur_pcs.cmd[argidx];
|
||||
const CmdStruct *cmd = _cur_pcs.consuming_commands[argidx];
|
||||
if (offset == -1) {
|
||||
/* Use default offset */
|
||||
if (cmd == nullptr || cmd->default_plural_offset < 0) {
|
||||
strgen_fatal("Command '%s' has no (default) plural position", cmd == nullptr ? "<empty>" : cmd->cmd);
|
||||
StrgenFatal("Command '{}' has no (default) plural position", cmd == nullptr ? "<empty>" : cmd->cmd);
|
||||
}
|
||||
offset = cmd->default_plural_offset;
|
||||
}
|
||||
@@ -410,15 +352,15 @@ void EmitPlural(Buffer *buffer, char *buf, int value)
|
||||
}
|
||||
|
||||
if (nw == 0) {
|
||||
strgen_fatal("%s: No plural words", _cur_ident);
|
||||
StrgenFatal("{}: No plural words", _cur_ident);
|
||||
}
|
||||
|
||||
if (expected != nw) {
|
||||
if (_translated) {
|
||||
strgen_fatal("%s: Invalid number of plural forms. Expecting %d, found %d.", _cur_ident,
|
||||
StrgenFatal("{}: Invalid number of plural forms. Expecting {}, found {}.", _cur_ident,
|
||||
expected, nw);
|
||||
} else {
|
||||
if ((_show_todo & 2) != 0) strgen_warning("'%s' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
|
||||
if ((_show_todo & 2) != 0) StrgenWarning("'{}' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
|
||||
if (nw > expected) {
|
||||
nw = expected;
|
||||
} else {
|
||||
@@ -435,8 +377,7 @@ void EmitPlural(Buffer *buffer, char *buf, int value)
|
||||
EmitWordList(buffer, words, nw);
|
||||
}
|
||||
|
||||
|
||||
void EmitGender(Buffer *buffer, char *buf, int value)
|
||||
void EmitGender(Buffer *buffer, char *buf, int)
|
||||
{
|
||||
int argidx = _cur_argidx;
|
||||
int offset = 0;
|
||||
@@ -447,28 +388,28 @@ void EmitGender(Buffer *buffer, char *buf, int value)
|
||||
|
||||
/* This is a {G=DER} command */
|
||||
nw = _lang.GetGenderIndex(buf);
|
||||
if (nw >= MAX_NUM_GENDERS) strgen_fatal("G argument '%s' invalid", buf);
|
||||
if (nw >= MAX_NUM_GENDERS) StrgenFatal("G argument '{}' invalid", buf);
|
||||
|
||||
/* now nw contains the gender index */
|
||||
buffer->AppendUtf8(SCC_GENDER_INDEX);
|
||||
buffer->AppendByte(nw);
|
||||
} else {
|
||||
const char *words[MAX_NUM_GENDERS];
|
||||
std::vector<const char *> words(MAX_NUM_GENDERS, nullptr);
|
||||
|
||||
/* This is a {G 0 foo bar two} command.
|
||||
* If no relative number exists, default to +0 */
|
||||
ParseRelNum(&buf, &argidx, &offset);
|
||||
|
||||
const CmdStruct *cmd = _cur_pcs.cmd[argidx];
|
||||
const CmdStruct *cmd = _cur_pcs.consuming_commands[argidx];
|
||||
if (cmd == nullptr || (cmd->flags & C_GENDER) == 0) {
|
||||
strgen_fatal("Command '%s' can't have a gender", cmd == nullptr ? "<empty>" : cmd->cmd);
|
||||
StrgenFatal("Command '{}' can't have a gender", cmd == nullptr ? "<empty>" : cmd->cmd);
|
||||
}
|
||||
|
||||
for (nw = 0; nw < MAX_NUM_GENDERS; nw++) {
|
||||
words[nw] = ParseWord(&buf);
|
||||
if (words[nw] == nullptr) break;
|
||||
}
|
||||
if (nw != _lang.num_genders) strgen_fatal("Bad # of arguments for gender command");
|
||||
if (nw != _lang.num_genders) StrgenFatal("Bad # of arguments for gender command");
|
||||
|
||||
assert(IsInsideBS(cmd->value, SCC_CONTROL_START, UINT8_MAX));
|
||||
buffer->AppendUtf8(SCC_GENDER_LIST);
|
||||
@@ -493,8 +434,8 @@ static uint ResolveCaseName(const char *str, size_t len)
|
||||
memcpy(case_str, str, len);
|
||||
case_str[len] = '\0';
|
||||
|
||||
uint8 case_idx = _lang.GetCaseIndex(case_str);
|
||||
if (case_idx >= MAX_NUM_CASES) strgen_fatal("Invalid case-name '%s'", case_str);
|
||||
uint8_t case_idx = _lang.GetCaseIndex(case_str);
|
||||
if (case_idx >= MAX_NUM_CASES) StrgenFatal("Invalid case-name '{}'", case_str);
|
||||
return case_idx + 1;
|
||||
}
|
||||
|
||||
@@ -518,8 +459,8 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
|
||||
if (*s >= '0' && *s <= '9') {
|
||||
char *end;
|
||||
|
||||
*argno = strtoul(s, &end, 0);
|
||||
if (*end != ':') strgen_fatal("missing arg #");
|
||||
*argno = std::strtoul(s, &end, 0);
|
||||
if (*end != ':') StrgenFatal("missing arg #");
|
||||
s = end + 1;
|
||||
}
|
||||
|
||||
@@ -531,7 +472,8 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
|
||||
|
||||
const CmdStruct *cmd = FindCmd(start, s - start - 1);
|
||||
if (cmd == nullptr) {
|
||||
strgen_error("Undefined command '%.*s'", (int)(s - start - 1), start);
|
||||
std::string command(start, s - start - 1);
|
||||
StrgenError("Undefined command '{}'", command);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -539,7 +481,7 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
|
||||
const char *casep = s;
|
||||
|
||||
if (!(cmd->flags & C_CASE)) {
|
||||
strgen_fatal("Command '%s' can't have a case", cmd->cmd);
|
||||
StrgenFatal("Command '{}' can't have a case", cmd->cmd);
|
||||
}
|
||||
|
||||
do {
|
||||
@@ -549,7 +491,7 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
|
||||
}
|
||||
|
||||
if (c == '\0') {
|
||||
strgen_error("Missing } from command '%s'", start);
|
||||
StrgenError("Missing }} from command '{}'", start);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -562,10 +504,10 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
|
||||
c = *s++;
|
||||
if (c == '}') break;
|
||||
if (c == '\0') {
|
||||
strgen_error("Missing } from command '%s'", start);
|
||||
StrgenError("Missing }} from command '{}'", start);
|
||||
return nullptr;
|
||||
}
|
||||
if (s - start == MAX_COMMAND_PARAM_SIZE) error("param command too long");
|
||||
if (s - start == MAX_COMMAND_PARAM_SIZE) FatalError("param command too long");
|
||||
*param++ = c;
|
||||
}
|
||||
}
|
||||
@@ -583,25 +525,19 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
|
||||
* @param master Are we reading the master file?
|
||||
* @param translation Are we reading a translation?
|
||||
*/
|
||||
StringReader::StringReader(StringData &data, const char *file, bool master, bool translation) :
|
||||
data(data), file(stredup(file)), master(master), translation(translation)
|
||||
StringReader::StringReader(StringData &data, const std::string &file, bool master, bool translation) :
|
||||
data(data), file(file), master(master), translation(translation)
|
||||
{
|
||||
}
|
||||
|
||||
/** Make sure the right reader gets freed. */
|
||||
StringReader::~StringReader()
|
||||
{
|
||||
free(file);
|
||||
}
|
||||
|
||||
static void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool warnings)
|
||||
ParsedCommandStruct ExtractCommandString(const char *s, bool)
|
||||
{
|
||||
char param[MAX_COMMAND_PARAM_SIZE];
|
||||
int argno;
|
||||
int argidx = 0;
|
||||
int casei;
|
||||
|
||||
memset(p, 0, sizeof(*p));
|
||||
ParsedCommandStruct p;
|
||||
|
||||
for (;;) {
|
||||
/* read until next command from a. */
|
||||
@@ -610,25 +546,24 @@ static void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool war
|
||||
if (ar == nullptr) break;
|
||||
|
||||
/* Sanity checking */
|
||||
if (argno != -1 && ar->consumes == 0) strgen_fatal("Non consumer param can't have a paramindex");
|
||||
if (argno != -1 && ar->consumes == 0) StrgenFatal("Non consumer param can't have a paramindex");
|
||||
|
||||
if (ar->consumes) {
|
||||
if (argno != -1) argidx = argno;
|
||||
if (argidx < 0 || (uint)argidx >= lengthof(p->cmd)) strgen_fatal("invalid param idx %d", argidx);
|
||||
if (p->cmd[argidx] != nullptr && p->cmd[argidx] != ar) strgen_fatal("duplicate param idx %d", argidx);
|
||||
if (argidx < 0 || (uint)argidx >= p.consuming_commands.max_size()) StrgenFatal("invalid param idx {}", argidx);
|
||||
if (p.consuming_commands[argidx] != nullptr && p.consuming_commands[argidx] != ar) StrgenFatal("duplicate param idx {}", argidx);
|
||||
|
||||
p->cmd[argidx++] = ar;
|
||||
p.consuming_commands[argidx++] = ar;
|
||||
} else if (!(ar->flags & C_DONTCOUNT)) { // Ignore some of them
|
||||
if (p->np >= lengthof(p->pairs)) strgen_fatal("too many commands in string, max " PRINTF_SIZE, lengthof(p->pairs));
|
||||
p->pairs[p->np].a = ar;
|
||||
p->pairs[p->np].v = param[0] != '\0' ? stredup(param) : "";
|
||||
p->np++;
|
||||
p.non_consuming_commands.emplace_back(CmdPair{ar, param});
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
|
||||
const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
|
||||
{
|
||||
if (a == nullptr) return nullptr;
|
||||
|
||||
@@ -647,7 +582,7 @@ static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
|
||||
}
|
||||
|
||||
|
||||
static bool CheckCommandsMatch(char *a, char *b, const char *name)
|
||||
static bool CheckCommandsMatch(const char *a, const char *b, const char *name)
|
||||
{
|
||||
/* If we're not translating, i.e. we're compiling the base language,
|
||||
* it is pointless to do all these checks as it'll always be correct.
|
||||
@@ -655,45 +590,42 @@ static bool CheckCommandsMatch(char *a, char *b, const char *name)
|
||||
*/
|
||||
if (!_translation) return true;
|
||||
|
||||
ParsedCommandStruct templ;
|
||||
ParsedCommandStruct lang;
|
||||
bool result = true;
|
||||
|
||||
ExtractCommandString(&templ, b, true);
|
||||
ExtractCommandString(&lang, a, true);
|
||||
ParsedCommandStruct templ = ExtractCommandString(b, true);
|
||||
ParsedCommandStruct lang = ExtractCommandString(a, true);
|
||||
|
||||
/* For each string in templ, see if we find it in lang */
|
||||
if (templ.np != lang.np) {
|
||||
strgen_warning("%s: template string and language string have a different # of commands", name);
|
||||
if (templ.non_consuming_commands.max_size() != lang.non_consuming_commands.max_size()) {
|
||||
StrgenWarning("{}: template string and language string have a different # of commands", name);
|
||||
result = false;
|
||||
}
|
||||
|
||||
for (uint i = 0; i < templ.np; i++) {
|
||||
for (auto &templ_nc : templ.non_consuming_commands) {
|
||||
/* see if we find it in lang, and zero it out */
|
||||
bool found = false;
|
||||
for (uint j = 0; j < lang.np; j++) {
|
||||
if (templ.pairs[i].a == lang.pairs[j].a &&
|
||||
strcmp(templ.pairs[i].v, lang.pairs[j].v) == 0) {
|
||||
for (auto &lang_nc : lang.non_consuming_commands) {
|
||||
if (templ_nc.cmd == lang_nc.cmd && templ_nc.param == lang_nc.param) {
|
||||
/* it was found in both. zero it out from lang so we don't find it again */
|
||||
lang.pairs[j].a = nullptr;
|
||||
lang_nc.cmd = nullptr;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
strgen_warning("%s: command '%s' exists in template file but not in language file", name, templ.pairs[i].a->cmd);
|
||||
StrgenWarning("{}: command '{}' exists in template file but not in language file", name, templ_nc.cmd->cmd);
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
/* if we reach here, all non consumer commands match up.
|
||||
* Check if the non consumer commands match up also. */
|
||||
for (uint i = 0; i < lengthof(templ.cmd); i++) {
|
||||
if (TranslateCmdForCompare(templ.cmd[i]) != lang.cmd[i]) {
|
||||
strgen_warning("%s: Param idx #%d '%s' doesn't match with template command '%s'", name, i,
|
||||
lang.cmd[i] == nullptr ? "<empty>" : TranslateCmdForCompare(lang.cmd[i])->cmd,
|
||||
templ.cmd[i] == nullptr ? "<empty>" : templ.cmd[i]->cmd);
|
||||
for (uint i = 0; i < templ.consuming_commands.max_size(); i++) {
|
||||
if (TranslateCmdForCompare(templ.consuming_commands[i]) != lang.consuming_commands[i]) {
|
||||
StrgenWarning("{}: Param idx #{} '{}' doesn't match with template command '{}'", name, i,
|
||||
lang.consuming_commands[i] == nullptr ? "<empty>" : TranslateCmdForCompare(lang.consuming_commands[i])->cmd,
|
||||
templ.consuming_commands[i] == nullptr ? "<empty>" : templ.consuming_commands[i]->cmd);
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
@@ -713,7 +645,7 @@ void StringReader::HandleString(char *str)
|
||||
|
||||
char *s = strchr(str, ':');
|
||||
if (s == nullptr) {
|
||||
strgen_error("Line has no ':' delimiter");
|
||||
StrgenError("Line has no ':' delimiter");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -728,15 +660,15 @@ void StringReader::HandleString(char *str)
|
||||
const char *tmp;
|
||||
for (tmp = s; *tmp != '\0';) {
|
||||
size_t len = Utf8Validate(tmp);
|
||||
if (len == 0) strgen_fatal("Invalid UTF-8 sequence in '%s'", s);
|
||||
if (len == 0) StrgenFatal("Invalid UTF-8 sequence in '{}'", s);
|
||||
|
||||
WChar c;
|
||||
char32_t c;
|
||||
Utf8Decode(&c, tmp);
|
||||
if (c <= 0x001F || // ASCII control character range
|
||||
c == 0x200B || // Zero width space
|
||||
(c >= 0xE000 && c <= 0xF8FF) || // Private range
|
||||
(c >= 0xFFF0 && c <= 0xFFFF)) { // Specials range
|
||||
strgen_fatal("Unwanted UTF-8 character U+%04X in sequence '%s'", c, s);
|
||||
StrgenFatal("Unwanted UTF-8 character U+{:04X} in sequence '{}'", (int)c, s);
|
||||
}
|
||||
|
||||
tmp += len;
|
||||
@@ -752,40 +684,40 @@ void StringReader::HandleString(char *str)
|
||||
|
||||
if (this->master) {
|
||||
if (casep != nullptr) {
|
||||
strgen_error("Cases in the base translation are not supported.");
|
||||
StrgenError("Cases in the base translation are not supported.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ent != nullptr) {
|
||||
strgen_error("String name '%s' is used multiple times", str);
|
||||
StrgenError("String name '{}' is used multiple times", str);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->data.strings[this->data.next_string_id] != nullptr) {
|
||||
strgen_error("String ID 0x" PRINTF_SIZEX " for '%s' already in use by '%s'", this->data.next_string_id, str, this->data.strings[this->data.next_string_id]->name);
|
||||
StrgenError("String ID 0x{:X} for '{}' already in use by '{}'", this->data.next_string_id, str, this->data.strings[this->data.next_string_id]->name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Allocate a new LangString */
|
||||
this->data.Add(str, new LangString(str, s, this->data.next_string_id++, _cur_line));
|
||||
this->data.Add(std::make_unique<LangString>(str, s, this->data.next_string_id++, _cur_line));
|
||||
} else {
|
||||
if (ent == nullptr) {
|
||||
strgen_warning("String name '%s' does not exist in master file", str);
|
||||
StrgenWarning("String name '{}' does not exist in master file", str);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ent->translated && casep == nullptr) {
|
||||
strgen_error("String name '%s' is used multiple times", str);
|
||||
if (!ent->translated.empty() && casep == nullptr) {
|
||||
StrgenError("String name '{}' is used multiple times", str);
|
||||
return;
|
||||
}
|
||||
|
||||
/* make sure that the commands match */
|
||||
if (!CheckCommandsMatch(s, ent->english, str)) return;
|
||||
if (!CheckCommandsMatch(s, ent->english.c_str(), str)) return;
|
||||
|
||||
if (casep != nullptr) {
|
||||
ent->translated_case = new Case(ResolveCaseName(casep, strlen(casep)), s, ent->translated_case);
|
||||
ent->translated_cases.emplace_back(ResolveCaseName(casep, strlen(casep)), s);
|
||||
} else {
|
||||
ent->translated = stredup(s);
|
||||
ent->translated = s;
|
||||
/* If the string was translated, use the line from the
|
||||
* translated language so errors in the translated file
|
||||
* are properly referenced to. */
|
||||
@@ -799,27 +731,24 @@ void StringReader::HandlePragma(char *str)
|
||||
if (!memcmp(str, "plural ", 7)) {
|
||||
_lang.plural_form = atoi(str + 7);
|
||||
if (_lang.plural_form >= lengthof(_plural_forms)) {
|
||||
strgen_fatal("Invalid pluralform %d", _lang.plural_form);
|
||||
StrgenFatal("Invalid pluralform {}", _lang.plural_form);
|
||||
}
|
||||
} else {
|
||||
strgen_fatal("unknown pragma '%s'", str);
|
||||
StrgenFatal("unknown pragma '{}'", str);
|
||||
}
|
||||
}
|
||||
|
||||
static void rstrip(char *buf)
|
||||
static void StripTrailingWhitespace(std::string &str)
|
||||
{
|
||||
size_t i = strlen(buf);
|
||||
while (i > 0 && (buf[i - 1] == '\r' || buf[i - 1] == '\n' || buf[i - 1] == ' ')) i--;
|
||||
buf[i] = '\0';
|
||||
str.erase(str.find_last_not_of("\r\n ") + 1);
|
||||
}
|
||||
|
||||
void StringReader::ParseFile()
|
||||
{
|
||||
char buf[2048];
|
||||
_warnings = _errors = 0;
|
||||
|
||||
_translation = this->translation;
|
||||
_file = this->file;
|
||||
_file = this->file.c_str();
|
||||
|
||||
/* Abusing _show_todo to replace "warning" with "info" for translations. */
|
||||
_show_todo &= 3;
|
||||
@@ -832,14 +761,17 @@ void StringReader::ParseFile()
|
||||
strecpy(_lang.digit_decimal_separator, ".", lastof(_lang.digit_decimal_separator));
|
||||
|
||||
_cur_line = 1;
|
||||
while (this->data.next_string_id < this->data.max_strings && this->ReadLine(buf, lastof(buf)) != nullptr) {
|
||||
rstrip(buf);
|
||||
this->HandleString(buf);
|
||||
while (this->data.next_string_id < this->data.max_strings) {
|
||||
std::optional<std::string> line = this->ReadLine();
|
||||
if (!line.has_value()) return;
|
||||
|
||||
StripTrailingWhitespace(line.value());
|
||||
this->HandleString(line.value().data());
|
||||
_cur_line++;
|
||||
}
|
||||
|
||||
if (this->data.next_string_id == this->data.max_strings) {
|
||||
strgen_error("Too many strings, maximum allowed is " PRINTF_SIZE, this->data.max_strings);
|
||||
StrgenError("Too many strings, maximum allowed is {}", this->data.max_strings);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -864,20 +796,20 @@ static int TranslateArgumentIdx(int argidx, int offset)
|
||||
{
|
||||
int sum;
|
||||
|
||||
if (argidx < 0 || (uint)argidx >= lengthof(_cur_pcs.cmd)) {
|
||||
strgen_fatal("invalid argidx %d", argidx);
|
||||
if (argidx < 0 || (uint)argidx >= _cur_pcs.consuming_commands.max_size()) {
|
||||
StrgenFatal("invalid argidx {}", argidx);
|
||||
}
|
||||
const CmdStruct *cs = _cur_pcs.cmd[argidx];
|
||||
const CmdStruct *cs = _cur_pcs.consuming_commands[argidx];
|
||||
if (cs != nullptr && cs->consumes <= offset) {
|
||||
strgen_fatal("invalid argidx offset %d:%d", argidx, offset);
|
||||
StrgenFatal("invalid argidx offset {}:{}", argidx, offset);
|
||||
}
|
||||
|
||||
if (_cur_pcs.cmd[argidx] == nullptr) {
|
||||
strgen_fatal("no command for this argidx %d", argidx);
|
||||
if (_cur_pcs.consuming_commands[argidx] == nullptr) {
|
||||
StrgenFatal("no command for this argidx {}", argidx);
|
||||
}
|
||||
|
||||
for (int i = sum = 0; i < argidx; i++) {
|
||||
const CmdStruct *cs = _cur_pcs.cmd[i];
|
||||
cs = _cur_pcs.consuming_commands[i];
|
||||
|
||||
sum += (cs != nullptr) ? cs->consumes : 1;
|
||||
}
|
||||
@@ -923,9 +855,9 @@ static void PutCommandString(Buffer *buffer, const char *str)
|
||||
}
|
||||
|
||||
/* Output the one from the master string... it's always accurate. */
|
||||
cs = _cur_pcs.cmd[_cur_argidx++];
|
||||
cs = _cur_pcs.consuming_commands[_cur_argidx++];
|
||||
if (cs == nullptr) {
|
||||
strgen_fatal("%s: No argument exists at position %d", _cur_ident, _cur_argidx - 1);
|
||||
StrgenFatal("{}: No argument exists at position {}", _cur_ident, _cur_argidx - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -942,7 +874,7 @@ void LanguageWriter::WriteLength(uint length)
|
||||
char buffer[2];
|
||||
int offs = 0;
|
||||
if (length >= 0x4000) {
|
||||
strgen_fatal("string too long");
|
||||
StrgenFatal("string too long");
|
||||
}
|
||||
|
||||
if (length >= 0xC0) {
|
||||
@@ -958,16 +890,16 @@ void LanguageWriter::WriteLength(uint length)
|
||||
*/
|
||||
void LanguageWriter::WriteLang(const StringData &data)
|
||||
{
|
||||
uint *in_use = AllocaM(uint, data.tabs);
|
||||
std::vector<uint> in_use;
|
||||
for (size_t tab = 0; tab < data.tabs; tab++) {
|
||||
uint n = data.CountInUse((uint)tab);
|
||||
|
||||
in_use[tab] = n;
|
||||
in_use.push_back(n);
|
||||
_lang.offsets[tab] = TO_LE16(n);
|
||||
|
||||
for (uint j = 0; j != in_use[tab]; j++) {
|
||||
const LangString *ls = data.strings[(tab * TAB_SIZE) + j];
|
||||
if (ls != nullptr && ls->translated == nullptr) _lang.missing++;
|
||||
const LangString *ls = data.strings[(tab * TAB_SIZE) + j].get();
|
||||
if (ls != nullptr && ls->translated.empty()) _lang.missing++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -981,9 +913,8 @@ void LanguageWriter::WriteLang(const StringData &data)
|
||||
|
||||
for (size_t tab = 0; tab < data.tabs; tab++) {
|
||||
for (uint j = 0; j != in_use[tab]; j++) {
|
||||
const LangString *ls = data.strings[(tab * TAB_SIZE) + j];
|
||||
const Case *casep;
|
||||
const char *cmdp;
|
||||
const LangString *ls = data.strings[(tab * TAB_SIZE) + j].get();
|
||||
const std::string *cmdp;
|
||||
|
||||
/* For undefined strings, just set that it's an empty string */
|
||||
if (ls == nullptr) {
|
||||
@@ -991,13 +922,13 @@ void LanguageWriter::WriteLang(const StringData &data)
|
||||
continue;
|
||||
}
|
||||
|
||||
_cur_ident = ls->name;
|
||||
_cur_ident = ls->name.c_str();
|
||||
_cur_line = ls->line;
|
||||
|
||||
/* Produce a message if a string doesn't have a translation. */
|
||||
if (_show_todo > 0 && ls->translated == nullptr) {
|
||||
if (_show_todo > 0 && ls->translated.empty()) {
|
||||
if ((_show_todo & 2) != 0) {
|
||||
strgen_warning("'%s' is untranslated", ls->name);
|
||||
StrgenWarning("'{}' is untranslated", ls->name);
|
||||
}
|
||||
if ((_show_todo & 1) != 0) {
|
||||
const char *s = "<TODO> ";
|
||||
@@ -1006,40 +937,33 @@ void LanguageWriter::WriteLang(const StringData &data)
|
||||
}
|
||||
|
||||
/* Extract the strings and stuff from the english command string */
|
||||
ExtractCommandString(&_cur_pcs, ls->english, false);
|
||||
_cur_pcs = ExtractCommandString(ls->english.c_str(), false);
|
||||
|
||||
if (ls->translated_case != nullptr || ls->translated != nullptr) {
|
||||
casep = ls->translated_case;
|
||||
cmdp = ls->translated;
|
||||
if (!ls->translated_cases.empty() || !ls->translated.empty()) {
|
||||
cmdp = &ls->translated;
|
||||
} else {
|
||||
casep = nullptr;
|
||||
cmdp = ls->english;
|
||||
cmdp = &ls->english;
|
||||
}
|
||||
|
||||
_translated = cmdp != ls->english;
|
||||
|
||||
if (casep != nullptr) {
|
||||
const Case *c;
|
||||
uint num;
|
||||
_translated = cmdp != &ls->english;
|
||||
|
||||
if (!ls->translated_cases.empty()) {
|
||||
/* Need to output a case-switch.
|
||||
* It has this format
|
||||
* <0x9E> <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
|
||||
* Each LEN is printed using 2 bytes in big endian order. */
|
||||
buffer.AppendUtf8(SCC_SWITCH_CASE);
|
||||
/* Count the number of cases */
|
||||
for (num = 0, c = casep; c; c = c->next) num++;
|
||||
buffer.AppendByte(num);
|
||||
buffer.AppendByte((byte)ls->translated_cases.size());
|
||||
|
||||
/* Write each case */
|
||||
for (c = casep; c != nullptr; c = c->next) {
|
||||
buffer.AppendByte(c->caseidx);
|
||||
for (const Case &c : ls->translated_cases) {
|
||||
buffer.AppendByte(c.caseidx);
|
||||
/* Make some space for the 16-bit length */
|
||||
uint pos = (uint)buffer.size();
|
||||
buffer.AppendByte(0);
|
||||
buffer.AppendByte(0);
|
||||
/* Write string */
|
||||
PutCommandString(&buffer, c->string);
|
||||
PutCommandString(&buffer, c.string.c_str());
|
||||
buffer.AppendByte(0); // terminate with a zero
|
||||
/* Fill in the length */
|
||||
uint size = (uint)buffer.size() - (pos + 2);
|
||||
@@ -1048,7 +972,7 @@ void LanguageWriter::WriteLang(const StringData &data)
|
||||
}
|
||||
}
|
||||
|
||||
if (cmdp != nullptr) PutCommandString(&buffer, cmdp);
|
||||
if (!cmdp->empty()) PutCommandString(&buffer, cmdp->c_str());
|
||||
|
||||
this->WriteLength((uint)buffer.size());
|
||||
this->Write(buffer.data(), buffer.size());
|
||||
|
||||
Reference in New Issue
Block a user