Add: INR currency (#8136)
[openttd-github.git] / src / strgen / strgen.h
blob2110d3087607f1302f93fa626c9e99e60956cb14
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 */
8 /** @file strgen.h Structures related to strgen. */
10 #ifndef STRGEN_H
11 #define STRGEN_H
13 #include "../language.h"
15 /** Container for the different cases of a string. */
16 struct Case {
17 int caseidx; ///< The index of the case.
18 char *string; ///< The translation of the case.
19 Case *next; ///< The next, chained, case.
21 Case(int caseidx, const char *string, Case *next);
22 ~Case();
25 /** Information about a single string. */
26 struct LangString {
27 char *name; ///< Name of the string.
28 char *english; ///< English text.
29 char *translated; ///< Translated text.
30 size_t hash_next; ///< Next hash entry.
31 size_t index; ///< The index in the language file.
32 int line; ///< Line of string in source-file.
33 Case *translated_case; ///< Cases of the translation.
35 LangString(const char *name, const char *english, size_t index, int line);
36 ~LangString();
37 void FreeTranslation();
40 /** Information about the currently known strings. */
41 struct StringData {
42 LangString **strings; ///< Array of all known strings.
43 size_t *hash_heads; ///< Hash table for the strings.
44 size_t tabs; ///< The number of 'tabs' of strings.
45 size_t max_strings; ///< The maximum number of strings.
46 size_t next_string_id;///< The next string ID to allocate.
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;
59 /** Helper for reading strings. */
60 struct StringReader {
61 StringData &data; ///< The data to fill during reading.
62 const char *file; ///< The file we are reading.
63 bool master; ///< Are we reading the master file?
64 bool translation; ///< Are we reading a translation, implies !master. However, the base translation will have this false.
66 StringReader(StringData &data, const char *file, bool master, bool translation);
67 virtual ~StringReader();
68 void HandleString(char *str);
70 /**
71 * Read a single line from the source of strings.
72 * @param buffer The buffer to read the data in to.
73 * @param last The last element in the buffer.
74 * @return The buffer, or nullptr if at the end of the file.
76 virtual char *ReadLine(char *buffer, const char *last) = 0;
78 /**
79 * Handle the pragma of the file.
80 * @param str The pragma string to parse.
82 virtual void HandlePragma(char *str);
84 /**
85 * Start parsing the file.
87 virtual void ParseFile();
90 /** Base class for writing the header, i.e. the STR_XXX to numeric value. */
91 struct HeaderWriter {
92 /**
93 * Write the string ID.
94 * @param name The name of the string.
95 * @param stringid The ID of the string.
97 virtual void WriteStringID(const char *name, int stringid) = 0;
99 /**
100 * Finalise writing the file.
101 * @param data The data about the string.
103 virtual void Finalise(const StringData &data) = 0;
105 /** Especially destroy the subclasses. */
106 virtual ~HeaderWriter() {};
108 void WriteHeader(const StringData &data);
111 /** Base class for all language writers. */
112 struct LanguageWriter {
114 * Write the header metadata. The multi-byte integers are already converted to
115 * the little endian format.
116 * @param header The header to write.
118 virtual void WriteHeader(const LanguagePackHeader *header) = 0;
121 * Write a number of bytes.
122 * @param buffer The buffer to write.
123 * @param length The amount of byte to write.
125 virtual void Write(const byte *buffer, size_t length) = 0;
128 * Finalise writing the file.
130 virtual void Finalise() = 0;
132 /** Especially destroy the subclasses. */
133 virtual ~LanguageWriter() {}
135 virtual void WriteLength(uint length);
136 virtual void WriteLang(const StringData &data);
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);
144 extern const char *_file;
145 extern int _cur_line;
146 extern int _errors, _warnings, _show_todo;
147 extern LanguagePackHeader _lang;
149 #endif /* STRGEN_H */