1
0

CCNS.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "base/CCNS.h"
  22. #include <string>
  23. #include <vector>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "base/ccUtils.h"
  27. using namespace std;
  28. NS_CC_BEGIN
  29. typedef std::vector<std::string> strArray;
  30. // string toolkit
  31. static inline void split(const std::string& src, const std::string& token, strArray& vect)
  32. {
  33. size_t nend = 0;
  34. size_t nbegin = 0;
  35. size_t tokenSize = token.size();
  36. while(nend != std::string::npos)
  37. {
  38. nend = src.find(token, nbegin);
  39. if(nend == std::string::npos)
  40. vect.push_back(src.substr(nbegin, src.length()-nbegin));
  41. else
  42. vect.push_back(src.substr(nbegin, nend-nbegin));
  43. nbegin = nend + tokenSize;
  44. }
  45. }
  46. // first, judge whether the form of the string like this: {x,y}
  47. // if the form is right,the string will be split into the parameter strs;
  48. // or the parameter strs will be empty.
  49. // if the form is right return true,else return false.
  50. static bool splitWithForm(const std::string& content, strArray& strs)
  51. {
  52. bool bRet = false;
  53. do
  54. {
  55. CC_BREAK_IF(content.empty());
  56. size_t nPosLeft = content.find('{');
  57. size_t nPosRight = content.find('}');
  58. // don't have '{' and '}'
  59. CC_BREAK_IF(nPosLeft == std::string::npos || nPosRight == std::string::npos);
  60. // '}' is before '{'
  61. CC_BREAK_IF(nPosLeft > nPosRight);
  62. const std::string pointStr = content.substr(nPosLeft + 1, nPosRight - nPosLeft - 1);
  63. // nothing between '{' and '}'
  64. CC_BREAK_IF(pointStr.length() == 0);
  65. size_t nPos1 = pointStr.find('{');
  66. size_t nPos2 = pointStr.find('}');
  67. // contain '{' or '}'
  68. CC_BREAK_IF(nPos1 != std::string::npos || nPos2 != std::string::npos);
  69. split(pointStr, ",", strs);
  70. if (strs.size() != 2 || strs[0].length() == 0 || strs[1].length() == 0)
  71. {
  72. strs.clear();
  73. break;
  74. }
  75. bRet = true;
  76. } while (0);
  77. return bRet;
  78. }
  79. // implement the functions
  80. Rect RectFromString(const std::string& str)
  81. {
  82. Rect result = Rect::ZERO;
  83. do
  84. {
  85. CC_BREAK_IF(str.empty());
  86. std::string content = str;
  87. // find the first '{' and the third '}'
  88. size_t nPosLeft = content.find('{');
  89. size_t nPosRight = content.find('}');
  90. for (int i = 1; i < 3; ++i)
  91. {
  92. if (nPosRight == std::string::npos)
  93. {
  94. break;
  95. }
  96. nPosRight = content.find('}', nPosRight + 1);
  97. }
  98. CC_BREAK_IF(nPosLeft == std::string::npos || nPosRight == std::string::npos);
  99. content = content.substr(nPosLeft + 1, nPosRight - nPosLeft - 1);
  100. size_t nPointEnd = content.find('}');
  101. CC_BREAK_IF(nPointEnd == std::string::npos);
  102. nPointEnd = content.find(',', nPointEnd);
  103. CC_BREAK_IF(nPointEnd == std::string::npos);
  104. // get the point string and size string
  105. const std::string pointStr = content.substr(0, nPointEnd);
  106. const std::string sizeStr = content.substr(nPointEnd + 1, content.length() - nPointEnd);
  107. // split the string with ','
  108. strArray pointInfo;
  109. CC_BREAK_IF(!splitWithForm(pointStr, pointInfo));
  110. strArray sizeInfo;
  111. CC_BREAK_IF(!splitWithForm(sizeStr, sizeInfo));
  112. float x = (float) utils::atof(pointInfo[0].c_str());
  113. float y = (float) utils::atof(pointInfo[1].c_str());
  114. float width = (float) utils::atof(sizeInfo[0].c_str());
  115. float height = (float) utils::atof(sizeInfo[1].c_str());
  116. result = Rect(x, y, width, height);
  117. } while (0);
  118. return result;
  119. }
  120. Vec2 PointFromString(const std::string& str)
  121. {
  122. Vec2 ret;
  123. do
  124. {
  125. strArray strs;
  126. CC_BREAK_IF(!splitWithForm(str, strs));
  127. float x = (float) utils::atof(strs[0].c_str());
  128. float y = (float) utils::atof(strs[1].c_str());
  129. ret.set(x, y);
  130. } while (0);
  131. return ret;
  132. }
  133. Size SizeFromString(const std::string& pszContent)
  134. {
  135. Size ret = Size::ZERO;
  136. do
  137. {
  138. strArray strs;
  139. CC_BREAK_IF(!splitWithForm(pszContent, strs));
  140. float width = (float) utils::atof(strs[0].c_str());
  141. float height = (float) utils::atof(strs[1].c_str());
  142. ret = Size(width, height);
  143. } while (0);
  144. return ret;
  145. }
  146. NS_CC_END