2011-03-24 16:16:49 +08:00
|
|
|
#include "CCSAXParser.h"
|
|
|
|
#include "expat.h"
|
|
|
|
#include "CCMutableDictionary.h"
|
2011-03-23 22:16:20 +08:00
|
|
|
#include "IwUtil.h"
|
2011-03-24 16:16:49 +08:00
|
|
|
|
2011-03-23 22:16:20 +08:00
|
|
|
NS_CC_BEGIN;
|
|
|
|
|
2011-03-24 16:16:49 +08:00
|
|
|
static XML_Parser s_pParser = NULL;
|
|
|
|
|
2011-03-23 22:16:20 +08:00
|
|
|
CCSAXParser::CCSAXParser()
|
|
|
|
{
|
2011-03-24 16:16:49 +08:00
|
|
|
s_pParser = NULL;
|
2011-03-23 22:16:20 +08:00
|
|
|
m_pDelegator = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
CCSAXParser::~CCSAXParser(void)
|
|
|
|
{
|
2011-03-24 16:16:49 +08:00
|
|
|
if(s_pParser)
|
2011-03-23 22:16:20 +08:00
|
|
|
{
|
2011-03-24 16:16:49 +08:00
|
|
|
XML_ParserFree(s_pParser);
|
|
|
|
s_pParser = NULL;
|
2011-03-23 22:16:20 +08:00
|
|
|
}
|
|
|
|
}
|
2011-03-24 17:51:29 +08:00
|
|
|
bool CCSAXParser::init(const char *pszEncoding)
|
2011-03-23 22:16:20 +08:00
|
|
|
{
|
2011-03-24 16:16:49 +08:00
|
|
|
s_pParser = XML_ParserCreate(reinterpret_cast<const XML_Char*>(pszEncoding));
|
|
|
|
XML_SetUserData(s_pParser, this);
|
|
|
|
XML_SetElementHandler(s_pParser, startElement, endElement);
|
|
|
|
XML_SetCharacterDataHandler(s_pParser, textHandler);
|
2011-03-24 17:51:29 +08:00
|
|
|
return true;
|
2011-03-23 22:16:20 +08:00
|
|
|
}
|
2011-03-24 17:51:29 +08:00
|
|
|
bool CCSAXParser::parse(const char *pszFile)
|
2011-03-23 22:16:20 +08:00
|
|
|
{
|
2011-03-24 17:51:29 +08:00
|
|
|
bool bRet = false;
|
|
|
|
char* buf = NULL;
|
2011-03-24 18:17:35 +08:00
|
|
|
s3eFile* file = NULL;
|
2011-03-24 17:51:29 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-03-24 18:17:35 +08:00
|
|
|
file = s3eFileOpen(pszFile, "r");
|
2011-03-24 17:51:29 +08:00
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
IwAssertMsg(GAME, file, ("Open file %s Failed. s3eFileError Code : %i", pszFile, s3eFileGetError()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-23 22:16:20 +08:00
|
|
|
s3eFileSeek(file, 0, S3E_FILESEEK_END);
|
|
|
|
int size = s3eFileTell(file);
|
|
|
|
s3eFileSeek(file, 0, S3E_FILESEEK_SET);
|
2011-03-24 17:51:29 +08:00
|
|
|
buf = new char[size];
|
2011-03-23 22:16:20 +08:00
|
|
|
int done =0;
|
|
|
|
int len = (int)s3eFileRead(buf, 1, size, file);
|
2011-03-24 16:16:49 +08:00
|
|
|
if (XML_Parse(s_pParser, buf, len, 1) == XML_STATUS_ERROR)
|
2011-03-23 22:16:20 +08:00
|
|
|
{
|
2011-03-24 16:16:49 +08:00
|
|
|
CCLog("GAME: cocos2d: plist err: %s at line %d", XML_ErrorString(XML_GetErrorCode(s_pParser)), XML_GetCurrentLineNumber(s_pParser));
|
2011-03-24 17:51:29 +08:00
|
|
|
break;
|
2011-03-23 22:16:20 +08:00
|
|
|
}
|
2011-03-24 17:51:29 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
if (file)
|
|
|
|
{
|
2011-03-23 22:16:20 +08:00
|
|
|
s3eFileClose(file);
|
2011-03-24 17:51:29 +08:00
|
|
|
}
|
|
|
|
if (buf)
|
|
|
|
{
|
2011-03-23 22:16:20 +08:00
|
|
|
delete []buf;
|
2011-03-24 17:51:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
2011-03-23 22:16:20 +08:00
|
|
|
|
|
|
|
}
|
2011-03-24 16:16:49 +08:00
|
|
|
void CCSAXParser::startElement(void *ctx, const char *name, const char **atts)
|
2011-03-23 22:16:20 +08:00
|
|
|
{
|
2011-03-24 18:17:35 +08:00
|
|
|
((CCSAXParser*)(ctx))->m_pDelegator->startElement(ctx, (const char*)name, (const char**)atts);
|
2011-03-23 22:16:20 +08:00
|
|
|
}
|
|
|
|
|
2011-03-24 16:16:49 +08:00
|
|
|
void CCSAXParser::endElement(void *ctx, const char *name)
|
2011-03-23 22:16:20 +08:00
|
|
|
{
|
2011-03-24 18:17:35 +08:00
|
|
|
((CCSAXParser*)(ctx))->m_pDelegator->endElement(ctx, (const char*)name);
|
2011-03-23 22:16:20 +08:00
|
|
|
}
|
2011-03-24 16:16:49 +08:00
|
|
|
void CCSAXParser::textHandler(void *ctx, const char *name, int len)
|
2011-03-23 22:16:20 +08:00
|
|
|
{
|
2011-03-24 18:17:35 +08:00
|
|
|
((CCSAXParser*)(ctx))->m_pDelegator->textHandler(ctx, (const char*)name, len);
|
2011-03-23 22:16:20 +08:00
|
|
|
}
|
|
|
|
void CCSAXParser::setDelegator(CCSAXDelegator* pDelegator)
|
|
|
|
{
|
|
|
|
m_pDelegator = pDelegator;
|
|
|
|
}
|
2011-03-24 16:16:49 +08:00
|
|
|
|
2011-03-23 22:16:20 +08:00
|
|
|
NS_CC_END;
|
|
|
|
|
|
|
|
|