fix a bug of CCUserDefault

This commit is contained in:
minggo 2013-02-26 12:44:20 +08:00
parent 8713072c40
commit 7941d70a04
1 changed files with 14 additions and 10 deletions

View File

@ -101,28 +101,32 @@ static void setValueForKey(const char* pKey, const char* pValue)
// find the node // find the node
node = getXMLNodeForKey(pKey, &rootNode, &doc); node = getXMLNodeForKey(pKey, &rootNode, &doc);
// if node exist, change the content // if node exist, change the content
if (node && node->FirstChild()) if (node)
{ {
node->FirstChild()->SetValue(pValue); if (node->FirstChild())
{
node->FirstChild()->SetValue(pValue);
}
else
{
tinyxml2::XMLText* content = doc->NewText(pValue);
node->LinkEndChild(content);
}
} }
else else
{ {
if (rootNode) if (rootNode)
{ {
tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey); tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
rootNode->LinkEndChild(tmpNode); rootNode->LinkEndChild(tmpNode);
tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue); tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
tmpNode->LinkEndChild(content); tmpNode->LinkEndChild(content);
} }
} }
// save file and free doc
// save file and free doc
if (doc) if (doc)
{ {
doc->SaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str()); doc->SaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str());
delete doc; delete doc;
} }
@ -169,7 +173,7 @@ bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
tinyxml2::XMLElement* node; tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc); node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node // find the node
if (node) if (node && node->FirstChild())
{ {
value = (const char*)(node->FirstChild()->Value()); value = (const char*)(node->FirstChild()->Value());
} }
@ -199,7 +203,7 @@ int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
tinyxml2::XMLElement* node; tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc); node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node // find the node
if (node) if (node && node->FirstChild())
{ {
value = (const char*)(node->FirstChild()->Value()); value = (const char*)(node->FirstChild()->Value());
} }
@ -245,7 +249,7 @@ double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
tinyxml2::XMLElement* node; tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc); node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node // find the node
if (node) if (node && node->FirstChild())
{ {
value = (const char*)(node->FirstChild()->Value()); value = (const char*)(node->FirstChild()->Value());
} }