123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "base/CCUserDefault.h"
- #include "platform/CCCommon.h"
- #include "platform/CCFileUtils.h"
- #include "tinyxml2.h"
- #include "base/base64.h"
- #include "base/ccUtils.h"
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_MAC && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
- // root name of xml
- #define USERDEFAULT_ROOT_NAME "userDefaultRoot"
- #define XML_FILE_NAME "UserDefault.xml"
- using namespace std;
- NS_CC_BEGIN
- /**
- * define the functions here because we don't want to
- * export xmlNodePtr and other types in "CCUserDefault.h"
- */
- static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLElement** rootNode, tinyxml2::XMLDocument **doc)
- {
- tinyxml2::XMLElement* curNode = nullptr;
- // check the key value
- if (! pKey)
- {
- return nullptr;
- }
- do
- {
- tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
- *doc = xmlDoc;
- *rootNode = nullptr;
- std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
- if (!xmlBuffer.empty())
- {
- xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
- // get root node
- *rootNode = xmlDoc->RootElement();
- }
- if (nullptr == *rootNode)
- {
- // try to insert xml declaration
- if (!xmlDoc->FirstChild())
- {
- tinyxml2::XMLDeclaration *xmlDeclaration = xmlDoc->NewDeclaration(nullptr);
- if (nullptr != xmlDeclaration)
- {
- xmlDoc->LinkEndChild(xmlDeclaration);
- }
- }
- // create root element
- tinyxml2::XMLElement *rootEle = xmlDoc->NewElement(USERDEFAULT_ROOT_NAME);
- if (nullptr == rootEle)
- break;
- xmlDoc->LinkEndChild(rootEle);
- *rootNode = rootEle;
- }
- // find the node
- curNode = (*rootNode)->FirstChildElement();
- while (nullptr != curNode)
- {
- const char* nodeName = curNode->Value();
- if (!strcmp(nodeName, pKey))
- {
- break;
- }
- curNode = curNode->NextSiblingElement();
- }
- } while (0);
- return curNode;
- }
- static void setValueForKey(const char* pKey, const char* pValue)
- {
- tinyxml2::XMLElement* rootNode;
- tinyxml2::XMLDocument* doc;
- tinyxml2::XMLElement* node;
- // check the params
- if (! pKey || ! pValue)
- {
- return;
- }
- // find the node
- node = getXMLNodeForKey(pKey, &rootNode, &doc);
- // if node exist, change the content
- if (node)
- {
- if (node->FirstChild())
- {
- node->FirstChild()->SetValue(pValue);
- }
- else
- {
- tinyxml2::XMLText* content = doc->NewText(pValue);
- node->LinkEndChild(content);
- }
- }
- else
- {
- if (rootNode)
- {
- tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
- rootNode->LinkEndChild(tmpNode);
- tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
- tmpNode->LinkEndChild(content);
- }
- }
- // save file and free doc
- if (doc)
- {
- doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
- delete doc;
- }
- }
- /**
- * implements of UserDefault
- */
- UserDefault* UserDefault::_userDefault = nullptr;
- string UserDefault::_filePath = string("");
- bool UserDefault::_isFilePathInitialized = false;
- UserDefault::~UserDefault()
- {
- }
- UserDefault::UserDefault()
- {
- }
- bool UserDefault::getBoolForKey(const char* pKey)
- {
- return getBoolForKey(pKey, false);
- }
- bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
- {
- const char* value = nullptr;
- tinyxml2::XMLElement* rootNode;
- tinyxml2::XMLDocument* doc;
- tinyxml2::XMLElement* node;
- node = getXMLNodeForKey(pKey, &rootNode, &doc);
- // find the node
- if (node && node->FirstChild())
- {
- value = (const char*)(node->FirstChild()->Value());
- }
- bool ret = defaultValue;
- if (value)
- {
- ret = (! strcmp(value, "true"));
- }
- if (doc) delete doc;
- return ret;
- }
- int UserDefault::getIntegerForKey(const char* pKey)
- {
- return getIntegerForKey(pKey, 0);
- }
- int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
- {
- const char* value = nullptr;
- tinyxml2::XMLElement* rootNode;
- tinyxml2::XMLDocument* doc;
- tinyxml2::XMLElement* node;
- node = getXMLNodeForKey(pKey, &rootNode, &doc);
- // find the node
- if (node && node->FirstChild())
- {
- value = (const char*)(node->FirstChild()->Value());
- }
- int ret = defaultValue;
- if (value)
- {
- ret = atoi(value);
- }
- if(doc)
- {
- delete doc;
- }
- return ret;
- }
- float UserDefault::getFloatForKey(const char* pKey)
- {
- return getFloatForKey(pKey, 0.0f);
- }
- float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
- {
- float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
-
- return ret;
- }
- double UserDefault::getDoubleForKey(const char* pKey)
- {
- return getDoubleForKey(pKey, 0.0);
- }
- double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
- {
- const char* value = nullptr;
- tinyxml2::XMLElement* rootNode;
- tinyxml2::XMLDocument* doc;
- tinyxml2::XMLElement* node;
- node = getXMLNodeForKey(pKey, &rootNode, &doc);
- // find the node
- if (node && node->FirstChild())
- {
- value = (const char*)(node->FirstChild()->Value());
- }
- double ret = defaultValue;
- if (value)
- {
- ret = utils::atof(value);
- }
- if (doc) delete doc;
- return ret;
- }
- std::string UserDefault::getStringForKey(const char* pKey)
- {
- return getStringForKey(pKey, "");
- }
- string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
- {
- const char* value = nullptr;
- tinyxml2::XMLElement* rootNode;
- tinyxml2::XMLDocument* doc;
- tinyxml2::XMLElement* node;
- node = getXMLNodeForKey(pKey, &rootNode, &doc);
- // find the node
- if (node && node->FirstChild())
- {
- value = (const char*)(node->FirstChild()->Value());
- }
- string ret = defaultValue;
- if (value)
- {
- ret = string(value);
- }
- if (doc) delete doc;
- return ret;
- }
- Data UserDefault::getDataForKey(const char* pKey)
- {
- return getDataForKey(pKey, Data::Null);
- }
- Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
- {
- const char* encodedData = nullptr;
- tinyxml2::XMLElement* rootNode;
- tinyxml2::XMLDocument* doc;
- tinyxml2::XMLElement* node;
- node = getXMLNodeForKey(pKey, &rootNode, &doc);
- // find the node
- if (node && node->FirstChild())
- {
- encodedData = (const char*)(node->FirstChild()->Value());
- }
-
- Data ret = defaultValue;
-
- if (encodedData)
- {
- unsigned char * decodedData = nullptr;
- int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
-
- if (decodedData) {
- ret.fastSet(decodedData, decodedDataLen);
- }
- }
-
- if (doc) delete doc;
-
- return ret;
- }
- void UserDefault::setBoolForKey(const char* pKey, bool value)
- {
- // save bool value as string
- if (true == value)
- {
- setStringForKey(pKey, "true");
- }
- else
- {
- setStringForKey(pKey, "false");
- }
- }
- void UserDefault::setIntegerForKey(const char* pKey, int value)
- {
- // check key
- if (! pKey)
- {
- return;
- }
- // format the value
- char tmp[50];
- memset(tmp, 0, 50);
- sprintf(tmp, "%d", value);
- setValueForKey(pKey, tmp);
- }
- void UserDefault::setFloatForKey(const char* pKey, float value)
- {
- setDoubleForKey(pKey, value);
- }
- void UserDefault::setDoubleForKey(const char* pKey, double value)
- {
- // check key
- if (! pKey)
- {
- return;
- }
- // format the value
- char tmp[50];
- memset(tmp, 0, 50);
- sprintf(tmp, "%f", value);
- setValueForKey(pKey, tmp);
- }
- void UserDefault::setStringForKey(const char* pKey, const std::string & value)
- {
- // check key
- if (! pKey)
- {
- return;
- }
- setValueForKey(pKey, value.c_str());
- }
- void UserDefault::setDataForKey(const char* pKey, const Data& value) {
- // check key
- if (! pKey)
- {
- return;
- }
- char *encodedData = nullptr;
-
- base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData);
-
- setValueForKey(pKey, encodedData);
-
- if (encodedData)
- free(encodedData);
- }
- UserDefault* UserDefault::getInstance()
- {
- if (!_userDefault)
- {
- initXMLFilePath();
- // only create xml file one time
- // the file exists after the program exit
- if ((!isXMLFileExist()) && (!createXMLFile()))
- {
- return nullptr;
- }
- _userDefault = new (std::nothrow) UserDefault();
- }
- return _userDefault;
- }
- void UserDefault::destroyInstance()
- {
- CC_SAFE_DELETE(_userDefault);
- }
- void UserDefault::setDelegate(UserDefault *delegate)
- {
- if (_userDefault)
- delete _userDefault;
- _userDefault = delegate;
- }
- // FIXME:: deprecated
- UserDefault* UserDefault::sharedUserDefault()
- {
- return UserDefault::getInstance();
- }
- // FIXME:: deprecated
- void UserDefault::purgeSharedUserDefault()
- {
- return UserDefault::destroyInstance();
- }
- bool UserDefault::isXMLFileExist()
- {
- return FileUtils::getInstance()->isFileExist(_filePath);
- }
- void UserDefault::initXMLFilePath()
- {
- if (! _isFilePathInitialized)
- {
- _filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;
- _isFilePathInitialized = true;
- }
- }
- // create new xml file
- bool UserDefault::createXMLFile()
- {
- bool bRet = false;
- tinyxml2::XMLDocument *pDoc = new (std::nothrow) tinyxml2::XMLDocument();
- if (nullptr==pDoc)
- {
- return false;
- }
- tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
- if (nullptr==pDeclaration)
- {
- delete pDoc;
- return false;
- }
- pDoc->LinkEndChild(pDeclaration);
- tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
- if (nullptr==pRootEle)
- {
- delete pDoc;
- return false;
- }
- pDoc->LinkEndChild(pRootEle);
- bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str());
- delete pDoc;
- return bRet;
- }
- const string& UserDefault::getXMLFilePath()
- {
- return _filePath;
- }
- void UserDefault::flush()
- {
- }
- void UserDefault::deleteValueForKey(const char* key)
- {
- tinyxml2::XMLElement* rootNode;
- tinyxml2::XMLDocument* doc;
- tinyxml2::XMLElement* node;
- // check the params
- if (!key)
- {
- CCLOG("the key is invalid");
- return;
- }
- // find the node
- node = getXMLNodeForKey(key, &rootNode, &doc);
- // if node not exist, don't need to delete
- if (!node)
- {
- CC_SAFE_DELETE(doc);
- return;
- }
- // save file and free doc
- if (doc)
- {
- doc->DeleteNode(node);
- doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
- delete doc;
- }
- flush();
- }
- NS_CC_END
- #endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)
|