mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r23565) -Codechange: create some classes for writing language header and translation files
This commit is contained in:
parent
24b7be856b
commit
15906ca874
|
@ -18,6 +18,7 @@
|
||||||
#include "../table/control_codes.h"
|
#include "../table/control_codes.h"
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
#if (!defined(WIN32) && !defined(WIN64)) || defined(__CYGWIN__)
|
#if (!defined(WIN32) && !defined(WIN64)) || defined(__CYGWIN__)
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -48,8 +49,6 @@ struct Case {
|
||||||
static bool _translated; ///< Whether the current language is not the master language
|
static bool _translated; ///< Whether the current language is not the master language
|
||||||
static bool _translation; ///< Is the current file actually a translation or not
|
static bool _translation; ///< Is the current file actually a translation or not
|
||||||
static const char *_file = "(unknown file)"; ///< The filename of the input, so we can refer to it in errors/warnings
|
static const char *_file = "(unknown file)"; ///< The filename of the input, so we can refer to it in errors/warnings
|
||||||
static FILE *_output_file = NULL; ///< The file we are currently writing output to
|
|
||||||
static const char *_output_filename = NULL; ///< The filename of the output, so we can delete it if compilation fails
|
|
||||||
static int _cur_line; ///< The current line we're parsing in the input file
|
static int _cur_line; ///< The current line we're parsing in the input file
|
||||||
static int _errors, _warnings, _show_todo;
|
static int _errors, _warnings, _show_todo;
|
||||||
|
|
||||||
|
@ -165,12 +164,7 @@ void NORETURN CDECL error(const char *s, ...)
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
|
||||||
#endif
|
#endif
|
||||||
/* We were writing output to a file, remove it. */
|
throw std::exception();
|
||||||
if (_output_file != NULL) {
|
|
||||||
fclose(_output_file);
|
|
||||||
unlink(_output_filename);
|
|
||||||
}
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PutByte(byte c)
|
static void PutByte(byte c)
|
||||||
|
@ -907,59 +901,134 @@ bool CompareFiles(const char *n1, const char *n2)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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.
|
||||||
|
|
||||||
static void WriteStringsH(const char *filename)
|
/**
|
||||||
{
|
* Open a file to write to.
|
||||||
int next = -1;
|
* @param filename The file to open.
|
||||||
|
*/
|
||||||
|
FileWriter(const char *filename)
|
||||||
|
{
|
||||||
|
this->filename = strdup(filename);
|
||||||
|
this->fh = fopen(this->filename, "wb");
|
||||||
|
|
||||||
_output_filename = "tmp.xxx";
|
if (this->fh == NULL) {
|
||||||
_output_file = fopen(_output_filename, "w");
|
error("Could not open %s", this->filename);
|
||||||
if (_output_file == NULL) error("can't open tmp.xxx");
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(_output_file, "/* This file is automatically generated. Do not modify */\n\n");
|
/** Finalise the writing. */
|
||||||
fprintf(_output_file, "#ifndef TABLE_STRINGS_H\n");
|
void Finalise()
|
||||||
fprintf(_output_file, "#define TABLE_STRINGS_H\n");
|
{
|
||||||
|
fclose(this->fh);
|
||||||
|
this->fh = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Make sure the file is closed. */
|
||||||
|
virtual ~FileWriter()
|
||||||
|
{
|
||||||
|
printf("close %s %p\n", this->filename, this->fh);
|
||||||
|
/* If we weren't closed an exception was thrown, so remove the termporary file. */
|
||||||
|
if (fh != NULL) {
|
||||||
|
fclose(this->fh);
|
||||||
|
unlink(this->filename);
|
||||||
|
}
|
||||||
|
free(this->filename);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Base class for writing the header. */
|
||||||
|
struct HeaderWriter {
|
||||||
|
/**
|
||||||
|
* Write the string ID.
|
||||||
|
* @param name The name of the string.
|
||||||
|
* @param stringid The ID of the string.
|
||||||
|
*/
|
||||||
|
virtual void WriteStringID(const char *name, int stringid) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finalise writing the file.
|
||||||
|
*/
|
||||||
|
virtual void Finalise() = 0;
|
||||||
|
|
||||||
|
/** Especially destroy the subclasses. */
|
||||||
|
virtual ~HeaderWriter() {};
|
||||||
|
|
||||||
|
/** Write the header information. */
|
||||||
|
void WriteHeader()
|
||||||
|
{
|
||||||
|
int last = 0;
|
||||||
for (int i = 0; i != lengthof(_strings); i++) {
|
for (int i = 0; i != lengthof(_strings); i++) {
|
||||||
if (_strings[i] != NULL) {
|
if (_strings[i] != NULL) {
|
||||||
if (next != i) fprintf(_output_file, "\n");
|
this->WriteStringID(_strings[i]->name, i);
|
||||||
fprintf(_output_file, "static const StringID %s = 0x%X;\n", _strings[i]->name, i);
|
last = i;
|
||||||
next = i + 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(_output_file, "\nstatic const StringID STR_LAST_STRINGID = 0x%X;\n\n", next - 1);
|
this->WriteStringID("STR_LAST_STRINGID", last);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct HeaderFileWriter : HeaderWriter, FileWriter {
|
||||||
|
/** The real file name we eventually want to write to. */
|
||||||
|
const char *real_filename;
|
||||||
|
/** The previous string ID that was printed. */
|
||||||
|
int prev;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open a file to write to.
|
||||||
|
* @param filename The file to open.
|
||||||
|
*/
|
||||||
|
HeaderFileWriter(const char *filename) : FileWriter("tmp.xxx"),
|
||||||
|
real_filename(strdup(filename)), prev(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");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
prev = stringid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Finalise()
|
||||||
|
{
|
||||||
/* Find the plural form with the most amount of cases. */
|
/* Find the plural form with the most amount of cases. */
|
||||||
int max_plural_forms = 0;
|
int max_plural_forms = 0;
|
||||||
for (uint i = 0; i < lengthof(_plural_forms); i++) {
|
for (uint i = 0; i < lengthof(_plural_forms); i++) {
|
||||||
max_plural_forms = max(max_plural_forms, _plural_forms[i].plural_count);
|
max_plural_forms = max(max_plural_forms, _plural_forms[i].plural_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(_output_file,
|
fprintf(this->fh,
|
||||||
|
"\n"
|
||||||
"static const uint LANGUAGE_PACK_VERSION = 0x%X;\n"
|
"static const uint LANGUAGE_PACK_VERSION = 0x%X;\n"
|
||||||
"static const uint LANGUAGE_MAX_PLURAL = %d;\n"
|
"static const uint LANGUAGE_MAX_PLURAL = %d;\n"
|
||||||
"static const uint LANGUAGE_MAX_PLURAL_FORMS = %d;\n\n",
|
"static const uint LANGUAGE_MAX_PLURAL_FORMS = %d;\n\n",
|
||||||
(uint)_hash, (uint)lengthof(_plural_forms), max_plural_forms
|
(uint)_hash, (uint)lengthof(_plural_forms), max_plural_forms
|
||||||
);
|
);
|
||||||
|
|
||||||
fprintf(_output_file, "#endif /* TABLE_STRINGS_H */\n");
|
fprintf(this->fh, "#endif /* TABLE_STRINGS_H */\n");
|
||||||
|
|
||||||
fclose(_output_file);
|
this->FileWriter::Finalise();
|
||||||
_output_file = NULL;
|
|
||||||
|
|
||||||
if (CompareFiles(_output_filename, filename)) {
|
if (CompareFiles(this->filename, this->real_filename)) {
|
||||||
/* files are equal. tmp.xxx is not needed */
|
/* files are equal. tmp.xxx is not needed */
|
||||||
unlink(_output_filename);
|
unlink(this->filename);
|
||||||
} else {
|
} else {
|
||||||
/* else rename tmp.xxx into filename */
|
/* else rename tmp.xxx into filename */
|
||||||
#if defined(WIN32) || defined(WIN64)
|
#if defined(WIN32) || defined(WIN64)
|
||||||
unlink(filename);
|
unlink(filename);
|
||||||
#endif
|
#endif
|
||||||
if (rename(_output_filename, filename) == -1) error("rename() failed");
|
if (rename(this->filename, this->real_filename) == -1) error("rename() failed");
|
||||||
}
|
}
|
||||||
_output_filename = NULL;
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
static int TranslateArgumentIdx(int argidx, int offset)
|
static int TranslateArgumentIdx(int argidx, int offset)
|
||||||
{
|
{
|
||||||
|
@ -1034,27 +1103,55 @@ static void PutCommandString(const char *str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteLength(FILE *f, uint length)
|
/** Base class for all language writers. */
|
||||||
{
|
struct LanguageWriter {
|
||||||
if (length < 0xC0) {
|
/**
|
||||||
fputc(length, f);
|
* Write the header metadata. The multi-byte integers are already converted to
|
||||||
} else if (length < 0x4000) {
|
* the little endian format.
|
||||||
fputc((length >> 8) | 0xC0, f);
|
* @param header The header to write.
|
||||||
fputc(length & 0xFF, f);
|
*/
|
||||||
} else {
|
virtual void WriteHeader(const LanguagePackHeader *header) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a number of bytes.
|
||||||
|
* @param buffer The buffer to write.
|
||||||
|
* @param length The amount of byte to write.
|
||||||
|
*/
|
||||||
|
virtual void Write(const byte *buffer, size_t length) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finalise writing the file.
|
||||||
|
*/
|
||||||
|
virtual void Finalise() = 0;
|
||||||
|
|
||||||
|
/** Especially destroy the subclasses. */
|
||||||
|
virtual ~LanguageWriter() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write the length as a simple gamma.
|
||||||
|
* @param length The number to write.
|
||||||
|
*/
|
||||||
|
void WriteLength(uint length)
|
||||||
|
{
|
||||||
|
char buffer[2];
|
||||||
|
int offs = 0;
|
||||||
|
if (length >= 0x4000) {
|
||||||
error("string too long");
|
error("string too long");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (length >= 0xC0) {
|
||||||
|
buffer[offs++] = (length >> 8) | 0xC0;
|
||||||
|
}
|
||||||
|
buffer[offs++] = length & 0xFF;
|
||||||
|
this->Write((byte*)buffer, offs);
|
||||||
|
}
|
||||||
|
|
||||||
static void WriteLangfile(const char *filename)
|
/**
|
||||||
{
|
* Actually write the language.
|
||||||
|
*/
|
||||||
|
void WriteLang()
|
||||||
|
{
|
||||||
uint in_use[32];
|
uint in_use[32];
|
||||||
|
|
||||||
_output_filename = filename;
|
|
||||||
_output_file = fopen(filename, "wb");
|
|
||||||
if (_output_file == NULL) error("can't open %s", filename);
|
|
||||||
|
|
||||||
for (int i = 0; i != 32; i++) {
|
for (int i = 0; i != 32; i++) {
|
||||||
uint n = CountInUse(i);
|
uint n = CountInUse(i);
|
||||||
|
|
||||||
|
@ -1072,7 +1169,7 @@ static void WriteLangfile(const char *filename)
|
||||||
_lang.missing = TO_LE16(_lang.missing);
|
_lang.missing = TO_LE16(_lang.missing);
|
||||||
_lang.winlangid = TO_LE16(_lang.winlangid);
|
_lang.winlangid = TO_LE16(_lang.winlangid);
|
||||||
|
|
||||||
fwrite(&_lang, sizeof(_lang), 1, _output_file);
|
this->WriteHeader(&_lang);
|
||||||
|
|
||||||
for (int i = 0; i != 32; i++) {
|
for (int i = 0; i != 32; i++) {
|
||||||
for (uint j = 0; j != in_use[i]; j++) {
|
for (uint j = 0; j != in_use[i]; j++) {
|
||||||
|
@ -1082,7 +1179,7 @@ static void WriteLangfile(const char *filename)
|
||||||
|
|
||||||
/* For undefined strings, just set that it's an empty string */
|
/* For undefined strings, just set that it's an empty string */
|
||||||
if (ls == NULL) {
|
if (ls == NULL) {
|
||||||
WriteLength(_output_file, 0);
|
this->WriteLength(0);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1146,18 +1243,42 @@ static void WriteLangfile(const char *filename)
|
||||||
|
|
||||||
if (cmdp != NULL) PutCommandString(cmdp);
|
if (cmdp != NULL) PutCommandString(cmdp);
|
||||||
|
|
||||||
WriteLength(_output_file, _put_pos);
|
this->WriteLength(_put_pos);
|
||||||
fwrite(_put_buf, 1, _put_pos, _output_file);
|
this->Write(_put_buf, _put_pos);
|
||||||
_put_pos = 0;
|
_put_pos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
fputc(0, _output_file);
|
/** Class for writing a language to disk. */
|
||||||
fclose(_output_file);
|
struct LanguageFileWriter : LanguageWriter, FileWriter {
|
||||||
|
/**
|
||||||
|
* Open a file to write to.
|
||||||
|
* @param filename The file to open.
|
||||||
|
*/
|
||||||
|
LanguageFileWriter(const char *filename) : FileWriter(filename)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
_output_file = NULL;
|
void WriteHeader(const LanguagePackHeader *header)
|
||||||
_output_filename = NULL;
|
{
|
||||||
}
|
this->Write((const byte *)header, sizeof(*header));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Finalise()
|
||||||
|
{
|
||||||
|
fputc(0, this->fh);
|
||||||
|
this->FileWriter::Finalise();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Write(const byte *buffer, size_t length)
|
||||||
|
{
|
||||||
|
if (fwrite(buffer, sizeof(*buffer), length, this->fh) != length) {
|
||||||
|
error("Could not write to %s", this->filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/** Multi-OS mkdirectory function */
|
/** Multi-OS mkdirectory function */
|
||||||
static inline void ottd_mkdir(const char *directory)
|
static inline void ottd_mkdir(const char *directory)
|
||||||
|
@ -1314,6 +1435,7 @@ int CDECL main(int argc, char *argv[])
|
||||||
|
|
||||||
if (dest_dir == NULL) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
|
if (dest_dir == NULL) 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
|
/* strgen has two modes of operation. If no (free) arguments are passed
|
||||||
* strgen generates strings.h to the destination directory. If it is supplied
|
* strgen generates strings.h to the destination directory. If it is supplied
|
||||||
* with a (free) parameter the program will translate that language to destination
|
* with a (free) parameter the program will translate that language to destination
|
||||||
|
@ -1329,7 +1451,10 @@ int CDECL main(int argc, char *argv[])
|
||||||
/* write strings.h */
|
/* write strings.h */
|
||||||
ottd_mkdir(dest_dir);
|
ottd_mkdir(dest_dir);
|
||||||
mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h");
|
mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h");
|
||||||
WriteStringsH(pathbuf);
|
|
||||||
|
HeaderFileWriter writer(pathbuf);
|
||||||
|
writer.WriteHeader();
|
||||||
|
writer.Finalise();
|
||||||
} else if (mgo.numleft == 1) {
|
} else if (mgo.numleft == 1) {
|
||||||
char *r;
|
char *r;
|
||||||
|
|
||||||
|
@ -1349,7 +1474,10 @@ int CDECL main(int argc, char *argv[])
|
||||||
r = strrchr(pathbuf, '.');
|
r = strrchr(pathbuf, '.');
|
||||||
if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
|
if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
|
||||||
ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf));
|
ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf));
|
||||||
WriteLangfile(pathbuf);
|
|
||||||
|
LanguageFileWriter writer(pathbuf);
|
||||||
|
writer.WriteLang();
|
||||||
|
writer.Finalise();
|
||||||
|
|
||||||
/* if showing warnings, print a summary of the language */
|
/* if showing warnings, print a summary of the language */
|
||||||
if ((_show_todo & 2) != 0) {
|
if ((_show_todo & 2) != 0) {
|
||||||
|
@ -1358,6 +1486,9 @@ int CDECL main(int argc, char *argv[])
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Invalid arguments\n");
|
fprintf(stderr, "Invalid arguments\n");
|
||||||
}
|
}
|
||||||
|
} catch (...) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue