StringUtils.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  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. #include "Precompiled.h"
  24. #include "StringUtils.h"
  25. #include <cstdio>
  26. #include <ctype.h>
  27. #include "DebugNew.h"
  28. bool ToBool(const String& source)
  29. {
  30. String temp = source.ToLower();
  31. if (temp.Find("true") != String::NPOS)
  32. return true;
  33. else
  34. return false;
  35. }
  36. int ToInt(const String& source)
  37. {
  38. if (!source.Length())
  39. return 0;
  40. return atoi(source.CString());
  41. }
  42. unsigned ToUInt(const String& source)
  43. {
  44. if (!source.Length())
  45. return 0;
  46. return (unsigned)atoi(source.CString());
  47. }
  48. float ToFloat(const String& source)
  49. {
  50. if (!source.Length())
  51. return 0.0f;
  52. return (float)atof(source.CString());
  53. }
  54. Color ToColor(const String& source)
  55. {
  56. Vector<String> colors = source.Split(' ');
  57. if (colors.Size() < 3)
  58. return Color();
  59. Color ret(ToFloat(colors[0]), ToFloat(colors[1]), ToFloat(colors[2]));
  60. if (colors.Size() > 3)
  61. ret.a_ = ToFloat(colors[3]);
  62. return ret;
  63. }
  64. IntRect ToIntRect(const String& source)
  65. {
  66. Vector<String> coords = source.Split(' ');
  67. if (coords.Size() < 4)
  68. return IntRect::ZERO;
  69. else
  70. return IntRect(ToInt(coords[0]), ToInt(coords[1]), ToInt(coords[2]), ToInt(coords[3]));
  71. }
  72. IntVector2 ToIntVector2(const String& source)
  73. {
  74. Vector<String> coords = source.Split(' ');
  75. if (coords.Size() < 2)
  76. return IntVector2::ZERO;
  77. else
  78. return IntVector2(ToInt(coords[0]), ToInt(coords[1]));
  79. }
  80. Rect ToRect(const String& source)
  81. {
  82. Vector<String> coords = source.Split(' ');
  83. if (coords.Size() < 4)
  84. return Rect::ZERO;
  85. else
  86. return Rect(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3]));
  87. }
  88. Quaternion ToQuaternion(const String& source)
  89. {
  90. Vector<String> coords = source.Split(' ');
  91. if (coords.Size() < 3)
  92. return Quaternion::IDENTITY;
  93. else if (coords.Size() < 4)
  94. // 3 coords specified: conversion from Euler angles
  95. return Quaternion(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]));
  96. else
  97. // 4 coords specified: full quaternion
  98. return Quaternion(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3])).GetNormalized();
  99. }
  100. Vector2 ToVector2(const String& source)
  101. {
  102. Vector<String> coords = source.Split(' ');
  103. if (coords.Size() < 2)
  104. return Vector2::ZERO;
  105. else
  106. return Vector2(ToFloat(coords[0]), ToFloat(coords[1]));
  107. }
  108. Vector3 ToVector3(const String& source)
  109. {
  110. Vector<String> coords = source.Split(' ');
  111. if (coords.Size() < 3)
  112. return Vector3::ZERO;
  113. else
  114. return Vector3(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]));
  115. }
  116. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  117. {
  118. Vector<String> coords = source.Split(' ');
  119. if (!allowMissingCoords)
  120. {
  121. if (coords.Size() < 4)
  122. return Vector4::ZERO;
  123. return Vector4(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3]));
  124. }
  125. else
  126. {
  127. unsigned num = coords.Size();
  128. Vector4 ret(Vector4::ZERO);
  129. if (num > 0)
  130. ret.x_ = ToFloat(coords[0]);
  131. if (num > 1)
  132. ret.y_ = ToFloat(coords[1]);
  133. if (num > 2)
  134. ret.z_ = ToFloat(coords[2]);
  135. if (num > 3)
  136. ret.w_ = ToFloat(coords[3]);
  137. return ret;
  138. }
  139. }
  140. String ToString(void* value)
  141. {
  142. return ToStringHex((int)value);
  143. }
  144. String ToStringHex(unsigned value)
  145. {
  146. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  147. sprintf(tempBuffer, "%08x", value);
  148. return String(tempBuffer);
  149. }
  150. unsigned GetStringListIndex(const String& value, const String* strings, unsigned count, unsigned defaultIndex,
  151. bool caseSensitive)
  152. {
  153. if (caseSensitive)
  154. {
  155. for (unsigned i = 0; i < count; ++i)
  156. {
  157. if (value == strings[i])
  158. return i;
  159. }
  160. }
  161. else
  162. {
  163. String valueLower = value.ToLower();
  164. for (unsigned i = 0; i < count; ++i)
  165. {
  166. /// \todo Write an insensitive compare function instead of creating new strings
  167. if (valueLower == strings[i].ToLower())
  168. return i;
  169. }
  170. }
  171. return defaultIndex;
  172. }