CmString.cpp 21 KB

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