| 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-2006 Torus Knot Software Ltd |
|---|
| 8 | Also see acknowledgements in Readme.html |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | |
|---|
| 24 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #ifndef __StringConverter_H__ |
|---|
| 31 | #define __StringConverter_H__ |
|---|
| 32 | |
|---|
| 33 | #include "OgrePrerequisites.h" |
|---|
| 34 | #include "OgreMath.h" |
|---|
| 35 | #include "OgreString.h" |
|---|
| 36 | #include "OgreStringVector.h" |
|---|
| 37 | |
|---|
| 38 | namespace Ogre { |
|---|
| 39 | |
|---|
| 40 | /** Class for converting the core Ogre data types to/from Strings. |
|---|
| 41 | @remarks |
|---|
| 42 | The code for converting values to and from strings is here as a separate |
|---|
| 43 | class to avoid coupling String to other datatypes (and vice-versa) which reduces |
|---|
| 44 | compilation dependency: important given how often the core types are used. |
|---|
| 45 | @par |
|---|
| 46 | This class is mainly used for parsing settings in text files. External applications |
|---|
| 47 | can also use it to interface with classes which use the StringInterface template |
|---|
| 48 | class. |
|---|
| 49 | @par |
|---|
| 50 | The String formats of each of the major types is listed with the methods. The basic types |
|---|
| 51 | like int and Real just use the underlying C runtime library atof and atoi family methods, |
|---|
| 52 | however custom types like Vector3, ColourValue and Matrix4 are also supported by this class |
|---|
| 53 | using custom formats. |
|---|
| 54 | @author |
|---|
| 55 | Steve Streeting |
|---|
| 56 | */ |
|---|
| 57 | class _OgreExport StringConverter |
|---|
| 58 | { |
|---|
| 59 | public: |
|---|
| 60 | |
|---|
| 61 | /** Converts a Real to a String. */ |
|---|
| 62 | static String toString(Real val, unsigned short precision = 6, |
|---|
| 63 | unsigned short width = 0, char fill = ' ', |
|---|
| 64 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 65 | /** Converts a Radian to a String. */ |
|---|
| 66 | static String toString(Radian val, unsigned short precision = 6, |
|---|
| 67 | unsigned short width = 0, char fill = ' ', |
|---|
| 68 | std::ios::fmtflags flags = std::ios::fmtflags(0) ) |
|---|
| 69 | { |
|---|
| 70 | return toString(val.valueAngleUnits(), precision, width, fill, flags); |
|---|
| 71 | } |
|---|
| 72 | /** Converts a Degree to a String. */ |
|---|
| 73 | static String toString(Degree val, unsigned short precision = 6, |
|---|
| 74 | unsigned short width = 0, char fill = ' ', |
|---|
| 75 | std::ios::fmtflags flags = std::ios::fmtflags(0) ) |
|---|
| 76 | { |
|---|
| 77 | return toString(val.valueAngleUnits(), precision, width, fill, flags); |
|---|
| 78 | } |
|---|
| 79 | /** Converts an int to a String. */ |
|---|
| 80 | static String toString(int val, unsigned short width = 0, |
|---|
| 81 | char fill = ' ', |
|---|
| 82 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 83 | #if OGRE_ARCH_TYPE == OGRE_ARCHITECTURE_64 || OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
|---|
| 84 | /** Converts an unsigned int to a String. */ |
|---|
| 85 | static String toString(unsigned int val, |
|---|
| 86 | unsigned short width = 0, char fill = ' ', |
|---|
| 87 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 88 | /** Converts a size_t to a String. */ |
|---|
| 89 | static String toString(size_t val, |
|---|
| 90 | unsigned short width = 0, char fill = ' ', |
|---|
| 91 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 92 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC |
|---|
| 93 | /** Converts an unsigned long to a String. */ |
|---|
| 94 | static String toString(unsigned long val, |
|---|
| 95 | unsigned short width = 0, char fill = ' ', |
|---|
| 96 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 97 | |
|---|
| 98 | #endif |
|---|
| 99 | #else |
|---|
| 100 | /** Converts a size_t to a String. */ |
|---|
| 101 | static String toString(size_t val, |
|---|
| 102 | unsigned short width = 0, char fill = ' ', |
|---|
| 103 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 104 | /** Converts an unsigned long to a String. */ |
|---|
| 105 | static String toString(unsigned long val, |
|---|
| 106 | unsigned short width = 0, char fill = ' ', |
|---|
| 107 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 108 | #endif |
|---|
| 109 | /** Converts a long to a String. */ |
|---|
| 110 | static String toString(long val, |
|---|
| 111 | unsigned short width = 0, char fill = ' ', |
|---|
| 112 | std::ios::fmtflags flags = std::ios::fmtflags(0) ); |
|---|
| 113 | /** Converts a boolean to a String. |
|---|
| 114 | @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false' |
|---|
| 115 | */ |
|---|
| 116 | static String toString(bool val, bool yesNo = false); |
|---|
| 117 | /** Converts a Vector2 to a String. |
|---|
| 118 | @remarks |
|---|
| 119 | Format is "x y" (i.e. 2x Real values, space delimited) |
|---|
| 120 | */ |
|---|
| 121 | static String toString(const Vector2& val); |
|---|
| 122 | /** Converts a Vector3 to a String. |
|---|
| 123 | @remarks |
|---|
| 124 | Format is "x y z" (i.e. 3x Real values, space delimited) |
|---|
| 125 | */ |
|---|
| 126 | static String toString(const Vector3& val); |
|---|
| 127 | /** Converts a Vector4 to a String. |
|---|
| 128 | @remarks |
|---|
| 129 | Format is "x y z w" (i.e. 4x Real values, space delimited) |
|---|
| 130 | */ |
|---|
| 131 | static String toString(const Vector4& val); |
|---|
| 132 | /** Converts a Matrix3 to a String. |
|---|
| 133 | @remarks |
|---|
| 134 | Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc. |
|---|
| 135 | */ |
|---|
| 136 | static String toString(const Matrix3& val); |
|---|
| 137 | /** Converts a Matrix4 to a String. |
|---|
| 138 | @remarks |
|---|
| 139 | Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where |
|---|
| 140 | '01' means row 0 column 1 etc. |
|---|
| 141 | */ |
|---|
| 142 | static String toString(const Matrix4& val); |
|---|
| 143 | /** Converts a Quaternion to a String. |
|---|
| 144 | @remarks |
|---|
| 145 | Format is "w x y z" (i.e. 4x Real values, space delimited) |
|---|
| 146 | */ |
|---|
| 147 | static String toString(const Quaternion& val); |
|---|
| 148 | /** Converts a ColourValue to a String. |
|---|
| 149 | @remarks |
|---|
| 150 | Format is "r g b a" (i.e. 4x Real values, space delimited). |
|---|
| 151 | */ |
|---|
| 152 | static String toString(const ColourValue& val); |
|---|
| 153 | /** Converts a StringVector to a string. |
|---|
| 154 | @remarks |
|---|
| 155 | Strings must not contain spaces since space is used as a delimeter in |
|---|
| 156 | the output. |
|---|
| 157 | */ |
|---|
| 158 | static String toString(const StringVector& val); |
|---|
| 159 | |
|---|
| 160 | /** Converts a String to a Real. |
|---|
| 161 | @returns |
|---|
| 162 | 0.0 if the value could not be parsed, otherwise the Real version of the String. |
|---|
| 163 | */ |
|---|
| 164 | static Real parseReal(const String& val); |
|---|
| 165 | /** Converts a String to a Angle. |
|---|
| 166 | @returns |
|---|
| 167 | 0.0 if the value could not be parsed, otherwise the Angle version of the String. |
|---|
| 168 | */ |
|---|
| 169 | static inline Radian parseAngle(const String& val) { |
|---|
| 170 | return Angle(parseReal(val)); |
|---|
| 171 | } |
|---|
| 172 | /** Converts a String to a whole number. |
|---|
| 173 | @returns |
|---|
| 174 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
|---|
| 175 | */ |
|---|
| 176 | static int parseInt(const String& val); |
|---|
| 177 | /** Converts a String to a whole number. |
|---|
| 178 | @returns |
|---|
| 179 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
|---|
| 180 | */ |
|---|
| 181 | static unsigned int parseUnsignedInt(const String& val); |
|---|
| 182 | /** Converts a String to a whole number. |
|---|
| 183 | @returns |
|---|
| 184 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
|---|
| 185 | */ |
|---|
| 186 | static long parseLong(const String& val); |
|---|
| 187 | /** Converts a String to a whole number. |
|---|
| 188 | @returns |
|---|
| 189 | 0.0 if the value could not be parsed, otherwise the numeric version of the String. |
|---|
| 190 | */ |
|---|
| 191 | static unsigned long parseUnsignedLong(const String& val); |
|---|
| 192 | /** Converts a String to a boolean. |
|---|
| 193 | @remarks |
|---|
| 194 | Returns true if case-insensitive match of the start of the string |
|---|
| 195 | matches "true", "yes" or "1", false otherwise. |
|---|
| 196 | */ |
|---|
| 197 | static bool parseBool(const String& val); |
|---|
| 198 | /** Parses a Vector3 out of a String. |
|---|
| 199 | @remarks |
|---|
| 200 | Format is "x y z" ie. 3 Real components, space delimited. Failure to parse returns |
|---|
| 201 | Vector3::ZERO. |
|---|
| 202 | */ |
|---|
| 203 | static Vector3 parseVector3(const String& val); |
|---|
| 204 | /** Parses a Matrix3 out of a String. |
|---|
| 205 | @remarks |
|---|
| 206 | Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc. |
|---|
| 207 | Failure to parse returns Matrix3::IDENTITY. |
|---|
| 208 | */ |
|---|
| 209 | static Matrix3 parseMatrix3(const String& val); |
|---|
| 210 | /** Parses a Matrix4 out of a String. |
|---|
| 211 | @remarks |
|---|
| 212 | Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where |
|---|
| 213 | '01' means row 0 column 1 etc. Failure to parse returns Matrix4::IDENTITY. |
|---|
| 214 | */ |
|---|
| 215 | static Matrix4 parseMatrix4(const String& val); |
|---|
| 216 | /** Parses a Quaternion out of a String. |
|---|
| 217 | @remarks |
|---|
| 218 | Format is "w x y z" (i.e. 4x Real values, space delimited). |
|---|
| 219 | Failure to parse returns Quaternion::IDENTITY. |
|---|
| 220 | |
|---|
| 221 | */ |
|---|
| 222 | static Quaternion parseQuaternion(const String& val); |
|---|
| 223 | /** Parses a ColourValue out of a String. |
|---|
| 224 | @remarks |
|---|
| 225 | Format is "r g b a" (i.e. 4x Real values, space delimited), or "r g b" which implies |
|---|
| 226 | an alpha value of 1.0 (opaque). Failure to parse returns ColourValue::Black. |
|---|
| 227 | */ |
|---|
| 228 | static ColourValue parseColourValue(const String& val); |
|---|
| 229 | |
|---|
| 230 | /** Pareses a StringVector from a string. |
|---|
| 231 | @remarks |
|---|
| 232 | Strings must not contain spaces since space is used as a delimeter in |
|---|
| 233 | the output. |
|---|
| 234 | */ |
|---|
| 235 | static StringVector parseStringVector(const String& val); |
|---|
| 236 | /** Checks the String is a valid number value. */ |
|---|
| 237 | static bool isNumber(const String& val); |
|---|
| 238 | }; |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | #endif |
|---|
| 246 | |
|---|