12 #include "../stdafx.h" 13 #include "../strgen/strgen.h" 15 #include "../fileio_func.h" 16 #include "../tar_type.h" 17 #include "../script/squirrel_class.hpp" 18 #include "../strings_func.h" 23 #include "table/strings.h" 27 #include "../safeguards.h" 29 void CDECL strgen_warning(
const char *s, ...)
40 void CDECL strgen_error(
const char *s, ...)
51 void NORETURN CDECL strgen_fatal(
const char *s, ...)
59 throw std::exception();
69 this->language =
stredup(language, end != NULL ? end - 1 : NULL);
94 const char *langname = strrchr(file, PATHSEPCHAR);
95 if (langname == NULL) {
102 if (*langname ==
'.' || *langname == 0) {
110 while (to_read != 0 && fgets(buffer,
sizeof(buffer), fh) != NULL) {
111 size_t len = strlen(buffer);
115 while (i > 0 && (buffer[i - 1] ==
'\r' || buffer[i - 1] ==
'\n' || buffer[i - 1] ==
' ')) i--;
130 if (fh != NULL) fclose(fh);
139 const char *
const *
p;
156 if (this->p == this->end)
return NULL;
158 strecpy(buffer, *this->p, last);
192 void Write(
const byte *buffer,
size_t length)
194 char *dest = MallocT<char>(length + 1);
195 memcpy(dest, buffer, length);
197 *this->strings->
Append() = dest;
240 void Scan(
const char *directory)
245 bool AddFile(
const char *filename,
size_t basepath_length,
const char *tar_filename)
247 if (strcmp(filename, exclude) == 0)
return true;
263 char *e = strrchr(filename, PATHSEPCHAR);
264 if (e == NULL)
return NULL;
277 size_t len = strlen(filename);
279 const char *tar_filename = info->
GetTarFile();
280 TarList::iterator iter;
281 if (tar_filename != NULL && (iter = _tar_list[
GAME_DIR].find(tar_filename)) != _tar_list[
GAME_DIR].end()) {
284 TarFileList::iterator tar;
287 if (tar->second.tar_filename != iter->first)
continue;
290 if (tar->first.size() <= len || tar->first.compare(0, len, filename) != 0)
continue;
291 if (tar->first.compare(tar->first.size() - 4, 4,
".txt") != 0)
continue;
293 scanner.
AddFile(tar->first.c_str(), 0, tar_filename);
297 scanner.
Scan(filename);
314 if (_errors != 0)
throw std::exception();
316 this->version = data.
Version();
321 for (
LanguageStrings **p = this->raw_strings.Begin(); p != this->raw_strings.End(); p++) {
323 StringListReader translation_reader(data, *p,
false, strcmp((*p)->language,
"english") != 0);
325 if (_errors != 0)
throw std::exception();
355 if (_current_data == NULL)
return;
357 HSQUIRRELVM vm = engine->
GetVM();
358 sq_pushroottable(vm);
359 sq_pushstring(vm,
"GSText", -1);
360 if (SQ_FAILED(sq_get(vm, -2)))
return;
364 sq_pushstring(vm, *p, -1);
365 sq_pushinteger(vm, idx);
379 if (_current_data == NULL)
return;
385 char *l = strrchr(temp,
'.');
390 char *
language = strrchr(temp, PATHSEPCHAR);
391 assert(language != NULL);
395 if (strcmp((*p)->language, language) == 0) {
LanguageStrings * cur_language
The current (compiled) language.
Class for writing an encoded language.
virtual void WriteLang(const StringData &data)
Actually write the language.
int _cur_line
The current line we're parsing in the input file.
Container for all the game strings.
void WriteStringID(const char *name, int stringid)
Write the string ID.
A reader that simply reads using fopen.
void RegisterGameTranslation(Squirrel *engine)
Register the current translation to the Squirrel engine.
const char *const * end
The end of the iteration.
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
AutoDeleteSmallVector< LanguageStrings *, 4 > raw_strings
The raw strings per language, first must be English/the master language!.
AutoDeleteSmallVector< LanguageStrings *, 4 > compiled_strings
The compiled strings per language, first must be English/the master language!.
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
const LanguageMetadata * _current_language
The currently loaded language.
StringList * strings
The string names.
const T * Begin() const
Get the pointer to the first item (const)
Base functions regarding game texts.
Subdirectory for all game scripts.
#define lastof(x)
Get the last element of an fixed size array.
All static information from an Game like name, version, etc.
const T * End() const
Get the pointer behind the last valid item (const)
LanguageStrings(const char *language, const char *end=NULL)
Create a new container for language strings.
T * Append(uint to_add=1)
Append an item and return it.
Helper for scanning for files with a given name.
const char * GetMainScript() const
Get the filename of the main.nut script.
Information about the currently known strings.
void Scan(const char *directory)
Scan.
LanguageScanner(GameStrings *gs, const char *exclude)
Initialise.
uint Length() const
Get the number of items in the list.
Scanner to find language files in a GameScript directory.
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
void Compile()
Compile the language.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
GameStrings * _current_data
The currently loaded game strings.
StringNameWriter(StringList *strings)
Writer for the string names.
Container for the raw (unencoded) language strings of a language.
char * ReadLine(char *buffer, const char *last)
Read a single line from the source of strings.
StringList lines
The lines of the file to pass into the parser/encoder.
uint Version() const
Make a hash of the file to get a unique "version number".
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
void WriteHeader(const LanguagePackHeader *header)
Write the header metadata.
TranslationWriter(StringList *strings)
Writer for the encoded data.
GameStrings * LoadTranslations()
Load all translations that we know of.
const char *const * p
The current location of the iteration.
void FreeTranslation()
Free all data related to the translation.
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
Check whether the given file exists.
static class GameInfo * GetInfo()
Get the current GameInfo.
HSQUIRRELVM GetVM()
Get the squirrel VM.
void Finalise()
Finalise writing the file.
virtual void ParseFile()
Start parsing the file.
#define DEBUG(name, level,...)
Output a line of debugging information.
StringListReader(StringData &data, const LanguageStrings *strings, bool master, bool translation)
Create the reader.
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
GameInfo keeps track of all information of an Game, like Author, Description, ... ...
Base class for all language writers.
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
Add a file with the given filename.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
StringList * strings
The encoded strings.
Base functions for all Games.
~LanguageStrings()
Free everything.
void Write(const byte *buffer, size_t length)
Write a number of bytes.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Class for writing the string IDs.
void Finalise(const StringData &data)
Finalise writing the file.
const char * GetGameStringPtr(uint id)
Get the string pointer of a particular game string.
const char * language
Name of the language (base filename).
StringList string_names
The names of the compiled strings.
const char * GetTarFile() const
Get the filename of the tar the script is in.
Helper for reading strings.
void WriteLength(uint length)
Write the length as a simple gamma.
LanguageStrings * ReadRawLanguageStrings(const char *file)
Read all the raw language strings from the given file.