2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCUserDefault.h"
|
2012-06-19 17:22:55 +08:00
|
|
|
#include "platform/CCCommon.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "tinyxml2.h"
|
|
|
|
#include "base64.h"
|
2013-03-05 14:53:37 +08:00
|
|
|
|
2013-04-01 13:24:10 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && 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
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
|
|
|
*doc = xmlDoc;
|
2013-07-12 06:24:23 +08:00
|
|
|
//CCFileData data(UserDefault::getInstance()->getXMLFilePath().c_str(),"rt");
|
2013-12-05 17:19:01 +08:00
|
|
|
ssize_t nSize;
|
2013-11-12 10:09:47 +08:00
|
|
|
char* pXmlBuffer = (char*)FileUtils::getInstance()->getFileData(UserDefault::getInstance()->getXMLFilePath().c_str(), "rb", &nSize);
|
2013-01-26 14:27:49 +08:00
|
|
|
//const char* pXmlBuffer = (const char*)data.getBuffer();
|
2013-12-18 17:47:20 +08:00
|
|
|
if(nullptr == pXmlBuffer)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
CCLOG("can not read xml file");
|
|
|
|
break;
|
|
|
|
}
|
2013-11-03 10:17:04 +08:00
|
|
|
xmlDoc->Parse(pXmlBuffer, nSize);
|
2013-11-12 10:09:47 +08:00
|
|
|
free(pXmlBuffer);
|
2013-01-26 10:41:27 +08:00
|
|
|
// get root node
|
|
|
|
*rootNode = xmlDoc->RootElement();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr == *rootNode)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
CCLOG("read root node error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// find the node
|
|
|
|
curNode = (*rootNode)->FirstChildElement();
|
2013-12-18 17:47:20 +08:00
|
|
|
while (nullptr != curNode)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2013-01-26 10:41:27 +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
|
2013-02-26 12:44:20 +08:00
|
|
|
if (node)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
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);
|
|
|
|
}
|
2013-01-26 10:41:27 +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-02-26 12:44:20 +08:00
|
|
|
// save file and free doc
|
2013-01-26 10:41:27 +08:00
|
|
|
if (doc)
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());
|
2013-01-26 10:41:27 +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-07-12 06:24:23 +08:00
|
|
|
UserDefault* UserDefault::_userDefault = 0;
|
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-07-12 06:24:23 +08:00
|
|
|
* If the user invoke delete UserDefault::getInstance(), should set _userDefault
|
|
|
|
* to null to avoid error when he invoke UserDefault::getInstance() later.
|
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
|
|
|
CC_SAFE_DELETE(_userDefault);
|
2013-12-18 17:47:20 +08:00
|
|
|
_userDefault = nullptr;
|
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-12-18 17:47:20 +08:00
|
|
|
_userDefault = nullptr;
|
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;
|
2013-01-26 10:41:27 +08:00
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
2013-02-26 12:44:20 +08:00
|
|
|
if (node && node->FirstChild())
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = defaultValue;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = (! strcmp(value, "true"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
const char* value = nullptr;
|
2013-01-26 10:41:27 +08:00
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
2013-02-26 12:44:20 +08:00
|
|
|
if (node && node->FirstChild())
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = defaultValue;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = atoi(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(doc)
|
|
|
|
{
|
|
|
|
delete doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
const char* value = nullptr;
|
2013-01-26 10:41:27 +08:00
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
2013-02-26 12:44:20 +08:00
|
|
|
if (node && node->FirstChild())
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
double ret = defaultValue;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = atof(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
|
|
|
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;
|
2013-01-26 10:41:27 +08:00
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
2013-02-22 11:01:57 +08:00
|
|
|
if (node && node->FirstChild())
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
value = (const char*)(node->FirstChild()->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
string ret = defaultValue;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = string(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Data* UserDefault::getDataForKey(const char* pKey)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return getDataForKey(pKey, nullptr);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Data* UserDefault::getDataForKey(const char* pKey, Data* defaultValue)
|
2013-05-29 08:06:41 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
const char* encodedData = nullptr;
|
2013-05-29 08:06:41 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Data* ret = defaultValue;
|
2013-05-29 08:06:41 +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-06-20 14:13:12 +08:00
|
|
|
ret = Data::create(decodedData, decodedDataLen);
|
2013-05-29 08:06:41 +08:00
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
free(decodedData);
|
2013-05-29 08:06:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc) delete doc;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *encodedData = 0;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
initXMLFilePath();
|
|
|
|
|
|
|
|
// only create xml file one time
|
2012-09-17 15:02:24 +08:00
|
|
|
// the file exists after the program exit
|
2012-04-19 14:35:52 +08:00
|
|
|
if ((! isXMLFileExist()) && (! createXMLFile()))
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
if (! _userDefault)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
_userDefault = new UserDefault();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
return _userDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserDefault::destroyInstance()
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_userDefault = nullptr;
|
2013-07-12 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
|
|
|
UserDefault* UserDefault::sharedUserDefault()
|
|
|
|
{
|
|
|
|
return UserDefault::getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
|
|
|
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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
FILE *fp = fopen(_filePath.c_str(), "r");
|
2013-01-26 10:41:27 +08:00
|
|
|
bool bRet = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-01-26 10:41:27 +08:00
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-01-26 10:41:27 +08:00
|
|
|
return bRet;
|
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
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
bool bRet = false;
|
|
|
|
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr==pDoc)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-14 11:08:50 +08:00
|
|
|
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr==pDeclaration)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pDoc->LinkEndChild(pDeclaration);
|
|
|
|
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr==pRootEle)
|
2013-01-26 10:41:27 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pDoc->LinkEndChild(pRootEle);
|
2013-06-15 14:03:30 +08:00
|
|
|
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(_filePath.c_str());
|
2013-01-26 10:41:27 +08:00
|
|
|
|
|
|
|
if(pDoc)
|
|
|
|
{
|
|
|
|
delete pDoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
2013-03-05 14:53:37 +08:00
|
|
|
|
|
|
|
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)
|