2011-12-15 01:46:55 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
2011-08-05 11:02:42 +08:00
|
|
|
****************************************************************************/
|
2011-05-06 15:31:51 +08:00
|
|
|
#include "CCUserDefault.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2011-12-15 01:46:55 +08:00
|
|
|
|
2011-08-17 11:18:58 +08:00
|
|
|
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
2011-05-06 15:31:51 +08:00
|
|
|
#include <libxml/parser.h>
|
2011-12-15 01:46:55 +08:00
|
|
|
#include <libxml/tree.h>
|
|
|
|
//#else
|
|
|
|
//#include <libxml/parser.h>
|
|
|
|
//#include <libxml/tree.h>
|
|
|
|
//#endif
|
2011-08-17 11:18:58 +08:00
|
|
|
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
// root name of xml
|
|
|
|
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
|
|
|
|
|
|
|
|
#define XML_FILE_NAME "UserDefault.xml"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN;
|
|
|
|
|
2012-02-02 14:18:13 +08:00
|
|
|
static xmlDocPtr g_sharedDoc = NULL;
|
|
|
|
|
2011-05-06 15:31:51 +08:00
|
|
|
/**
|
|
|
|
* define the functions here because we don't want to
|
|
|
|
* export xmlNodePtr and other types in "CCUserDefault.h"
|
|
|
|
*/
|
|
|
|
|
2012-02-02 14:18:13 +08:00
|
|
|
static xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr *rootNode)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
xmlNodePtr curNode = NULL;
|
|
|
|
|
|
|
|
// check the key value
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// get root node
|
2012-02-02 14:18:13 +08:00
|
|
|
*rootNode = xmlDocGetRootElement(g_sharedDoc);
|
2011-05-06 15:31:51 +08:00
|
|
|
if (NULL == *rootNode)
|
|
|
|
{
|
|
|
|
CCLOG("read root node error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the node
|
|
|
|
curNode = (*rootNode)->xmlChildrenNode;
|
|
|
|
while (NULL != curNode)
|
|
|
|
{
|
|
|
|
if (! xmlStrcmp(curNode->name, BAD_CAST pKey))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
curNode = curNode->next;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return curNode;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:46:55 +08:00
|
|
|
static inline const char* getValueForKey(const char* pKey)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
const char* ret = NULL;
|
|
|
|
xmlNodePtr rootNode;
|
2012-02-02 14:18:13 +08:00
|
|
|
xmlNodePtr node = getXMLNodeForKey(pKey, &rootNode);
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
// find the node
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
ret = (const char*)xmlNodeGetContent(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:46:55 +08:00
|
|
|
static void setValueForKey(const char* pKey, const char* pValue)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
xmlNodePtr rootNode;
|
|
|
|
xmlNodePtr node;
|
|
|
|
|
|
|
|
// check the params
|
|
|
|
if (! pKey || ! pValue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the node
|
2012-02-02 14:18:13 +08:00
|
|
|
node = getXMLNodeForKey(pKey, &rootNode);
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
// if node exist, change the content
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
xmlNodeSetContent(node, BAD_CAST pValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rootNode)
|
|
|
|
{
|
|
|
|
// the node doesn't exist, add a new one
|
|
|
|
// libxml in android donesn't support xmlNewTextChild, so use this approach
|
|
|
|
xmlNodePtr tmpNode = xmlNewNode(NULL, BAD_CAST pKey);
|
|
|
|
xmlNodePtr content = xmlNewText(BAD_CAST pValue);
|
|
|
|
xmlAddChild(rootNode, tmpNode);
|
|
|
|
xmlAddChild(tmpNode, content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* implements of CCUserDefault
|
|
|
|
*/
|
|
|
|
|
|
|
|
CCUserDefault* CCUserDefault::m_spUserDefault = 0;
|
|
|
|
string CCUserDefault::m_sFilePath = string("");
|
|
|
|
bool CCUserDefault::m_sbIsFilePathInitialized = false;
|
|
|
|
|
2011-05-06 17:46:19 +08:00
|
|
|
/**
|
|
|
|
* If the user invoke delete CCUserDefault::sharedUserDefault(), should set m_spUserDefault
|
|
|
|
* to null to avoid error when he invoke CCUserDefault::sharedUserDefault() later.
|
|
|
|
*/
|
|
|
|
CCUserDefault::~CCUserDefault()
|
|
|
|
{
|
2012-02-02 14:18:13 +08:00
|
|
|
flush();
|
|
|
|
if (g_sharedDoc)
|
|
|
|
{
|
|
|
|
xmlFreeDoc(g_sharedDoc);
|
|
|
|
g_sharedDoc = NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:46:19 +08:00
|
|
|
m_spUserDefault = NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-02 14:18:13 +08:00
|
|
|
CCUserDefault::CCUserDefault()
|
|
|
|
{
|
|
|
|
g_sharedDoc = xmlReadFile(getXMLFilePath().c_str(), "utf-8", XML_PARSE_RECOVER);
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:46:19 +08:00
|
|
|
void CCUserDefault::purgeSharedUserDefault()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(m_spUserDefault);
|
|
|
|
m_spUserDefault = NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:08:25 +08:00
|
|
|
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
const char* value = getValueForKey(pKey);
|
2011-07-31 20:08:25 +08:00
|
|
|
bool ret = defaultValue;
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
2011-07-31 20:31:39 +08:00
|
|
|
ret = (! strcmp(value, "true"));
|
|
|
|
xmlFree((void*)value);
|
2011-05-06 15:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:08:25 +08:00
|
|
|
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
const char* value = getValueForKey(pKey);
|
2011-07-31 20:08:25 +08:00
|
|
|
int ret = defaultValue;
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = atoi(value);
|
|
|
|
xmlFree((void*)value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:08:25 +08:00
|
|
|
float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
2011-07-31 20:08:25 +08:00
|
|
|
float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:08:25 +08:00
|
|
|
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
const char* value = getValueForKey(pKey);
|
2011-07-31 20:08:25 +08:00
|
|
|
double ret = defaultValue;
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = atof(value);
|
|
|
|
xmlFree((void*)value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:08:25 +08:00
|
|
|
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
const char* value = getValueForKey(pKey);
|
2011-07-31 20:08:25 +08:00
|
|
|
string ret = defaultValue;
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ret = string(value);
|
|
|
|
xmlFree((void*)value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::setBoolForKey(const char* pKey, bool value)
|
|
|
|
{
|
|
|
|
// save bool value as sring
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:08:25 +08:00
|
|
|
void CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
// check key
|
|
|
|
if (! pKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setValueForKey(pKey, value.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
CCUserDefault* CCUserDefault::sharedUserDefault()
|
|
|
|
{
|
|
|
|
initXMLFilePath();
|
|
|
|
|
|
|
|
// only create xml file one time
|
|
|
|
// the file exists after the programe exit
|
|
|
|
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");
|
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCUserDefault::initXMLFilePath()
|
|
|
|
{
|
|
|
|
if (! m_sbIsFilePathInitialized)
|
|
|
|
{
|
|
|
|
m_sFilePath += CCFileUtils::getWriteablePath() + XML_FILE_NAME;
|
|
|
|
m_sbIsFilePathInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new xml file
|
|
|
|
bool CCUserDefault::createXMLFile()
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
2012-02-02 14:18:13 +08:00
|
|
|
xmlDocPtr doc = NULL;
|
2011-05-06 15:31:51 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// new doc
|
|
|
|
doc = xmlNewDoc(BAD_CAST"1.0");
|
|
|
|
if (doc == NULL)
|
|
|
|
{
|
|
|
|
CCLOG("can not create xml doc");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new root node
|
|
|
|
xmlNodePtr rootNode = xmlNewNode(NULL, BAD_CAST USERDEFAULT_ROOT_NAME);
|
|
|
|
if (rootNode == NULL)
|
|
|
|
{
|
|
|
|
CCLOG("can not create root node");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set root node
|
|
|
|
xmlDocSetRootElement(doc, rootNode);
|
|
|
|
|
|
|
|
// save xml file
|
|
|
|
xmlSaveFile(m_sFilePath.c_str(), doc);
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string& CCUserDefault::getXMLFilePath()
|
|
|
|
{
|
|
|
|
return m_sFilePath;
|
|
|
|
}
|
|
|
|
|
2012-02-02 14:18:13 +08:00
|
|
|
void CCUserDefault::flush()
|
|
|
|
{
|
|
|
|
// save to file
|
|
|
|
if (g_sharedDoc)
|
|
|
|
{
|
|
|
|
xmlSaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), g_sharedDoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-06 15:31:51 +08:00
|
|
|
NS_CC_END;
|