OpenTTD Source: src/strgen/strgen.h Source File

OpenTTD Source 12.0-beta1

strgen.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #ifndef STRGEN_H
11 #define STRGEN_H
12
13 #include "../language.h"
14
16 struct Case {
17 int caseidx;
18 char *string;
19 Case *next;
20
21 Case(int caseidx, const char *string, Case *next);
22 ~Case();
23 };
24
26 struct LangString {
27 char *name;
28 char *english;
29 char *translated;
30 size_t hash_next;
31 size_t index;
32 int line;
33 Case *translated_case;
34
35 LangString(const char *name, const char *english, size_t index, int line);
36 ~LangString();
37 void FreeTranslation();
38 };
39
41 struct StringData {
42 LangString **strings;
43 size_t *hash_heads;
44 size_t tabs;
45 size_t max_strings;
46 size_t next_string_id;
47
48 StringData(size_t tabs);
49 ~StringData();
50 void FreeTranslation();
51 uint HashStr(const char *s) const;
52 void Add(const char *s, LangString *ls);
53 LangString *Find(const char *s);
54 uint VersionHashStr(uint hash, const char *s) const;
55 uint Version() const;
56 uint CountInUse(uint tab) const;
57 };
58
60 struct StringReader {
61 StringData &data;
62 const char *file;
63 bool master;
64 bool translation;
65
66 StringReader(StringData &data, const char *file, bool master, bool translation);
67 virtual ~StringReader();
68 void HandleString(char *str);
69
76 virtual char *ReadLine(char *buffer, const char *last) = 0;
77
82 virtual void HandlePragma(char *str);
83
87 virtual void ParseFile();
88 };
89
91 struct HeaderWriter {
97 virtual void WriteStringID(const char *name, int stringid) = 0;
98
103 virtual void Finalise(const StringData &data) = 0;
104
106 virtual ~HeaderWriter() {};
107
108 void WriteHeader(const StringData &data);
109 };
110
112 struct LanguageWriter {
118 virtual void WriteHeader(const LanguagePackHeader *header) = 0;
119
125 virtual void Write(const byte *buffer, size_t length) = 0;
126
130 virtual void Finalise() = 0;
131
133 virtual ~LanguageWriter() {}
134
135 virtual void WriteLength(uint length);
136 virtual void WriteLang(const StringData &data);
137 };
138
139 void CDECL strgen_warning(const char *s, ...) WARN_FORMAT(1, 2);
140 void CDECL strgen_error(const char *s, ...) WARN_FORMAT(1, 2);
141 void NORETURN CDECL strgen_fatal(const char *s, ...) WARN_FORMAT(1, 2);
142 char *ParseWord(char **buf);
143
144 extern const char *_file;
145 extern int _cur_line;
146 extern int _errors, _warnings, _show_todo;
147 extern LanguagePackHeader _lang;
148
149 #endif /* STRGEN_H */
LanguageWriter::Finalise
virtual void Finalise()=0
Finalise writing the file.
_file
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
Definition: strgen_base.cpp:26
StringData::HashStr
uint HashStr(const char *s) const
Create a hash of the string for finding them back quickly.
Definition: strgen_base.cpp:117
StringReader::HandlePragma
virtual void HandlePragma(char *str)
Handle the pragma of the file.
Definition: strgen_base.cpp:797
StringData::max_strings
size_t max_strings
The maximum number of strings.
Definition: strgen.h:45
Case::Case
Case(int caseidx, const char *string, Case *next)
Create a new case.
Definition: strgen_base.cpp:40
StringData::VersionHashStr
uint VersionHashStr(uint hash, const char *s) const
Create a compound hash.
Definition: strgen_base.cpp:162
StringData::Add
void Add(const char *s, LangString *ls)
Add a newly created LangString.
Definition: strgen_base.cpp:129
LangString::translated
char * translated
Translated text.
Definition: strgen.h:29
LanguageWriter::Write
virtual void Write(const byte *buffer, size_t length)=0
Write a number of bytes.
HeaderWriter
Base class for writing the header, i.e.
Definition: strgen.h:91
StringData::StringData
StringData(size_t tabs)
Create a new string data container.
Definition: strgen_base.cpp:88
StringReader::StringReader
StringReader(StringData &data, const char *file, bool master, bool translation)
Prepare reading.
Definition: strgen_base.cpp:586
StringReader::~StringReader
virtual ~StringReader()
Make sure the right reader gets freed.
Definition: strgen_base.cpp:592
LangString::~LangString
~LangString()
Free everything we allocated.
Definition: strgen_base.cpp:66
_lang
LanguagePackHeader _lang
Header information about a language.
Definition: strgen_base.cpp:29
LangString::LangString
LangString(const char *name, const char *english, size_t index, int line)
Create a new string.
Definition: strgen_base.cpp:59
Case::caseidx
int caseidx
The index of the case.
Definition: strgen.h:17
HeaderWriter::~HeaderWriter
virtual ~HeaderWriter()
Especially destroy the subclasses.
Definition: strgen.h:106
_cur_line
int _cur_line
The current line we're parsing in the input file.
Definition: strgen_base.cpp:27
LanguageWriter::WriteLang
virtual void WriteLang(const StringData &data)
Actually write the language.
Definition: strgen_base.cpp:959
StringData::Find
LangString * Find(const char *s)
Find a LangString based on the string name.
Definition: strgen_base.cpp:143
StringReader::master
bool master
Are we reading the master file?
Definition: strgen.h:63
StringData::next_string_id
size_t next_string_id
The next string ID to allocate.
Definition: strgen.h:46
LangString
Information about a single string.
Definition: strgen.h:26
StringData::Version
uint Version() const
Make a hash of the file to get a unique "version number".
Definition: strgen_base.cpp:175
HeaderWriter::Finalise
virtual void Finalise(const StringData &data)=0
Finalise writing the file.
LangString::english
char * english
English text.
Definition: strgen.h:28
LangString::name
char * name
Name of the string.
Definition: strgen.h:27
StringData
Information about the currently known strings.
Definition: strgen.h:41
Case::~Case
~Case()
Free everything we allocated.
Definition: strgen_base.cpp:46
StringData::CountInUse
uint CountInUse(uint tab) const
Count the number of tab elements that are in use.
Definition: strgen_base.cpp:211
StringData::FreeTranslation
void FreeTranslation()
Free all data related to the translation.
Definition: strgen_base.cpp:104
StringData::~StringData
~StringData()
Free everything we allocated.
Definition: strgen_base.cpp:96
StringData::strings
LangString ** strings
Array of all known strings.
Definition: strgen.h:42
Case::next
Case * next
The next, chained, case.
Definition: strgen.h:19
LanguageWriter::WriteHeader
virtual void WriteHeader(const LanguagePackHeader *header)=0
Write the header metadata.
Case::string
char * string
The translation of the case.
Definition: strgen.h:18
LangString::index
size_t index
The index in the language file.
Definition: strgen.h:31
StringData::tabs
size_t tabs
The number of 'tabs' of strings.
Definition: strgen.h:44
LangString::hash_next
size_t hash_next
Next hash entry.
Definition: strgen.h:30
StringReader::data
StringData & data
The data to fill during reading.
Definition: strgen.h:61
StringReader::ReadLine
virtual char * ReadLine(char *buffer, const char *last)=0
Read a single line from the source of strings.
LangString::line
int line
Line of string in source-file.
Definition: strgen.h:32
StringReader::translation
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false.
Definition: strgen.h:64
StringReader::ParseFile
virtual void ParseFile()
Start parsing the file.
Definition: strgen_base.cpp:816
LanguageWriter
Base class for all language writers.
Definition: strgen.h:112
LanguageWriter::~LanguageWriter
virtual ~LanguageWriter()
Especially destroy the subclasses.
Definition: strgen.h:133
HeaderWriter::WriteHeader
void WriteHeader(const StringData &data)
Write the header information.
Definition: strgen_base.cpp:850
LangString::translated_case
Case * translated_case
Cases of the translation.
Definition: strgen.h:33
StringReader::file
const char * file
The file we are reading.
Definition: strgen.h:62
StringData::hash_heads
size_t * hash_heads
Hash table for the strings.
Definition: strgen.h:43
HeaderWriter::WriteStringID
virtual void WriteStringID(const char *name, int stringid)=0
Write the string ID.
Case
Container for the different cases of a string.
Definition: strgen.h:16
LangString::FreeTranslation
void FreeTranslation()
Free all data related to the translation.
Definition: strgen_base.cpp:75
LanguageWriter::WriteLength
virtual void WriteLength(uint length)
Write the length as a simple gamma.
Definition: strgen_base.cpp:940
StringReader
Helper for reading strings.
Definition: strgen.h:60
LanguagePackHeader
Header of a language file.
Definition: language.h:24