Merge pull request #845 from dumganhar/gles20_optimize_data

fixed #1166: Added CCArray::arrayWithContentOfFile function.
This commit is contained in:
James Chen 2012-04-16 00:59:56 -07:00
commit 1fe71210ed
4 changed files with 98 additions and 8 deletions

View File

@ -74,6 +74,8 @@ class CC_DLL CCArray : public CCObject
{ {
public: public:
~CCArray(); ~CCArray();
/* static functions */
/** Create an array */ /** Create an array */
static CCArray* array(); static CCArray* array();
/** Create an array with one object */ /** Create an array with one object */
@ -84,6 +86,18 @@ public:
static CCArray* arrayWithCapacity(unsigned int capacity); static CCArray* arrayWithCapacity(unsigned int capacity);
/** Create an array with an existing array */ /** Create an array with an existing array */
static CCArray* arrayWithArray(CCArray* otherArray); static CCArray* arrayWithArray(CCArray* otherArray);
/**
@brief Generate a CCArray pointer by file
@param pFileName The file name of *.plist file
@return The CCArray pointer generated from the file
*/
static CCArray* arrayWithContentsOfFile(const char* pFileName);
/*
@brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the
invoker should call release().
*/
static CCArray* arrayWithContentsOfFileThreadSafe(const char* pFileName);
/** Initializes an array */ /** Initializes an array */
bool init(); bool init();
@ -152,7 +166,9 @@ public:
void replaceObjectAtIndex(unsigned int uIndex, CCObject* pObject, bool bReleaseObject = true); void replaceObjectAtIndex(unsigned int uIndex, CCObject* pObject, bool bReleaseObject = true);
/* override functions */
virtual CCObject* copyWithZone(CCZone* pZone); virtual CCObject* copyWithZone(CCZone* pZone);
public: public:
ccArray* data; ccArray* data;
CCArray(); CCArray();

View File

@ -54,9 +54,18 @@ typedef enum
SAX_ARRAY SAX_ARRAY
}CCSAXState; }CCSAXState;
typedef enum
{
SAX_RESULT_NONE = 0,
SAX_RESULT_DICT,
SAX_RESULT_ARRAY
}CCSAXResult;
class CCDictMaker : public CCSAXDelegator class CCDictMaker : public CCSAXDelegator
{ {
public: public:
CCSAXResult m_eResultType;
CCArray* m_pRootArray;
CCDictionary *m_pRootDict; CCDictionary *m_pRootDict;
CCDictionary *m_pCurDict; CCDictionary *m_pCurDict;
std::stack<CCDictionary*> m_tDictStack; std::stack<CCDictionary*> m_tDictStack;
@ -68,8 +77,10 @@ public:
std::stack<CCSAXState> m_tStateStack; std::stack<CCSAXState> m_tStateStack;
public: public:
CCDictMaker() CCDictMaker()
: m_pRootDict(NULL), : m_eResultType(SAX_RESULT_NONE),
m_pRootArray(NULL),
m_pRootDict(NULL),
m_pCurDict(NULL), m_pCurDict(NULL),
m_tState(SAX_NONE), m_tState(SAX_NONE),
m_pArray(NULL) m_pArray(NULL)
@ -82,6 +93,7 @@ public:
CCDictionary* dictionaryWithContentsOfFile(const char *pFileName) CCDictionary* dictionaryWithContentsOfFile(const char *pFileName)
{ {
m_eResultType = SAX_RESULT_DICT;
CCSAXParser parser; CCSAXParser parser;
if (false == parser.init("UTF-8")) if (false == parser.init("UTF-8"))
@ -94,6 +106,21 @@ public:
return m_pRootDict; return m_pRootDict;
} }
CCArray* arrayWithContentsOfFile(const char* pFileName)
{
m_eResultType = SAX_RESULT_ARRAY;
CCSAXParser parser;
if (false == parser.init("UTF-8"))
{
return NULL;
}
parser.setDelegator(this);
parser.parse(pFileName);
return m_pArray;
}
void startElement(void *ctx, const char *name, const char **atts) void startElement(void *ctx, const char *name, const char **atts)
{ {
CC_UNUSED_PARAM(ctx); CC_UNUSED_PARAM(ctx);
@ -154,10 +181,18 @@ public:
else if (sName == "array") else if (sName == "array")
{ {
m_tState = SAX_ARRAY; m_tState = SAX_ARRAY;
m_pArray = CCArray::array(); m_pArray = new CCArray();
m_pArray->retain(); if (m_eResultType == SAX_RESULT_ARRAY && m_pRootArray == NULL)
{
m_pRootArray = m_pArray;
m_pRootArray->retain();
}
CCSAXState preState = SAX_NONE;
if (! m_tStateStack.empty())
{
preState = m_tStateStack.top();
}
CCSAXState preState = m_tStateStack.empty() ? SAX_DICT : m_tStateStack.top();
if (preState == SAX_DICT) if (preState == SAX_DICT)
{ {
m_pCurDict->setObject(m_pArray, m_sCurKey.c_str()); m_pCurDict->setObject(m_pArray, m_sCurKey.c_str());
@ -297,6 +332,12 @@ CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFi
return tMaker.dictionaryWithContentsOfFile(pFileName); return tMaker.dictionaryWithContentsOfFile(pFileName);
} }
CCArray* ccFileUtils_arrayWithContentsOfFileThreadSafe(const char* pFileName)
{
CCDictMaker tMaker;
return tMaker.arrayWithContentsOfFile(pFileName);
}
unsigned char* CCFileUtils::getFileDataFromZip(const char* pszZipFilePath, const char* pszFileName, unsigned long * pSize) unsigned char* CCFileUtils::getFileDataFromZip(const char* pszZipFilePath, const char* pszFileName, unsigned long * pSize)
{ {
unsigned char * pBuffer = NULL; unsigned char * pBuffer = NULL;

View File

@ -431,6 +431,22 @@ namespace cocos2d {
return pRet; return pRet;
} }
CCArray* ccFileUtils_arrayWithContentsOfFileThreadSafe(const char* pFileName)
{
NSString* pPath = [NSString stringWithUTF8String:pFileName];
NSString* pathExtension= [pPath pathExtension];
pPath = [pPath stringByDeletingPathExtension];
pPath = [[NSBundle mainBundle] pathForResource:pPath ofType:pathExtension];
NSArray* pArray = [NSArray arrayWithContentsOfFile:pPath];
CCArray* pRet = new CCArray();
for (id value in pArray) {
static_addItemToCCArray(value, pRet);
}
return pRet;
}
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{ {
unsigned char * pBuffer = NULL; unsigned char * pBuffer = NULL;

View File

@ -116,9 +116,26 @@ CCArray* CCArray::arrayWithCapacity(unsigned int capacity)
CCArray* CCArray::arrayWithArray(CCArray* otherArray) CCArray* CCArray::arrayWithArray(CCArray* otherArray)
{ {
CCArray* pArray = (CCArray*)otherArray->copyWithZone(NULL); CCArray* pRet = (CCArray*)otherArray->copy();
pArray->autorelease(); pRet->autorelease();
return pArray; return pRet;
}
CCArray* CCArray::arrayWithContentsOfFile(const char* pFileName)
{
CCArray* pRet = arrayWithContentsOfFileThreadSafe(pFileName);
if (pRet != NULL)
{
pRet->autorelease();
}
return pRet;
}
extern CCArray* ccFileUtils_arrayWithContentsOfFileThreadSafe(const char* pFileName);
CCArray* CCArray::arrayWithContentsOfFileThreadSafe(const char* pFileName)
{
return ccFileUtils_arrayWithContentsOfFileThreadSafe(pFileName);
} }
bool CCArray::init() bool CCArray::init()