CmString.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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. */
  24. #include "CmString.h"
  25. #include "CmColor.h"
  26. #include "CmMath.h"
  27. #include "CmMatrix3.h"
  28. #include "CmMatrix4.h"
  29. #include "CmQuaternion.h"
  30. #include "CmVector2.h"
  31. #include "CmVector3.h"
  32. #include "CmVector4.h"
  33. #include "CmException.h"
  34. namespace CamelotFramework
  35. {
  36. const String StringUtil::BLANK;
  37. const WString StringUtil::WBLANK;
  38. void StringUtil::trim(String& str, bool left, bool right)
  39. {
  40. static const String delims = " \t\r";
  41. if(right)
  42. str.erase(str.find_last_not_of(delims)+1); // trim right
  43. if(left)
  44. str.erase(0, str.find_first_not_of(delims)); // trim left
  45. }
  46. void StringUtil::trim(WString& str, bool left, bool right)
  47. {
  48. static const WString delims = L" \t\r";
  49. if(right)
  50. str.erase(str.find_last_not_of(delims)+1); // trim right
  51. if(left)
  52. str.erase(0, str.find_first_not_of(delims)); // trim left
  53. }
  54. vector<String>::type StringUtil::split(const String& str, const String& delims, unsigned int maxSplits)
  55. {
  56. return splitInternal<char>(str, delims, maxSplits);
  57. }
  58. vector<WString>::type StringUtil::split(const WString& str, const WString& delims, unsigned int maxSplits)
  59. {
  60. return splitInternal<wchar_t>(str, delims, maxSplits);
  61. }
  62. vector<String>::type StringUtil::tokenise(const String& str, const String& singleDelims, const String& doubleDelims, unsigned int maxSplits)
  63. {
  64. return tokeniseInternal<char>(str, singleDelims, doubleDelims, maxSplits);
  65. }
  66. vector<WString>::type StringUtil::tokenise(const WString& str, const WString& singleDelims, const WString& doubleDelims, unsigned int maxSplits)
  67. {
  68. return tokeniseInternal<wchar_t>(str, singleDelims, doubleDelims, maxSplits);
  69. }
  70. void StringUtil::toLowerCase(String& str)
  71. {
  72. std::transform(str.begin(), str.end(), str.begin(), tolower);
  73. }
  74. void StringUtil::toLowerCase(WString& str)
  75. {
  76. std::transform(str.begin(), str.end(), str.begin(), tolower);
  77. }
  78. void StringUtil::toUpperCase(String& str)
  79. {
  80. std::transform(str.begin(), str.end(), str.begin(), toupper);
  81. }
  82. void StringUtil::toUpperCase(WString& str)
  83. {
  84. std::transform(str.begin(), str.end(), str.begin(), toupper);
  85. }
  86. bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase)
  87. {
  88. return startsWithInternal<char>(str, pattern, lowerCase);
  89. }
  90. bool StringUtil::startsWith(const WString& str, const WString& pattern, bool lowerCase)
  91. {
  92. return startsWithInternal<wchar_t>(str, pattern, lowerCase);
  93. }
  94. bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase)
  95. {
  96. return endsWithInternal<char>(str, pattern, lowerCase);
  97. }
  98. bool StringUtil::endsWith(const WString& str, const WString& pattern, bool lowerCase)
  99. {
  100. return endsWithInternal<wchar_t>(str, pattern, lowerCase);
  101. }
  102. bool StringUtil::match(const String& str, const String& pattern, bool caseSensitive)
  103. {
  104. return matchInternal<char>(str, pattern, caseSensitive);
  105. }
  106. bool StringUtil::match(const WString& str, const WString& pattern, bool caseSensitive)
  107. {
  108. return matchInternal<wchar_t>(str, pattern, caseSensitive);
  109. }
  110. const String StringUtil::replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat)
  111. {
  112. return replaceAllInternal<char>(source, replaceWhat, replaceWithWhat);
  113. }
  114. const WString StringUtil::replaceAll(const WString& source, const WString& replaceWhat, const WString& replaceWithWhat)
  115. {
  116. return replaceAllInternal<wchar_t>(source, replaceWhat, replaceWithWhat);
  117. }
  118. /************************************************************************/
  119. /* VARIOUS TO STRING CONVERSIONS */
  120. /************************************************************************/
  121. WString toWString(const String& source)
  122. {
  123. return WString(source.begin(), source.end());
  124. }
  125. String toString(const WString& source)
  126. {
  127. return String(source.begin(), source.end());
  128. }
  129. String toString(float val, unsigned short precision,
  130. unsigned short width, char fill, std::ios::fmtflags flags)
  131. {
  132. StringStream stream;
  133. stream.precision(precision);
  134. stream.width(width);
  135. stream.fill(fill);
  136. if (flags)
  137. stream.setf(flags);
  138. stream << val;
  139. return stream.str();
  140. }
  141. String toString(Radian val, unsigned short precision,
  142. unsigned short width, char fill, std::ios::fmtflags flags)
  143. {
  144. return toString(val.valueAngleUnits(), precision, width, fill, flags);
  145. }
  146. String toString(Degree val, unsigned short precision,
  147. unsigned short width, char fill, std::ios::fmtflags flags)
  148. {
  149. return toString(val.valueAngleUnits(), precision, width, fill, flags);
  150. }
  151. String toString(int val,
  152. unsigned short width, char fill, std::ios::fmtflags flags)
  153. {
  154. StringStream stream;
  155. stream.width(width);
  156. stream.fill(fill);
  157. if (flags)
  158. stream.setf(flags);
  159. stream << val;
  160. return stream.str();
  161. }
  162. #if CM_ARCH_TYPE == CM_ARCHITECTURE_64 || CM_PLATFORM == CM_PLATFORM_APPLE
  163. String toString(unsigned int val, unsigned short width, char fill, std::ios::fmtflags flags)
  164. {
  165. StringStream stream;
  166. stream.width(width);
  167. stream.fill(fill);
  168. if (flags)
  169. stream.setf(flags);
  170. stream << val;
  171. return stream.str();
  172. }
  173. String toString(size_t val, unsigned short width, char fill, std::ios::fmtflags flags)
  174. {
  175. StringStream stream;
  176. stream.width(width);
  177. stream.fill(fill);
  178. if (flags)
  179. stream.setf(flags);
  180. stream << val;
  181. return stream.str();
  182. }
  183. #if CM_COMPILER == CM_COMPILER_MSVC
  184. String toString(unsigned long val, unsigned short width, char fill, std::ios::fmtflags flags)
  185. {
  186. StringStream stream;
  187. stream.width(width);
  188. stream.fill(fill);
  189. if (flags)
  190. stream.setf(flags);
  191. stream << val;
  192. return stream.str();
  193. }
  194. #endif
  195. #else
  196. String toString(size_t val, unsigned short width, char fill, std::ios::fmtflags flags)
  197. {
  198. StringStream stream;
  199. stream.width(width);
  200. stream.fill(fill);
  201. if (flags)
  202. stream.setf(flags);
  203. stream << val;
  204. return stream.str();
  205. }
  206. String toString(unsigned long val, unsigned short width, char fill, std::ios::fmtflags flags)
  207. {
  208. StringStream stream;
  209. stream.width(width);
  210. stream.fill(fill);
  211. if (flags)
  212. stream.setf(flags);
  213. stream << val;
  214. return stream.str();
  215. }
  216. String toString(unsigned long long int val, unsigned short width, char fill, std::ios::fmtflags flags)
  217. {
  218. StringStream stream;
  219. stream.width(width);
  220. stream.fill(fill);
  221. if (flags)
  222. stream.setf(flags);
  223. stream << val;
  224. return stream.str();
  225. }
  226. #endif
  227. String toString(long val,
  228. unsigned short width, char fill, std::ios::fmtflags flags)
  229. {
  230. StringStream stream;
  231. stream.width(width);
  232. stream.fill(fill);
  233. if (flags)
  234. stream.setf(flags);
  235. stream << val;
  236. return stream.str();
  237. }
  238. String toString(const Vector2& val)
  239. {
  240. StringStream stream;
  241. stream << val.x << " " << val.y;
  242. return stream.str();
  243. }
  244. String toString(const Vector3& val)
  245. {
  246. StringStream stream;
  247. stream << val.x << " " << val.y << " " << val.z;
  248. return stream.str();
  249. }
  250. String toString(const Vector4& val)
  251. {
  252. StringStream stream;
  253. stream << val.x << " " << val.y << " " << val.z << " " << val.w;
  254. return stream.str();
  255. }
  256. String toString(const Matrix3& val)
  257. {
  258. StringStream stream;
  259. stream << val[0][0] << " "
  260. << val[0][1] << " "
  261. << val[0][2] << " "
  262. << val[1][0] << " "
  263. << val[1][1] << " "
  264. << val[1][2] << " "
  265. << val[2][0] << " "
  266. << val[2][1] << " "
  267. << val[2][2];
  268. return stream.str();
  269. }
  270. String toString(bool val, bool yesNo)
  271. {
  272. if (val)
  273. {
  274. if (yesNo)
  275. {
  276. return "yes";
  277. }
  278. else
  279. {
  280. return "true";
  281. }
  282. }
  283. else
  284. if (yesNo)
  285. {
  286. return "no";
  287. }
  288. else
  289. {
  290. return "false";
  291. }
  292. }
  293. String toString(const Matrix4& val)
  294. {
  295. StringStream stream;
  296. stream << val[0][0] << " "
  297. << val[0][1] << " "
  298. << val[0][2] << " "
  299. << val[0][3] << " "
  300. << val[1][0] << " "
  301. << val[1][1] << " "
  302. << val[1][2] << " "
  303. << val[1][3] << " "
  304. << val[2][0] << " "
  305. << val[2][1] << " "
  306. << val[2][2] << " "
  307. << val[2][3] << " "
  308. << val[3][0] << " "
  309. << val[3][1] << " "
  310. << val[3][2] << " "
  311. << val[3][3];
  312. return stream.str();
  313. }
  314. String toString(const Quaternion& val)
  315. {
  316. StringStream stream;
  317. stream << val.w << " " << val.x << " " << val.y << " " << val.z;
  318. return stream.str();
  319. }
  320. String toString(const Color& val)
  321. {
  322. StringStream stream;
  323. stream << val.r << " " << val.g << " " << val.b << " " << val.a;
  324. return stream.str();
  325. }
  326. String toString(const vector<CamelotFramework::String>::type& val)
  327. {
  328. StringStream stream;
  329. vector<CamelotFramework::String>::type::const_iterator i, iend, ibegin;
  330. ibegin = val.begin();
  331. iend = val.end();
  332. for (i = ibegin; i != iend; ++i)
  333. {
  334. if (i != ibegin)
  335. stream << " ";
  336. stream << *i;
  337. }
  338. return stream.str();
  339. }
  340. float parseFloat(const String& val, float defaultValue)
  341. {
  342. // Use istringstream for direct correspondence with toString
  343. StringStream str(val);
  344. float ret = defaultValue;
  345. str >> ret;
  346. return ret;
  347. }
  348. int parseInt(const String& val, int defaultValue)
  349. {
  350. // Use istringstream for direct correspondence with toString
  351. StringStream str(val);
  352. int ret = defaultValue;
  353. str >> ret;
  354. return ret;
  355. }
  356. unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue)
  357. {
  358. // Use istringstream for direct correspondence with toString
  359. StringStream str(val);
  360. unsigned int ret = defaultValue;
  361. str >> ret;
  362. return ret;
  363. }
  364. long parseLong(const String& val, long defaultValue)
  365. {
  366. // Use istringstream for direct correspondence with toString
  367. StringStream str(val);
  368. long ret = defaultValue;
  369. str >> ret;
  370. return ret;
  371. }
  372. unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue)
  373. {
  374. // Use istringstream for direct correspondence with toString
  375. StringStream str(val);
  376. unsigned long ret = defaultValue;
  377. str >> ret;
  378. return ret;
  379. }
  380. bool parseBool(const String& val, bool defaultValue)
  381. {
  382. if ((StringUtil::startsWith(val, "true") || StringUtil::startsWith(val, "yes")
  383. || StringUtil::startsWith(val, "1")))
  384. return true;
  385. else if ((StringUtil::startsWith(val, "false") || StringUtil::startsWith(val, "no")
  386. || StringUtil::startsWith(val, "0")))
  387. return false;
  388. else
  389. return defaultValue;
  390. }
  391. bool isNumber(const String& val)
  392. {
  393. StringStream str(val);
  394. float tst;
  395. str >> tst;
  396. return !str.fail() && str.eof();
  397. }
  398. void __string_throwDataOverflowException()
  399. {
  400. CM_EXCEPT(InternalErrorException, "Data overflow! Size doesn't fit into 32 bits.");
  401. }
  402. }