(svn r27707) -Fix: Make the result of NewGRF's CARGO_NAME string code reliably print...
[openttd.git] / src / strings_func.h
blob2c7809d020e740c1435801e7d9ec7f631b82f236
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_type.h"
17 #include "gfx_type.h"
19 class StringParameters {
20 StringParameters *parent; ///< If not NULL, this instance references data from this parent instance.
21 uint64 *data; ///< Array with the actual data.
22 WChar *type; ///< Array with type information about the data. Can be NULL when no type information is needed. See #StringControlCode.
24 public:
25 uint offset; ///< Current offset in the data/type arrays.
26 uint num_param; ///< Length of the data array.
28 /** Create a new StringParameters instance. */
29 StringParameters(uint64 *data, uint num_param, WChar *type) :
30 parent(NULL),
31 data(data),
32 type(type),
33 offset(0),
34 num_param(num_param)
35 { }
37 /** Create a new StringParameters instance. */
38 template <size_t Tnum_param>
39 StringParameters(int64 (&data)[Tnum_param]) :
40 parent(NULL),
41 data((uint64 *)data),
42 type(NULL),
43 offset(0),
44 num_param(Tnum_param)
46 assert_compile(sizeof(data[0]) == sizeof(uint64));
49 /**
50 * Create a new StringParameters instance that can reference part of the data of
51 * the given partent instance.
53 StringParameters(StringParameters &parent, uint size) :
54 parent(&parent),
55 data(parent.data + parent.offset),
56 offset(0),
57 num_param(size)
59 assert(size <= parent.GetDataLeft());
60 if (parent.type == NULL) {
61 this->type = NULL;
62 } else {
63 this->type = parent.type + parent.offset;
67 ~StringParameters()
69 if (this->parent != NULL) {
70 this->parent->offset += this->num_param;
74 void ClearTypeInformation();
76 int64 GetInt64(WChar type = 0);
78 /** Read an int32 from the argument array. @see GetInt64. */
79 int32 GetInt32(WChar type = 0)
81 return (int32)this->GetInt64(type);
84 void ShiftParameters(uint amount);
86 /** Get a pointer to the current element in the data array. */
87 uint64 *GetDataPointer() const
89 return &this->data[this->offset];
92 /** Return the amount of elements which can still be read. */
93 uint GetDataLeft() const
95 return this->num_param - this->offset;
98 /** Get a pointer to a specific element in the data array. */
99 uint64 *GetPointerToOffset(uint offset) const
101 assert(offset < this->num_param);
102 return &this->data[offset];
105 /** Does this instance store information about the type of the parameters. */
106 bool HasTypeInformation() const
108 return this->type != NULL;
111 /** Get the type of a specific element. */
112 WChar GetTypeAtOffset(uint offset) const
114 assert(offset < this->num_param);
115 assert(this->HasTypeInformation());
116 return this->type[offset];
119 void SetParam(uint n, uint64 v)
121 assert(n < this->num_param);
122 this->data[n] = v;
125 uint64 GetParam(uint n) const
127 assert(n < this->num_param);
128 return this->data[n];
131 extern StringParameters _global_string_params;
133 char *GetString(char *buffr, StringID string, const char *last);
134 char *GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index = 0, bool game_script = false);
135 const char *GetStringPtr(StringID string);
137 uint ConvertKmhishSpeedToDisplaySpeed(uint speed);
138 uint ConvertDisplaySpeedToKmhishSpeed(uint speed);
140 void InjectDParam(uint amount);
143 * Set a string parameter \a v at index \a n in a given array \a s.
144 * @param s Array of string parameters.
145 * @param n Index of the string parameter.
146 * @param v Value of the string parameter.
148 static inline void SetDParamX(uint64 *s, uint n, uint64 v)
150 s[n] = v;
154 * Set a string parameter \a v at index \a n in the global string parameter array.
155 * @param n Index of the string parameter.
156 * @param v Value of the string parameter.
158 static inline void SetDParam(uint n, uint64 v)
160 _global_string_params.SetParam(n, v);
163 void SetDParamMaxValue(uint n, uint64 max_value, uint min_count = 0, FontSize size = FS_NORMAL);
164 void SetDParamMaxDigits(uint n, uint count, FontSize size = FS_NORMAL);
166 void SetDParamStr(uint n, const char *str);
168 void CopyInDParam(int offs, const uint64 *src, int num);
169 void CopyOutDParam(uint64 *dst, int offs, int num);
170 void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num);
173 * Get the current string parameter at index \a n from parameter array \a s.
174 * @param s Array of string parameters.
175 * @param n Index of the string parameter.
176 * @return Value of the requested string parameter.
178 static inline uint64 GetDParamX(const uint64 *s, uint n)
180 return s[n];
184 * Get the current string parameter at index \a n from the global string parameter array.
185 * @param n Index of the string parameter.
186 * @return Value of the requested string parameter.
188 static inline uint64 GetDParam(uint n)
190 return _global_string_params.GetParam(n);
193 extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
195 void InitializeLanguagePacks();
196 const char *GetCurrentLanguageIsoCode();
198 int CDECL StringIDSorter(const StringID *a, const StringID *b);
201 * A searcher for missing glyphs.
203 class MissingGlyphSearcher {
204 public:
205 /** Make sure everything gets destructed right. */
206 virtual ~MissingGlyphSearcher() {}
209 * Get the next string to search through.
210 * @return The next string or NULL if there is none.
212 virtual const char *NextString() = 0;
215 * Get the default (font) size of the string.
216 * @return The font size.
218 virtual FontSize DefaultSize() = 0;
221 * Reset the search, i.e. begin from the beginning again.
223 virtual void Reset() = 0;
226 * Whether to search for a monospace font or not.
227 * @return True if searching for monospace.
229 virtual bool Monospace() = 0;
232 * Set the right font names.
233 * @param settings The settings to modify.
234 * @param font_name The new font name.
236 virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name) = 0;
238 bool FindMissingGlyphs(const char **str);
241 void CheckForMissingGlyphs(bool base_font = true, MissingGlyphSearcher *search = NULL);
243 #endif /* STRINGS_FUNC_H */