StringUtils.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "StringUtils.h"
  24. #include <cstdio>
  25. #include <ctype.h>
  26. #include "DebugNew.h"
  27. namespace Urho3D
  28. {
  29. unsigned CountElements(const char* buffer, char separator)
  30. {
  31. if (!buffer)
  32. return 0;
  33. const char* endPos = buffer + String::CStringLength(buffer);
  34. const char* pos = buffer;
  35. unsigned ret = 0;
  36. while (pos < endPos)
  37. {
  38. if (*pos != separator)
  39. break;
  40. ++pos;
  41. }
  42. while (pos < endPos)
  43. {
  44. const char* start = pos;
  45. while (start < endPos)
  46. {
  47. if (*start == separator)
  48. break;
  49. ++start;
  50. }
  51. if (start == endPos)
  52. {
  53. ++ret;
  54. break;
  55. }
  56. const char* end = start;
  57. while (end < endPos)
  58. {
  59. if (*end != separator)
  60. break;
  61. ++end;
  62. }
  63. ++ret;
  64. pos = end;
  65. }
  66. return ret;
  67. }
  68. bool ToBool(const String& source)
  69. {
  70. return ToBool(source.CString());
  71. }
  72. bool ToBool(const char* source)
  73. {
  74. unsigned length = String::CStringLength(source);
  75. for (unsigned i = 0; i < length; ++i)
  76. {
  77. char c = tolower(source[i]);
  78. if (c == 't' || c == 'y' || c == '1')
  79. return true;
  80. else if (c != ' ' && c != '\t')
  81. break;
  82. }
  83. return false;
  84. }
  85. int ToInt(const String& source)
  86. {
  87. return ToInt(source.CString());
  88. }
  89. int ToInt(const char* source)
  90. {
  91. if (!source)
  92. return 0;
  93. return strtol(source, 0, 0);
  94. }
  95. unsigned ToUInt(const String& source)
  96. {
  97. return ToUInt(source.CString());
  98. }
  99. unsigned ToUInt(const char* source)
  100. {
  101. if (!source)
  102. return 0;
  103. return strtoul(source, 0, 0);
  104. }
  105. float ToFloat(const String& source)
  106. {
  107. return ToFloat(source.CString());
  108. }
  109. float ToFloat(const char* source)
  110. {
  111. if (!source)
  112. return 0;
  113. return (float)strtod(source, 0);
  114. }
  115. Color ToColor(const String& source)
  116. {
  117. return ToColor(source.CString());
  118. }
  119. Color ToColor(const char* source)
  120. {
  121. Color ret;
  122. unsigned elements = CountElements(source, ' ');
  123. if (elements < 3)
  124. return ret;
  125. char* ptr = (char*)source;
  126. ret.r_ = (float)strtod(ptr, &ptr);
  127. ret.g_ = (float)strtod(ptr, &ptr);
  128. ret.b_ = (float)strtod(ptr, &ptr);
  129. if (elements > 3)
  130. ret.a_ = (float)strtod(ptr, &ptr);
  131. return ret;
  132. }
  133. IntRect ToIntRect(const String& source)
  134. {
  135. return ToIntRect(source.CString());
  136. }
  137. IntRect ToIntRect(const char* source)
  138. {
  139. IntRect ret(IntRect::ZERO);
  140. unsigned elements = CountElements(source, ' ');
  141. if (elements < 4)
  142. return ret;
  143. char* ptr = (char*)source;
  144. ret.left_ = strtol(ptr, &ptr, 0);
  145. ret.top_ = strtol(ptr, &ptr, 0);
  146. ret.right_ = strtol(ptr, &ptr, 0);
  147. ret.bottom_ = strtol(ptr, &ptr, 0);
  148. return ret;
  149. }
  150. IntVector2 ToIntVector2(const String& source)
  151. {
  152. return ToIntVector2(source.CString());
  153. }
  154. IntVector2 ToIntVector2(const char* source)
  155. {
  156. IntVector2 ret(IntVector2::ZERO);
  157. unsigned elements = CountElements(source, ' ');
  158. if (elements < 2)
  159. return ret;
  160. char* ptr = (char*)source;
  161. ret.x_ = strtol(ptr, &ptr, 0);
  162. ret.y_ = strtol(ptr, &ptr, 0);
  163. return ret;
  164. }
  165. Rect ToRect(const String& source)
  166. {
  167. return ToRect(source.CString());
  168. }
  169. Rect ToRect(const char* source)
  170. {
  171. Rect ret(Rect::ZERO);
  172. unsigned elements = CountElements(source, ' ');
  173. if (elements < 4)
  174. return ret;
  175. char* ptr = (char*)source;
  176. ret.min_.x_ = (float)strtod(ptr, &ptr);
  177. ret.min_.y_ = (float)strtod(ptr, &ptr);
  178. ret.max_.x_ = (float)strtod(ptr, &ptr);
  179. ret.max_.y_ = (float)strtod(ptr, &ptr);
  180. return ret;
  181. }
  182. Quaternion ToQuaternion(const String& source)
  183. {
  184. return ToQuaternion(source.CString());
  185. }
  186. Quaternion ToQuaternion(const char* source)
  187. {
  188. unsigned elements = CountElements(source, ' ');
  189. char* ptr = (char*)source;
  190. if (elements < 3)
  191. return Quaternion::IDENTITY;
  192. else if (elements < 4)
  193. {
  194. // 3 coords specified: conversion from Euler angles
  195. float x, y, z;
  196. x = (float)strtod(ptr, &ptr);
  197. y = (float)strtod(ptr, &ptr);
  198. z = (float)strtod(ptr, &ptr);
  199. return Quaternion(x, y, z);
  200. }
  201. else
  202. {
  203. // 4 coords specified: full quaternion
  204. Quaternion ret;
  205. ret.w_ = (float)strtod(ptr, &ptr);
  206. ret.x_ = (float)strtod(ptr, &ptr);
  207. ret.y_ = (float)strtod(ptr, &ptr);
  208. ret.z_ = (float)strtod(ptr, &ptr);
  209. return ret;
  210. }
  211. }
  212. Vector2 ToVector2(const String& source)
  213. {
  214. return ToVector2(source.CString());
  215. }
  216. Vector2 ToVector2(const char* source)
  217. {
  218. Vector2 ret(Vector2::ZERO);
  219. unsigned elements = CountElements(source, ' ');
  220. if (elements < 2)
  221. return ret;
  222. char* ptr = (char*)source;
  223. ret.x_ = (float)strtod(ptr, &ptr);
  224. ret.y_ = (float)strtod(ptr, &ptr);
  225. return ret;
  226. }
  227. Vector3 ToVector3(const String& source)
  228. {
  229. return ToVector3(source.CString());
  230. }
  231. Vector3 ToVector3(const char* source)
  232. {
  233. Vector3 ret(Vector3::ZERO);
  234. unsigned elements = CountElements(source, ' ');
  235. if (elements < 3)
  236. return ret;
  237. char* ptr = (char*)source;
  238. ret.x_ = (float)strtod(ptr, &ptr);
  239. ret.y_ = (float)strtod(ptr, &ptr);
  240. ret.z_ = (float)strtod(ptr, &ptr);
  241. return ret;
  242. }
  243. Vector4 ToVector4(const String& source, bool allowMissingCoords)
  244. {
  245. return ToVector4(source.CString(), allowMissingCoords);
  246. }
  247. Vector4 ToVector4(const char* source, bool allowMissingCoords)
  248. {
  249. Vector4 ret(Vector4::ZERO);
  250. unsigned elements = CountElements(source, ' ');
  251. char* ptr = (char*)source;
  252. if (!allowMissingCoords)
  253. {
  254. if (elements < 4)
  255. return ret;
  256. ret.x_ = (float)strtod(ptr, &ptr);
  257. ret.y_ = (float)strtod(ptr, &ptr);
  258. ret.z_ = (float)strtod(ptr, &ptr);
  259. ret.w_ = (float)strtod(ptr, &ptr);
  260. return ret;
  261. }
  262. else
  263. {
  264. if (elements > 0)
  265. ret.x_ = (float)strtod(ptr, &ptr);
  266. if (elements > 1)
  267. ret.y_ = (float)strtod(ptr, &ptr);
  268. if (elements > 2)
  269. ret.z_ = (float)strtod(ptr, &ptr);
  270. if (elements > 3)
  271. ret.w_ = (float)strtod(ptr, &ptr);
  272. return ret;
  273. }
  274. }
  275. String ToString(void* value)
  276. {
  277. return ToStringHex((int)value);
  278. }
  279. String ToStringHex(unsigned value)
  280. {
  281. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  282. sprintf(tempBuffer, "%08x", value);
  283. return String(tempBuffer);
  284. }
  285. unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  286. {
  287. return GetStringListIndex(value.CString(), strings, defaultIndex, caseSensitive);
  288. }
  289. unsigned GetStringListIndex(const char* value, const String* strings, unsigned defaultIndex, bool caseSensitive)
  290. {
  291. unsigned i = 0;
  292. while (!strings[i].Empty())
  293. {
  294. if (!strings[i].Compare(value, caseSensitive))
  295. return i;
  296. ++i;
  297. }
  298. return defaultIndex;
  299. }
  300. unsigned GetStringListIndex(const char* value, const char** strings, unsigned defaultIndex, bool caseSensitive)
  301. {
  302. unsigned i = 0;
  303. while (strings[i])
  304. {
  305. if (!String::Compare(value, strings[i], caseSensitive))
  306. return i;
  307. ++i;
  308. }
  309. return defaultIndex;
  310. }
  311. String ToString(const char* formatString, ...)
  312. {
  313. String ret;
  314. va_list args;
  315. va_start(args, formatString);
  316. ret.AppendWithFormatArgs(formatString, args);
  317. va_end(args);
  318. return ret;
  319. }
  320. }