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 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"
18 class StringParameters
{
19 StringParameters
*parent
; ///< If not NULL, this instance references data from this parent instance.
20 uint64
*data
; ///< Array with the actual data.
21 WChar
*type
; ///< Array with type information about the data. Can be NULL when no type information is needed. See #StringControlCode.
24 uint offset
; ///< Current offset in the data/type arrays.
25 uint num_param
; ///< Length of the data array.
27 /** Create a new StringParameters instance. */
28 StringParameters(uint64
*data
, uint num_param
, WChar
*type
) :
36 /** Create a new StringParameters instance. */
37 template <size_t Tnum_param
>
38 StringParameters(int64 (&data
)[Tnum_param
]) :
45 assert_compile(sizeof(data
[0]) == sizeof(uint64
));
49 * Create a new StringParameters instance that can reference part of the data of
50 * the given partent instance.
52 StringParameters(StringParameters
&parent
, uint size
) :
54 data(parent
.data
+ parent
.offset
),
58 assert(size
<= parent
.num_param
- parent
.offset
);
59 if (parent
.type
== NULL
) {
62 this->type
= parent
.type
+ parent
.offset
;
68 if (this->parent
!= NULL
) {
69 this->parent
->offset
+= this->num_param
;
73 void ClearTypeInformation();
76 * Read an int64 from the argument array. The offset is increased
77 * so the next time GetInt64 is called the next value is read.
79 int64
GetInt64(WChar type
= 0)
81 assert(this->offset
< this->num_param
);
82 if (this->type
!= NULL
) {
83 assert(this->type
[this->offset
] == 0 || this->type
[this->offset
] == type
);
84 this->type
[this->offset
] = type
;
86 return this->data
[this->offset
++];
89 /** Read an int32 from the argument array. @see GetInt64. */
90 int32
GetInt32(WChar type
= 0)
92 return (int32
)this->GetInt64(type
);
95 void ShiftParameters(uint amount
);
97 /** Get a pointer to the current element in the data array. */
98 uint64
*GetDataPointer() const
100 return &this->data
[this->offset
];
103 /** Get a pointer to a specific element in the data array. */
104 uint64
*GetPointerToOffset(uint offset
) const
106 assert(offset
< this->num_param
);
107 return &this->data
[offset
];
110 /** Does this instance store information about the type of the parameters. */
111 bool HasTypeInformation() const
113 return this->type
!= NULL
;
116 /** Get the type of a specific element. */
117 WChar
GetTypeAtOffset(uint offset
) const
119 assert(offset
< this->num_param
);
120 assert(this->HasTypeInformation());
121 return this->type
[offset
];
124 void SetParam(uint n
, uint64 v
)
126 assert(n
< this->num_param
);
130 uint64
GetParam(uint n
) const
132 assert(n
< this->num_param
);
133 return this->data
[n
];
136 extern StringParameters _global_string_params
;
138 char *InlineString(char *buf
, StringID string
);
139 char *GetString(char *buffr
, StringID string
, const char *last
);
140 char *GetStringWithArgs(char *buffr
, uint string
, StringParameters
*args
, const char *last
);
141 const char *GetStringPtr(StringID string
);
143 void InjectDParam(uint amount
);
146 * Set a string parameter \a v at index \a n in a given array \a s.
147 * @param s Array of string parameters.
148 * @param n Index of the string parameter.
149 * @param v Value of the string parameter.
151 static inline void SetDParamX(uint64
*s
, uint n
, uint64 v
)
157 * Set a string parameter \a v at index \a n in the global string parameter array.
158 * @param n Index of the string parameter.
159 * @param v Value of the string parameter.
161 static inline void SetDParam(uint n
, uint64 v
)
163 _global_string_params
.SetParam(n
, v
);
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
);
172 * Get the current string parameter at index \a n from parameter array \a s.
173 * @param s Array of string parameters.
174 * @param n Index of the string parameter.
175 * @return Value of the requested string parameter.
177 static inline uint64
GetDParamX(const uint64
*s
, uint n
)
183 * Get the current string parameter at index \a n from the global string parameter array.
184 * @param n Index of the string parameter.
185 * @return Value of the requested string parameter.
187 static inline uint64
GetDParam(uint n
)
189 return _global_string_params
.GetParam(n
);
192 extern TextDirection _current_text_dir
; ///< Text direction of the currently selected language
194 void InitializeLanguagePacks();
195 const char *GetCurrentLanguageIsoCode();
197 int CDECL
StringIDSorter(const StringID
*a
, const StringID
*b
);
199 void CheckForMissingGlyphsInLoadedLanguagePack();
201 #endif /* STRINGS_FUNC_H */