Merge pull request #1959 from minggo/tinyxml

use tinyxml2 instead of libxml2
This commit is contained in:
minggo 2013-02-06 00:43:00 -08:00
commit 7672eaba58
14 changed files with 4282 additions and 206 deletions

View File

@ -114,6 +114,7 @@ support/ccUtils.cpp \
support/CCVertex.cpp \ support/CCVertex.cpp \
support/data_support/ccCArray.cpp \ support/data_support/ccCArray.cpp \
support/image_support/TGAlib.cpp \ support/image_support/TGAlib.cpp \
support/tinyxml2/tinyxml2.cpp \
support/zip_support/ZipUtils.cpp \ support/zip_support/ZipUtils.cpp \
support/zip_support/ioapi.cpp \ support/zip_support/ioapi.cpp \
support/zip_support/unzip.cpp \ support/zip_support/unzip.cpp \
@ -167,6 +168,5 @@ include $(BUILD_STATIC_LIBRARY)
$(call import-module,libjpeg) $(call import-module,libjpeg)
$(call import-module,libpng) $(call import-module,libpng)
$(call import-module,libxml2)
$(call import-module,libtiff) $(call import-module,libtiff)
$(call import-module,libwebp) $(call import-module,libwebp)

View File

@ -215,6 +215,7 @@ THE SOFTWARE.
#include "support/CCProfiling.h" #include "support/CCProfiling.h"
#include "support/CCUserDefault.h" #include "support/CCUserDefault.h"
#include "support/CCVertex.h" #include "support/CCVertex.h"
#include "support/tinyxml2/tinyxml2.h"
// text_input_node // text_input_node
#include "text_input_node/CCIMEDelegate.h" #include "text_input_node/CCIMEDelegate.h"

View File

