2010-11-12 17:16:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stack>
|
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxml/xmlmemory.h>
|
2011-03-09 16:19:20 +08:00
|
|
|
#include "CCString.h"
|
2011-03-26 17:24:37 +08:00
|
|
|
#include "CCFileUtils.h"
|
2011-02-23 18:22:05 +08:00
|
|
|
#include "CCDirector.h"
|
2011-03-26 17:24:37 +08:00
|
|
|
#include "CCSAXParser.h"
|
2010-11-12 17:16:15 +08:00
|
|
|
|
|
|
|
#define MAX_PATH 260
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
static const char *static_ccRemoveHDSuffixFromFile( const char *pszPath)
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2011-01-07 09:35:27 +08:00
|
|
|
#if CC_IS_RETINA_DISPLAY_SUPPORTED
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
if(cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 ) {
|
|
|
|
NSString *path = [NSString stringWithUTF8String: pszPath];
|
|
|
|
NSString *name = [path lastPathComponent];
|
|
|
|
NSString *suffix = [NSString stringWithUTF8String: CC_RETINA_DISPLAY_FILENAME_SUFFIX];
|
|
|
|
|
|
|
|
// check if path already has the suffix.
|
|
|
|
if( [name rangeOfString: suffix].location != NSNotFound ) {
|
2011-01-07 09:35:27 +08:00
|
|
|
|
2011-03-18 15:17:15 +08:00
|
|
|
CCLOG("cocos2d: Filename(%@) contains %@ suffix. Removing it. See cocos2d issue #1040", path, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
|
2011-01-07 09:35:27 +08:00
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
NSString *newLastname = [name stringByReplacingOccurrencesOfString: suffix withString:@""];
|
2011-01-07 09:35:27 +08:00
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
NSString *pathWithoutLastname = [path stringByDeletingLastPathComponent];
|
|
|
|
return [[pathWithoutLastname stringByAppendingPathComponent:newLastname] UTF8String];
|
|
|
|
}
|
|
|
|
}
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
#endif // CC_IS_RETINA_DISPLAY_SUPPORTED
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
return pszPath;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2011-01-07 09:35:27 +08:00
|
|
|
static NSString* getDoubleResolutionImage(NSString* path)
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2011-01-07 09:35:27 +08:00
|
|
|
#if CC_IS_RETINA_DISPLAY_SUPPORTED
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
if( cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 )
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
NSString *pathWithoutExtension = [path stringByDeletingPathExtension];
|
|
|
|
NSString *name = [pathWithoutExtension lastPathComponent];
|
2011-02-23 18:22:05 +08:00
|
|
|
NSString *suffix = [NSString stringWithUTF8String: CC_RETINA_DISPLAY_FILENAME_SUFFIX];
|
|
|
|
|
2011-01-07 09:35:27 +08:00
|
|
|
// check if path already has the suffix.
|
2011-02-23 18:22:05 +08:00
|
|
|
if( [name rangeOfString: suffix].location != NSNotFound ) {
|
2011-01-07 09:35:27 +08:00
|
|
|
|
2011-03-18 15:17:15 +08:00
|
|
|
CCLOG("cocos2d: WARNING Filename(%@) already has the suffix %@. Using it.", name, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
|
2011-01-07 09:35:27 +08:00
|
|
|
return path;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
NSString *extension = [path pathExtension];
|
|
|
|
|
|
|
|
if( [extension isEqualToString:@"ccz"] || [extension isEqualToString:@"gz"] )
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2011-01-07 09:35:27 +08:00
|
|
|
// All ccz / gz files should be in the format filename.xxx.ccz
|
|
|
|
// so we need to pull off the .xxx part of the extension as well
|
|
|
|
extension = [NSString stringWithFormat:@"%@.%@", [pathWithoutExtension pathExtension], extension];
|
|
|
|
pathWithoutExtension = [pathWithoutExtension stringByDeletingPathExtension];
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
NSString *retinaName = [pathWithoutExtension stringByAppendingString: suffix];
|
2011-01-07 09:35:27 +08:00
|
|
|
retinaName = [retinaName stringByAppendingPathExtension:extension];
|
|
|
|
|
|
|
|
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
|
|
|
|
if( [fileManager fileExistsAtPath:retinaName] )
|
|
|
|
return retinaName;
|
|
|
|
|
2011-03-18 15:17:15 +08:00
|
|
|
CCLOG("cocos2d: CCFileUtils: Warning HD file not found: %@", [retinaName lastPathComponent] );
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
#endif // CC_IS_RETINA_DISPLAY_SUPPORTED
|
|
|
|
|
|
|
|
return path;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2011-01-07 09:35:27 +08:00
|
|
|
static const char* static_fullPathFromRelativePath(const char *pszRelativePath)
|
2010-11-12 17:16:15 +08:00
|
|
|
|
|
|
|
{
|
2011-01-07 09:35:27 +08:00
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
// NSAssert(pszRelativePath != nil, @"CCFileUtils: Invalid path");
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
// do not convert an absolute path (starting with '/')
|
|
|
|
NSString *relPath = [NSString stringWithUTF8String: pszRelativePath];
|
|
|
|
NSString *fullpath = nil;
|
|
|
|
|
|
|
|
// only if it is not an absolute path
|
|
|
|
if( ! [relPath isAbsolutePath] )
|
|
|
|
{
|
|
|
|
NSString *file = [relPath lastPathComponent];
|
|
|
|
NSString *imageDirectory = [relPath stringByDeletingLastPathComponent];
|
|
|
|
|
|
|
|
fullpath = [[NSBundle mainBundle] pathForResource:file
|
|
|
|
ofType:nil
|
|
|
|
inDirectory:imageDirectory];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullpath == nil)
|
|
|
|
fullpath = relPath;
|
|
|
|
|
|
|
|
fullpath = getDoubleResolutionImage(fullpath);
|
|
|
|
|
|
|
|
return [fullpath UTF8String];
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2011-01-07 09:35:27 +08:00
|
|
|
namespace cocos2d {
|
2011-03-26 17:24:37 +08:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SAX_NONE = 0,
|
|
|
|
SAX_KEY,
|
|
|
|
SAX_DICT,
|
|
|
|
SAX_INT,
|
|
|
|
SAX_REAL,
|
|
|
|
SAX_STRING
|
|
|
|
}CCSAXState;
|
|
|
|
|
|
|
|
class CCDictMaker : public CCSAXDelegator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCDictionary<std::string, CCObject*> *m_pRootDict;
|
|
|
|
CCDictionary<std::string, CCObject*> *m_pCurDict;
|
|
|
|
std::stack<CCDictionary<std::string, CCObject*>*> m_tDictStack;
|
|
|
|
std::string m_sCurKey;///< parsed key
|
|
|
|
CCSAXState m_tState;
|
|
|
|
public:
|
|
|
|
CCDictMaker()
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_pRootDict = NULL;
|
|
|
|
m_pCurDict = NULL;
|
|
|
|
m_tState = SAX_NONE;
|
|
|
|
}
|
|
|
|
~CCDictMaker()
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
}
|
|
|
|
CCDictionary<std::string, CCObject*> *dictionaryWithContentsOfFile(const char *pFileName)
|
|
|
|
{
|
|
|
|
CCSAXParser parser;
|
|
|
|
|
|
|
|
if (false == parser.init("UTF-8"))
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
return NULL;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
2011-03-26 17:24:37 +08:00
|
|
|
parser.setDelegator(this);
|
|
|
|
|
|
|
|
parser.parse(pFileName);
|
|
|
|
return m_pRootDict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void startElement(void *ctx, const char *name, const char **atts)
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
|
|
|
std::string sName((char*)name);
|
|
|
|
if( sName == "dict" )
|
|
|
|
{
|
2011-03-09 16:19:20 +08:00
|
|
|
CCDictionary<std::string, CCObject*> *pNewDict = new CCDictionary<std::string, CCObject*>();
|
2011-03-26 17:24:37 +08:00
|
|
|
if(! m_pRootDict)
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_pRootDict = pNewDict;
|
2011-01-07 09:35:27 +08:00
|
|
|
pNewDict->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
CCAssert(m_pCurDict && !m_sCurKey.empty(), "");
|
|
|
|
m_pCurDict->setObject(pNewDict, m_sCurKey);
|
2011-01-07 09:35:27 +08:00
|
|
|
pNewDict->release();
|
2011-03-26 17:24:37 +08:00
|
|
|
m_sCurKey.clear();
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
2011-03-26 17:24:37 +08:00
|
|
|
m_pCurDict = pNewDict;
|
|
|
|
m_tDictStack.push(m_pCurDict);
|
|
|
|
m_tState = SAX_DICT;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
else if(sName == "key")
|
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_tState = SAX_KEY;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
else if(sName == "integer")
|
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_tState = SAX_INT;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
else if(sName == "real")
|
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_tState = SAX_REAL;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
else if(sName == "string")
|
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_tState = SAX_STRING;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_tState = SAX_NONE;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
}
|
2011-03-26 17:24:37 +08:00
|
|
|
|
|
|
|
void endElement(void *ctx, const char *name)
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
|
|
|
std::string sName((char*)name);
|
|
|
|
if( sName == "dict" )
|
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_tDictStack.pop();
|
|
|
|
if ( !m_tDictStack.empty() )
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
m_pCurDict = (CCDictionary<std::string, CCObject*>*)(m_tDictStack.top());
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
}
|
2011-03-26 17:24:37 +08:00
|
|
|
m_tState = SAX_NONE;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
2011-03-26 17:24:37 +08:00
|
|
|
|
|
|
|
void textHandler(void *ctx, const char *ch, int len)
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
if (m_tState == SAX_NONE)
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-03-09 16:19:20 +08:00
|
|
|
CCString *pText = new CCString();
|
2011-01-07 09:35:27 +08:00
|
|
|
pText->m_sString = std::string((char*)ch,0,len);
|
2011-03-26 17:24:37 +08:00
|
|
|
|
|
|
|
switch(m_tState)
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
case SAX_KEY:
|
|
|
|
m_sCurKey = pText->m_sString;
|
|
|
|
break;
|
|
|
|
case SAX_INT:
|
|
|
|
case SAX_REAL:
|
|
|
|
case SAX_STRING:
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-26 17:24:37 +08:00
|
|
|
CCAssert(!m_sCurKey.empty(), "not found key : <integet/real>");
|
|
|
|
m_pCurDict->setObject(pText, m_sCurKey);
|
2011-01-07 09:35:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pText->release();
|
|
|
|
}
|
2011-03-26 17:24:37 +08:00
|
|
|
};
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
// record the resource path
|
|
|
|
static char s_pszResourcePath[MAX_PATH] = {0};
|
|
|
|
|
|
|
|
void CCFileUtils::setResourcePath(const char *pszResourcePath)
|
|
|
|
{
|
2011-02-23 18:22:05 +08:00
|
|
|
// NSAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path");
|
|
|
|
// NSAssert(strlen(pszResourcePath) <= MAX_PATH, "[FileUtils setResourcePath] -- resource path too long");
|
2011-01-07 09:35:27 +08:00
|
|
|
|
|
|
|
strcpy(s_pszResourcePath, pszResourcePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCFileUtils::getResourcePath()
|
|
|
|
{
|
|
|
|
return s_pszResourcePath;
|
|
|
|
}
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2011-01-07 09:35:27 +08:00
|
|
|
int CCFileUtils::ccLoadFileIntoMemory(const char *filename, unsigned char **out)
|
|
|
|
{
|
|
|
|
assert( out );
|
|
|
|
assert( &*out );
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
FILE *f = fopen(filename, "rb");
|
|
|
|
if( !f ) {
|
|
|
|
*out = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
size = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
|
|
|
*out = (unsigned char*)malloc(size);
|
|
|
|
int read = fread(*out, 1, size, f);
|
|
|
|
if( read != size ) {
|
|
|
|
free(*out);
|
|
|
|
*out = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2011-03-09 16:19:20 +08:00
|
|
|
std::string& CCFileUtils::ccRemoveHDSuffixFromFile(std::string& path )
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
2011-03-02 17:05:22 +08:00
|
|
|
path = static_ccRemoveHDSuffixFromFile(path.c_str());
|
|
|
|
return path;
|
2011-01-07 09:35:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
|
|
|
|
{
|
|
|
|
return static_fullPathFromRelativePath(pszRelativePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
|
|
|
|
{
|
|
|
|
std::string relativeFile = fullPathFromRelativePath(pszRelativeFile);
|
2011-03-09 16:19:20 +08:00
|
|
|
CCString *pRet = new CCString();
|
2011-01-07 09:35:27 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1);
|
|
|
|
pRet->m_sString += pszFilename;
|
|
|
|
return pRet->m_sString.c_str();
|
|
|
|
}
|
2011-03-09 16:19:20 +08:00
|
|
|
CCDictionary<std::string, CCObject*> *CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName)
|
2011-01-07 09:35:27 +08:00
|
|
|
{
|
|
|
|
CCDictMaker tMaker;
|
|
|
|
return tMaker.dictionaryWithContentsOfFile(pFileName);
|
2011-03-26 17:24:37 +08:00
|
|
|
}
|
|
|
|
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
|
|
|
|
{
|
2011-03-29 15:59:37 +08:00
|
|
|
unsigned char * Buffer = NULL;
|
2011-03-26 17:24:37 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// read the file from hardware
|
|
|
|
FILE *fp = fopen(pszFileName, pszMode);
|
|
|
|
CC_BREAK_IF(!fp);
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
*pSize = ftell(fp);
|
|
|
|
fseek(fp,0,SEEK_SET);
|
2011-03-29 15:59:37 +08:00
|
|
|
Buffer = new unsigned char[*pSize];
|
|
|
|
*pSize = fread(Buffer,sizeof(unsigned char), *pSize,fp);
|
2011-03-26 17:24:37 +08:00
|
|
|
fclose(fp);
|
|
|
|
} while (0);
|
|
|
|
|
2011-03-29 15:59:37 +08:00
|
|
|
return Buffer;
|
2011-03-26 17:24:37 +08:00
|
|
|
}
|
|
|
|
void CCFileUtils::setResource(const char* pszZipFileName, const char* pszResPath)
|
|
|
|
{
|
|
|
|
CCAssert(0, "Have not implement!");
|
|
|
|
}
|
|
|
|
void CCFileUtils::setRelativePath(const char* pszRelativePath)
|
|
|
|
{
|
|
|
|
CCAssert(0, "Have not implement!");
|
|
|
|
}
|
2010-11-12 17:16:15 +08:00
|
|
|
}//namespace cocos2d
|