issue #7, add xml parser

This commit is contained in:
Walzer 2010-07-30 04:02:36 +00:00
parent 8ab8a1bb57
commit a35d71a4fe
3 changed files with 162 additions and 3 deletions

View File

@ -40,7 +40,7 @@
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories=".\include;.\Res;..\..\PRJ_TG3\Include;..\..\PRJ_TG3\Include\MTAPI;..\..\PRJ_TG3\Include\ThirdParty\gles;..\..\PRJ_TG3\Include\TCOM;..\..\PRJ_TG3\TG3\Include;..\..\PRJ_TG3\TG3\TG3_Implement;..\..\PRJ_TG3\EOS_SYS;..\..\PRJ_TG3\Common\SoftSupport;..\..\PRJ_TG3\Common\ICU\Include;.\"
AdditionalIncludeDirectories="..\..\PRJ_TG3\Include\ThirdParty\iconv;..\..\PRJ_TG3\Include\ThirdParty\libxml2;.\include;.\Res;..\..\PRJ_TG3\Include;..\..\PRJ_TG3\Include\MTAPI;..\..\PRJ_TG3\Include\ThirdParty\gles;..\..\PRJ_TG3\Include\TCOM;..\..\PRJ_TG3\TG3\Include;..\..\PRJ_TG3\TG3\TG3_Implement;..\..\PRJ_TG3\EOS_SYS;..\..\PRJ_TG3\Common\SoftSupport;..\..\PRJ_TG3\Common\ICU\Include;.\"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_TRANZDA_VM_;SS_MAKEDLL;__TG3_PURE_DLL__"
MinimalRebuild="true"
BasicRuntimeChecks="3"
@ -64,7 +64,7 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="WS2_32.Lib EosConfig.lib SoftSupport.lib TG3_DLL.lib libEGL.lib libgles_cl.lib libgles_cm.lib"
AdditionalDependencies="WS2_32.Lib EosConfig.lib SoftSupport.lib TG3_DLL.lib libEGL.lib libgles_cl.lib libgles_cm.lib libxml2.lib"
OutputFile="$(OutDir)/libcocos2d.dll"
LinkIncremental="2"
AdditionalLibraryDirectories="../../PRJ_TG3/Common/ICU/lib;../../PRJ_TG3/Mtapi/Win32/lib;../../PRJ_TG3/LIB/Win32Lib;../../PRJ_TG3/Common/SoftSupport"
@ -192,6 +192,10 @@
<Filter
Name="base_nodes"
>
<File
RelativePath=".\base_nodes\CCAtlasNode.cpp"
>
</File>
<File
RelativePath=".\base_nodes\CCNode.cpp"
>

View File

@ -22,9 +22,154 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include <string>
#include <stack>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include "CCFileUtils.h"
#include <string.h>
#include "Cocos2dDefine.h"
typedef std::pair<std::string, void*> Pair;
typedef enum
{
SAX_NONE = 0,
SAX_KEY,
SAX_DICT,
SAX_INT,
SAX_REAL
}CCSAXState;
class CCDictMaker
{
public:
CCDictionary *m_pRootDict;
CCDictionary *m_pCurDict;
std::stack<CCDictionary*> m_tDictStack;
std::string m_sCurKey;///< parsed key
CCSAXState m_tState;
public:
CCDictMaker()
{
m_pRootDict = NULL;
m_pCurDict = NULL;
m_tState = SAX_NONE;
}
~CCDictMaker()
{
}
CCDictionary *dictionaryWithContentsOfFile(const char *pFileName)
{
FILE *fp;
if( !(fp = fopen(pFileName, "r")) )
{
return NULL;
}
fseek(fp,0,SEEK_END);
int size = ftell(fp);
fseek(fp,0,SEEK_SET);
char *buffer = new char[size];
fread(buffer,sizeof(char),size,fp);
/*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
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 = &startElement;
saxHandler.endElement = &endElement;
saxHandler.characters = &characters;
int result = xmlSAXUserParseMemory( &saxHandler, this, buffer, size );
if ( result != 0 )
{
return NULL;
}
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();
delete []buffer;
return m_pRootDict;
}
};
void startElement(void *ctx, const xmlChar *name, const xmlChar **atts)
{
CCDictMaker *pMaker = static_cast<CCDictMaker*>(ctx);
std::string sName((char*)name);
if( sName == "dict" )
{
CCDictionary *pNewDict = new CCDictionary();
if(! pMaker->m_pRootDict)
{
pMaker->m_pRootDict = pNewDict;
}
else
{
NSAssert(pMaker->m_pCurDict && !pMaker->m_sCurKey.empty(), "");
pMaker->m_pCurDict->insert( Pair(pMaker->m_sCurKey, pNewDict) );
pMaker->m_sCurKey.clear();
}
pMaker->m_pCurDict = pNewDict;
pMaker->m_tDictStack.push(pMaker->m_pCurDict);
pMaker->m_tState = SAX_DICT;
}
else if(sName == "key")
{
pMaker->m_tState = SAX_KEY;
}
else if(sName == "integer")
{
pMaker->m_tState = SAX_INT;
}
else if(sName == "real")
{
pMaker->m_tState = SAX_REAL;
}
else
{
pMaker->m_tState = SAX_NONE;
}
}
void endElement(void *ctx, const xmlChar *name)
{
CCDictMaker * pMaker = static_cast<CCDictMaker*>(ctx);
std::string sName((char*)name);
if( sName == "dict" )
{
pMaker->m_tDictStack.pop();
if ( !pMaker->m_tDictStack.empty() )
{
pMaker->m_pCurDict = static_cast<CCDictionary*>(pMaker->m_tDictStack.top());
}
}
}
void characters(void *ctx, const xmlChar *ch, int len)
{
CCDictMaker * pMaker = static_cast<CCDictMaker*>(ctx);
std::string *pText = new std::string((char*)ch,0,len);
switch(pMaker->m_tState)
{
case SAX_KEY:
pMaker->m_sCurKey = *pText;
break;
case SAX_INT:
case SAX_REAL:
{
NSAssert(!pMaker->m_sCurKey.empty(), "not found key : <integet/real>");
pMaker->m_pCurDict->insert( Pair(pMaker->m_sCurKey, pText) );
break;
}
}
}
char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
// do not convert an absolute path (starting with '/')
@ -44,3 +189,9 @@ char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
return pszRet;
}
CCDictionary *CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName)
{
CCDictMaker tMaker;
return tMaker.dictionaryWithContentsOfFile(pFileName);
}

View File

@ -25,13 +25,17 @@ THE SOFTWARE.
#ifndef __PLATFORM_UPHONE_CCFILE_UTILS_H__
#define __PLATFORM_UPHONE_CCFILE_UTILS_H__
#include <string>
#include <map>
#include <ssFile.h>
typedef std::map<std::string, void*> CCDictionary;
// Helper class to handle file operations
class CCFileUtils
{
public:
static char* fullPathFromRelativePath(const char *pszRealPath);
static std::map<std::string, void*> *dictionaryWithContentsOfFile(const char *pFileName);
};
#endif // __PLATFORM_UPHONE_CCFILE_UTILS_H__