@ -34,10 +34,6 @@ using namespace std;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) #if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
NS_CC_BEGIN NS_CC_BEGIN
typedef enum typedef enum

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2010 cocos2d-x.org http://cocos2d-x.org Copyright (c) 2010 cocos2d-x.org http://cocos2d-x.org
Copyright (c) 2010 Максим Аксенов Copyright (c) 2010 Максим Аксенов
Copyright (c) 2013 Martell Malone
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -21,15 +22,70 @@
THE SOFTWARE. THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include "CCSAXParser.h" #include "CCSAXParser.h"
#include "cocoa/CCDictionary.h" #include "cocoa/CCDictionary.h"
#include "CCFileUtils.h" #include "CCFileUtils.h"
#include "support/tinyxml2/tinyxml2.h"
#include <vector> // because its based on windows 8 build :P
NS_CC_BEGIN NS_CC_BEGIN
class XmlSaxHander : public tinyxml2::XMLVisitor
{
public:
XmlSaxHander():m_ccsaxParserImp(0){};
virtual bool VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute );
virtual bool VisitExit( const tinyxml2::XMLElement& element );
virtual bool Visit( const tinyxml2::XMLText& text );
virtual bool Visit( const tinyxml2::XMLUnknown&){ return true; }
void setCCSAXParserImp(CCSAXParser* parser)
{
m_ccsaxParserImp = parser;
}
private:
CCSAXParser *m_ccsaxParserImp;
};
bool XmlSaxHander::VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute )
{
//CCLog(" VisitEnter %s",element.Value());
std::vector<const char*> attsVector;
for( const tinyxml2::XMLAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
{
//CCLog("%s", attrib->Name());
attsVector.push_back(attrib->Name());
//CCLog("%s",attrib->Value());
attsVector.push_back(attrib->Value());
}
// nullptr is used in c++11
//attsVector.push_back(nullptr);
attsVector.push_back(NULL);
CCSAXParser::startElement(m_ccsaxParserImp, (const CC_XML_CHAR *)element.Value(), (const CC_XML_CHAR **)(&attsVector[0]));
return true;
}
bool XmlSaxHander::VisitExit( const tinyxml2::XMLElement& element )
{
//CCLog("VisitExit %s",element.Value());
CCSAXParser::endElement(m_ccsaxParserImp, (const CC_XML_CHAR *)element.Value());
return true;
}
bool XmlSaxHander::Visit( const tinyxml2::XMLText& text )
{
//CCLog("Visit %s",text.Value());
CCSAXParser::textHandler(m_ccsaxParserImp, (const CC_XML_CHAR *)text.Value(), strlen(text.Value()));
return true;
}
CCSAXParser::CCSAXParser() CCSAXParser::CCSAXParser()
{ {
m_pDelegator = NULL; m_pDelegator = NULL;
@ -48,37 +104,12 @@ bool CCSAXParser::init(const char *pszEncoding)
bool CCSAXParser::parse(const char* pXMLData, unsigned int uDataLength) bool CCSAXParser::parse(const char* pXMLData, unsigned int uDataLength)
{ {
/* tinyxml2::XMLDocument tinyDoc;
* this initializes the library and checks potential ABI mismatches tinyDoc.Parse(pXMLData);
* between the version it was compiled for and the actual shared XmlSaxHander printer;
* library used. printer.setCCSAXParserImp(this);
*/
LIBXML_TEST_VERSION
xmlSAXHandler saxHandler;
memset( &saxHandler, 0, sizeof(saxHandler) );
// Using xmlSAXVersion( &saxHandler, 2 ) generate crash as it sets plenty of other pointers...
saxHandler.initialized = XML_SAX2_MAGIC; // so we do this to force parsing as SAX2.
saxHandler.startElement = &CCSAXParser::startElement;
saxHandler.endElement = &CCSAXParser::endElement;
saxHandler.characters = &CCSAXParser::textHandler;
int result = xmlSAXUserParseMemory( &saxHandler, this, pXMLData, uDataLength ); return tinyDoc.Accept( &printer );
if ( result != 0 )
{
return false;
}
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
xmlMemoryDump();
#endif
return true;
} }
bool CCSAXParser::parse(const char *pszFile) bool CCSAXParser::parse(const char *pszFile)

View File

@ -1 +1 @@
9c70c4fcfd0e094b9bb09f4a8e7f7a82b76caac3 81abe6adef9de3477eb86bdb9c31bd542aa0eef8

View File

@ -19,7 +19,6 @@ INCLUDES = -I.. \
-I../../external/chipmunk/include/chipmunk \ -I../../external/chipmunk/include/chipmunk \
-I../../extensions/network \ -I../../extensions/network \
-I../platform/linux \ -I../platform/linux \
-I../platform/third_party/linux/libxml2 \
-I../platform/third_party/linux/libpng \ -I../platform/third_party/linux/libpng \
-I../platform/third_party/linux/libjpeg \ -I../platform/third_party/linux/libjpeg \
-I../platform/third_party/linux/libtiff/include \ -I../platform/third_party/linux/libtiff/include \
@ -113,6 +112,7 @@ OBJECTS = ../actions/CCAction.o \
../support/CCVertex.o \ ../support/CCVertex.o \
../support/CCNotificationCenter.o \ ../support/CCNotificationCenter.o \
../support/image_support/TGAlib.o \ ../support/image_support/TGAlib.o \
../support/tinyxml2/tinyxml2.cpp \
../support/zip_support/ZipUtils.o \ ../support/zip_support/ZipUtils.o \
../support/zip_support/ioapi.o \ ../support/zip_support/ioapi.o \
../support/zip_support/unzip.o \ ../support/zip_support/unzip.o \
@ -163,7 +163,6 @@ STATICLIBS_DIR = ../platform/third_party/linux/libraries
endif endif
STATICLIBS = STATICLIBS =
STATICLIBS = $(STATICLIBS_DIR)/libfreetype.a \ STATICLIBS = $(STATICLIBS_DIR)/libfreetype.a \
$(STATICLIBS_DIR)/libxml2.a \
$(STATICLIBS_DIR)/libpng.a \ $(STATICLIBS_DIR)/libpng.a \
$(STATICLIBS_DIR)/libjpeg.a \ $(STATICLIBS_DIR)/libjpeg.a \
$(STATICLIBS_DIR)/libtiff.a \ $(STATICLIBS_DIR)/libtiff.a \

View File

@ -1 +1 @@
302344d8d667c49b09f5f558cd10008f0864c71f b9e66ce70e5a94fb03e1bc5ce1189d7e86f31aaa

View File

@ -16,7 +16,6 @@ subprojects
{ {
zlib zlib
libpng libpng
libxml2
freetype freetype
libjpeg libjpeg
libtiff libtiff
@ -47,6 +46,7 @@ includepaths
"../script_support" "../script_support"
"../sprite_nodes" "../sprite_nodes"
"../support" "../support"
"../support/tinyxml2"
"../text_input_node" "../text_input_node"
"../textures" "../textures"
"../tileMap_parallax_nodes" "../tileMap_parallax_nodes"
@ -179,6 +179,9 @@ files
"*.cpp" # MH: Many ccCArray linker errors without ccCArray.cpp "*.cpp" # MH: Many ccCArray linker errors without ccCArray.cpp
"*.h" "*.h"
("../support/image_support") ("../support/image_support")
"*.h"
"*.cpp"
("../support/tinyxml2")
"*.h" "*.h"
"*.cpp" "*.cpp"
("../support/zip_support") ("../support/zip_support")

View File

@ -69,7 +69,7 @@
</PreBuildEvent> </PreBuildEvent>
<ClCompile> <ClCompile>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(ProjectDir)..;$(ProjectDir)..\platform\win32;$(ProjectDir)..\platform\third_party\win32\iconv;$(ProjectDir)..\platform\third_party\win32\zlib;$(ProjectDir)..\platform\third_party\win32\libpng;$(ProjectDir)..\platform\third_party\win32\libjpeg;$(ProjectDir)..\platform\third_party\win32\libtiff;$(ProjectDir)..\platform\third_party\win32\libxml2;$(ProjectDir)..\platform\third_party\win32\libwebp;$(ProjectDir)..\platform\third_party\win32\pthread;$(ProjectDir)..\platform\third_party\win32\OGLES;..\include;$(ProjectDir)..\kazmath\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(ProjectDir)..;$(ProjectDir)..\platform\win32;$(ProjectDir)..\platform\third_party\win32\iconv;$(ProjectDir)..\platform\third_party\win32\zlib;$(ProjectDir)..\platform\third_party\win32\libpng;$(ProjectDir)..\platform\third_party\win32\libjpeg;$(ProjectDir)..\platform\third_party\win32\libtiff;$(ProjectDir)..\platform\third_party\win32\libwebp;$(ProjectDir)..\platform\third_party\win32\pthread;$(ProjectDir)..\platform\third_party\win32\OGLES;..\include;$(ProjectDir)..\kazmath\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;COCOS2DXWIN32_EXPORTS;GL_GLEXT_PROTOTYPES;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;COCOS2DXWIN32_EXPORTS;GL_GLEXT_PROTOTYPES;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild> <MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
@ -86,7 +86,7 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir
</Command> </Command>
</PreLinkEvent> </PreLinkEvent>
<Link> <Link>
<AdditionalDependencies>opengl32.lib;glew32.lib;libxml2.lib;libzlib.lib;libpng.lib;libjpeg.lib;libtiff.lib;libwebp.lib;libiconv.lib;pthreadVCE2.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>opengl32.lib;glew32.lib;libzlib.lib;libpng.lib;libjpeg.lib;libtiff.lib;libwebp.lib;libiconv.lib;pthreadVCE2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)$(ProjectName).dll</OutputFile> <OutputFile>$(OutDir)$(ProjectName).dll</OutputFile>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries> <IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
@ -107,7 +107,7 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir
</Command> </Command>
</PreBuildEvent> </PreBuildEvent>
<ClCompile> <ClCompile>
<AdditionalIncludeDirectories>$(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(ProjectDir)..;$(ProjectDir)..\platform\win32;$(ProjectDir)..\platform\third_party\win32\iconv;$(ProjectDir)..\platform\third_party\win32\zlib;$(ProjectDir)..\platform\third_party\win32\libpng;$(ProjectDir)..\platform\third_party\win32\libjpeg;$(ProjectDir)..\platform\third_party\win32\libtiff;$(ProjectDir)..\platform\third_party\win32\libxml2;$(ProjectDir)..\platform\third_party\win32\libwebp;$(ProjectDir)..\platform\third_party\win32\pthread;$(ProjectDir)..\platform\third_party\win32\OGLES;..\include;$(ProjectDir)..\kazmath\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(ProjectDir)..;$(ProjectDir)..\platform\win32;$(ProjectDir)..\platform\third_party\win32\iconv;$(ProjectDir)..\platform\third_party\win32\zlib;$(ProjectDir)..\platform\third_party\win32\libpng;$(ProjectDir)..\platform\third_party\win32\libjpeg;$(ProjectDir)..\platform\third_party\win32\libtiff;$(ProjectDir)..\platform\third_party\win32\libwebp;$(ProjectDir)..\platform\third_party\win32\pthread;$(ProjectDir)..\platform\third_party\win32\OGLES;..\include;$(ProjectDir)..\kazmath\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;COCOS2DXWIN32_EXPORTS;GL_GLEXT_PROTOTYPES;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;COCOS2DXWIN32_EXPORTS;GL_GLEXT_PROTOTYPES;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<PrecompiledHeader> <PrecompiledHeader>
@ -122,7 +122,7 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir
</Command> </Command>
</PreLinkEvent> </PreLinkEvent>
<Link> <Link>
<AdditionalDependencies>opengl32.lib;glew32.lib;libxml2.lib;libzlib.lib;libpng.lib;libjpeg.lib;libtiff.lib;libwebp.lib;libiconv.lib;pthreadVCE2.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>opengl32.lib;glew32.lib;libzlib.lib;libpng.lib;libjpeg.lib;libtiff.lib;libwebp.lib;libiconv.lib;pthreadVCE2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>$(OutDir)$(ProjectName).dll</OutputFile> <OutputFile>$(OutDir)$(ProjectName).dll</OutputFile>
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<IgnoreSpecificDefaultLibraries> ;%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries> <IgnoreSpecificDefaultLibraries> ;%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
@ -216,6 +216,7 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir
<ClCompile Include="..\support\CCUserDefault.cpp" /> <ClCompile Include="..\support\CCUserDefault.cpp" />
<ClCompile Include="..\support\ccUtils.cpp" /> <ClCompile Include="..\support\ccUtils.cpp" />
<ClCompile Include="..\support\CCVertex.cpp" /> <ClCompile Include="..\support\CCVertex.cpp" />
<ClCompile Include="..\support\tinyxml2\tinyxml2.cpp" />
<ClCompile Include="..\support\TransformUtils.cpp" /> <ClCompile Include="..\support\TransformUtils.cpp" />
<ClCompile Include="..\support\data_support\ccCArray.cpp" /> <ClCompile Include="..\support\data_support\ccCArray.cpp" />
<ClCompile Include="..\support\image_support\TGAlib.cpp" /> <ClCompile Include="..\support\image_support\TGAlib.cpp" />
@ -366,6 +367,7 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir
<ClInclude Include="..\support\CCUserDefault.h" /> <ClInclude Include="..\support\CCUserDefault.h" />
<ClInclude Include="..\support\ccUtils.h" /> <ClInclude Include="..\support\ccUtils.h" />
<ClInclude Include="..\support\CCVertex.h" /> <ClInclude Include="..\support\CCVertex.h" />
<ClInclude Include="..\support\tinyxml2\tinyxml2.h" />
<ClInclude Include="..\support\TransformUtils.h" /> <ClInclude Include="..\support\TransformUtils.h" />
<ClInclude Include="..\support\data_support\ccCArray.h" /> <ClInclude Include="..\support\data_support\ccCArray.h" />
<ClInclude Include="..\support\data_support\uthash.h" /> <ClInclude Include="..\support\data_support\uthash.h" />

View File

@ -94,6 +94,9 @@
<Filter Include="draw_nodes"> <Filter Include="draw_nodes">
<UniqueIdentifier>{1179d205-d065-49f0-8457-bc4c3f1d0cb3}</UniqueIdentifier> <UniqueIdentifier>{1179d205-d065-49f0-8457-bc4c3f1d0cb3}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="support\tinyxml2">
<UniqueIdentifier>{cc25bb83-527d-4218-8d68-ebf963ce7698}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\base_nodes\CCAtlasNode.cpp"> <ClCompile Include="..\base_nodes\CCAtlasNode.cpp">
@ -443,6 +446,7 @@
<ClCompile Include="..\platform\CCImageCommonWebp.cpp"> <ClCompile Include="..\platform\CCImageCommonWebp.cpp">
<Filter>platform</Filter> <Filter>platform</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\support\tinyxml2\tinyxml2.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\base_nodes\CCAtlasNode.h"> <ClInclude Include="..\base_nodes\CCAtlasNode.h">
@ -893,5 +897,6 @@
<ClInclude Include="..\platform\win32\CCFileUtilsWin32.h"> <ClInclude Include="..\platform\win32\CCFileUtilsWin32.h">
<Filter>platform\win32</Filter> <Filter>platform\win32</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\support\tinyxml2\tinyxml2.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -24,8 +24,7 @@ THE SOFTWARE.
#include "CCUserDefault.h" #include "CCUserDefault.h"
#include "platform/CCCommon.h" #include "platform/CCCommon.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include <libxml/parser.h> #include "tinyxml2/tinyxml2.h"
#include <libxml/tree.h>
// root name of xml // root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot" #define USERDEFAULT_ROOT_NAME "userDefaultRoot"
@ -36,16 +35,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) static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLElement** rootNode, tinyxml2::XMLDocument **doc)
{ {
xmlNodePtr curNode = NULL; tinyxml2::XMLElement* curNode = NULL;
// check the key value // check the key value
if (! pKey) if (! pKey)
@ -55,76 +52,80 @@ static xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr *rootNode)
do do
{ {
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
*doc = xmlDoc;
//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();
if(NULL == pXmlBuffer)
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(pXmlBuffer);
// get root node // get root node
*rootNode = xmlDocGetRootElement(g_sharedDoc); *rootNode = xmlDoc->RootElement();
if (NULL == *rootNode) if (NULL == *rootNode)
{ {
CCLOG("read root node error"); CCLOG("read root node error");
break; break;
} }
// find the node // find the node
curNode = (*rootNode)->xmlChildrenNode; curNode = (*rootNode)->FirstChildElement();
while (NULL != curNode) while (NULL != curNode)
{ {
if (! xmlStrcmp(curNode->name, BAD_CAST pKey)) const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{ {
break; break;
} }
curNode = curNode->next; curNode = curNode->NextSiblingElement();
} }
} while (0); } while (0);
return curNode; 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) static void setValueForKey(const char* pKey, const char* pValue)
{ {
xmlNodePtr rootNode; tinyxml2::XMLElement* rootNode;
xmlNodePtr node; tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
// check the params // check the params
if (! pKey || ! pValue) if (! pKey || ! pValue)
{ {
return; return;
} }
// find the node // find the node
node = getXMLNodeForKey(pKey, &rootNode); node = getXMLNodeForKey(pKey, &rootNode, &doc);
// if node exist, change the content // if node exist, change the content
if (node) if (node)
{ {
xmlNodeSetContent(node, BAD_CAST pValue); node->FirstChild()->SetValue(pValue);
} }
else else
{ {
if (rootNode) if (rootNode)
{ {
// the node doesn't exist, add a new one
// libxml in android doesn't support xmlNewTextChild, so use this approach tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
xmlNodePtr tmpNode = xmlNewNode(NULL, BAD_CAST pKey); rootNode->LinkEndChild(tmpNode);
xmlNodePtr content = xmlNewText(BAD_CAST pValue); tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
xmlAddChild(rootNode, tmpNode); tmpNode->LinkEndChild(content);
xmlAddChild(tmpNode, content);
} }
} }
// save file and free doc
if (doc)
{
doc->SaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str());
delete doc;
}
} }
/** /**
@ -141,19 +142,13 @@ bool CCUserDefault::m_sbIsFilePathInitialized = false;
*/ */
CCUserDefault::~CCUserDefault() CCUserDefault::~CCUserDefault()
{ {
flush(); CC_SAFE_DELETE(m_spUserDefault);
if (g_sharedDoc)
{
xmlFreeDoc(g_sharedDoc);
g_sharedDoc = NULL;
}
m_spUserDefault = NULL; m_spUserDefault = NULL;
} }
CCUserDefault::CCUserDefault() CCUserDefault::CCUserDefault()
{ {
g_sharedDoc = xmlReadFile(getXMLFilePath().c_str(), "utf-8", XML_PARSE_RECOVER); m_spUserDefault = NULL;
} }
void CCUserDefault::purgeSharedUserDefault() void CCUserDefault::purgeSharedUserDefault()
@ -169,15 +164,26 @@ void CCUserDefault::purgeSharedUserDefault()
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue) 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; bool ret = defaultValue;
if (value) if (value)
{ {
ret = (! strcmp(value, "true")); ret = (! strcmp(value, "true"));
xmlFree((void*)value);
} }
if (doc) delete doc;
return ret; return ret;
} }
@ -188,15 +194,30 @@ int CCUserDefault::getIntegerForKey(const char* pKey)
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue) 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; int ret = defaultValue;
if (value) if (value)
{ {
ret = atoi(value); ret = atoi(value);
xmlFree((void*)value);
} }
if(doc)
{
delete doc;
}
return ret; return ret;
} }
@ -219,15 +240,26 @@ double CCUserDefault::getDoubleForKey(const char* pKey)
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue) 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; double ret = defaultValue;
if (value) if (value)
{ {
ret = atof(value); ret = atof(value);
xmlFree((void*)value);
} }
if (doc) delete doc;
return ret; return ret;
} }
@ -238,15 +270,26 @@ std::string CCUserDefault::getStringForKey(const char* pKey)
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue) 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; string ret = defaultValue;
if (value) if (value)
{ {
ret = string(value); ret = string(value);
xmlFree((void*)value);
} }
if (doc) delete doc;
return ret; return ret;
} }
@ -358,39 +401,28 @@ void CCUserDefault::initXMLFilePath()
bool CCUserDefault::createXMLFile() bool CCUserDefault::createXMLFile()
{ {
bool bRet = false; bool bRet = false;
xmlDocPtr doc = NULL; tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
if (NULL==pDoc)
do
{ {
// new doc return false;
doc = xmlNewDoc(BAD_CAST"1.0");
if (doc == NULL)
{
CCLOG("can not create xml doc");
break;
} }
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration("1.0");
// new root node if (NULL==pDeclaration)
xmlNodePtr rootNode = xmlNewNode(NULL, BAD_CAST USERDEFAULT_ROOT_NAME);
if (rootNode == NULL)
{ {
CCLOG("can not create root node"); return false;
break;
} }
pDoc->LinkEndChild(pDeclaration);
// set root node tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
xmlDocSetRootElement(doc, rootNode); if (NULL==pRootEle)
// save xml file
xmlSaveFile(m_sFilePath.c_str(), doc);
bRet = true;
} while (0);
// if doc is not null, free it
if (doc)
{ {
xmlFreeDoc(doc); return false;
}
pDoc->LinkEndChild(pRootEle);
bRet = pDoc->SaveFile(m_sFilePath.c_str());
if(pDoc)
{
delete pDoc;
} }
return bRet; return bRet;
@ -403,11 +435,6 @@ const string& CCUserDefault::getXMLFilePath()
void CCUserDefault::flush() void CCUserDefault::flush()
{ {
// save to file
if (g_sharedDoc)
{
xmlSaveFile(CCUserDefault::sharedUserDefault()->getXMLFilePath().c_str(), g_sharedDoc);
}
} }
NS_CC_END NS_CC_END

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
e61db43bc8354b24f352187dd11c2cc4edf62ed0 f43bbe90da807bd211fb4463ea65bf0f3aa8faa4