StringUtils.cpp 5.2 KB

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