added ccuserdefault tinyxml2 ver

This commit is contained in:
martell 2013-01-26 02:41:27 +00:00
parent 684b46dd74
commit 05bece9d07
1 changed files with 179 additions and 154 deletions

View File

@ -24,8 +24,7 @@ THE SOFTWARE.
#include "CCUserDefault.h"
#include "platform/CCCommon.h"
#include "platform/CCFileUtils.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "tinyxml2/tinyxml2.h"
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
@ -36,8 +35,6 @@ using namespace std;
NS_CC_BEGIN
static xmlDocPtr g_sharedDoc = NULL;
/**
* define the functions here because we don't want to
* export xmlNodePtr and other types in "CCUserDefault.h"
@ -45,7 +42,7 @@ static xmlDocPtr g_sharedDoc = NULL;
static xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr *rootNode)
{
xmlNodePtr curNode = NULL;
tinyxml2::XMLElement* curNode = NULL;
// check the key value
if (! pKey)
@ -55,76 +52,78 @@ static xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr *rootNode)
do
{
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
*doc = xmlDoc;
CCFileData data(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(),"rt");
const char* pXmlBuffer = (const char*)data.getBuffer();
if(NULL == pXmlBuffer)
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(pXmlBuffer);
// get root node
*rootNode = xmlDocGetRootElement(g_sharedDoc);
*rootNode = xmlDoc->RootElement();
if (NULL == *rootNode)
{
CCLOG("read root node error");
break;
}
// find the node
curNode = (*rootNode)->xmlChildrenNode;
curNode = (*rootNode)->FirstChildElement();
while (NULL != curNode)
{
if (! xmlStrcmp(curNode->name, BAD_CAST pKey))
const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{
break;
}
curNode = curNode->next;
curNode = curNode->NextSiblingElement();
}
} while (0);
return curNode;
}
static inline const char* getValueForKey(const char* pKey)
{
const char* ret = NULL;
xmlNodePtr rootNode;
xmlNodePtr node = getXMLNodeForKey(pKey, &rootNode);
// find the node
if (node)
{
ret = (const char*)xmlNodeGetContent(node);
}
return ret;
}
static void setValueForKey(const char* pKey, const char* pValue)
{
xmlNodePtr rootNode;
xmlNodePtr node;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
// check the params
if (! pKey || ! pValue)
{
return;
}
// find the node
node = getXMLNodeForKey(pKey, &rootNode);
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// if node exist, change the content
if (node)
{
xmlNodeSetContent(node, BAD_CAST pValue);
node->FirstChild()->SetValue(pValue);
}
else
{
if (rootNode)
{
// the node doesn't exist, add a new one
// libxml in android doesn'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);
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;
}
}
/**
@ -141,19 +140,13 @@ bool CCUserDefault::m_sbIsFilePathInitialized = false;
*/
CCUserDefault::~CCUserDefault()
{
flush();
if (g_sharedDoc)
{
xmlFreeDoc(g_sharedDoc);
g_sharedDoc = NULL;
}
CC_SAFE_DELETE(m_spUserDefault);
m_spUserDefault = NULL;
}
CCUserDefault::CCUserDefault()
{
g_sharedDoc = xmlReadFile(getXMLFilePath().c_str(), "utf-8", XML_PARSE_RECOVER);
m_spUserDefault = NULL;
}
void CCUserDefault::purgeSharedUserDefault()
@ -169,15 +162,26 @@ void CCUserDefault::purgeSharedUserDefault()
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
const char* value = getValueForKey(pKey);
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"));
xmlFree((void*)value);
}
if (doc) delete doc;
return ret;
}
@ -188,15 +192,30 @@ int CCUserDefault::getIntegerForKey(const char* pKey)
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
const char* value = getValueForKey(pKey);
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);
xmlFree((void*)value);
}
if(doc)
{
delete doc;
}
return ret;
}
@ -219,15 +238,26 @@ double CCUserDefault::getDoubleForKey(const char* pKey)
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
const char* value = getValueForKey(pKey);
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);
xmlFree((void*)value);
}
if (doc) delete doc;
return ret;
}
@ -238,15 +268,26 @@ std::string CCUserDefault::getStringForKey(const char* pKey)
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
const char* value = getValueForKey(pKey);
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);
xmlFree((void*)value);
}
if (doc) delete doc;
return ret;
}
@ -358,39 +399,28 @@ void CCUserDefault::initXMLFilePath()
bool CCUserDefault::createXMLFile()
{
bool bRet = false;
xmlDocPtr doc = NULL;
do
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
if (NULL==pDoc)
{
// new doc
doc = xmlNewDoc(BAD_CAST"1.0");
if (doc == NULL)
{
CCLOG("can not create xml doc");
break;
return false;
}
// new root node
xmlNodePtr rootNode = xmlNewNode(NULL, BAD_CAST USERDEFAULT_ROOT_NAME);
if (rootNode == NULL)
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration("1.0");
if (NULL==pDeclaration)
{
CCLOG("can not create root node");
break;
return false;
}
// set root node
xmlDocSetRootElement(doc, rootNode);
// save xml file
xmlSaveFile(m_sFilePath.c_str(), doc);
bRet = true;
} while (0);
// if doc is not null, free it
if (doc)
pDoc->LinkEndChild(pDeclaration);
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
if (NULL==pRootEle)
{
xmlFreeDoc(doc);
return false;
}
pDoc->LinkEndChild(pRootEle);
bRet = pDoc->SaveFile(m_sFilePath.c_str());
if(pDoc)
{
delete pDoc;
}
return bRet;
@ -403,11 +433,6 @@ const string& CCUserDefault::getXMLFilePath()
void CCUserDefault::flush()
{
// save to file
if (g_sharedDoc)
{
xmlSaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), g_sharedDoc);
}
}
NS_CC_END