axmol/cocos2dx/platform/linux/CCFileUtils.cpp

242 lines
6.1 KiB
C++
Raw Normal View History

2012-08-02 13:02:59 +08:00
/*
* CCFileUtils_Linux.cpp
*
* Created on: Aug 9, 2011
* Author: laschweinski
*/
#include "platform/CCCommon.h"
2012-08-02 13:02:59 +08:00
#include "ccMacros.h"
#define __CC_PLATFORM_FILEUTILS_CPP__
2012-08-02 13:02:59 +08:00
#include "platform/CCFileUtilsCommon_cpp.h"
#include "platform/CCFileUtils.h"
#include "CCApplication.h"
#include "cocoa/CCString.h"
2012-08-02 13:02:59 +08:00
#include <unistd.h>
2012-08-24 17:55:29 +08:00
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
2012-08-02 13:02:59 +08:00
using namespace std;
NS_CC_BEGIN
static CCFileUtils* s_pFileUtils = NULL;
CCFileUtils* CCFileUtils::sharedFileUtils()
{
if (s_pFileUtils == NULL)
{
s_pFileUtils = new CCFileUtils();
2013-01-24 16:44:13 +08:00
s_pFileUtils->init();
2012-08-02 13:02:59 +08:00
}
return s_pFileUtils;
}
2013-01-24 16:44:13 +08:00
bool CCFileUtils::init()
{
m_pSearchPathArray = new CCArray();
m_pSearchPathArray->addObject(CCString::create(""));
m_pSearchResolutionsOrderArray = new CCArray();
m_pSearchResolutionsOrderArray->addObject(CCString::create(""));
return true;
}
2012-08-02 13:02:59 +08:00
void CCFileUtils::purgeFileUtils()
{
if (s_pFileUtils != NULL)
{
s_pFileUtils->purgeCachedEntries();
2013-01-24 16:44:13 +08:00
CC_SAFE_RELEASE(s_pFileUtils->m_pFilenameLookupDict);
CC_SAFE_RELEASE(s_pFileUtils->m_pSearchPathArray);
CC_SAFE_RELEASE(s_pFileUtils->m_pSearchResolutionsOrderArray);
2012-08-02 13:02:59 +08:00
}
CC_SAFE_DELETE(s_pFileUtils);
}
void CCFileUtils::purgeCachedEntries()
{
}
2013-01-24 16:44:13 +08:00
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
2012-08-02 13:02:59 +08:00
{
2013-01-24 16:44:13 +08:00
std::string ret;
const std::string& resourceRootPath = CCApplication::sharedApplication()->getResourceRootPath();
ret = resourceRootPath;
if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/')
2012-08-24 17:55:29 +08:00
{
2013-01-24 16:44:13 +08:00
ret += "/";
2012-08-24 17:55:29 +08:00
}
2013-01-24 16:44:13 +08:00
std::string file = filename;
std::string file_path = "";
size_t pos = filename.find_last_of("/");
if (pos != std::string::npos)
{
file_path = filename.substr(0, pos+1);
file = filename.substr(pos+1);
}
// searchPath + file_path + resourceDirectory
std::string path = searchPath;
if (path.size() > 0 && path[path.length()-1] != '/')
{
path += "/";
}
path += file_path;
path += resourceDirectory;
2013-01-24 16:44:13 +08:00
if (path.size() > 0 && path[path.length()-1] != '/')
2012-08-02 13:02:59 +08:00
{
2013-01-24 16:44:13 +08:00
path += "/";
2012-08-02 13:02:59 +08:00
}
2013-01-24 16:44:13 +08:00
path += file;
ret += path;
2013-01-24 16:44:13 +08:00
return ret;
}
const char* CCFileUtils::fullPathForFilename(const char* pszFileName)
{
if (pszFileName && pszFileName[0] == '/')
2012-08-02 13:02:59 +08:00
{
2013-01-24 16:44:13 +08:00
return pszFileName;
2012-08-02 13:02:59 +08:00
}
2013-01-24 16:44:13 +08:00
bool bFound = false;
CCString* pRet = CCString::create("");
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
do
2012-08-24 17:55:29 +08:00
{
2013-01-24 16:44:13 +08:00
CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
2012-08-24 17:55:29 +08:00
{
2013-01-24 16:44:13 +08:00
CCString* pSearchPath = (CCString*)pSearchObj;
CCObject* pResourceDirObj = NULL;
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
{
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
// Search in subdirectories
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
// check if file or path exist
struct stat sts;
if (stat(fullpath.c_str(), &sts) != -1)
{
pRet->m_sString = fullpath;
bFound = true;
break;
}
}
if (bFound)
{
break;
}
2012-08-24 17:55:29 +08:00
}
2013-01-24 16:44:13 +08:00
}while(false);
if (!bFound)
{ // Can't find the file, return the relative path.
pRet->m_sString = newFileName;
2012-08-24 17:55:29 +08:00
}
return pRet->getCString();
2012-08-02 13:02:59 +08:00
}
2013-01-24 16:44:13 +08:00
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
return fullPathForFilename(pszRelativePath);
}
2012-08-02 13:02:59 +08:00
2012-08-27 16:43:02 +08:00
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
{
2012-08-02 13:02:59 +08:00
std::string relativeFile = pszRelativeFile;
2013-01-24 16:44:13 +08:00
CCString *pRet = CCString::create("");
2012-08-02 13:02:59 +08:00
pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1);
2013-01-24 16:44:13 +08:00
pRet->m_sString += getNewFilename(pszFilename);
2012-08-02 13:02:59 +08:00
return pRet->m_sString.c_str();
}
2013-01-24 16:44:13 +08:00
void CCFileUtils::loadFilenameLookupDictionaryFromFile(const char* filename)
{
const char* pFullPath = this->fullPathForFilename(filename);
if (pFullPath)
{
CCDictionary* pDict = CCDictionary::createWithContentsOfFile(filename);
if (pDict)
{
CCDictionary* pMetadata = (CCDictionary*)pDict->objectForKey("metadata");
int version = ((CCString*)pMetadata->objectForKey("version"))->intValue();
if (version != 1)
{
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %ld. Filename: %s", (long)version, filename);
return;
}
setFilenameLookupDictionary((CCDictionary*)pDict->objectForKey("filenames"));
}
}
}
2012-08-27 16:43:02 +08:00
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
2012-08-02 13:02:59 +08:00
unsigned char * pData = 0;
2012-08-27 16:43:02 +08:00
if (!pszFileName || !pszMode)
{
return 0;
}
2012-08-02 13:02:59 +08:00
do
{
// read rrom other path than user set it
2013-01-24 16:44:13 +08:00
FILE *fp = fopen(pszFileName, pszMode);
2012-08-02 13:02:59 +08:00
CC_BREAK_IF(!fp);
fseek(fp,0,SEEK_END);
*pSize = ftell(fp);
fseek(fp,0,SEEK_SET);
pData = new unsigned char[*pSize];
*pSize = fread(pData,sizeof(unsigned char), *pSize,fp);
fclose(fp);
}while (0);
if (! pData && isPopupNotify())
{
std::string title = "Notification";
std::string msg = "Get data from file(";
2013-01-24 16:44:13 +08:00
msg.append(pszFileName).append(") failed!");
2012-08-02 13:02:59 +08:00
CCMessageBox(msg.c_str(), title.c_str());
}
return pData;
}
void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory)
{
m_obDirectory = pszResourceDirectory;
if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/')
{
m_obDirectory.append("/");
}
m_pSearchPathArray->insertObject(CCString::create(m_obDirectory.c_str()), 0);
}
2012-08-27 16:43:02 +08:00
string CCFileUtils::getWriteablePath()
{
2012-08-02 13:02:59 +08:00
//return current resource path
return CCApplication::sharedApplication()->getResourceRootPath();
2012-08-02 13:02:59 +08:00
}
NS_CC_END