CCString.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "deprecated/CCString.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "base/ccMacros.h"
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include "deprecated/CCArray.h"
  28. #include "base/ccUtils.h"
  29. NS_CC_BEGIN
  30. #define kMaxStringLen (1024*100)
  31. __String::__String()
  32. :_string("")
  33. {}
  34. __String::__String(const char * str)
  35. :_string(str)
  36. {}
  37. __String::__String(const std::string& str)
  38. :_string(str)
  39. {}
  40. __String::__String(const __String& str)
  41. :_string(str.getCString())
  42. {}
  43. __String::~__String()
  44. {
  45. CCLOGINFO("deallocing __String: %p", this);
  46. _string.clear();
  47. }
  48. __String& __String::operator= (const __String& other)
  49. {
  50. if (this != &other) {
  51. _string = other._string;
  52. }
  53. return *this;
  54. }
  55. bool __String::initWithFormatAndValist(const char* format, va_list ap)
  56. {
  57. bool bRet = false;
  58. char* pBuf = (char*)malloc(kMaxStringLen);
  59. if (pBuf != nullptr)
  60. {
  61. vsnprintf(pBuf, kMaxStringLen, format, ap);
  62. _string = pBuf;
  63. free(pBuf);
  64. bRet = true;
  65. }
  66. return bRet;
  67. }
  68. bool __String::initWithFormat(const char* format, ...)
  69. {
  70. _string.clear();
  71. va_list ap;
  72. va_start(ap, format);
  73. bool bRet = initWithFormatAndValist(format, ap);
  74. va_end(ap);
  75. return bRet;
  76. }
  77. int __String::intValue() const
  78. {
  79. if (length() == 0)
  80. {
  81. return 0;
  82. }
  83. return atoi(_string.c_str());
  84. }
  85. unsigned int __String::uintValue() const
  86. {
  87. if (length() == 0)
  88. {
  89. return 0;
  90. }
  91. return (unsigned int)atoi(_string.c_str());
  92. }
  93. float __String::floatValue() const
  94. {
  95. if (length() == 0)
  96. {
  97. return 0.0f;
  98. }
  99. return (float)utils::atof(_string.c_str());
  100. }
  101. double __String::doubleValue() const
  102. {
  103. if (length() == 0)
  104. {
  105. return 0.0;
  106. }
  107. return utils::atof(_string.c_str());
  108. }
  109. bool __String::boolValue() const
  110. {
  111. if (length() == 0)
  112. {
  113. return false;
  114. }
  115. if (0 == strcmp(_string.c_str(), "0") || 0 == strcmp(_string.c_str(), "false"))
  116. {
  117. return false;
  118. }
  119. return true;
  120. }
  121. const char* __String::getCString() const
  122. {
  123. return _string.c_str();
  124. }
  125. int __String::length() const
  126. {
  127. return static_cast<int>(_string.length());
  128. }
  129. int __String::compare(const char * pStr) const
  130. {
  131. return strcmp(getCString(), pStr);
  132. }
  133. void __String::append(const std::string& str)
  134. {
  135. _string.append(str);
  136. }
  137. void __String::appendWithFormat(const char* format, ...)
  138. {
  139. va_list ap;
  140. va_start(ap, format);
  141. char* pBuf = (char*)malloc(kMaxStringLen);
  142. if (pBuf != nullptr)
  143. {
  144. vsnprintf(pBuf, kMaxStringLen, format, ap);
  145. _string.append(pBuf);
  146. free(pBuf);
  147. }
  148. va_end(ap);
  149. }
  150. __Array* __String::componentsSeparatedByString(const char *delimiter)
  151. {
  152. __Array* result = __Array::create();
  153. std::string strTmp = _string;
  154. size_t cutAt;
  155. while( (cutAt = strTmp.find_first_of(delimiter)) != strTmp.npos )
  156. {
  157. if(cutAt > 0)
  158. {
  159. result->addObject(__String::create(strTmp.substr(0, cutAt)));
  160. }
  161. strTmp = strTmp.substr(cutAt + 1);
  162. }
  163. if(!strTmp.empty())
  164. {
  165. result->addObject(__String::create(strTmp));
  166. }
  167. return result;
  168. }
  169. bool __String::isEqual(const Ref* pObject)
  170. {
  171. bool bRet = false;
  172. const __String* pStr = dynamic_cast<const __String*>(pObject);
  173. if (pStr != nullptr)
  174. {
  175. if (0 == _string.compare(pStr->_string))
  176. {
  177. bRet = true;
  178. }
  179. }
  180. return bRet;
  181. }
  182. __String* __String::create(const std::string& str)
  183. {
  184. __String* ret = new (std::nothrow) __String(str);
  185. ret->autorelease();
  186. return ret;
  187. }
  188. __String* __String::createWithData(const unsigned char* data, size_t nLen)
  189. {
  190. __String* ret = nullptr;
  191. if (data != nullptr)
  192. {
  193. char* pStr = (char*)malloc(nLen+1);
  194. if (pStr != nullptr)
  195. {
  196. pStr[nLen] = '\0';
  197. if (nLen > 0)
  198. {
  199. memcpy(pStr, data, nLen);
  200. }
  201. ret = __String::create(pStr);
  202. free(pStr);
  203. }
  204. }
  205. return ret;
  206. }
  207. __String* __String::createWithFormat(const char* format, ...)
  208. {
  209. __String* ret = __String::create("");
  210. va_list ap;
  211. va_start(ap, format);
  212. ret->initWithFormatAndValist(format, ap);
  213. va_end(ap);
  214. return ret;
  215. }
  216. __String* __String::createWithContentsOfFile(const std::string &filename)
  217. {
  218. std::string str = FileUtils::getInstance()->getStringFromFile(filename);
  219. return __String::create(std::move(str));
  220. }
  221. void __String::acceptVisitor(DataVisitor &visitor)
  222. {
  223. visitor.visit(this);
  224. }
  225. __String* __String::clone() const
  226. {
  227. return __String::create(_string);
  228. }
  229. NS_CC_END