2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCUserDefault.h"
|
|
|
|
#include "platform/CCCommon.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "tinyxml2.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/base64.h"
|
2014-07-14 23:05:16 +08:00
|
|
|
#include "base/ccUtils.h"
|
2013-03-05 14:53:37 +08:00
|
|
|
|
2014-05-29 22:36:15 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_MAC && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// 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"
|
|
|
|
*/
|
|
|
|
|
2013-01-26 14:27:49 +08:00
|
|
|
static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLElement** rootNode, tinyxml2::XMLDocument **doc)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* curNode = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// check the key value
|
|
|
|
if (! pKey)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
|
2015-07-06 17:01:17 +08:00
|
|
|
*doc = xmlDoc;
|
2013-12-18 14:58:17 +08:00
|
|
|
|
|
|
|
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if (xmlBuffer.empty())
|
|
|
|
{
|
|
|
|
CCLOG("can not read xml file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
|
|
|
|
|
|
|
|
// get root node
|
|
|
|
*rootNode = xmlDoc->RootElement();
|
|
|
|
if (nullptr == *rootNode)
|
|
|
|
{
|
|
|
|
CCLOG("read root node error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// 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;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setValueForKey(const char* pKey, const char* pValue)
|
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
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)
|
|
|
|
{
|
2013-02-26 12:44:20 +08:00
|
|
|
if (node->FirstChild())
|
|
|
|
{
|
|
|
|
node->FirstChild()->SetValue(pValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tinyxml2::XMLText* content = doc->NewText(pValue);
|
|
|
|
node->LinkEndChild(content);
|
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2013-02-26 12:44:20 +08:00
|
|
|
// save file and free doc
|
2015-07-06 17:01:17 +08:00
|
|
|
if (doc)
|
|
|
|
{
|
2015-04-19 19:00:27 +08:00
|
|
|
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
|
2015-07-06 17:01:17 +08:00
|
|
|
delete doc;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
* implements of UserDefault
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
|
2013-12-20 14:06:24 +08:00
|
|
|
UserDefault* UserDefault::_userDefault = nullptr;
|
2013-06-20 14:13:12 +08:00
|
|
|
string UserDefault::_filePath = string("");
|
2013-07-12 06:24:23 +08:00
|
|
|
bool UserDefault::_isFilePathInitialized = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault::~UserDefault()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
UserDefault::UserDefault()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
bool UserDefault::getBoolForKey(const char* pKey)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
return getBoolForKey(pKey, false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
const char* value = nullptr;
|
2015-07-06 17:01:17 +08:00
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
|
|
|
if (node && node->FirstChild())
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
2015-07-06 17:01:17 +08:00
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
bool ret = defaultValue;
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = (! strcmp(value, "true"));
|
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int UserDefault::getIntegerForKey(const char* pKey)
|
2012-11-16 14:11:21 +08:00
|
|
|
{
|
|
|
|
return getIntegerForKey(pKey, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
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())
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
2015-07-06 17:01:17 +08:00
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
int ret = defaultValue;
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = atoi(value);
|
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if(doc)
|
|
|
|
{
|
|
|
|
delete doc;
|
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float UserDefault::getFloatForKey(const char* pKey)
|
2012-11-16 14:11:21 +08:00
|
|
|
{
|
|
|
|
return getFloatForKey(pKey, 0.0f);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
double UserDefault::getDoubleForKey(const char* pKey)
|
2012-11-16 14:11:21 +08:00
|
|
|
{
|
|
|
|
return getDoubleForKey(pKey, 0.0);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
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())
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
2015-07-06 17:01:17 +08:00
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
double ret = defaultValue;
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = utils::atof(value);
|
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::string UserDefault::getStringForKey(const char* pKey)
|
2012-11-16 14:11:21 +08:00
|
|
|
{
|
|
|
|
return getStringForKey(pKey, "");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
const char* value = nullptr;
|
2015-07-06 17:01:17 +08:00
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
|
|
|
if (node && node->FirstChild())
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
2015-07-06 17:01:17 +08:00
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
string ret = defaultValue;
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = string(value);
|
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
Data UserDefault::getDataForKey(const char* pKey)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
2013-12-20 21:16:46 +08:00
|
|
|
return getDataForKey(pKey, Data::Null);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
const char* encodedData = nullptr;
|
2015-07-06 17:01:17 +08:00
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
|
|
|
if (node && node->FirstChild())
|
|
|
|
{
|
2013-05-29 08:06:41 +08:00
|
|
|
encodedData = (const char*)(node->FirstChild()->Value());
|
2015-07-06 17:01:17 +08:00
|
|
|
}
|
2013-05-29 08:06:41 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
Data ret = defaultValue;
|
2013-05-29 08:06:41 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if (encodedData)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
unsigned char * decodedData = nullptr;
|
2013-05-29 08:06:41 +08:00
|
|
|
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
|
|
|
|
|
|
|
|
if (decodedData) {
|
2013-12-18 14:58:17 +08:00
|
|
|
ret.fastSet(decodedData, decodedDataLen);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
}
|
2013-05-29 08:06:41 +08:00
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
return ret;
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setBoolForKey(const char* pKey, bool value)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// save bool value as string
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (true == value)
|
|
|
|
{
|
|
|
|
setStringForKey(pKey, "true");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setStringForKey(pKey, "false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setIntegerForKey(const char* pKey, int value)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// check key
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// format the value
|
|
|
|
char tmp[50];
|
|
|
|
memset(tmp, 0, 50);
|
|
|
|
sprintf(tmp, "%d", value);
|
|
|
|
|
|
|
|
setValueForKey(pKey, tmp);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setFloatForKey(const char* pKey, float value)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
setDoubleForKey(pKey, value);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setDoubleForKey(const char* pKey, double value)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// check key
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// format the value
|
|
|
|
char tmp[50];
|
|
|
|
memset(tmp, 0, 50);
|
|
|
|
sprintf(tmp, "%f", value);
|
|
|
|
|
|
|
|
setValueForKey(pKey, tmp);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setStringForKey(const char* pKey, const std::string & value)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// check key
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setValueForKey(pKey, value.c_str());
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::setDataForKey(const char* pKey, const Data& value) {
|
2013-05-29 08:06:41 +08:00
|
|
|
// check key
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-25 02:09:11 +08:00
|
|
|
char *encodedData = nullptr;
|
2013-05-29 08:06:41 +08:00
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData);
|
2013-05-29 08:06:41 +08:00
|
|
|
|
|
|
|
setValueForKey(pKey, encodedData);
|
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
if (encodedData)
|
|
|
|
free(encodedData);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault* UserDefault::getInstance()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 03:07:48 +08:00
|
|
|
if (!_userDefault)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 03:07:48 +08:00
|
|
|
initXMLFilePath();
|
|
|
|
|
|
|
|
// only create xml file one time
|
|
|
|
// the file exists after the program exit
|
|
|
|
if ((!isXMLFileExist()) && (!createXMLFile()))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
_userDefault = new (std::nothrow) UserDefault();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
return _userDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserDefault::destroyInstance()
|
|
|
|
{
|
2013-12-20 14:06:24 +08:00
|
|
|
CC_SAFE_DELETE(_userDefault);
|
2013-07-12 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
2015-04-20 03:07:48 +08:00
|
|
|
void UserDefault::setDelegate(UserDefault *delegate)
|
|
|
|
{
|
|
|
|
if (_userDefault)
|
|
|
|
delete _userDefault;
|
|
|
|
|
|
|
|
_userDefault = delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: deprecated
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault* UserDefault::sharedUserDefault()
|
|
|
|
{
|
|
|
|
return UserDefault::getInstance();
|
|
|
|
}
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: deprecated
|
2013-07-12 06:24:23 +08:00
|
|
|
void UserDefault::purgeSharedUserDefault()
|
|
|
|
{
|
2013-07-12 14:34:57 +08:00
|
|
|
return UserDefault::destroyInstance();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::isXMLFileExist()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 03:07:48 +08:00
|
|
|
return FileUtils::getInstance()->isFileExist(_filePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::initXMLFilePath()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
if (! _isFilePathInitialized)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
_filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;
|
|
|
|
_isFilePathInitialized = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new xml file
|
2013-06-20 14:13:12 +08:00
|
|
|
bool UserDefault::createXMLFile()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
bool bRet = false;
|
2015-12-16 14:02:55 +08:00
|
|
|
tinyxml2::XMLDocument *pDoc = new (std::nothrow) tinyxml2::XMLDocument();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr==pDoc)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
|
|
|
|
if (nullptr==pDeclaration)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pDoc->LinkEndChild(pDeclaration);
|
|
|
|
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
|
|
|
|
if (nullptr==pRootEle)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pDoc->LinkEndChild(pRootEle);
|
2015-04-19 19:00:27 +08:00
|
|
|
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str());
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
if(pDoc)
|
|
|
|
{
|
|
|
|
delete pDoc;
|
|
|
|
}
|
2013-01-26 10:41:27 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
return bRet;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const string& UserDefault::getXMLFilePath()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _filePath;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void UserDefault::flush()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// save file and free doc
|
|
|
|
if (doc)
|
|
|
|
{
|
|
|
|
doc->DeleteNode(node);
|
|
|
|
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
|
|
|
|
delete doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
2013-03-05 14:53:37 +08:00
|
|
|
|
|
|
|
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)
|