Translations update
[openttd/fttd.git] / src / strings_func.h
blobfd0a012f8e1fdb69661de7f0f306ebee62070dda
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file strings_func.h Functions related to OTTD's strings. */
12 #ifndef STRINGS_FUNC_H
13 #define STRINGS_FUNC_H
15 #include "strings_type.h"
16 #include "string.h"
17 #include "gfx_type.h"
18 #include "core/bitmath_func.hpp"
20 /**
21 * Extract the StringTab from a StringID.
22 * @param str String identifier
23 * @return StringTab from \a str
25 static inline StringTab GetStringTab(StringID str)
27 StringTab result = (StringTab)(str >> TAB_SIZE_BITS);
28 if (result >= TEXT_TAB_NEWGRF_START) return TEXT_TAB_NEWGRF_START;
29 if (result >= TEXT_TAB_GAMESCRIPT_START) return TEXT_TAB_GAMESCRIPT_START;
30 return result;
33 /**
34 * Extract the StringIndex from a StringID.
35 * @param str String identifier
36 * @return StringIndex from \a str
38 static inline uint GetStringIndex(StringID str)
40 return str - (GetStringTab(str) << TAB_SIZE_BITS);
43 /**
44 * Create a StringID
45 * @param tab StringTab
46 * @param index StringIndex
47 * @return StringID composed from \a tab and \a index
49 static inline StringID MakeStringID(StringTab tab, uint index)
51 if (tab == TEXT_TAB_NEWGRF_START) {
52 assert(index < TAB_SIZE_NEWGRF);
53 } else if (tab == TEXT_TAB_GAMESCRIPT_START) {
54 assert(index < TAB_SIZE_GAMESCRIPT);
55 } else {
56 assert(tab < TEXT_TAB_END);
57 assert(index < TAB_SIZE);
59 return (tab << TAB_SIZE_BITS) + index;
62 class StringParameters {
63 StringParameters *parent; ///< If not NULL, this instance references data from this parent instance.
64 uint64 *data; ///< Array with the actual data.
65 WChar *type; ///< Array with type information about the data. Can be NULL when no type information is needed. See #StringControlCode.
67 public:
68 uint offset; ///< Current offset in the data/type arrays.
69 uint num_param; ///< Length of the data array.
71 /** Create a new StringParameters instance. */
72 StringParameters(uint64 *data, uint num_param, WChar *type) :
73 parent(NULL),
74 data(data),
75 type(type),
76 offset(0),
77 num_param(num_param)
78 { }
80 /** Create a new StringParameters instance. */
81 template <size_t Tnum_param>
82 StringParameters(int64 (&data)[Tnum_param]) :
83 parent(NULL),
84 data((uint64 *)data),
85 type(NULL),
86 offset(0),
87 num_param(Tnum_param)
89 assert_compile(sizeof(data[0]) == sizeof(uint64));
92 /**
93 * Create a new StringParameters instance that can reference part of the data of
94 * the given partent instance.
96 StringParameters(StringParameters &parent, uint size) :
97 parent(&parent),
98 data(parent.data + parent.offset),
99 offset(0),
100 num_param(size)
102 assert(size <= parent.GetDataLeft());
103 if (parent.type == NULL) {
104 this->type = NULL;
105 } else {
106 this->type = parent.type + parent.offset;
110 ~StringParameters()
112 if (this->parent != NULL) {
113 this->parent->offset += this->num_param;
117 void ClearTypeInformation();
119 int64 GetInt64(WChar type = 0);
121 /** Read an int32 from the argument array. @see GetInt64. */
122 int32 GetInt32(WChar type = 0)
124 return (int32)this->GetInt64(type);
127 /** Get a pointer to the current element in the data array. */
128 uint64 *GetDataPointer() const
130 return &this->data[this->offset];
133 /** Return the amount of elements which can still be read. */
134 uint GetDataLeft() const
136 return this->num_param - this->offset;
139 /** Get a pointer to a specific element in the data array. */
140 uint64 *GetPointerToOffset(uint offset) const
142 assert(offset < this->num_param);
143 return &this->data[offset];
146 /** Does this instance store information about the type of the parameters. */
147 bool HasTypeInformation() const
149 return this->type != NULL;
152 /** Get the type of a specific element. */
153 WChar GetTypeAtOffset(uint offset) const
155 assert(offset < this->num_param);
156 assert(this->HasTypeInformation());
157 return this->type[offset];
160 void SetParam(uint n, uint64 v)
162 assert(n < this->num_param);
163 this->data[n] = v;
166 uint64 GetParam(uint n) const
168 assert(n < this->num_param);
169 return this->data[n];
172 extern StringParameters _global_string_params;
174 const char *GetStringPtr(StringID string);
176 void AppendString (stringb *buf, StringID string);
178 static inline void GetString (stringb *buf, StringID string)
180 buf->clear();
181 AppendString (buf, string);
184 template <uint N>
185 static inline void GetString (char (*buf) [N], StringID string)
187 stringb tmp (N, &(*buf)[0]);
188 GetString (&tmp, string);
191 template <uint N>
192 static inline void GetString (char (&buf) [N], StringID string)
194 GetString (&buf, string);
197 void AppendStringWithArgs (stringb *buf, StringID string, StringParameters *args, uint case_index = 0, bool game_script = false);
199 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
200 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
203 * Set a string parameter \a v at index \a n in the global string parameter array.
204 * @param n Index of the string parameter.
205 * @param v Value of the string parameter.
207 static inline void SetDParam(uint n, uint64 v)
209 _global_string_params.SetParam(n, v);
212 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
213 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
215 void SetDParamStr(uint n, const char *str);
217 void CopyInDParam(int offs, const uint64 *src, int num);
218 void CopyOutDParam(uint64 *dst, int offs, int num);
219 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
222 * Get the current string parameter at index \a n from the global string parameter array.
223 * @param n Index of the string parameter.
224 * @return Value of the requested string parameter.
226 static inline uint64 GetDParam(uint n)
228 return _global_string_params.GetParam(n);
231 extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
233 void InitializeLanguagePacks();
234 const char *GetCurrentLanguageIsoCode();
236 int CDECL StringIDSorter(const StringID *a, const StringID *b);
239 * A searcher for missing glyphs.
241 class MissingGlyphSearcher {
242 public:
243 const FontSize default_size; ///< default font size of the string
244 const bool monospace; ///< whether to search for a monospace font
246 /** Construct an instance. */
247 CONSTEXPR MissingGlyphSearcher (FontSize size, bool mono)
248 : default_size(size), monospace(mono)
252 /** Make sure everything gets destructed right. */
253 virtual ~MissingGlyphSearcher() {}
256 * Get the next string to search through.
257 * @return The next string or NULL if there is none.
259 virtual const char *NextString() = 0;
262 * Get the default (font) size of the string.
263 * @return The font size.
265 FontSize DefaultSize() const
267 return this->default_size;
271 * Reset the search, i.e. begin from the beginning again.
273 virtual void Reset() = 0;
276 * Whether to search for a monospace font or not.
277 * @return True if searching for monospace.
279 bool Monospace() const
281 return this->monospace;
284 bool FindMissingGlyphs (void);
287 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = NULL);
289 #endif /* STRINGS_FUNC_H */