CCUserDefault.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  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/CCUserDefault.h"
  22. #include "platform/CCCommon.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "tinyxml2.h"
  25. #include "base/base64.h"
  26. #include "base/ccUtils.h"
  27. #if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_MAC && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
  28. // root name of xml
  29. #define USERDEFAULT_ROOT_NAME "userDefaultRoot"
  30. #define XML_FILE_NAME "UserDefault.xml"
  31. using namespace std;
  32. NS_CC_BEGIN
  33. /**
  34. * define the functions here because we don't want to
  35. * export xmlNodePtr and other types in "CCUserDefault.h"
  36. */
  37. static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLElement** rootNode, tinyxml2::XMLDocument **doc)
  38. {
  39. tinyxml2::XMLElement* curNode = nullptr;
  40. // check the key value
  41. if (! pKey)
  42. {
  43. return nullptr;
  44. }
  45. do
  46. {
  47. tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
  48. *doc = xmlDoc;
  49. std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
  50. if (xmlBuffer.empty())
  51. {
  52. CCLOG("can not read xml file");
  53. break;
  54. }
  55. xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
  56. // get root node
  57. *rootNode = xmlDoc->RootElement();
  58. if (nullptr == *rootNode)
  59. {
  60. CCLOG("read root node error");
  61. break;
  62. }
  63. // find the node
  64. curNode = (*rootNode)->FirstChildElement();
  65. while (nullptr != curNode)
  66. {
  67. const char* nodeName = curNode->Value();
  68. if (!strcmp(nodeName, pKey))
  69. {
  70. break;
  71. }
  72. curNode = curNode->NextSiblingElement();
  73. }
  74. } while (0);
  75. return curNode;
  76. }
  77. static void setValueForKey(const char* pKey, const char* pValue)
  78. {
  79. tinyxml2::XMLElement* rootNode;
  80. tinyxml2::XMLDocument* doc;
  81. tinyxml2::XMLElement* node;
  82. // check the params
  83. if (! pKey || ! pValue)
  84. {
  85. return;
  86. }
  87. // find the node
  88. node = getXMLNodeForKey(pKey, &rootNode, &doc);
  89. // if node exist, change the content
  90. if (node)
  91. {
  92. if (node->FirstChild())
  93. {
  94. node->FirstChild()->SetValue(pValue);
  95. }
  96. else
  97. {
  98. tinyxml2::XMLText* content = doc->NewText(pValue);
  99. node->LinkEndChild(content);
  100. }
  101. }
  102. else
  103. {
  104. if (rootNode)
  105. {
  106. tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
  107. rootNode->LinkEndChild(tmpNode);
  108. tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
  109. tmpNode->LinkEndChild(content);
  110. }
  111. }
  112. // save file and free doc
  113. if (doc)
  114. {
  115. doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
  116. delete doc;
  117. }
  118. }
  119. /**
  120. * implements of UserDefault
  121. */
  122. UserDefault* UserDefault::_userDefault = nullptr;
  123. string UserDefault::_filePath = string("");
  124. bool UserDefault::_isFilePathInitialized = false;
  125. UserDefault::~UserDefault()
  126. {
  127. }
  128. UserDefault::UserDefault()
  129. {
  130. }
  131. bool UserDefault::getBoolForKey(const char* pKey)
  132. {
  133. return getBoolForKey(pKey, false);
  134. }
  135. bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
  136. {
  137. const char* value = nullptr;
  138. tinyxml2::XMLElement* rootNode;
  139. tinyxml2::XMLDocument* doc;
  140. tinyxml2::XMLElement* node;
  141. node = getXMLNodeForKey(pKey, &rootNode, &doc);
  142. // find the node
  143. if (node && node->FirstChild())
  144. {
  145. value = (const char*)(node->FirstChild()->Value());
  146. }
  147. bool ret = defaultValue;
  148. if (value)
  149. {
  150. ret = (! strcmp(value, "true"));
  151. }
  152. if (doc) delete doc;
  153. return ret;
  154. }
  155. int UserDefault::getIntegerForKey(const char* pKey)
  156. {
  157. return getIntegerForKey(pKey, 0);
  158. }
  159. int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
  160. {
  161. const char* value = nullptr;
  162. tinyxml2::XMLElement* rootNode;
  163. tinyxml2::XMLDocument* doc;
  164. tinyxml2::XMLElement* node;
  165. node = getXMLNodeForKey(pKey, &rootNode, &doc);
  166. // find the node
  167. if (node && node->FirstChild())
  168. {
  169. value = (const char*)(node->FirstChild()->Value());
  170. }
  171. int ret = defaultValue;
  172. if (value)
  173. {
  174. ret = atoi(value);
  175. }
  176. if(doc)
  177. {
  178. delete doc;
  179. }
  180. return ret;
  181. }
  182. float UserDefault::getFloatForKey(const char* pKey)
  183. {
  184. return getFloatForKey(pKey, 0.0f);
  185. }
  186. float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
  187. {
  188. float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
  189. return ret;
  190. }
  191. double UserDefault::getDoubleForKey(const char* pKey)
  192. {
  193. return getDoubleForKey(pKey, 0.0);
  194. }
  195. double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
  196. {
  197. const char* value = nullptr;
  198. tinyxml2::XMLElement* rootNode;
  199. tinyxml2::XMLDocument* doc;
  200. tinyxml2::XMLElement* node;
  201. node = getXMLNodeForKey(pKey, &rootNode, &doc);
  202. // find the node
  203. if (node && node->FirstChild())
  204. {
  205. value = (const char*)(node->FirstChild()->Value());
  206. }
  207. double ret = defaultValue;
  208. if (value)
  209. {
  210. ret = utils::atof(value);
  211. }
  212. if (doc) delete doc;
  213. return ret;
  214. }
  215. std::string UserDefault::getStringForKey(const char* pKey)
  216. {
  217. return getStringForKey(pKey, "");
  218. }
  219. string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
  220. {
  221. const char* value = nullptr;
  222. tinyxml2::XMLElement* rootNode;
  223. tinyxml2::XMLDocument* doc;
  224. tinyxml2::XMLElement* node;
  225. node = getXMLNodeForKey(pKey, &rootNode, &doc);
  226. // find the node
  227. if (node && node->FirstChild())
  228. {
  229. value = (const char*)(node->FirstChild()->Value());
  230. }
  231. string ret = defaultValue;
  232. if (value)
  233. {
  234. ret = string(value);
  235. }
  236. if (doc) delete doc;
  237. return ret;
  238. }
  239. Data UserDefault::getDataForKey(const char* pKey)
  240. {
  241. return getDataForKey(pKey, Data::Null);
  242. }
  243. Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
  244. {
  245. const char* encodedData = nullptr;
  246. tinyxml2::XMLElement* rootNode;
  247. tinyxml2::XMLDocument* doc;
  248. tinyxml2::XMLElement* node;
  249. node = getXMLNodeForKey(pKey, &rootNode, &doc);
  250. // find the node
  251. if (node && node->FirstChild())
  252. {
  253. encodedData = (const char*)(node->FirstChild()->Value());
  254. }
  255. Data ret = defaultValue;
  256. if (encodedData)
  257. {
  258. unsigned char * decodedData = nullptr;
  259. int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
  260. if (decodedData) {
  261. ret.fastSet(decodedData, decodedDataLen);
  262. }
  263. }
  264. if (doc) delete doc;
  265. return ret;
  266. }
  267. void UserDefault::setBoolForKey(const char* pKey, bool value)
  268. {
  269. // save bool value as string
  270. if (true == value)
  271. {
  272. setStringForKey(pKey, "true");
  273. }
  274. else
  275. {
  276. setStringForKey(pKey, "false");
  277. }
  278. }
  279. void UserDefault::setIntegerForKey(const char* pKey, int value)
  280. {
  281. // check key
  282. if (! pKey)
  283. {
  284. return;
  285. }
  286. // format the value
  287. char tmp[50];
  288. memset(tmp, 0, 50);
  289. sprintf(tmp, "%d", value);
  290. setValueForKey(pKey, tmp);
  291. }
  292. void UserDefault::setFloatForKey(const char* pKey, float value)
  293. {
  294. setDoubleForKey(pKey, value);
  295. }
  296. void UserDefault::setDoubleForKey(const char* pKey, double value)
  297. {
  298. // check key
  299. if (! pKey)
  300. {
  301. return;
  302. }
  303. // format the value
  304. char tmp[50];
  305. memset(tmp, 0, 50);
  306. sprintf(tmp, "%f", value);
  307. setValueForKey(pKey, tmp);
  308. }
  309. void UserDefault::setStringForKey(const char* pKey, const std::string & value)
  310. {
  311. // check key
  312. if (! pKey)
  313. {
  314. return;
  315. }
  316. setValueForKey(pKey, value.c_str());
  317. }
  318. void UserDefault::setDataForKey(const char* pKey, const Data& value) {
  319. // check key
  320. if (! pKey)
  321. {
  322. return;
  323. }
  324. char *encodedData = nullptr;
  325. base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData);
  326. setValueForKey(pKey, encodedData);
  327. if (encodedData)
  328. free(encodedData);
  329. }
  330. UserDefault* UserDefault::getInstance()
  331. {
  332. if (!_userDefault)
  333. {
  334. initXMLFilePath();
  335. // only create xml file one time
  336. // the file exists after the program exit
  337. if ((!isXMLFileExist()) && (!createXMLFile()))
  338. {
  339. return nullptr;
  340. }
  341. _userDefault = new (std::nothrow) UserDefault();
  342. }
  343. return _userDefault;
  344. }
  345. void UserDefault::destroyInstance()
  346. {
  347. CC_SAFE_DELETE(_userDefault);
  348. }
  349. void UserDefault::setDelegate(UserDefault *delegate)
  350. {
  351. if (_userDefault)
  352. delete _userDefault;
  353. _userDefault = delegate;
  354. }
  355. // FIXME:: deprecated
  356. UserDefault* UserDefault::sharedUserDefault()
  357. {
  358. return UserDefault::getInstance();
  359. }
  360. // FIXME:: deprecated
  361. void UserDefault::purgeSharedUserDefault()
  362. {
  363. return UserDefault::destroyInstance();
  364. }
  365. bool UserDefault::isXMLFileExist()
  366. {
  367. return FileUtils::getInstance()->isFileExist(_filePath);
  368. }
  369. void UserDefault::initXMLFilePath()
  370. {
  371. if (! _isFilePathInitialized)
  372. {
  373. _filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;
  374. _isFilePathInitialized = true;
  375. }
  376. }
  377. // create new xml file
  378. bool UserDefault::createXMLFile()
  379. {
  380. bool bRet = false;
  381. tinyxml2::XMLDocument *pDoc = new (std::nothrow) tinyxml2::XMLDocument();
  382. if (nullptr==pDoc)
  383. {
  384. return false;
  385. }
  386. tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
  387. if (nullptr==pDeclaration)
  388. {
  389. return false;
  390. }
  391. pDoc->LinkEndChild(pDeclaration);
  392. tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
  393. if (nullptr==pRootEle)
  394. {
  395. return false;
  396. }
  397. pDoc->LinkEndChild(pRootEle);
  398. bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str());
  399. if(pDoc)
  400. {
  401. delete pDoc;
  402. }
  403. return bRet;
  404. }
  405. const string& UserDefault::getXMLFilePath()
  406. {
  407. return _filePath;
  408. }
  409. void UserDefault::flush()
  410. {
  411. }
  412. void UserDefault::deleteValueForKey(const char* key)
  413. {
  414. tinyxml2::XMLElement* rootNode;
  415. tinyxml2::XMLDocument* doc;
  416. tinyxml2::XMLElement* node;
  417. // check the params
  418. if (!key)
  419. {
  420. CCLOG("the key is invalid");
  421. return;
  422. }
  423. // find the node
  424. node = getXMLNodeForKey(key, &rootNode, &doc);
  425. // if node not exist, don't need to delete
  426. if (!node)
  427. {
  428. return;
  429. }
  430. // save file and free doc
  431. if (doc)
  432. {
  433. doc->DeleteNode(node);
  434. doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
  435. delete doc;
  436. }
  437. flush();
  438. }
  439. NS_CC_END
  440. #endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)