axmol/cocos2dx/platform/linux/CCFileUtils.cpp

249 lines
6.9 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;
2013-01-26 14:19:14 +08:00
static std::map<std::string, std::string> s_fullPathCache;
2012-08-02 13:02:59 +08:00
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()
{
// get application path
int length = 0;
char fullpath[256] = {0};
length = readlink("/proc/self/exe", fullpath, sizeof(fullpath));
fullpath[length] = '\0';
std::string resourcePath = fullpath;
resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/"));
resourcePath += "/../../../Resources/";
m_strDefaultResRootPath = resourcePath;
2013-01-29 19:12:55 +08:00
//CCLOG("DEFAULT RES PATH = %s", m_strDefaultResRootPath.c_str());
m_searchPathArray.push_back(m_strDefaultResRootPath);
m_searchResolutionsOrderArray.push_back("");
2013-01-24 16:44:13 +08:00
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);
2012-08-02 13:02:59 +08:00
}
CC_SAFE_DELETE(s_pFileUtils);
}
void CCFileUtils::purgeCachedEntries()
{
2013-01-26 14:19:14 +08:00
s_fullPathCache.clear();
2012-08-02 13:02:59 +08:00
}
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 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;
return path;
2013-01-24 16:44:13 +08:00
}
2013-01-26 14:19:14 +08:00
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
2013-01-24 16:44:13 +08:00
{
2013-01-26 14:19:14 +08:00
CCAssert(pszFileName != NULL, "CCFileUtils: Invalid path");
// Return directly if it's an absolute path.
if (pszFileName[0] == '/')
2012-08-02 13:02:59 +08:00
{
2013-01-27 20:16:46 +08:00
//CCLOG("return absolute path directly: %s ", pszFileName);
2013-01-24 16:44:13 +08:00
return pszFileName;
2012-08-02 13:02:59 +08:00
}
2013-01-26 14:19:14 +08:00
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end()) {
2013-01-27 20:16:46 +08:00
//CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
2013-01-26 14:19:14 +08:00
return cacheIter->second;
}
2013-01-24 16:44:13 +08:00
2013-01-26 14:19:14 +08:00
// in Lookup Filename dictionary ?
2013-01-24 16:44:13 +08:00
std::string newFileName = getNewFilename(pszFileName);
2013-01-26 14:19:14 +08:00
2013-01-24 16:44:13 +08:00
std::string fullpath;
for (std::vector<std::string>::iterator searchPathsIter = m_searchPathArray.begin();
searchPathsIter != m_searchPathArray.end(); ++searchPathsIter) {
for (std::vector<std::string>::iterator resOrderIter = m_searchResolutionsOrderArray.begin();
resOrderIter != m_searchResolutionsOrderArray.end(); ++resOrderIter) {
2013-01-29 19:12:55 +08:00
//CCLOG("\n\nSEARCHING: %s, %s, %s", newFileName.c_str(), resOrderIter->c_str(), searchPathsIter->c_str());
fullpath = this->getPathForFilename(newFileName, *resOrderIter, *searchPathsIter);
2013-01-24 16:44:13 +08:00
2013-01-26 14:19:14 +08:00
// check if file or path exist
struct stat sts;
if (stat(fullpath.c_str(), &sts) != -1)
2013-01-24 16:44:13 +08:00
{
2013-01-26 14:19:14 +08:00
// Adding the full path to cache if the file was found.
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
2013-01-29 19:12:55 +08:00
//CCLOG("Returning path: %s", fullpath.c_str());
2013-01-26 14:19:14 +08:00
return fullpath;
2013-01-24 16:44:13 +08:00
}
2012-08-24 17:55:29 +08:00
}
}
2013-01-26 14:19:14 +08:00
// The file wasn't found, return the file name passed in.
return pszFileName;
2012-08-02 13:02:59 +08:00
}
2013-01-24 16:44:13 +08:00
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
2013-01-26 14:19:14 +08:00
return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
2013-01-24 16:44:13 +08:00
}
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();
}
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
{
std::string fullPath = fullPathForFilename(pszFileName);
2012-08-02 13:02:59 +08:00
// read rrom other path than user set it
FILE *fp = fopen(fullPath.c_str(), 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)
{
if (pszResourceDirectory == NULL) return;
m_obDirectory = pszResourceDirectory;
std::vector<std::string> searchPaths = this->getSearchPaths();;
searchPaths.insert(searchPaths.begin(), pszResourceDirectory);
this->setSearchPaths(searchPaths);
}
void CCFileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
{
bool bExistDefaultRootPath = false;
m_searchPathArray.clear();
for (std::vector<std::string>::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter)
{
std::string strPrefix;
std::string path;
if ((*iter)[0] != '/')
{ // Not an absolute path
if (iter->find(m_strDefaultResRootPath) != 0)
{ // The path contains no default resource root path, insert the root path.
strPrefix = m_strDefaultResRootPath;
}
}
path = strPrefix+(*iter);
if (path.length() > 0 && path[path.length()-1] != '/')
{
path += "/";
}
if (!bExistDefaultRootPath && path == m_strDefaultResRootPath)
{
bExistDefaultRootPath = true;
}
m_searchPathArray.push_back(path);
}
if (!bExistDefaultRootPath)
{
2013-01-29 19:12:55 +08:00
//CCLOG("Default root path doesn't exist, adding it.");
m_searchPathArray.push_back(m_strDefaultResRootPath);
}
}
2012-08-27 16:43:02 +08:00
string CCFileUtils::getWriteablePath()
{
2012-08-02 13:02:59 +08:00
//return current resource path
return m_strDefaultResRootPath;
2012-08-02 13:02:59 +08:00
}
NS_CC_END