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-2013 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 "OgrePrerequisites.h" |
---|
33 | #include "OgreStringVector.h" |
---|
34 | #include "OgreColourValue.h" |
---|
35 | #include "OgreMath.h" |
---|
36 | #include "OgreMatrix3.h" |
---|
37 | #include "OgreMatrix4.h" |
---|
38 | #include "OgreQuaternion.h" |
---|
39 | #include "OgreVector2.h" |
---|
40 | #include "OgreVector3.h" |
---|
41 | #include "OgreVector4.h" |
---|
42 | |
---|
43 | namespace Ogre { |
---|
44 | |
---|
45 | /** \addtogroup Core |
---|
46 | * @{ |
---|
47 | */ |
---|
48 | /** \addtogroup General |
---|
49 | * @{ |
---|
50 | */ |
---|
51 | /** Class for converting the core Ogre data types to/from Strings. |
---|
52 | @remarks |
---|
53 | The code for converting values to and from strings is here as a separate |
---|
54 | class to avoid coupling String to other datatypes (and vice-versa) which reduces |
---|
55 | compilation dependency: important given how often the core types are used. |
---|
56 | @par |
---|
57 | This class is mainly used for parsing settings in text files. External applications |
---|
58 | can also use it to interface with classes which use the StringInterface template |
---|
59 | class. |
---|
60 | @par |
---|
61 | The String formats of each of the major types is listed with the methods. The basic types |
---|
62 | like int and Real just use the underlying C runtime library atof and atoi family methods, |
---|
63 | however custom types like Vector3, ColourValue and Matrix4 are also supported by this class |
---|
64 | using custom formats. |
---|
65 | @author |
---|
66 | Steve Streeting |
---|
67 | */ |
---|
68 | class _OgreExport StringConverter |
---|
69 | { |
---|
70 | public: |
---|
71 | |
---|
72 | /** Converts a Real to a String. */ |
---|
73 | static String toString(Real val, unsigned short precision = 6, |
---|
74 | unsigned short width = 0, char fill = ' ', |
---|
75 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
76 | #if OGRE_DOUBLE_PRECISION == 1 |
---|
77 | /** Converts a float to a String. */ |
---|
78 | static String toString(float val, unsigned short precision = 6, |
---|
79 | unsigned short width = 0, char fill = ' ', |
---|
80 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
81 | #else |
---|
82 | /** Converts a double to a String. */ |
---|
83 | static String toString(double val, unsigned short precision = 6, |
---|
84 | unsigned short width = 0, char fill = ' ', |
---|
85 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
86 | #endif |
---|
87 | /** Converts a Radian to a String. */ |
---|
88 | static String toString(Radian val, unsigned short precision = 6, |
---|
89 | unsigned short width = 0, char fill = ' ', |
---|
90 | std::ios::fmtflags flags = std::ios::fmtflags(0)) |
---|
91 | { |
---|
92 | return toString(val.valueAngleUnits(), precision, width, fill, flags); |
---|
93 | } |
---|
94 | /** Converts a Degree to a String. */ |
---|
95 | static String toString(Degree val, unsigned short precision = 6, |
---|
96 | unsigned short width = 0, char fill = ' ', |
---|
97 | std::ios::fmtflags flags = std::ios::fmtflags(0)) |
---|
98 | { |
---|
99 | return toString(val.valueAngleUnits(), precision, width, fill, flags); |
---|
100 | } |
---|
101 | /** Converts an int to a String. */ |
---|
102 | static String toString(int val, unsigned short width = 0, |
---|
103 | char fill = ' ', |
---|
104 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
105 | #if OGRE_PLATFORM != OGRE_PLATFORM_NACL && ( OGRE_ARCH_TYPE == OGRE_ARCHITECTURE_64 || OGRE_PLATFORM == OGRE_PLATFORM_APPLE || OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS ) |
---|
106 | /** Converts an unsigned int to a String. */ |
---|
107 | static String toString(unsigned int val, |
---|
108 | unsigned short width = 0, char fill = ' ', |
---|
109 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
110 | /** Converts a size_t to a String. */ |
---|
111 | static String toString(size_t val, |
---|
112 | unsigned short width = 0, char fill = ' ', |
---|
113 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
114 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC |
---|
115 | /** Converts an unsigned long to a String. */ |
---|
116 | static String toString(unsigned long val, |
---|
117 | unsigned short width = 0, char fill = ' ', |
---|
118 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
119 | #endif |
---|
120 | #else |
---|
121 | /** Converts a size_t to a String. */ |
---|
122 | static String toString(size_t val, |
---|
123 | unsigned short width = 0, char fill = ' ', |
---|
124 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
125 | /** Converts an unsigned long to a String. */ |
---|
126 | static String toString(unsigned long val, |
---|
127 | unsigned short width = 0, char fill = ' ', |
---|
128 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
129 | #endif |
---|
130 | /** Converts a long to a String. */ |
---|
131 | static String toString(long val, |
---|
132 | unsigned short width = 0, char fill = ' ', |
---|
133 | std::ios::fmtflags flags = std::ios::fmtflags(0)); |
---|
134 | /** Converts a boolean to a String. |
---|
135 | @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false' |
---|
136 | */ |
---|
137 | static String toString(bool val, bool yesNo = false); |
---|
138 | /** Converts a Vector2 to a String. |
---|
139 | @remarks |
---|
140 | Format is "x y" (i.e. 2x Real values, space delimited) |
---|
141 | */ |
---|
142 | static String toString(const Vector2& val); |
---|
143 | /** Converts a Vector3 to a String. |
---|
144 | @remarks |
---|
145 | Format is "x y z" (i.e. 3x Real values, space delimited) |
---|
146 | */ |
---|
147 | static String toString(const Vector3& val); |
---|
148 | /** Converts a Vector4 to a String. |
---|
149 | @remarks |
---|
150 | Format is "x y z w" (i.e. 4x Real values, space delimited) |
---|
151 | */ |
---|
152 | static String toString(const Vector4& val); |
---|
153 | /** Converts a Matrix3 to a String. |
---|
154 | @remarks |
---|
155 | Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc. |
---|
156 | */ |
---|
157 | static String toString(const Matrix3& val); |
---|
158 | /** Converts a Matrix4 to a String. |
---|
159 | @remarks |
---|
160 | Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where |
---|
161 | '01' means row 0 column 1 etc. |
---|
162 | */ |
---|
163 | static String toString(const Matrix4& val); |
---|
164 | /** Converts a Quaternion to a String. |
---|
165 | @remarks |
---|
166 | Format is "w x y z" (i.e. 4x Real values, space delimited) |
---|
167 | */ |
---|
168 | static String toString(const Quaternion& val); |
---|
169 | /** Converts a ColourValue to a String. |
---|
170 | @remarks |
---|
171 | Format is "r g b a" (i.e. 4x Real values, space delimited). |
---|
172 | */ |
---|
173 | static String toString(const ColourValue& val); |
---|
174 | /** Converts a StringVector to a string. |
---|
175 | @remarks |
---|
176 | Strings must not contain spaces since space is used as a delimiter in |
---|
177 | the output. |
---|
178 | */ |
---|
179 | static String toString(const StringVector& val); |
---|
180 | |
---|
181 | /** Converts a String to a Real. |
---|
182 | @return |
---|
183 | 0.0 if the value could not be parsed, otherwise the Real version of the String. |
---|
184 | */ |
---|
185 | static Real parseReal(const String& val, Real defaultValue = 0); |
---|
186 | /** Converts a String to a Angle. |
---|
187 | @return |
---|
188 | 0.0 if the value could not be parsed, otherwise the Angle version of the String. |
---|
189 | */ |
---|
190 | static inline Radian parseAngle(const String& val, Radian defaultValue = Radian(0)) { |
---|
191 | return Angle(parseReal(val, defaultValue.valueRadians())); |
---|
192 | } |
---|
193 | /** Converts a String to a whole number. |
---|
194 | @return |
---|
195 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
---|
196 | */ |
---|
197 | static int parseInt(const String& val, int defaultValue = 0); |
---|
198 | /** Converts a String to a whole number. |
---|
199 | @return |
---|
200 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
---|
201 | */ |
---|
202 | static unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue = 0); |
---|
203 | /** Converts a String to a whole number. |
---|
204 | @return |
---|
205 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
---|
206 | */ |
---|
207 | static long parseLong(const String& val, long defaultValue = 0); |
---|
208 | /** Converts a String to a whole number. |
---|
209 | @return |
---|
210 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
---|
211 | */ |
---|
212 | static unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue = 0); |
---|
213 | /** Converts a String to size_t. |
---|
214 | @return |
---|
215 | defaultValue if the value could not be parsed, otherwise the numeric version of the String. |
---|
216 | */ |
---|
217 | static size_t parseSizeT(const String& val, size_t defaultValue = 0); |
---|
218 | /** Converts a String to a boolean. |
---|
219 | @remarks |
---|
220 | Returns true if case-insensitive match of the start of the string |
---|
221 | matches "true", "yes" or "1", false otherwise. |
---|
222 | */ |
---|
223 | static bool parseBool(const String& val, bool defaultValue = 0); |
---|
224 | /** Parses a Vector2 out of a String. |
---|
225 | @remarks |
---|
226 | Format is "x y" ie. 2 Real components, space delimited. Failure to parse returns |
---|
227 | Vector2::ZERO. |
---|
228 | */ |
---|
229 | static Vector2 parseVector2(const String& val, const Vector2& defaultValue = Vector2::ZERO); |
---|
230 | /** Parses a Vector3 out of a String. |
---|
231 | @remarks |
---|
232 | Format is "x y z" ie. 3 Real components, space delimited. Failure to parse returns |
---|
233 | Vector3::ZERO. |
---|
234 | */ |
---|
235 | static Vector3 parseVector3(const String& val, const Vector3& defaultValue = Vector3::ZERO); |
---|
236 | /** Parses a Vector4 out of a String. |
---|
237 | @remarks |
---|
238 | Format is "x y z w" ie. 4 Real components, space delimited. Failure to parse returns |
---|
239 | Vector4::ZERO. |
---|
240 | */ |
---|
241 | static Vector4 parseVector4(const String& val, const Vector4& defaultValue = Vector4::ZERO); |
---|
242 | /** Parses a Matrix3 out of a String. |
---|
243 | @remarks |
---|
244 | Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc. |
---|
245 | Failure to parse returns Matrix3::IDENTITY. |
---|
246 | */ |
---|
247 | static Matrix3 parseMatrix3(const String& val, const Matrix3& defaultValue = Matrix3::IDENTITY); |
---|
248 | /** Parses a Matrix4 out of a String. |
---|
249 | @remarks |
---|
250 | Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where |
---|
251 | '01' means row 0 column 1 etc. Failure to parse returns Matrix4::IDENTITY. |
---|
252 | */ |
---|
253 | static Matrix4 parseMatrix4(const String& val, const Matrix4& defaultValue = Matrix4::IDENTITY); |
---|
254 | /** Parses a Quaternion out of a String. |
---|
255 | @remarks |
---|
256 | Format is "w x y z" (i.e. 4x Real values, space delimited). |
---|
257 | Failure to parse returns Quaternion::IDENTITY. |
---|
258 | */ |
---|
259 | static Quaternion parseQuaternion(const String& val, const Quaternion& defaultValue = Quaternion::IDENTITY); |
---|
260 | /** Parses a ColourValue out of a String. |
---|
261 | @remarks |
---|
262 | Format is "r g b a" (i.e. 4x Real values, space delimited), or "r g b" which implies |
---|
263 | an alpha value of 1.0 (opaque). Failure to parse returns ColourValue::Black. |
---|
264 | */ |
---|
265 | static ColourValue parseColourValue(const String& val, const ColourValue& defaultValue = ColourValue::Black); |
---|
266 | |
---|
267 | /** Parses a StringVector from a string. |
---|
268 | @remarks |
---|
269 | Strings must not contain spaces since space is used as a delimiter in |
---|
270 | the output. |
---|
271 | */ |
---|
272 | static StringVector parseStringVector(const String& val); |
---|
273 | /** Checks the String is a valid number value. */ |
---|
274 | static bool isNumber(const String& val); |
---|
275 | |
---|
276 | //----------------------------------------------------------------------- |
---|
277 | static void setDefaultStringLocale(String loc) |
---|
278 | { |
---|
279 | msDefaultStringLocale = loc; |
---|
280 | msLocale = std::locale(msDefaultStringLocale.c_str()); |
---|
281 | } |
---|
282 | //----------------------------------------------------------------------- |
---|
283 | static String getDefaultStringLocale(void) { return msDefaultStringLocale; } |
---|
284 | //----------------------------------------------------------------------- |
---|
285 | static void setUseLocale(bool useLocale) { msUseLocale = useLocale; } |
---|
286 | //----------------------------------------------------------------------- |
---|
287 | static bool isUseLocale() { return msUseLocale; } |
---|
288 | //----------------------------------------------------------------------- |
---|
289 | |
---|
290 | protected: |
---|
291 | static String msDefaultStringLocale; |
---|
292 | static std::locale msLocale; |
---|
293 | static bool msUseLocale; |
---|
294 | }; |
---|
295 | |
---|
296 | /** @} */ |
---|
297 | /** @} */ |
---|
298 | |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | #endif |
---|
304 | |
---|