OpenTTD
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;
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;
34 
35  LangString(const char *name, const char *english, size_t index, int line);
36  ~LangString();
37  void FreeTranslation();
38 };
39 
41 struct StringData {
43  size_t *hash_heads;
44  size_t tabs;
45  size_t max_strings;
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 {
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 
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;
148 
149 #endif /* STRGEN_H */
size_t max_strings
The maximum number of strings.
Definition: strgen.h:45
Container for the different cases of a string.
Definition: strgen.h:16
size_t * hash_heads
Hash table for the strings.
Definition: strgen.h:43
virtual ~LanguageWriter()
Especially destroy the subclasses.
Definition: strgen.h:133
int _cur_line
The current line we&#39;re parsing in the input file.
Definition: strgen_base.cpp:27
bool master
Are we reading the master file?
Definition: strgen.h:63
char * translated
Translated text.
Definition: strgen.h:29
Information about the currently known strings.
Definition: strgen.h:41
~Case()
Free everything we allocated.
Definition: strgen_base.cpp:46
char * english
English text.
Definition: strgen.h:28
bool translation
Are we reading a translation, implies !master. However, the base translation will have this false...
Definition: strgen.h:64
LangString ** strings
Array of all known strings.
Definition: strgen.h:42
Header of a language file.
Definition: language.h:24
int caseidx
The index of the case.
Definition: strgen.h:17
size_t next_string_id
The next string ID to allocate.
Definition: strgen.h:46
Information about a single string.
Definition: strgen.h:26
virtual ~HeaderWriter()
Especially destroy the subclasses.
Definition: strgen.h:106
char * name
Name of the string.
Definition: strgen.h:27
Case * next
The next, chained, case.
Definition: strgen.h:19
const char * file
The file we are reading.
Definition: strgen.h:62
LanguagePackHeader _lang
Header information about a language.
Definition: strgen_base.cpp:29
char * string
The translation of the case.
Definition: strgen.h:18
size_t index
The index in the language file.
Definition: strgen.h:31
Case * translated_case
Cases of the translation.
Definition: strgen.h:33
const char * _file
The filename of the input, so we can refer to it in errors/warnings.
Definition: strgen_base.cpp:26
StringData & data
The data to fill during reading.
Definition: strgen.h:61
Base class for all language writers.
Definition: strgen.h:112
size_t tabs
The number of &#39;tabs&#39; of strings.
Definition: strgen.h:44
Base class for writing the header, i.e.
Definition: strgen.h:91
int line
Line of string in source-file.
Definition: strgen.h:32
size_t hash_next
Next hash entry.
Definition: strgen.h:30
Helper for reading strings.
Definition: strgen.h:60
Case(int caseidx, const char *string, Case *next)
Create a new case.
Definition: strgen_base.cpp:40