axmol/cocos2dx/platform/linux/CCFileUtils.cpp

140 lines
3.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();
}
return s_pFileUtils;
}
void CCFileUtils::purgeFileUtils()
{
if (s_pFileUtils != NULL)
{
s_pFileUtils->purgeCachedEntries();
}
CC_SAFE_DELETE(s_pFileUtils);
}
void CCFileUtils::purgeCachedEntries()
{
}
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
2012-08-02 13:02:59 +08:00
{
2012-08-24 17:55:29 +08:00
if (pszRelativePath && pszRelativePath[0] == '/')
{
return pszRelativePath;
}
const char* pszRootPath = CCApplication::sharedApplication()->getResourceRootPath();
CCString* pRet = CCString::create(pszRootPath);
2012-08-24 17:55:29 +08:00
if (m_obDirectory.size() > 0)
2012-08-02 13:02:59 +08:00
{
2012-08-24 17:55:29 +08:00
pRet->m_sString += m_obDirectory.c_str();
2012-08-02 13:02:59 +08:00
}
if (pszRelativePath != NULL)
2012-08-02 13:02:59 +08:00
{
pRet->m_sString += pszRelativePath;
}
2012-08-24 17:55:29 +08:00
// check if file or path exist
struct stat sts;
if (stat(pRet->getCString(), &sts) == -1 && errno == ENOENT)
{
// find from "Resources/"
pRet->m_sString.clear();
pRet->m_sString = CCApplication::sharedApplication()->getResourceRootPath();
pRet->m_sString += pszRelativePath;
if (stat(pRet->getCString(), &sts) == -1 && errno == ENOENT)
{
return pszRelativePath;
}
}
return pRet->getCString();
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;
CCString *pRet = new CCString();
pRet->autorelease();
pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1);
pRet->m_sString += pszFilename;
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
string fullPath = pszFileName;
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
{
2012-08-27 16:43:02 +08:00
fullPath = fullPathFromRelativePath(fullPath.c_str());
2012-08-02 13:02:59 +08:00
// read rrom other path than user set it
2012-08-27 16:43:02 +08:00
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(";
msg.append(fullPath.c_str()).append(") failed!");
CCMessageBox(msg.c_str(), title.c_str());
}
return pData;
}
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