StringUtils.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  29. {
  30. unsigned CountElements(const char* buffer, char separator)
  31. {
  32. if (!buffer)
  33. return 0;
  34. const char* endPos = buffer + String::CStringLength(buffer);
  35. const char* pos = buffer;
  36. unsigned ret = 0;
  37. while (pos < endPos)
  38. {
  39. if (*pos != separator)
  40. break;
  41. ++pos;
  42. }
  43. while (pos < endPos)
  44. {
  45. const char* start = pos;
  46. while (start < endPos)
  47. {
  48. if (*start == separator)
  49. break;
  50. ++start;
  51. }
  52. if (start == endPos)
  53. {
  54. ++ret;
  55. break;
  56. }
  57. const char* end = start;
  58. while (end < endPos)
  59. {
  60. if (*end != separator)
  61. break;
  62. ++end;
  63. }
  64. ++ret;
  65. pos = end;
  66. }
  67. return ret;
  68. }
  69. bool ToBool(const String& source)
  70. {
  71. return ToBool(source.CString());
  72. }
  73. bool ToBool(const char* source)
  74. {
  75. unsigned length = String::CStringLength(source);
  76. for (unsigned i = 0; i < length; ++i)
  77. {
  78. char c = tolower(source[i]);
  79. if (c == 't' || c == 'y' || c == '1')
  80. return true;
  81. else if (c != ' ' && c != '\t')
  82. break;
  83. }
  84. return false;
  85. }
  86. int ToInt(const String& source)
  87. {
  88. return ToInt(source.CString());
  89. }
  90. int ToInt(const char* source)
  91. {
  92. if (!source)
  93. return 0;
  94. return strtol(source, 0, 0);
  95. }
  96. unsigned ToUInt(const String& source)
  97. {
  98. return ToUInt(source.CString());
  99. }
  100. unsigned ToUInt(const char* source)
  101. {
  102. if (!source)
  103. return 0;
  104. return strtoul(source, 0, 0);
  105. }
  106. float ToFloat(const String& source)
  107. {
  108. return ToFloat(source.CString());
  109. }
  110. float ToFloat(const char* source)
  111. {
  112. if (!source)
  113. return 0;
  114. return (float)strtod(source, 0);
  115. }
  116. Color ToColor(const String& source)
  117. {
  118. return ToColor(source.CString());
  119. }
  120. Color ToColor(const char* source)
  121. {
  122. Color ret;
  123. unsigned elements = CountElements(source, ' ');
  124. if (elements < 3)
  125. return ret;
  126. char* ptr = (char*)source;
  127. ret.r_ = (float)strtod(ptr, &ptr);
  128. ret.g_ = (float)strtod(ptr, &ptr);
  129. ret.b_ = (float)strtod(ptr, &ptr);
  130. if (elements > 3)
  131. ret.a_ = (float)strtod(ptr, &ptr);
  132. return ret;
  133. }
  134. IntRect ToIntRect(const String& source)
  135. {
  136. return ToIntRect(source.CString());
  137. }
  138. IntRect ToIntRect(const char* source)
  139. {
  140. IntRect ret(IntRect::ZERO);
  141. unsigned elements = CountElements(source, ' ');
  142. if (elements < 4)
  143. return ret;
  144. char* ptr = (char*)source;
  145. ret.left_ = strtol(ptr, &ptr, 0);
  146. ret.top_ = strtol(ptr, &ptr, 0);
  147. ret.right_ = strtol(ptr, &ptr, 0);
  148. ret.bottom_ = strtol(ptr, &ptr, 0);
  149. return ret;
  150. }
  151. IntVector2 ToIntVector2(const String& source)
  152. {
  153. return ToIntVector2(source.CString());
  154. }
  155. IntVector2 ToIntVector2(const char* source)
  156. {
  157. IntVector2 ret(IntVector2::ZERO);
  158. unsigned elements = CountElements(source, ' ');
  159. if (elements < 2)
  160. return ret;
  161. char* ptr = (char*)source;
  162. ret.x_ = strtol(ptr, &ptr, 0);
  163. ret.y_ = strtol(ptr, &ptr, 0);
  164. return ret;
  165. }
  166. Rect ToRect(const String& source)
  167. {
  168. return ToRect(source.CString());
  169. }
  170. Rect ToRect(const char* source)
  171. {
  172. Rect ret(Rect::ZERO);
  173. unsigned elements = CountElements(source, ' ');
  174. if (elements < 4)
  175. return ret;
  176. char* ptr = (char*)source;
  177. ret.min_.x_ = (float)strtod(ptr, &ptr);
  178. ret.min_.y_ = (float)strtod(ptr, &ptr);
  179. ret.max_.x_ = (float)strtod(ptr, &ptr);
  180. ret.max_.y_ = (float)strtod(ptr, &ptr);
  181. return ret;
  182. }
  183. Quaternion ToQuaternion(const String& source)
  184. {
  185. return ToQuaternion(source.CString());
  186. }
  187. Quaternion ToQuaternion(const char* source)
  188. {
  189. unsigned elements = CountElements(source, ' ');
  190. char* ptr = (char*)source;
  191. if (elements < 3)
  192. return Quaternion::IDENTITY;
  193. else if (elements < 4)
  194. {
  195. // 3 coords specified: conversion from Euler angles
  196. float x, y, z;
  197. x = (float)strtod(ptr, &ptr);
  198. y = (float)strtod(ptr, &ptr);
  199. z = (float)strtod(ptr, &ptr);
  200. return Quaternion(x, y, z);
  201. }
  202. else
  203. {
  204. // 4 coords specified: full quaternion
  205. Quaternion ret;
  206. ret.w_ = (float)strtod(ptr, &ptr);
  207. ret.x_ = (float)strtod(ptr, &ptr);
  208. ret.y_ = (float)strtod(ptr, &ptr);
  209. ret.z_ = (float)strtod(ptr, &ptr);
  210. return ret;
  211. }
  212. }
  213. Vector2 ToVector2(const String& source)
  214. {
  215. return ToVector2(source.CString());
  216. }
  217. Vector2 ToVector2(const char* source)
  218. {
  219. Vector2 ret(Vector2::ZERO);
  220. unsigned elements = CountElements(source, ' ');
  221. if (elements < 2)
  222. return ret;
  223. char* ptr = (char*)source;
  224. ret.x_ = (float)strtod(ptr, &ptr);
  225. ret.y_ = (float)strtod(ptr, &ptr);
  226. return ret;
  227. }
  228. Vector3 ToVector3(const String& source)
  229. {
  230. return ToVector3(source.CString());
  231. }
  232. Vector3 ToVector3(const char* source)
  233. {
  234. Vector3 ret(Vector3::ZERO);
  235. unsigned elements = CountElements(source, ' ');
  236. if (elements < 3)
  237. return ret;
  238. char* ptr = (char*)source;
  239. ret.x_ = (float)strtod(ptr, &ptr);
  240. ret.y_ = (float)strtod(ptr, &ptr);
  241. ret.z_ = (float)strtod(ptr, &ptr);
  242. return ret;
  243. }
  244. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  245. {
  246. return ToVector4(source.CString(), allowMissingCoords);
  247. }
  248. Vector4 ToVector4(const char* source, bool allowMissingCoords)
  249. {
  250. Vector4 ret(Vector4::ZERO);
  251. unsigned elements = CountElements(source, ' ');
  252. char* ptr = (char*)source;
  253. if (!allowMissingCoords)
  254. {
  255. if (elements < 4)
  256. return ret;
  257. ret.x_ = (float)strtod(ptr, &ptr);
  258. ret.y_ = (float)strtod(ptr, &ptr);
  259. ret.z_ = (float)strtod(ptr, &ptr);
  260. ret.w_ = (float)strtod(ptr, &ptr);
  261. return ret;
  262. }
  263. else
  264. {
  265. if (elements > 0)
  266. ret.x_ = (float)strtod(ptr, &ptr);
  267. if (elements > 1)
  268. ret.y_ = (float)strtod(ptr, &ptr);
  269. if (elements > 2)
  270. ret.z_ = (float)strtod(ptr, &ptr);
  271. if (elements > 3)
  272. ret.w_ = (float)strtod(ptr, &ptr);
  273. return ret;
  274. }
  275. }
  276. String ToString(void* value)
  277. {
  278. return ToStringHex((int)value);
  279. }
  280. String ToStringHex(unsigned value)
  281. {
  282. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  283. sprintf(tempBuffer, "%08x", value);
  284. return String(tempBuffer);
  285. }
  286. unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  287. {
  288. return GetStringListIndex(value.CString(), strings, defaultIndex, caseSensitive);
  289. }
  290. unsigned GetStringListIndex(const char* value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  291. {
  292. unsigned i = 0;
  293. while (!strings[i].Empty())
  294. {
  295. if (!strings[i].Compare(value, caseSensitive))
  296. return i;
  297. ++i;
  298. }
  299. return defaultIndex;
  300. }
  301. unsigned GetStringListIndex(const char* value, const char** strings, unsigned defaultIndex, bool caseSensitive)
  302. {
  303. unsigned i = 0;
  304. while (strings[i])
  305. {
  306. if (!String::Compare(value, strings[i], caseSensitive))
  307. return i;
  308. ++i;
  309. }
  310. return defaultIndex;
  311. }
  312. String ToString(const char* formatString, ...)
  313. {
  314. String ret;
  315. va_list args;
  316. va_start(args, formatString);
  317. ret.AppendWithFormatArgs(formatString, args);
  318. va_end(args);
  319. return ret;
  320. }
  321. }