mirror of https://github.com/axmolengine/axmol.git
fixed #933: profile CCUserDefault
This commit is contained in:
parent
1ae8f001b8
commit
591b7d0619
|
@ -94,13 +94,17 @@ public:
|
||||||
@brief Set string value by key.
|
@brief Set string value by key.
|
||||||
*/
|
*/
|
||||||
void setStringForKey(const char* pKey, const std::string & value);
|
void setStringForKey(const char* pKey, const std::string & value);
|
||||||
|
/**
|
||||||
|
@brief Save content to xml file
|
||||||
|
*/
|
||||||
|
void flush();
|
||||||
|
|
||||||
static CCUserDefault* sharedUserDefault();
|
static CCUserDefault* sharedUserDefault();
|
||||||
static void purgeSharedUserDefault();
|
static void purgeSharedUserDefault();
|
||||||
const static std::string& getXMLFilePath();
|
const static std::string& getXMLFilePath();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CCUserDefault() {}
|
CCUserDefault();
|
||||||
static bool createXMLFile();
|
static bool createXMLFile();
|
||||||
static bool isXMLFileExist();
|
static bool isXMLFileExist();
|
||||||
static void initXMLFilePath();
|
static void initXMLFilePath();
|
||||||
|
|
|
@ -42,12 +42,14 @@ using namespace std;
|
||||||
|
|
||||||
NS_CC_BEGIN;
|
NS_CC_BEGIN;
|
||||||
|
|
||||||
|
static xmlDocPtr g_sharedDoc = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* define the functions here because we don't want to
|
* define the functions here because we don't want to
|
||||||
* export xmlNodePtr and other types in "CCUserDefault.h"
|
* export xmlNodePtr and other types in "CCUserDefault.h"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr *rootNode, xmlDocPtr *doc)
|
static xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr *rootNode)
|
||||||
{
|
{
|
||||||
xmlNodePtr curNode = NULL;
|
xmlNodePtr curNode = NULL;
|
||||||
|
|
||||||
|
@ -59,16 +61,8 @@ static xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr *rootNode, xmlDo
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
// read doc
|
|
||||||
*doc = xmlReadFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), "utf-8", XML_PARSE_RECOVER);
|
|
||||||
if (NULL == *doc)
|
|
||||||
{
|
|
||||||
CCLOG("can not read xml file");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get root node
|
// get root node
|
||||||
*rootNode = xmlDocGetRootElement(*doc);
|
*rootNode = xmlDocGetRootElement(g_sharedDoc);
|
||||||
if (NULL == *rootNode)
|
if (NULL == *rootNode)
|
||||||
{
|
{
|
||||||
CCLOG("read root node error");
|
CCLOG("read root node error");
|
||||||
|
@ -95,8 +89,7 @@ static inline const char* getValueForKey(const char* pKey)
|
||||||
{
|
{
|
||||||
const char* ret = NULL;
|
const char* ret = NULL;
|
||||||
xmlNodePtr rootNode;
|
xmlNodePtr rootNode;
|
||||||
xmlDocPtr doc;
|
xmlNodePtr node = getXMLNodeForKey(pKey, &rootNode);
|
||||||
xmlNodePtr node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
|
||||||
|
|
||||||
// find the node
|
// find the node
|
||||||
if (node)
|
if (node)
|
||||||
|
@ -104,19 +97,12 @@ static inline const char* getValueForKey(const char* pKey)
|
||||||
ret = (const char*)xmlNodeGetContent(node);
|
ret = (const char*)xmlNodeGetContent(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
// free doc
|
|
||||||
if (doc)
|
|
||||||
{
|
|
||||||
xmlFreeDoc(doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setValueForKey(const char* pKey, const char* pValue)
|
static void setValueForKey(const char* pKey, const char* pValue)
|
||||||
{
|
{
|
||||||
xmlNodePtr rootNode;
|
xmlNodePtr rootNode;
|
||||||
xmlDocPtr doc;
|
|
||||||
xmlNodePtr node;
|
xmlNodePtr node;
|
||||||
|
|
||||||
// check the params
|
// check the params
|
||||||
|
@ -126,7 +112,7 @@ static void setValueForKey(const char* pKey, const char* pValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the node
|
// find the node
|
||||||
node = getXMLNodeForKey(pKey, &rootNode, &doc);
|
node = getXMLNodeForKey(pKey, &rootNode);
|
||||||
|
|
||||||
// if node exist, change the content
|
// if node exist, change the content
|
||||||
if (node)
|
if (node)
|
||||||
|
@ -145,13 +131,6 @@ static void setValueForKey(const char* pKey, const char* pValue)
|
||||||
xmlAddChild(tmpNode, content);
|
xmlAddChild(tmpNode, content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// save file and free doc
|
|
||||||
if (doc)
|
|
||||||
{
|
|
||||||
xmlSaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(),doc);
|
|
||||||
xmlFreeDoc(doc);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -168,9 +147,21 @@ bool CCUserDefault::m_sbIsFilePathInitialized = false;
|
||||||
*/
|
*/
|
||||||
CCUserDefault::~CCUserDefault()
|
CCUserDefault::~CCUserDefault()
|
||||||
{
|
{
|
||||||
|
flush();
|
||||||
|
if (g_sharedDoc)
|
||||||
|
{
|
||||||
|
xmlFreeDoc(g_sharedDoc);
|
||||||
|
g_sharedDoc = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
m_spUserDefault = NULL;
|
m_spUserDefault = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CCUserDefault::CCUserDefault()
|
||||||
|
{
|
||||||
|
g_sharedDoc = xmlReadFile(getXMLFilePath().c_str(), "utf-8", XML_PARSE_RECOVER);
|
||||||
|
}
|
||||||
|
|
||||||
void CCUserDefault::purgeSharedUserDefault()
|
void CCUserDefault::purgeSharedUserDefault()
|
||||||
{
|
{
|
||||||
CC_SAFE_DELETE(m_spUserDefault);
|
CC_SAFE_DELETE(m_spUserDefault);
|
||||||
|
@ -348,7 +339,7 @@ void CCUserDefault::initXMLFilePath()
|
||||||
bool CCUserDefault::createXMLFile()
|
bool CCUserDefault::createXMLFile()
|
||||||
{
|
{
|
||||||
bool bRet = false;
|
bool bRet = false;
|
||||||
xmlDocPtr doc = NULL;
|
xmlDocPtr doc = NULL;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
@ -377,12 +368,6 @@ bool CCUserDefault::createXMLFile()
|
||||||
bRet = true;
|
bRet = true;
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
// if doc is not null, free it
|
|
||||||
if (doc)
|
|
||||||
{
|
|
||||||
xmlFreeDoc(doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bRet;
|
return bRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,4 +376,13 @@ const string& CCUserDefault::getXMLFilePath()
|
||||||
return m_sFilePath;
|
return m_sFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CCUserDefault::flush()
|
||||||
|
{
|
||||||
|
// save to file
|
||||||
|
if (g_sharedDoc)
|
||||||
|
{
|
||||||
|
xmlSaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), g_sharedDoc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NS_CC_END;
|
NS_CC_END;
|
||||||
|
|
|
@ -50,6 +50,8 @@ void UserDefaultTest::doTest()
|
||||||
{
|
{
|
||||||
CCLOG("bool is false");
|
CCLOG("bool is false");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//CCUserDefault::sharedUserDefault()->flush();
|
||||||
|
|
||||||
CCLOG("********************** after change value ***********************");
|
CCLOG("********************** after change value ***********************");
|
||||||
|
|
||||||
|
@ -61,6 +63,8 @@ void UserDefaultTest::doTest()
|
||||||
CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.6);
|
CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.6);
|
||||||
CCUserDefault::sharedUserDefault()->setBoolForKey("bool", false);
|
CCUserDefault::sharedUserDefault()->setBoolForKey("bool", false);
|
||||||
|
|
||||||
|
CCUserDefault::sharedUserDefault()->flush();
|
||||||
|
|
||||||
// print value
|
// print value
|
||||||
|
|
||||||
ret = CCUserDefault::sharedUserDefault()->getStringForKey("string");
|
ret = CCUserDefault::sharedUserDefault()->getStringForKey("string");
|
||||||
|
|
Loading…
Reference in New Issue