Coverage Report

Created: 2026-08-29 06:21

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