1 /* $Id: strgen.h 24900 2013-01-08 22:46:42Z planetmaker $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file strgen.h Structures related to strgen. */
15 #include "../language.h"
17 /** Container for the different cases of a string. */
19 int caseidx
; ///< The index of the case.
20 char *string
; ///< The translation of the case.
21 Case
*next
; ///< The next, chained, case.
23 Case(int caseidx
, const char *string
, Case
*next
);
27 /** Information about a single string. */
29 char *name
; ///< Name of the string.
30 char *english
; ///< English text.
31 char *translated
; ///< Translated text.
32 uint16 hash_next
; ///< Next hash entry.
33 uint16 index
; ///< The index in the language file.
34 int line
; ///< Line of string in source-file.
35 Case
*translated_case
; ///< Cases of the translation.
37 LangString(const char *name
, const char *english
, int index
, int line
);
39 void FreeTranslation();
42 /** Information about the currently known strings. */
44 LangString
**strings
; ///< Array of all known strings.
45 uint16
*hash_heads
; ///< Hash table for the strings.
46 size_t tabs
; ///< The number of 'tabs' of strings.
47 size_t max_strings
; ///< The maximum number of strings.
48 int next_string_id
; ///< The next string ID to allocate.
50 StringData(size_t tabs
);
52 void FreeTranslation();
53 uint
HashStr(const char *s
) const;
54 void Add(const char *s
, LangString
*ls
);
55 LangString
*Find(const char *s
);
56 uint
VersionHashStr(uint hash
, const char *s
) const;
58 uint
CountInUse(uint tab
) const;
61 /** Helper for reading strings. */
63 StringData
&data
; ///< The data to fill during reading.
64 const char *file
; ///< The file we are reading.
65 bool master
; ///< Are we reading the master file?
66 bool translation
; ///< Are we reading a translation, implies !master. However, the base translation will have this false.
68 StringReader(StringData
&data
, const char *file
, bool master
, bool translation
);
69 virtual ~StringReader();
70 void HandleString(char *str
);
73 * Read a single line from the source of strings.
74 * @param buffer The buffer to read the data in to.
75 * @param last The last element in the buffer.
76 * @return The buffer, or NULL if at the end of the file.
78 virtual char *ReadLine(char *buffer
, const char *last
) = 0;
81 * Handle the pragma of the file.
82 * @param str The pragma string to parse.
84 virtual void HandlePragma(char *str
);
87 * Start parsing the file.
89 virtual void ParseFile();
92 /** Base class for writing the header, i.e. the STR_XXX to numeric value. */
95 * Write the string ID.
96 * @param name The name of the string.
97 * @param stringid The ID of the string.
99 virtual void WriteStringID(const char *name
, int stringid
) = 0;
102 * Finalise writing the file.
103 * @param data The data about the string.
105 virtual void Finalise(const StringData
&data
) = 0;
107 /** Especially destroy the subclasses. */
108 virtual ~HeaderWriter() {};
110 void WriteHeader(const StringData
&data
);
113 /** Base class for all language writers. */
114 struct LanguageWriter
{
116 * Write the header metadata. The multi-byte integers are already converted to
117 * the little endian format.
118 * @param header The header to write.
120 virtual void WriteHeader(const LanguagePackHeader
*header
) = 0;
123 * Write a number of bytes.
124 * @param buffer The buffer to write.
125 * @param length The amount of byte to write.
127 virtual void Write(const byte
*buffer
, size_t length
) = 0;
130 * Finalise writing the file.
132 virtual void Finalise() = 0;
134 /** Especially destroy the subclasses. */
135 virtual ~LanguageWriter() {}
137 virtual void WriteLength(uint length
);
138 virtual void WriteLang(const StringData
&data
);
141 void CDECL
strgen_warning(const char *s
, ...) WARN_FORMAT(1, 2);
142 void CDECL
strgen_error(const char *s
, ...) WARN_FORMAT(1, 2);
143 void NORETURN CDECL
strgen_fatal(const char *s
, ...) WARN_FORMAT(1, 2);
144 char *ParseWord(char **buf
);
146 extern const char *_file
;
147 extern int _cur_line
;
148 extern int _errors
, _warnings
, _show_todo
;
149 extern LanguagePackHeader _lang
;
151 #endif /* STRGEN_H */