StringUtils.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. unsigned CountElements(const String& str, char separator)
  29. {
  30. const char* buffer = str.CString();
  31. unsigned length = str.Length();
  32. unsigned pos = 0;
  33. unsigned ret = 0;
  34. while (pos < length)
  35. {
  36. unsigned start = pos;
  37. while (start < length)
  38. {
  39. if (buffer[start] == separator)
  40. break;
  41. ++start;
  42. }
  43. if (start == length)
  44. {
  45. ++ret;
  46. break;
  47. }
  48. unsigned end = start;
  49. while (end < length)
  50. {
  51. if (buffer[end] != separator)
  52. break;
  53. ++end;
  54. }
  55. ++ret;
  56. pos = end;
  57. }
  58. return ret;
  59. }
  60. bool ToBool(const String& source)
  61. {
  62. for (unsigned i = 0; i < source.Length(); ++i)
  63. {
  64. char c = tolower(source[0]);
  65. if (c == 't' || c == 'y' || c == '1')
  66. return true;
  67. else if (c != ' ' && c != '\t')
  68. break;
  69. }
  70. return false;
  71. }
  72. int ToInt(const String& source)
  73. {
  74. if (!source.Length())
  75. return 0;
  76. return strtol(source.CString(), 0, 0);
  77. }
  78. unsigned ToUInt(const String& source)
  79. {
  80. if (!source.Length())
  81. return 0;
  82. return strtoul(source.CString(), 0, 0);
  83. }
  84. float ToFloat(const String& source)
  85. {
  86. if (!source.Length())
  87. return 0.0f;
  88. return (float)strtod(source.CString(), 0);
  89. }
  90. Color ToColor(const String& source)
  91. {
  92. Color ret;
  93. unsigned elements = CountElements(source, ' ');
  94. if (elements < 3)
  95. return ret;
  96. char* ptr = (char*)source.CString();
  97. ret.r_ = (float)strtod(ptr, &ptr);
  98. ret.g_ = (float)strtod(ptr, &ptr);
  99. ret.b_ = (float)strtod(ptr, &ptr);
  100. if (elements > 3)
  101. ret.a_ = (float)strtod(ptr, &ptr);
  102. return ret;
  103. }
  104. IntRect ToIntRect(const String& source)
  105. {
  106. IntRect ret(IntRect::ZERO);
  107. unsigned elements = CountElements(source, ' ');
  108. if (elements < 4)
  109. return ret;
  110. char* ptr = (char*)source.CString();
  111. ret.left_ = strtol(ptr, &ptr, 0);
  112. ret.top_ = strtol(ptr, &ptr, 0);
  113. ret.right_ = strtol(ptr, &ptr, 0);
  114. ret.bottom_ = strtol(ptr, &ptr, 0);
  115. return ret;
  116. }
  117. IntVector2 ToIntVector2(const String& source)
  118. {
  119. IntVector2 ret(IntVector2::ZERO);
  120. unsigned elements = CountElements(source, ' ');
  121. if (elements < 2)
  122. return ret;
  123. char* ptr = (char*)source.CString();
  124. ret.x_ = strtol(ptr, &ptr, 0);
  125. ret.y_ = strtol(ptr, &ptr, 0);
  126. return ret;
  127. }
  128. Rect ToRect(const String& source)
  129. {
  130. Rect ret(Rect::ZERO);
  131. unsigned elements = CountElements(source, ' ');
  132. if (elements < 4)
  133. return ret;
  134. char* ptr = (char*)source.CString();
  135. ret.min_.x_ = (float)strtod(ptr, &ptr);
  136. ret.min_.y_ = (float)strtod(ptr, &ptr);
  137. ret.max_.x_ = (float)strtod(ptr, &ptr);
  138. ret.max_.y_ = (float)strtod(ptr, &ptr);
  139. return ret;
  140. }
  141. Quaternion ToQuaternion(const String& source)
  142. {
  143. unsigned elements = CountElements(source, ' ');
  144. char* ptr = (char*)source.CString();
  145. if (elements < 3)
  146. return Quaternion::IDENTITY;
  147. else if (elements < 4)
  148. {
  149. // 3 coords specified: conversion from Euler angles
  150. float x, y, z;
  151. x = (float)strtod(ptr, &ptr);
  152. y = (float)strtod(ptr, &ptr);
  153. z = (float)strtod(ptr, &ptr);
  154. return Quaternion(x, y, z);
  155. }
  156. else
  157. {
  158. // 4 coords specified: full quaternion
  159. Quaternion ret;
  160. ret.w_ = (float)strtod(ptr, &ptr);
  161. ret.x_ = (float)strtod(ptr, &ptr);
  162. ret.y_ = (float)strtod(ptr, &ptr);
  163. ret.z_ = (float)strtod(ptr, &ptr);
  164. return ret;
  165. }
  166. }
  167. Vector2 ToVector2(const String& source)
  168. {
  169. Vector2 ret(Vector2::ZERO);
  170. unsigned elements = CountElements(source, ' ');
  171. if (elements < 2)
  172. return ret;
  173. char* ptr = (char*)source.CString();
  174. ret.x_ = (float)strtod(ptr, &ptr);
  175. ret.y_ = (float)strtod(ptr, &ptr);
  176. return ret;
  177. }
  178. Vector3 ToVector3(const String& source)
  179. {
  180. Vector3 ret(Vector3::ZERO);
  181. unsigned elements = CountElements(source, ' ');
  182. if (elements < 3)
  183. return ret;
  184. char* ptr = (char*)source.CString();
  185. ret.x_ = (float)strtod(ptr, &ptr);
  186. ret.y_ = (float)strtod(ptr, &ptr);
  187. ret.z_ = (float)strtod(ptr, &ptr);
  188. return ret;
  189. }
  190. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  191. {
  192. Vector4 ret(Vector4::ZERO);
  193. unsigned elements = CountElements(source, ' ');
  194. char* ptr = (char*)source.CString();
  195. if (!allowMissingCoords)
  196. {
  197. if (elements < 4)
  198. return ret;
  199. ret.x_ = (float)strtod(ptr, &ptr);
  200. ret.y_ = (float)strtod(ptr, &ptr);
  201. ret.z_ = (float)strtod(ptr, &ptr);
  202. ret.w_ = (float)strtod(ptr, &ptr);
  203. return ret;
  204. }
  205. else
  206. {
  207. if (elements > 0)
  208. ret.x_ = (float)strtod(ptr, &ptr);
  209. if (elements > 1)
  210. ret.y_ = (float)strtod(ptr, &ptr);
  211. if (elements > 2)
  212. ret.z_ = (float)strtod(ptr, &ptr);
  213. if (elements > 3)
  214. ret.w_ = (float)strtod(ptr, &ptr);
  215. return ret;
  216. }
  217. }
  218. String ToString(void* value)
  219. {
  220. return ToStringHex((int)value);
  221. }
  222. String ToStringHex(unsigned value)
  223. {
  224. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  225. sprintf(tempBuffer, "%08x", value);
  226. return String(tempBuffer);
  227. }
  228. unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  229. {
  230. unsigned i = 0;
  231. while (!strings[i].Empty())
  232. {
  233. if (!value.Compare(strings[i], caseSensitive))
  234. return i;
  235. ++i;
  236. }
  237. return defaultIndex;
  238. }