CCUserDefault.cpp 13 KB

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