2011-03-25 15:30:10 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
Copyright (c) 2011 cocos2d-x.org http://cocos2d-x.org
|
|
|
|
|
Copyright (c) 2011 Максим Аксенов
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|