StringUtils.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. for (unsigned i = 0; i < source.Length(); ++i)
  31. {
  32. char c = tolower(source[0]);
  33. if (c == 't' || c == 'y' || c == '1')
  34. return true;
  35. else if (c != ' ' && c != '\t')
  36. break;
  37. }
  38. return false;
  39. }
  40. int ToInt(const String& source)
  41. {
  42. if (!source.Length())
  43. return 0;
  44. return atoi(source.CString());
  45. }
  46. unsigned ToUInt(const String& source)
  47. {
  48. if (!source.Length())
  49. return 0;
  50. return (unsigned)atoi(source.CString());
  51. }
  52. float ToFloat(const String& source)
  53. {
  54. if (!source.Length())
  55. return 0.0f;
  56. return (float)atof(source.CString());
  57. }
  58. Color ToColor(const String& source)
  59. {
  60. Vector<String> colors = source.Split(' ');
  61. if (colors.Size() < 3)
  62. return Color();
  63. Color ret(ToFloat(colors[0]), ToFloat(colors[1]), ToFloat(colors[2]));
  64. if (colors.Size() > 3)
  65. ret.a_ = ToFloat(colors[3]);
  66. return ret;
  67. }
  68. IntRect ToIntRect(const String& source)
  69. {
  70. Vector<String> coords = source.Split(' ');
  71. if (coords.Size() < 4)
  72. return IntRect::ZERO;
  73. else
  74. return IntRect(ToInt(coords[0]), ToInt(coords[1]), ToInt(coords[2]), ToInt(coords[3]));
  75. }
  76. IntVector2 ToIntVector2(const String& source)
  77. {
  78. Vector<String> coords = source.Split(' ');
  79. if (coords.Size() < 2)
  80. return IntVector2::ZERO;
  81. else
  82. return IntVector2(ToInt(coords[0]), ToInt(coords[1]));
  83. }
  84. Rect ToRect(const String& source)
  85. {
  86. Vector<String> coords = source.Split(' ');
  87. if (coords.Size() < 4)
  88. return Rect::ZERO;
  89. else
  90. return Rect(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3]));
  91. }
  92. Quaternion ToQuaternion(const String& source)
  93. {
  94. Vector<String> coords = source.Split(' ');
  95. if (coords.Size() < 3)
  96. return Quaternion::IDENTITY;
  97. else if (coords.Size() < 4)
  98. // 3 coords specified: conversion from Euler angles
  99. return Quaternion(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]));
  100. else
  101. // 4 coords specified: full quaternion
  102. return Quaternion(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3])).Normalized();
  103. }
  104. Vector2 ToVector2(const String& source)
  105. {
  106. Vector<String> coords = source.Split(' ');
  107. if (coords.Size() < 2)
  108. return Vector2::ZERO;
  109. else
  110. return Vector2(ToFloat(coords[0]), ToFloat(coords[1]));
  111. }
  112. Vector3 ToVector3(const String& source)
  113. {
  114. Vector<String> coords = source.Split(' ');
  115. if (coords.Size() < 3)
  116. return Vector3::ZERO;
  117. else
  118. return Vector3(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]));
  119. }
  120. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  121. {
  122. Vector<String> coords = source.Split(' ');
  123. if (!allowMissingCoords)
  124. {
  125. if (coords.Size() < 4)
  126. return Vector4::ZERO;
  127. return Vector4(ToFloat(coords[0]), ToFloat(coords[1]), ToFloat(coords[2]), ToFloat(coords[3]));
  128. }
  129. else
  130. {
  131. unsigned num = coords.Size();
  132. Vector4 ret(Vector4::ZERO);
  133. if (num > 0)
  134. ret.x_ = ToFloat(coords[0]);
  135. if (num > 1)
  136. ret.y_ = ToFloat(coords[1]);
  137. if (num > 2)
  138. ret.z_ = ToFloat(coords[2]);
  139. if (num > 3)
  140. ret.w_ = ToFloat(coords[3]);
  141. return ret;
  142. }
  143. }
  144. String ToString(void* value)
  145. {
  146. return ToStringHex((int)value);
  147. }
  148. String ToStringHex(unsigned value)
  149. {
  150. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  151. sprintf(tempBuffer, "%08x", value);
  152. return String(tempBuffer);
  153. }
  154. unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  155. {
  156. unsigned i = 0;
  157. while (!strings[i].Empty())
  158. {
  159. if (!value.Compare(strings[i], caseSensitive))
  160. return i;
  161. ++i;
  162. }
  163. return defaultIndex;
  164. }