OgreStringConverter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __StringConverter_H__
  25. #define __StringConverter_H__
  26. #include "OgrePrerequisites.h"
  27. #include "OgreStringVector.h"
  28. #include "OgreColourValue.h"
  29. #include "OgreMath.h"
  30. #include "OgreMatrix3.h"
  31. #include "OgreMatrix4.h"
  32. #include "OgreQuaternion.h"
  33. #include "OgreVector2.h"
  34. #include "OgreVector3.h"
  35. #include "OgreVector4.h"
  36. namespace Ogre {
  37. /** \addtogroup Core
  38. * @{
  39. */
  40. /** \addtogroup General
  41. * @{
  42. */
  43. /** Class for converting the core Ogre data types to/from Strings.
  44. @remarks
  45. The code for converting values to and from strings is here as a separate
  46. class to avoid coupling String to other datatypes (and vice-versa) which reduces
  47. compilation dependency: important given how often the core types are used.
  48. @par
  49. This class is mainly used for parsing settings in text files. External applications
  50. can also use it to interface with classes which use the StringInterface template
  51. class.
  52. @par
  53. The String formats of each of the major types is listed with the methods. The basic types
  54. like int and Real just use the underlying C runtime library atof and atoi family methods,
  55. however custom types like Vector3, ColourValue and Matrix4 are also supported by this class
  56. using custom formats.
  57. @author
  58. Steve Streeting
  59. */
  60. class _OgreExport StringConverter
  61. {
  62. public:
  63. /** Converts a Real to a String. */
  64. static String toString(Real val, unsigned short precision = 6,
  65. unsigned short width = 0, char fill = ' ',
  66. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  67. /** Converts a Radian to a String. */
  68. static String toString(Radian val, unsigned short precision = 6,
  69. unsigned short width = 0, char fill = ' ',
  70. std::ios::fmtflags flags = std::ios::fmtflags(0) )
  71. {
  72. return toString(val.valueAngleUnits(), precision, width, fill, flags);
  73. }
  74. /** Converts a Degree to a String. */
  75. static String toString(Degree val, unsigned short precision = 6,
  76. unsigned short width = 0, char fill = ' ',
  77. std::ios::fmtflags flags = std::ios::fmtflags(0) )
  78. {
  79. return toString(val.valueAngleUnits(), precision, width, fill, flags);
  80. }
  81. /** Converts an int to a String. */
  82. static String toString(int val, unsigned short width = 0,
  83. char fill = ' ',
  84. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  85. #if OGRE_ARCH_TYPE == OGRE_ARCHITECTURE_64 || OGRE_PLATFORM == OGRE_PLATFORM_APPLE || OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
  86. /** Converts an unsigned int to a String. */
  87. static String toString(unsigned int val,
  88. unsigned short width = 0, char fill = ' ',
  89. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  90. /** Converts a size_t to a String. */
  91. static String toString(size_t val,
  92. unsigned short width = 0, char fill = ' ',
  93. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  94. #if OGRE_COMPILER == OGRE_COMPILER_MSVC
  95. /** Converts an unsigned long to a String. */
  96. static String toString(unsigned long val,
  97. unsigned short width = 0, char fill = ' ',
  98. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  99. #endif
  100. #else
  101. /** Converts a size_t to a String. */
  102. static String toString(size_t val,
  103. unsigned short width = 0, char fill = ' ',
  104. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  105. /** Converts an unsigned long to a String. */
  106. static String toString(unsigned long val,
  107. unsigned short width = 0, char fill = ' ',
  108. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  109. #endif
  110. /** Converts a long to a String. */
  111. static String toString(long val,
  112. unsigned short width = 0, char fill = ' ',
  113. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  114. /** Converts a boolean to a String.
  115. @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false'
  116. */
  117. static String toString(bool val, bool yesNo = false);
  118. /** Converts a Vector2 to a String.
  119. @remarks
  120. Format is "x y" (i.e. 2x Real values, space delimited)
  121. */
  122. static String toString(const Vector2& val);
  123. /** Converts a Vector3 to a String.
  124. @remarks
  125. Format is "x y z" (i.e. 3x Real values, space delimited)
  126. */
  127. static String toString(const Vector3& val);
  128. /** Converts a Vector4 to a String.
  129. @remarks
  130. Format is "x y z w" (i.e. 4x Real values, space delimited)
  131. */
  132. static String toString(const Vector4& val);
  133. /** Converts a Matrix3 to a String.
  134. @remarks
  135. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  136. */
  137. static String toString(const Matrix3& val);
  138. /** Converts a Matrix4 to a String.
  139. @remarks
  140. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  141. '01' means row 0 column 1 etc.
  142. */
  143. static String toString(const Matrix4& val);
  144. /** Converts a Quaternion to a String.
  145. @remarks
  146. Format is "w x y z" (i.e. 4x Real values, space delimited)
  147. */
  148. static String toString(const Quaternion& val);
  149. /** Converts a ColourValue to a String.
  150. @remarks
  151. Format is "r g b a" (i.e. 4x Real values, space delimited).
  152. */
  153. static String toString(const ColourValue& val);
  154. /** Converts a StringVector to a string.
  155. @remarks
  156. Strings must not contain spaces since space is used as a delimiter in
  157. the output.
  158. */
  159. static String toString(const StringVector& val);
  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, Real defaultValue = 0);
  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, Radian defaultValue = Radian(0)) {
  170. return Angle(parseReal(val, defaultValue.valueRadians()));
  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, int defaultValue = 0);
  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, unsigned int defaultValue = 0);
  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, long defaultValue = 0);
  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, unsigned long defaultValue = 0);
  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, bool defaultValue = 0);
  198. /** Parses a Vector2 out of a String.
  199. @remarks
  200. Format is "x y" ie. 2 Real components, space delimited. Failure to parse returns
  201. Vector2::ZERO.
  202. */
  203. static Vector2 parseVector2(const String& val, const Vector2& defaultValue = Vector2::ZERO);
  204. /** Parses a Vector3 out of a String.
  205. @remarks
  206. Format is "x y z" ie. 3 Real components, space delimited. Failure to parse returns
  207. Vector3::ZERO.
  208. */
  209. static Vector3 parseVector3(const String& val, const Vector3& defaultValue = Vector3::ZERO);
  210. /** Parses a Vector4 out of a String.
  211. @remarks
  212. Format is "x y z w" ie. 4 Real components, space delimited. Failure to parse returns
  213. Vector4::ZERO.
  214. */
  215. static Vector4 parseVector4(const String& val, const Vector4& defaultValue = Vector4::ZERO);
  216. /** Parses a Matrix3 out of a String.
  217. @remarks
  218. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  219. Failure to parse returns Matrix3::IDENTITY.
  220. */
  221. static Matrix3 parseMatrix3(const String& val, const Matrix3& defaultValue = Matrix3::IDENTITY);
  222. /** Parses a Matrix4 out of a String.
  223. @remarks
  224. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  225. '01' means row 0 column 1 etc. Failure to parse returns Matrix4::IDENTITY.
  226. */
  227. static Matrix4 parseMatrix4(const String& val, const Matrix4& defaultValue = Matrix4::IDENTITY);
  228. /** Parses a Quaternion out of a String.
  229. @remarks
  230. Format is "w x y z" (i.e. 4x Real values, space delimited).
  231. Failure to parse returns Quaternion::IDENTITY.
  232. */
  233. static Quaternion parseQuaternion(const String& val, const Quaternion& defaultValue = Quaternion::IDENTITY);
  234. /** Parses a ColourValue out of a String.
  235. @remarks
  236. Format is "r g b a" (i.e. 4x Real values, space delimited), or "r g b" which implies
  237. an alpha value of 1.0 (opaque). Failure to parse returns ColourValue::Black.
  238. */
  239. static ColourValue parseColourValue(const String& val, const ColourValue& defaultValue = ColourValue::Black);
  240. /** Pareses a StringVector from a string.
  241. @remarks
  242. Strings must not contain spaces since space is used as a delimiter in
  243. the output.
  244. */
  245. static StringVector parseStringVector(const String& val);
  246. /** Checks the String is a valid number value. */
  247. static bool isNumber(const String& val);
  248. };
  249. /** @} */
  250. /** @} */
  251. }
  252. #endif