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-01-26 10:41:27 +08:00
|
|
|
#include "tinyxml2/tinyxml2.h"
|
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-01-26 10:41:27 +08:00
|
|
|
tinyxml2::XMLElement* curNode = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// check the key value
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
|
|
|
|
*doc = xmlDoc;
|
2013-01-26 14:27:49 +08:00
|
|
|
//CCFileData data(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(),"rt");
|
|
|
|
unsigned long nSize;
|
|
|
|
const char* pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), "rb", &nSize);
|
|
|
|
//const char* pXmlBuffer = (const char*)data.getBuffer();
|
2013-01-26 10:41:27 +08:00
|
|
|
if(NULL == pXmlBuffer)
|
|
|
|
{
|
|
|
|
CCLOG("can not read xml file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xmlDoc->Parse(pXmlBuffer);
|
|
|
|
// get root node
|
|
|
|
*rootNode = xmlDoc->RootElement();
|
|
|
|
if (NULL == *rootNode)
|
|
|
|
{
|
|
|
|
CCLOG("read root node error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// find the node
|
|
|
|
curNode = (*rootNode)->FirstChildElement();
|
|
|
|
while (NULL != 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)
|
|
|
|
{
|
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
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
node->FirstChild()->SetValue(pValue);
|
|
|
|
}
|
|
|
|
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(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str());
|
|
|
|
delete doc;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* implements of CCUserDefault
|
|
|
|
*/
|
|
|
|
|
|
|
|
CCUserDefault* CCUserDefault::m_spUserDefault = 0;
|
|
|
|
string CCUserDefault::m_sFilePath = string("");
|
|
|
|
bool CCUserDefault::m_sbIsFilePathInitialized = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the user invoke delete CCUserDefault::sharedUserDefault(), should set m_spUserDefault
|
|
|
|
* to null to avoid error when he invoke CCUserDefault::sharedUserDefault() later.
|
|
|
|
*/
|
|
|
|
CCUserDefault::~CCUserDefault()
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
CC_SAFE_DELETE(m_spUserDefault);
|
2012-04-19 14:35:52 +08:00
|
|
|
m_spUserDefault = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCUserDefault::CCUserDefault()
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
m_spUserDefault = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::purgeSharedUserDefault()
|
|
|
|
{
|
|
|
|
m_spUserDefault = NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-16 14:11:21 +08:00
|
|
|
bool CCUserDefault::getBoolForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getBoolForKey(pKey, false);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
const char* value = NULL;
|
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-16 14:11:21 +08:00
|
|
|
int CCUserDefault::getIntegerForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getIntegerForKey(pKey, 0);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
const char* value = NULL;
|
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-16 14:11:21 +08:00
|
|
|
float CCUserDefault::getFloatForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getFloatForKey(pKey, 0.0f);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
|
|
|
{
|
|
|
|
float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-16 14:11:21 +08:00
|
|
|
double CCUserDefault::getDoubleForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getDoubleForKey(pKey, 0.0);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
const char* value = NULL;
|
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-16 14:11:21 +08:00
|
|
|
std::string CCUserDefault::getStringForKey(const char* pKey)
|
|
|
|
{
|
|
|
|
return getStringForKey(pKey, "");
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
const char* value = NULL;
|
|
|
|
tinyxml2::XMLElement* rootNode;
|
|
|
|
tinyxml2::XMLDocument* doc;
|
|
|
|
tinyxml2::XMLElement* node;
|
|
|
|
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
|
|
// find the node
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::setBoolForKey(const char* pKey, bool value)
|
|
|
|
{
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::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 CCUserDefault::setFloatForKey(const char* pKey, float value)
|
|
|
|
{
|
|
|
|
setDoubleForKey(pKey, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::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 CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
|
|
|
|
{
|
|
|
|
// check key
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setValueForKey(pKey, value.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
CCUserDefault* CCUserDefault::sharedUserDefault()
|
|
|
|
{
|
|
|
|
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()))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! m_spUserDefault)
|
|
|
|
{
|
|
|
|
m_spUserDefault = new CCUserDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_spUserDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCUserDefault::isXMLFileExist()
|
|
|
|
{
|
|
|
|
FILE *fp = fopen(m_sFilePath.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
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::initXMLFilePath()
|
|
|
|
{
|
|
|
|
if (! m_sbIsFilePathInitialized)
|
|
|
|
{
|
2013-02-06 18:04:40 +08:00
|
|
|
m_sFilePath += CCFileUtils::sharedFileUtils()->getWritablePath() + XML_FILE_NAME;
|
2012-04-19 14:35:52 +08:00
|
|
|
m_sbIsFilePathInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new xml file
|
|
|
|
bool CCUserDefault::createXMLFile()
|
|
|
|
{
|
2013-01-26 10:41:27 +08:00
|
|
|
bool bRet = false;
|
|
|
|
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
|
|
|
|
if (NULL==pDoc)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration("1.0");
|
|
|
|
if (NULL==pDeclaration)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pDoc->LinkEndChild(pDeclaration);
|
|
|
|
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
|
|
|
|
if (NULL==pRootEle)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pDoc->LinkEndChild(pRootEle);
|
2013-02-06 17:20:54 +08:00
|
|
|
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(m_sFilePath.c_str());
|
2013-01-26 10:41:27 +08:00
|
|
|
|
|
|
|
if(pDoc)
|
|
|
|
{
|
|
|
|
delete pDoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const string& CCUserDefault::getXMLFilePath()
|
|
|
|
{
|
|
|
|
return m_sFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::flush()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|