OgreStringConverter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 "OgreStringConverter.h"
  25. namespace Ogre {
  26. //-----------------------------------------------------------------------
  27. String StringConverter::toString(Real val, unsigned short precision,
  28. unsigned short width, char fill, std::ios::fmtflags flags)
  29. {
  30. stringstream stream;
  31. stream.precision(precision);
  32. stream.width(width);
  33. stream.fill(fill);
  34. if (flags)
  35. stream.setf(flags);
  36. stream << val;
  37. return stream.str();
  38. }
  39. //-----------------------------------------------------------------------
  40. String StringConverter::toString(int val,
  41. unsigned short width, char fill, std::ios::fmtflags flags)
  42. {
  43. stringstream stream;
  44. stream.width(width);
  45. stream.fill(fill);
  46. if (flags)
  47. stream.setf(flags);
  48. stream << val;
  49. return stream.str();
  50. }
  51. //-----------------------------------------------------------------------
  52. #if CM_ARCH_TYPE == CM_ARCHITECTURE_64 || CM_PLATFORM == CM_PLATFORM_APPLE || CM_PLATFORM == OGRE_PLATFORM_IPHONE
  53. String StringConverter::toString(unsigned int val,
  54. unsigned short width, char fill, std::ios::fmtflags flags)
  55. {
  56. stringstream stream;
  57. stream.width(width);
  58. stream.fill(fill);
  59. if (flags)
  60. stream.setf(flags);
  61. stream << val;
  62. return stream.str();
  63. }
  64. //-----------------------------------------------------------------------
  65. String StringConverter::toString(size_t val,
  66. unsigned short width, char fill, std::ios::fmtflags flags)
  67. {
  68. stringstream stream;
  69. stream.width(width);
  70. stream.fill(fill);
  71. if (flags)
  72. stream.setf(flags);
  73. stream << val;
  74. return stream.str();
  75. }
  76. #if CM_COMPILER == CM_COMPILER_MSVC
  77. //-----------------------------------------------------------------------
  78. String StringConverter::toString(unsigned long val,
  79. unsigned short width, char fill, std::ios::fmtflags flags)
  80. {
  81. stringstream stream;
  82. stream.width(width);
  83. stream.fill(fill);
  84. if (flags)
  85. stream.setf(flags);
  86. stream << val;
  87. return stream.str();
  88. }
  89. #endif
  90. //-----------------------------------------------------------------------
  91. #else
  92. String StringConverter::toString(size_t val,
  93. unsigned short width, char fill, std::ios::fmtflags flags)
  94. {
  95. stringstream stream;
  96. stream.width(width);
  97. stream.fill(fill);
  98. if (flags)
  99. stream.setf(flags);
  100. stream << val;
  101. return stream.str();
  102. }
  103. //-----------------------------------------------------------------------
  104. String StringConverter::toString(unsigned long val,
  105. unsigned short width, char fill, std::ios::fmtflags flags)
  106. {
  107. stringstream stream;
  108. stream.width(width);
  109. stream.fill(fill);
  110. if (flags)
  111. stream.setf(flags);
  112. stream << val;
  113. return stream.str();
  114. }
  115. //-----------------------------------------------------------------------
  116. #endif
  117. String StringConverter::toString(long val,
  118. unsigned short width, char fill, std::ios::fmtflags flags)
  119. {
  120. stringstream stream;
  121. stream.width(width);
  122. stream.fill(fill);
  123. if (flags)
  124. stream.setf(flags);
  125. stream << val;
  126. return stream.str();
  127. }
  128. //-----------------------------------------------------------------------
  129. String StringConverter::toString(const Vector2& val)
  130. {
  131. stringstream stream;
  132. stream << val.x << " " << val.y;
  133. return stream.str();
  134. }
  135. //-----------------------------------------------------------------------
  136. String StringConverter::toString(const Vector3& val)
  137. {
  138. stringstream stream;
  139. stream << val.x << " " << val.y << " " << val.z;
  140. return stream.str();
  141. }
  142. //-----------------------------------------------------------------------
  143. String StringConverter::toString(const Vector4& val)
  144. {
  145. stringstream stream;
  146. stream << val.x << " " << val.y << " " << val.z << " " << val.w;
  147. return stream.str();
  148. }
  149. //-----------------------------------------------------------------------
  150. String StringConverter::toString(const Matrix3& val)
  151. {
  152. stringstream stream;
  153. stream << val[0][0] << " "
  154. << val[0][1] << " "
  155. << val[0][2] << " "
  156. << val[1][0] << " "
  157. << val[1][1] << " "
  158. << val[1][2] << " "
  159. << val[2][0] << " "
  160. << val[2][1] << " "
  161. << val[2][2];
  162. return stream.str();
  163. }
  164. //-----------------------------------------------------------------------
  165. String StringConverter::toString(bool val, bool yesNo)
  166. {
  167. if (val)
  168. {
  169. if (yesNo)
  170. {
  171. return "yes";
  172. }
  173. else
  174. {
  175. return "true";
  176. }
  177. }
  178. else
  179. if (yesNo)
  180. {
  181. return "no";
  182. }
  183. else
  184. {
  185. return "false";
  186. }
  187. }
  188. //-----------------------------------------------------------------------
  189. String StringConverter::toString(const Matrix4& val)
  190. {
  191. stringstream stream;
  192. stream << val[0][0] << " "
  193. << val[0][1] << " "
  194. << val[0][2] << " "
  195. << val[0][3] << " "
  196. << val[1][0] << " "
  197. << val[1][1] << " "
  198. << val[1][2] << " "
  199. << val[1][3] << " "
  200. << val[2][0] << " "
  201. << val[2][1] << " "
  202. << val[2][2] << " "
  203. << val[2][3] << " "
  204. << val[3][0] << " "
  205. << val[3][1] << " "
  206. << val[3][2] << " "
  207. << val[3][3];
  208. return stream.str();
  209. }
  210. //-----------------------------------------------------------------------
  211. String StringConverter::toString(const Quaternion& val)
  212. {
  213. stringstream stream;
  214. stream << val.w << " " << val.x << " " << val.y << " " << val.z;
  215. return stream.str();
  216. }
  217. //-----------------------------------------------------------------------
  218. String StringConverter::toString(const ColourValue& val)
  219. {
  220. stringstream stream;
  221. stream << val.r << " " << val.g << " " << val.b << " " << val.a;
  222. return stream.str();
  223. }
  224. //-----------------------------------------------------------------------
  225. String StringConverter::toString(const std::vector<Ogre::String>& val)
  226. {
  227. stringstream stream;
  228. std::vector<Ogre::String>::const_iterator i, iend, ibegin;
  229. ibegin = val.begin();
  230. iend = val.end();
  231. for (i = ibegin; i != iend; ++i)
  232. {
  233. if (i != ibegin)
  234. stream << " ";
  235. stream << *i;
  236. }
  237. return stream.str();
  238. }
  239. //-----------------------------------------------------------------------
  240. Real StringConverter::parseReal(const String& val, Real defaultValue)
  241. {
  242. // Use istringstream for direct correspondence with toString
  243. StringStream str(val);
  244. Real ret = defaultValue;
  245. str >> ret;
  246. return ret;
  247. }
  248. //-----------------------------------------------------------------------
  249. int StringConverter::parseInt(const String& val, int defaultValue)
  250. {
  251. // Use istringstream for direct correspondence with toString
  252. StringStream str(val);
  253. int ret = defaultValue;
  254. str >> ret;
  255. return ret;
  256. }
  257. //-----------------------------------------------------------------------
  258. unsigned int StringConverter::parseUnsignedInt(const String& val, unsigned int defaultValue)
  259. {
  260. // Use istringstream for direct correspondence with toString
  261. StringStream str(val);
  262. unsigned int ret = defaultValue;
  263. str >> ret;
  264. return ret;
  265. }
  266. //-----------------------------------------------------------------------
  267. long StringConverter::parseLong(const String& val, long defaultValue)
  268. {
  269. // Use istringstream for direct correspondence with toString
  270. StringStream str(val);
  271. long ret = defaultValue;
  272. str >> ret;
  273. return ret;
  274. }
  275. //-----------------------------------------------------------------------
  276. unsigned long StringConverter::parseUnsignedLong(const String& val, unsigned long defaultValue)
  277. {
  278. // Use istringstream for direct correspondence with toString
  279. StringStream str(val);
  280. unsigned long ret = defaultValue;
  281. str >> ret;
  282. return ret;
  283. }
  284. //-----------------------------------------------------------------------
  285. bool StringConverter::parseBool(const String& val, bool defaultValue)
  286. {
  287. if ((StringUtil::startsWith(val, "true") || StringUtil::startsWith(val, "yes")
  288. || StringUtil::startsWith(val, "1")))
  289. return true;
  290. else if ((StringUtil::startsWith(val, "false") || StringUtil::startsWith(val, "no")
  291. || StringUtil::startsWith(val, "0")))
  292. return false;
  293. else
  294. return defaultValue;
  295. }
  296. //-----------------------------------------------------------------------
  297. Vector2 StringConverter::parseVector2(const String& val, const Vector2& defaultValue)
  298. {
  299. // Split on space
  300. vector<String>::type vec = StringUtil::split(val);
  301. if (vec.size() != 2)
  302. {
  303. return defaultValue;
  304. }
  305. else
  306. {
  307. return Vector2(parseReal(vec[0]),parseReal(vec[1]));
  308. }
  309. }
  310. //-----------------------------------------------------------------------
  311. Vector3 StringConverter::parseVector3(const String& val, const Vector3& defaultValue)
  312. {
  313. // Split on space
  314. vector<String>::type vec = StringUtil::split(val);
  315. if (vec.size() != 3)
  316. {
  317. return defaultValue;
  318. }
  319. else
  320. {
  321. return Vector3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]));
  322. }
  323. }
  324. //-----------------------------------------------------------------------
  325. Vector4 StringConverter::parseVector4(const String& val, const Vector4& defaultValue)
  326. {
  327. // Split on space
  328. vector<String>::type vec = StringUtil::split(val);
  329. if (vec.size() != 4)
  330. {
  331. return defaultValue;
  332. }
  333. else
  334. {
  335. return Vector4(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]),parseReal(vec[3]));
  336. }
  337. }
  338. //-----------------------------------------------------------------------
  339. Matrix3 StringConverter::parseMatrix3(const String& val, const Matrix3& defaultValue)
  340. {
  341. // Split on space
  342. vector<String>::type vec = StringUtil::split(val);
  343. if (vec.size() != 9)
  344. {
  345. return defaultValue;
  346. }
  347. else
  348. {
  349. return Matrix3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]),
  350. parseReal(vec[3]),parseReal(vec[4]),parseReal(vec[5]),
  351. parseReal(vec[6]),parseReal(vec[7]),parseReal(vec[8]));
  352. }
  353. }
  354. //-----------------------------------------------------------------------
  355. Matrix4 StringConverter::parseMatrix4(const String& val, const Matrix4& defaultValue)
  356. {
  357. // Split on space
  358. vector<String>::type vec = StringUtil::split(val);
  359. if (vec.size() != 16)
  360. {
  361. return defaultValue;
  362. }
  363. else
  364. {
  365. return Matrix4(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]),
  366. parseReal(vec[4]),parseReal(vec[5]), parseReal(vec[6]), parseReal(vec[7]),
  367. parseReal(vec[8]),parseReal(vec[9]), parseReal(vec[10]), parseReal(vec[11]),
  368. parseReal(vec[12]),parseReal(vec[13]), parseReal(vec[14]), parseReal(vec[15]));
  369. }
  370. }
  371. //-----------------------------------------------------------------------
  372. Quaternion StringConverter::parseQuaternion(const String& val, const Quaternion& defaultValue)
  373. {
  374. // Split on space
  375. vector<String>::type vec = StringUtil::split(val);
  376. if (vec.size() != 4)
  377. {
  378. return defaultValue;
  379. }
  380. else
  381. {
  382. return Quaternion(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
  383. }
  384. }
  385. //-----------------------------------------------------------------------
  386. ColourValue StringConverter::parseColourValue(const String& val, const ColourValue& defaultValue)
  387. {
  388. // Split on space
  389. vector<String>::type vec = StringUtil::split(val);
  390. if (vec.size() == 4)
  391. {
  392. return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
  393. }
  394. else if (vec.size() == 3)
  395. {
  396. return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), 1.0f);
  397. }
  398. else
  399. {
  400. return defaultValue;
  401. }
  402. }
  403. //-----------------------------------------------------------------------
  404. std::vector<Ogre::String> StringConverter::parseStringVector(const String& val)
  405. {
  406. return StringUtil::split(val);
  407. }
  408. //-----------------------------------------------------------------------
  409. bool StringConverter::isNumber(const String& val)
  410. {
  411. StringStream str(val);
  412. float tst;
  413. str >> tst;
  414. return !str.fail() && str.eof();
  415. }
  416. }