/src/ogre/OgreMain/include/OgreStringConverter.h
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | |
29 | | #ifndef __StringConverter_H__ |
30 | | #define __StringConverter_H__ |
31 | | |
32 | | #include "OgreCommon.h" |
33 | | #include "OgrePrerequisites.h" |
34 | | #include "OgreStringVector.h" |
35 | | #include "OgreColourValue.h" |
36 | | #include "OgreMatrix4.h" |
37 | | #include "OgreVector.h" |
38 | | |
39 | | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT |
40 | | # define locale_t _locale_t |
41 | | #elif OGRE_PLATFORM == OGRE_PLATFORM_ANDROID || OGRE_PLATFORM == OGRE_PLATFORM_EMSCRIPTEN |
42 | | # define locale_t int |
43 | | #endif |
44 | | |
45 | | // If compiling with make on macOS, these headers need to be included to get |
46 | | // definitions of locale_t, strtod_l, etc... |
47 | | // See: http://www.unix.com/man-page/osx/3/strtod_l/ |
48 | | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
49 | | # include <stdlib.h> |
50 | | # include <xlocale.h> |
51 | | #endif |
52 | | |
53 | | namespace Ogre { |
54 | | |
55 | | /** \addtogroup Core |
56 | | * @{ |
57 | | */ |
58 | | /** \addtogroup General |
59 | | * @{ |
60 | | */ |
61 | | /** Class for converting the core Ogre data types to/from Strings. |
62 | | |
63 | | The code for converting values to and from strings is here as a separate |
64 | | class to avoid coupling String to other datatypes (and vice-versa) which reduces |
65 | | compilation dependency: important given how often the core types are used. |
66 | | @par |
67 | | This class is mainly used for parsing settings in text files. External applications |
68 | | can also use it to interface with classes which use the StringInterface template |
69 | | class. |
70 | | @par |
71 | | The String formats of each of the major types is listed with the methods. The basic types |
72 | | like int and Real just use the underlying C runtime library atof and atoi family methods, |
73 | | however custom types like Vector3, ColourValue and Matrix4 are also supported by this class |
74 | | using custom formats. |
75 | | */ |
76 | | class _OgreExport StringConverter |
77 | | { |
78 | | public: |
79 | 0 | static String toString(int32 val) { return std::to_string(val); }; |
80 | 0 | static String toString(uint32 val) { return std::to_string(val); }; |
81 | 0 | static String toString(unsigned long val) { return std::to_string(val); }; |
82 | 0 | static String toString(unsigned long long val) { return std::to_string(val); }; |
83 | 0 | static String toString(long val) { return std::to_string(val); }; |
84 | | |
85 | | /** Converts a float to a String. */ |
86 | | static String toString(float val, unsigned short precision = 6, |
87 | | unsigned short width = 0, char fill = ' ', |
88 | | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
89 | | |
90 | | /** Converts a double to a String. */ |
91 | | static String toString(double val, unsigned short precision = 6, |
92 | | unsigned short width = 0, char fill = ' ', |
93 | | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
94 | | |
95 | | /** Converts a Radian to a String. */ |
96 | | static String toString(Radian val, unsigned short precision = 6, |
97 | | unsigned short width = 0, char fill = ' ', |
98 | | std::ios::fmtflags flags = std::ios::fmtflags(0)) |
99 | 0 | { |
100 | 0 | return toString(val.valueAngleUnits(), precision, width, fill, flags); |
101 | 0 | } |
102 | | /** Converts a Degree to a String. */ |
103 | | static String toString(Degree val, unsigned short precision = 6, |
104 | | unsigned short width = 0, char fill = ' ', |
105 | | std::ios::fmtflags flags = std::ios::fmtflags(0)) |
106 | 0 | { |
107 | 0 | return toString(val.valueAngleUnits(), precision, width, fill, flags); |
108 | 0 | } |
109 | | /// @deprecated use StringUtil::format |
110 | | OGRE_DEPRECATED static String toString(int32 val, unsigned short width, |
111 | | char fill = ' ', |
112 | | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
113 | | /// @deprecated use StringUtil::format |
114 | | OGRE_DEPRECATED static String toString(uint32 val, unsigned short width, |
115 | | char fill = ' ', |
116 | | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
117 | | // provide both long long and long to catch size_t on all platforms |
118 | | /// @deprecated use StringUtil::format |
119 | | OGRE_DEPRECATED static String toString(unsigned long val, |
120 | | unsigned short width, char fill = ' ', |
121 | | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
122 | | /// @deprecated use StringUtil::format |
123 | | OGRE_DEPRECATED static String toString(unsigned long long val, |
124 | | unsigned short width, char fill = ' ', |
125 | | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
126 | | |
127 | | /// @deprecated use StringUtil::format |
128 | | OGRE_DEPRECATED static String toString(long val, |
129 | | unsigned short width, char fill = ' ', |
130 | | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
131 | | |
132 | | /** Converts a boolean to a String. |
133 | | @param val |
134 | | @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false' |
135 | | */ |
136 | | static String toString(bool val, bool yesNo = false); |
137 | | /** Converts a Vector2 to a String. |
138 | | |
139 | | Format is "x y" (i.e. 2x Real values, space delimited) |
140 | | */ |
141 | | static String toString(const Vector2& val); |
142 | | /** Converts a Vector3 to a String. |
143 | | |
144 | | Format is "x y z" (i.e. 3x Real values, space delimited) |
145 | | */ |
146 | | static String toString(const Vector3& val); |
147 | | /** Converts a Vector4 to a String. |
148 | | |
149 | | Format is "x y z w" (i.e. 4x Real values, space delimited) |
150 | | */ |
151 | | static String toString(const Vector4& val); |
152 | | /** Converts a Matrix3 to a String. |
153 | | |
154 | | Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc. |
155 | | */ |
156 | | static String toString(const Matrix3& val); |
157 | | /** Converts a Matrix4 to a String. |
158 | | |
159 | | Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where |
160 | | '01' means row 0 column 1 etc. |
161 | | */ |
162 | | static String toString(const Matrix4& val); |
163 | | /** Converts a Quaternion to a String. |
164 | | |
165 | | Format is "w x y z" (i.e. 4x Real values, space delimited) |
166 | | */ |
167 | | static String toString(const Quaternion& val); |
168 | | /** Converts a ColourValue to a String. |
169 | | |
170 | | Format is "r g b a" (i.e. 4x Real values, space delimited). |
171 | | */ |
172 | | static String toString(const ColourValue& val); |
173 | | /** Converts a StringVector to a string. |
174 | | |
175 | | Strings must not contain spaces since space is used as a delimiter in |
176 | | the output. |
177 | | */ |
178 | | static String toString(const StringVector& val); |
179 | | |
180 | | /** Converts a String to a basic value type |
181 | | @return whether the conversion was successful |
182 | | */ |
183 | | static bool parse(const String& str, ColourValue& v); |
184 | | static bool parse(const String& str, Quaternion& v); |
185 | | static bool parse(const String& str, Matrix4& v); |
186 | | static bool parse(const String& str, Matrix3& v); |
187 | | static bool parse(const String& str, Vector4& v); |
188 | | static bool parse(const String& str, Vector3& v); |
189 | | static bool parse(const String& str, Vector2& v); |
190 | | static bool parse(const String& str, int32& v); |
191 | | static bool parse(const String& str, uint32& v); |
192 | | static bool parse(const String& str, int64& v); |
193 | | // provide both long long and long to catch size_t on all platforms |
194 | | static bool parse(const String& str, unsigned long& v); |
195 | | static bool parse(const String& str, unsigned long long& v); |
196 | | static bool parse(const String& str, bool& v); |
197 | | static bool parse(const String& str, double& v); |
198 | | static bool parse(const String& str, float& v); |
199 | | |
200 | | /** Converts a String to a Real. |
201 | | @return |
202 | | 0.0 if the value could not be parsed, otherwise the Real version of the String. |
203 | | */ |
204 | | static Real parseReal(const String& val, Real defaultValue = 0) |
205 | 0 | { |
206 | 0 | Real ret; |
207 | 0 | return parse(val, ret) ? ret : defaultValue; |
208 | 0 | } |
209 | | /** Converts a String to a Angle. |
210 | | @return |
211 | | 0.0 if the value could not be parsed, otherwise the Angle version of the String. |
212 | | */ |
213 | 0 | static Radian parseAngle(const String& val, Radian defaultValue = Radian(0)) { |
214 | 0 | return Angle(parseReal(val, defaultValue.valueRadians())); |
215 | 0 | } |
216 | | /** Converts a String to a whole number. |
217 | | @return |
218 | | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
219 | | */ |
220 | | static int32 parseInt(const String& val, int32 defaultValue = 0) |
221 | 0 | { |
222 | 0 | int32 ret; |
223 | 0 | return parse(val, ret) ? ret : defaultValue; |
224 | 0 | } |
225 | | /** Converts a String to a whole number. |
226 | | @return |
227 | | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
228 | | */ |
229 | | static uint32 parseUnsignedInt(const String& val, uint32 defaultValue = 0) |
230 | 0 | { |
231 | 0 | uint32 ret; |
232 | 0 | return parse(val, ret) ? ret : defaultValue; |
233 | 0 | } |
234 | | /// @deprecated |
235 | | OGRE_DEPRECATED static int64 parseLong(const String& val, int64 defaultValue = 0) |
236 | 0 | { |
237 | 0 | int64 ret; |
238 | 0 | return parse(val, ret) ? ret : defaultValue; |
239 | 0 | } |
240 | | /// @deprecated |
241 | | OGRE_DEPRECATED static uint64 parseUnsignedLong(const String& val, uint64 defaultValue = 0) |
242 | 0 | { |
243 | 0 | uint64 ret; |
244 | 0 | return parse(val, ret) ? ret : defaultValue; |
245 | 0 | } |
246 | | /** Converts a String to size_t. |
247 | | @return |
248 | | defaultValue if the value could not be parsed, otherwise the numeric version of the String. |
249 | | */ |
250 | | static size_t parseSizeT(const String& val, size_t defaultValue = 0) |
251 | 0 | { |
252 | 0 | size_t ret; |
253 | 0 | return parse(val, ret) ? ret : defaultValue; |
254 | 0 | } |
255 | | /** Converts a String to a boolean. |
256 | | |
257 | | Returns true if case-insensitive match of the start of the string |
258 | | matches "true", "yes", "1", or "on", false if "false", "no", "0" |
259 | | or "off". |
260 | | */ |
261 | | static bool parseBool(const String& val, bool defaultValue = 0) |
262 | 0 | { |
263 | 0 | bool ret; |
264 | 0 | return parse(val, ret) ? ret : defaultValue; |
265 | 0 | } |
266 | | /** Parses a Vector2 out of a String. |
267 | | |
268 | | Format is "x y" ie. 2 Real components, space delimited. Failure to parse returns |
269 | | Vector2::ZERO. |
270 | | */ |
271 | | static Vector2 parseVector2(const String& val, const Vector2& defaultValue = Vector2::ZERO) |
272 | 0 | { |
273 | 0 | Vector2 ret; |
274 | 0 | return parse(val, ret) ? ret : defaultValue; |
275 | 0 | } |
276 | | /** Parses a Vector3 out of a String. |
277 | | |
278 | | Format is "x y z" ie. 3 Real components, space delimited. Failure to parse returns |
279 | | Vector3::ZERO. |
280 | | */ |
281 | | static Vector3 parseVector3(const String& val, const Vector3& defaultValue = Vector3::ZERO) |
282 | 0 | { |
283 | 0 | Vector3 ret; |
284 | 0 | return parse(val, ret) ? ret : defaultValue; |
285 | 0 | } |
286 | | /** Parses a Vector4 out of a String. |
287 | | |
288 | | Format is "x y z w" ie. 4 Real components, space delimited. Failure to parse returns |
289 | | Vector4::ZERO. |
290 | | */ |
291 | | static Vector4 parseVector4(const String& val, const Vector4& defaultValue = Vector4::ZERO) |
292 | 0 | { |
293 | 0 | Vector4 ret; |
294 | 0 | return parse(val, ret) ? ret : defaultValue; |
295 | 0 | } |
296 | | /** Parses a Matrix3 out of a String. |
297 | | |
298 | | Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc. |
299 | | Failure to parse returns Matrix3::IDENTITY. |
300 | | */ |
301 | | static Matrix3 parseMatrix3(const String& val, const Matrix3& defaultValue = Matrix3::IDENTITY) |
302 | 0 | { |
303 | 0 | Matrix3 ret; |
304 | 0 | return parse(val, ret) ? ret : defaultValue; |
305 | 0 | } |
306 | | /** Parses a Matrix4 out of a String. |
307 | | |
308 | | Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where |
309 | | '01' means row 0 column 1 etc. Failure to parse returns Matrix4::IDENTITY. |
310 | | */ |
311 | | static Matrix4 parseMatrix4(const String& val, const Matrix4& defaultValue = Matrix4::IDENTITY) |
312 | 0 | { |
313 | 0 | Matrix4 ret; |
314 | 0 | return parse(val, ret) ? ret : defaultValue; |
315 | 0 | } |
316 | | /** Parses a Quaternion out of a String. |
317 | | |
318 | | Format is "w x y z" (i.e. 4x Real values, space delimited). |
319 | | Failure to parse returns Quaternion::IDENTITY. |
320 | | */ |
321 | | static Quaternion parseQuaternion(const String& val, const Quaternion& defaultValue = Quaternion::IDENTITY) |
322 | 0 | { |
323 | 0 | Quaternion ret; |
324 | 0 | return parse(val, ret) ? ret : defaultValue; |
325 | 0 | } |
326 | | /** Parses a ColourValue out of a String. |
327 | | |
328 | | Format is "r g b a" (i.e. 4x Real values, space delimited), or "r g b" which implies |
329 | | an alpha value of 1.0 (opaque). Failure to parse returns ColourValue::Black. |
330 | | */ |
331 | | static ColourValue parseColourValue(const String& val, const ColourValue& defaultValue = ColourValue::Black) |
332 | 0 | { |
333 | 0 | ColourValue ret; |
334 | 0 | return parse(val, ret) ? ret : defaultValue; |
335 | 0 | } |
336 | | |
337 | | /// @deprecated use StringUtil::split |
338 | 0 | OGRE_DEPRECATED static StringVector parseStringVector(const String& val) { return StringUtil::split(val); } |
339 | | /// @deprecated use @ref parse() |
340 | | OGRE_DEPRECATED static bool isNumber(const String& val); |
341 | | |
342 | | static locale_t _numLocale; |
343 | | private: |
344 | | template<typename T> |
345 | | static String _toString(T val, uint16 width, char fill, std::ios::fmtflags flags); |
346 | | }; |
347 | | |
348 | 0 | inline String to_string(const Quaternion& v) { return StringConverter::toString(v); } |
349 | 0 | inline String to_string(const ColourValue& v) { return StringConverter::toString(v); } |
350 | 0 | inline String to_string(const Vector2& v) { return StringConverter::toString(v); } |
351 | 0 | inline String to_string(const Vector3& v) { return StringConverter::toString(v); } |
352 | 0 | inline String to_string(const Vector4& v) { return StringConverter::toString(v); } |
353 | 0 | inline String to_string(const Matrix3& v) { return StringConverter::toString(v); } |
354 | 0 | inline String to_string(const Matrix4& v) { return StringConverter::toString(v); } |
355 | | /** @} */ |
356 | | /** @} */ |
357 | | } |
358 | | |
359 | | |
360 | | |
361 | | #endif |
362 | | |