CmString.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. //-----------------------------------------------------------------------
  38. void StringUtil::trim(String& str, bool left, bool right)
  39. {
  40. /*
  41. size_t lspaces, rspaces, len = length(), i;
  42. lspaces = rspaces = 0;
  43. if( left )
  44. {
  45. // Find spaces / tabs on the left
  46. for( i = 0;
  47. i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
  48. ++lspaces, ++i );
  49. }
  50. if( right && lspaces < len )
  51. {
  52. // Find spaces / tabs on the right
  53. for( i = len - 1;
  54. i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
  55. rspaces++, i-- );
  56. }
  57. *this = substr(lspaces, len-lspaces-rspaces);
  58. */
  59. static const String delims = " \t\r";
  60. if(right)
  61. str.erase(str.find_last_not_of(delims)+1); // trim right
  62. if(left)
  63. str.erase(0, str.find_first_not_of(delims)); // trim left
  64. }
  65. //-----------------------------------------------------------------------
  66. vector<CamelotFramework::String>::type StringUtil::split( const String& str, const String& delims, unsigned int maxSplits)
  67. {
  68. vector<CamelotFramework::String>::type ret;
  69. // Pre-allocate some space for performance
  70. ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
  71. unsigned int numSplits = 0;
  72. // Use STL methods
  73. size_t start, pos;
  74. start = 0;
  75. do
  76. {
  77. pos = str.find_first_of(delims, start);
  78. if (pos == start)
  79. {
  80. // Do nothing
  81. start = pos + 1;
  82. }
  83. else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
  84. {
  85. // Copy the rest of the string
  86. ret.push_back( str.substr(start) );
  87. break;
  88. }
  89. else
  90. {
  91. // Copy up to delimiter
  92. ret.push_back( str.substr(start, pos - start) );
  93. start = pos + 1;
  94. }
  95. // parse up to next real data
  96. start = str.find_first_not_of(delims, start);
  97. ++numSplits;
  98. } while (pos != String::npos);
  99. return ret;
  100. }
  101. //-----------------------------------------------------------------------
  102. vector<CamelotFramework::String>::type StringUtil::tokenise( const String& str, const String& singleDelims, const String& doubleDelims, unsigned int maxSplits)
  103. {
  104. vector<CamelotFramework::String>::type ret;
  105. // Pre-allocate some space for performance
  106. ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
  107. unsigned int numSplits = 0;
  108. String delims = singleDelims + doubleDelims;
  109. // Use STL methods
  110. size_t start, pos;
  111. char curDoubleDelim = 0;
  112. start = 0;
  113. do
  114. {
  115. if (curDoubleDelim != 0)
  116. {
  117. pos = str.find(curDoubleDelim, start);
  118. }
  119. else
  120. {
  121. pos = str.find_first_of(delims, start);
  122. }
  123. if (pos == start)
  124. {
  125. char curDelim = str.at(pos);
  126. if (doubleDelims.find_first_of(curDelim) != String::npos)
  127. {
  128. curDoubleDelim = curDelim;
  129. }
  130. // Do nothing
  131. start = pos + 1;
  132. }
  133. else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
  134. {
  135. if (curDoubleDelim != 0)
  136. {
  137. //Missing closer. Warn or throw exception?
  138. }
  139. // Copy the rest of the string
  140. ret.push_back( str.substr(start) );
  141. break;
  142. }
  143. else
  144. {
  145. if (curDoubleDelim != 0)
  146. {
  147. curDoubleDelim = 0;
  148. }
  149. // Copy up to delimiter
  150. ret.push_back( str.substr(start, pos - start) );
  151. start = pos + 1;
  152. }
  153. if (curDoubleDelim == 0)
  154. {
  155. // parse up to next real data
  156. start = str.find_first_not_of(singleDelims, start);
  157. }
  158. ++numSplits;
  159. } while (pos != String::npos);
  160. return ret;
  161. }
  162. //-----------------------------------------------------------------------
  163. void StringUtil::toLowerCase(String& str)
  164. {
  165. std::transform(
  166. str.begin(),
  167. str.end(),
  168. str.begin(),
  169. tolower);
  170. }
  171. //-----------------------------------------------------------------------
  172. void StringUtil::toUpperCase(String& str)
  173. {
  174. std::transform(
  175. str.begin(),
  176. str.end(),
  177. str.begin(),
  178. toupper);
  179. }
  180. //-----------------------------------------------------------------------
  181. bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase)
  182. {
  183. size_t thisLen = str.length();
  184. size_t patternLen = pattern.length();
  185. if (thisLen < patternLen || patternLen == 0)
  186. return false;
  187. String startOfThis = str.substr(0, patternLen);
  188. if (lowerCase)
  189. StringUtil::toLowerCase(startOfThis);
  190. return (startOfThis == pattern);
  191. }
  192. //-----------------------------------------------------------------------
  193. bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase)
  194. {
  195. size_t thisLen = str.length();
  196. size_t patternLen = pattern.length();
  197. if (thisLen < patternLen || patternLen == 0)
  198. return false;
  199. String endOfThis = str.substr(thisLen - patternLen, patternLen);
  200. if (lowerCase)
  201. StringUtil::toLowerCase(endOfThis);
  202. return (endOfThis == pattern);
  203. }
  204. //-----------------------------------------------------------------------
  205. String StringUtil::standardisePath(const String& init)
  206. {
  207. String path = init;
  208. std::replace( path.begin(), path.end(), '\\', '/' );
  209. if( path[path.length() - 1] != '/' )
  210. path += '/';
  211. return path;
  212. }
  213. //-----------------------------------------------------------------------
  214. void StringUtil::splitFilename(const String& qualifiedName,
  215. String& outBasename, String& outPath)
  216. {
  217. String path = qualifiedName;
  218. // Replace \ with / first
  219. std::replace( path.begin(), path.end(), '\\', '/' );
  220. // split based on final /
  221. size_t i = path.find_last_of('/');
  222. if (i == String::npos)
  223. {
  224. outPath.clear();
  225. outBasename = qualifiedName;
  226. }
  227. else
  228. {
  229. outBasename = path.substr(i+1, path.size() - i - 1);
  230. outPath = path.substr(0, i+1);
  231. }
  232. }
  233. //-----------------------------------------------------------------------
  234. void StringUtil::splitBaseFilename(const CamelotFramework::String& fullName,
  235. CamelotFramework::String& outBasename, CamelotFramework::String& outExtention)
  236. {
  237. size_t i = fullName.find_last_of(".");
  238. if (i == CamelotFramework::String::npos)
  239. {
  240. outExtention.clear();
  241. outBasename = fullName;
  242. }
  243. else
  244. {
  245. outExtention = fullName.substr(i+1);
  246. outBasename = fullName.substr(0, i);
  247. }
  248. }
  249. // ----------------------------------------------------------------------------------------------------------------------------------------------
  250. void StringUtil::splitFullFilename( const CamelotFramework::String& qualifiedName,
  251. CamelotFramework::String& outBasename, CamelotFramework::String& outExtention, CamelotFramework::String& outPath )
  252. {
  253. CamelotFramework::String fullName;
  254. splitFilename( qualifiedName, fullName, outPath );
  255. splitBaseFilename( fullName, outBasename, outExtention );
  256. }
  257. //-----------------------------------------------------------------------
  258. bool StringUtil::match(const String& str, const String& pattern, bool caseSensitive)
  259. {
  260. String tmpStr = str;
  261. String tmpPattern = pattern;
  262. if (!caseSensitive)
  263. {
  264. StringUtil::toLowerCase(tmpStr);
  265. StringUtil::toLowerCase(tmpPattern);
  266. }
  267. String::const_iterator strIt = tmpStr.begin();
  268. String::const_iterator patIt = tmpPattern.begin();
  269. String::const_iterator lastWildCardIt = tmpPattern.end();
  270. while (strIt != tmpStr.end() && patIt != tmpPattern.end())
  271. {
  272. if (*patIt == '*')
  273. {
  274. lastWildCardIt = patIt;
  275. // Skip over looking for next character
  276. ++patIt;
  277. if (patIt == tmpPattern.end())
  278. {
  279. // Skip right to the end since * matches the entire rest of the string
  280. strIt = tmpStr.end();
  281. }
  282. else
  283. {
  284. // scan until we find next pattern character
  285. while(strIt != tmpStr.end() && *strIt != *patIt)
  286. ++strIt;
  287. }
  288. }
  289. else
  290. {
  291. if (*patIt != *strIt)
  292. {
  293. if (lastWildCardIt != tmpPattern.end())
  294. {
  295. // The last wildcard can match this incorrect sequence
  296. // rewind pattern to wildcard and keep searching
  297. patIt = lastWildCardIt;
  298. lastWildCardIt = tmpPattern.end();
  299. }
  300. else
  301. {
  302. // no wildwards left
  303. return false;
  304. }
  305. }
  306. else
  307. {
  308. ++patIt;
  309. ++strIt;
  310. }
  311. }
  312. }
  313. // If we reached the end of both the pattern and the string, we succeeded
  314. if (patIt == tmpPattern.end() && strIt == tmpStr.end())
  315. {
  316. return true;
  317. }
  318. else
  319. {
  320. return false;
  321. }
  322. }
  323. //-----------------------------------------------------------------------
  324. const String StringUtil::replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat)
  325. {
  326. String result = source;
  327. String::size_type pos = 0;
  328. while(1)
  329. {
  330. pos = result.find(replaceWhat,pos);
  331. if (pos == String::npos) break;
  332. result.replace(pos,replaceWhat.size(),replaceWithWhat);
  333. pos += replaceWithWhat.size();
  334. }
  335. return result;
  336. }
  337. //-----------------------------------------------------------------------
  338. String toString(float val, unsigned short precision,
  339. unsigned short width, char fill, std::ios::fmtflags flags)
  340. {
  341. stringstream stream;
  342. stream.precision(precision);
  343. stream.width(width);
  344. stream.fill(fill);
  345. if (flags)
  346. stream.setf(flags);
  347. stream << val;
  348. return stream.str();
  349. }
  350. //-----------------------------------------------------------------------
  351. String toString(Radian val, unsigned short precision,
  352. unsigned short width, char fill, std::ios::fmtflags flags)
  353. {
  354. return toString(val.valueAngleUnits(), precision, width, fill, flags);
  355. }
  356. //-----------------------------------------------------------------------
  357. String toString(Degree val, unsigned short precision,
  358. unsigned short width, char fill, std::ios::fmtflags flags)
  359. {
  360. return toString(val.valueAngleUnits(), precision, width, fill, flags);
  361. }
  362. //-----------------------------------------------------------------------
  363. String toString(int val,
  364. unsigned short width, char fill, std::ios::fmtflags flags)
  365. {
  366. stringstream stream;
  367. stream.width(width);
  368. stream.fill(fill);
  369. if (flags)
  370. stream.setf(flags);
  371. stream << val;
  372. return stream.str();
  373. }
  374. //-----------------------------------------------------------------------
  375. #if CM_ARCH_TYPE == CM_ARCHITECTURE_64 || CM_PLATFORM == CM_PLATFORM_APPLE
  376. String toString(unsigned int val,
  377. unsigned short width, char fill, std::ios::fmtflags flags)
  378. {
  379. stringstream stream;
  380. stream.width(width);
  381. stream.fill(fill);
  382. if (flags)
  383. stream.setf(flags);
  384. stream << val;
  385. return stream.str();
  386. }
  387. //-----------------------------------------------------------------------
  388. String toString(size_t val,
  389. unsigned short width, char fill, std::ios::fmtflags flags)
  390. {
  391. stringstream stream;
  392. stream.width(width);
  393. stream.fill(fill);
  394. if (flags)
  395. stream.setf(flags);
  396. stream << val;
  397. return stream.str();
  398. }
  399. #if CM_COMPILER == CM_COMPILER_MSVC
  400. //-----------------------------------------------------------------------
  401. String toString(unsigned long val,
  402. unsigned short width, char fill, std::ios::fmtflags flags)
  403. {
  404. stringstream stream;
  405. stream.width(width);
  406. stream.fill(fill);
  407. if (flags)
  408. stream.setf(flags);
  409. stream << val;
  410. return stream.str();
  411. }
  412. #endif
  413. //-----------------------------------------------------------------------
  414. #else
  415. String toString(size_t val,
  416. unsigned short width, char fill, std::ios::fmtflags flags)
  417. {
  418. stringstream stream;
  419. stream.width(width);
  420. stream.fill(fill);
  421. if (flags)
  422. stream.setf(flags);
  423. stream << val;
  424. return stream.str();
  425. }
  426. //-----------------------------------------------------------------------
  427. String toString(unsigned long val,
  428. unsigned short width, char fill, std::ios::fmtflags flags)
  429. {
  430. stringstream stream;
  431. stream.width(width);
  432. stream.fill(fill);
  433. if (flags)
  434. stream.setf(flags);
  435. stream << val;
  436. return stream.str();
  437. }
  438. //-----------------------------------------------------------------------
  439. String toString(unsigned long long int val,
  440. unsigned short width, char fill, std::ios::fmtflags flags)
  441. {
  442. stringstream stream;
  443. stream.width(width);
  444. stream.fill(fill);
  445. if (flags)
  446. stream.setf(flags);
  447. stream << val;
  448. return stream.str();
  449. }
  450. //-----------------------------------------------------------------------
  451. #endif
  452. String toString(long val,
  453. unsigned short width, char fill, std::ios::fmtflags flags)
  454. {
  455. stringstream stream;
  456. stream.width(width);
  457. stream.fill(fill);
  458. if (flags)
  459. stream.setf(flags);
  460. stream << val;
  461. return stream.str();
  462. }
  463. //-----------------------------------------------------------------------
  464. String toString(const Vector2& val)
  465. {
  466. stringstream stream;
  467. stream << val.x << " " << val.y;
  468. return stream.str();
  469. }
  470. //-----------------------------------------------------------------------
  471. String toString(const Vector3& val)
  472. {
  473. stringstream stream;
  474. stream << val.x << " " << val.y << " " << val.z;
  475. return stream.str();
  476. }
  477. //-----------------------------------------------------------------------
  478. String toString(const Vector4& val)
  479. {
  480. stringstream stream;
  481. stream << val.x << " " << val.y << " " << val.z << " " << val.w;
  482. return stream.str();
  483. }
  484. //-----------------------------------------------------------------------
  485. String toString(const Matrix3& val)
  486. {
  487. stringstream stream;
  488. stream << val[0][0] << " "
  489. << val[0][1] << " "
  490. << val[0][2] << " "
  491. << val[1][0] << " "
  492. << val[1][1] << " "
  493. << val[1][2] << " "
  494. << val[2][0] << " "
  495. << val[2][1] << " "
  496. << val[2][2];
  497. return stream.str();
  498. }
  499. //-----------------------------------------------------------------------
  500. String toString(bool val, bool yesNo)
  501. {
  502. if (val)
  503. {
  504. if (yesNo)
  505. {
  506. return "yes";
  507. }
  508. else
  509. {
  510. return "true";
  511. }
  512. }
  513. else
  514. if (yesNo)
  515. {
  516. return "no";
  517. }
  518. else
  519. {
  520. return "false";
  521. }
  522. }
  523. //-----------------------------------------------------------------------
  524. String toString(const Matrix4& val)
  525. {
  526. stringstream stream;
  527. stream << val[0][0] << " "
  528. << val[0][1] << " "
  529. << val[0][2] << " "
  530. << val[0][3] << " "
  531. << val[1][0] << " "
  532. << val[1][1] << " "
  533. << val[1][2] << " "
  534. << val[1][3] << " "
  535. << val[2][0] << " "
  536. << val[2][1] << " "
  537. << val[2][2] << " "
  538. << val[2][3] << " "
  539. << val[3][0] << " "
  540. << val[3][1] << " "
  541. << val[3][2] << " "
  542. << val[3][3];
  543. return stream.str();
  544. }
  545. //-----------------------------------------------------------------------
  546. String toString(const Quaternion& val)
  547. {
  548. stringstream stream;
  549. stream << val.w << " " << val.x << " " << val.y << " " << val.z;
  550. return stream.str();
  551. }
  552. //-----------------------------------------------------------------------
  553. String toString(const Color& val)
  554. {
  555. stringstream stream;
  556. stream << val.r << " " << val.g << " " << val.b << " " << val.a;
  557. return stream.str();
  558. }
  559. //-----------------------------------------------------------------------
  560. String toString(const std::vector<CamelotFramework::String>& val)
  561. {
  562. stringstream stream;
  563. std::vector<CamelotFramework::String>::const_iterator i, iend, ibegin;
  564. ibegin = val.begin();
  565. iend = val.end();
  566. for (i = ibegin; i != iend; ++i)
  567. {
  568. if (i != ibegin)
  569. stream << " ";
  570. stream << *i;
  571. }
  572. return stream.str();
  573. }
  574. //-----------------------------------------------------------------------
  575. float parseFloat(const String& val, float defaultValue)
  576. {
  577. // Use istringstream for direct correspondence with toString
  578. StringStream str(val);
  579. float ret = defaultValue;
  580. str >> ret;
  581. return ret;
  582. }
  583. //-----------------------------------------------------------------------
  584. int parseInt(const String& val, int defaultValue)
  585. {
  586. // Use istringstream for direct correspondence with toString
  587. StringStream str(val);
  588. int ret = defaultValue;
  589. str >> ret;
  590. return ret;
  591. }
  592. //-----------------------------------------------------------------------
  593. unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue)
  594. {
  595. // Use istringstream for direct correspondence with toString
  596. StringStream str(val);
  597. unsigned int ret = defaultValue;
  598. str >> ret;
  599. return ret;
  600. }
  601. //-----------------------------------------------------------------------
  602. long parseLong(const String& val, long defaultValue)
  603. {
  604. // Use istringstream for direct correspondence with toString
  605. StringStream str(val);
  606. long ret = defaultValue;
  607. str >> ret;
  608. return ret;
  609. }
  610. //-----------------------------------------------------------------------
  611. unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue)
  612. {
  613. // Use istringstream for direct correspondence with toString
  614. StringStream str(val);
  615. unsigned long ret = defaultValue;
  616. str >> ret;
  617. return ret;
  618. }
  619. //-----------------------------------------------------------------------
  620. bool parseBool(const String& val, bool defaultValue)
  621. {
  622. if ((StringUtil::startsWith(val, "true") || StringUtil::startsWith(val, "yes")
  623. || StringUtil::startsWith(val, "1")))
  624. return true;
  625. else if ((StringUtil::startsWith(val, "false") || StringUtil::startsWith(val, "no")
  626. || StringUtil::startsWith(val, "0")))
  627. return false;
  628. else
  629. return defaultValue;
  630. }
  631. //-----------------------------------------------------------------------
  632. vector<CamelotFramework::String>::type parseStringVector(const String& val)
  633. {
  634. return StringUtil::split(val);
  635. }
  636. //-----------------------------------------------------------------------
  637. bool isNumber(const String& val)
  638. {
  639. StringStream str(val);
  640. float tst;
  641. str >> tst;
  642. return !str.fail() && str.eof();
  643. }
  644. //----------------------------------------------------------------------
  645. void __string_throwDataOverflowException()
  646. {
  647. CM_EXCEPT(InternalErrorException, "Data overflow! Size doesn't fit into 32 bits.");
  648. }
  649. }