axmol/cocos2dx/platform/emscripten/CCFileUtilsEmscripten.cpp

74 lines
1.7 KiB
C++
Raw Normal View History

2013-04-09 09:21:53 +08:00
#include "CCFileUtilsEmscripten.h"
#include "platform/CCCommon.h"
#include "ccMacros.h"
#include "CCApplication.h"
#include "cocoa/CCString.h"
#include <unistd.h>
using namespace std;
NS_CC_BEGIN
FileUtils* FileUtils::getInstance()
2013-04-09 09:21:53 +08:00
{
if (s_sharedFileUtils == NULL)
{
s_sharedFileUtils = new FileUtilsEmscripten();
2013-07-23 20:44:08 +08:00
if(!s_sharedFileUtils->init())
{
delete s_sharedFileUtils;
s_sharedFileUtils = NULL;
CCLOG("ERROR: Could not init CCFileUtilsEmscripten");
}
2013-04-09 09:21:53 +08:00
}
return s_sharedFileUtils;
}
FileUtilsEmscripten::FileUtilsEmscripten()
2013-04-09 09:21:53 +08:00
{}
bool FileUtilsEmscripten::init()
2013-04-09 09:21:53 +08:00
{
2013-07-23 21:49:00 +08:00
_defaultResRootPath = "/";
return FileUtils::init();
2013-04-09 09:21:53 +08:00
}
string FileUtilsEmscripten::getWritablePath() const
2013-04-09 09:21:53 +08:00
{
// Let's write it in the current working directory's data folder
char cwd[FILENAME_MAX] = {0};
getcwd(cwd, FILENAME_MAX - 1);
cwd[FILENAME_MAX-1] = '\0';
std::string path = cwd;
path += "/data/";
return path;
}
bool FileUtilsEmscripten::isAbsolutePath(const std::string& strPath) const
2013-04-09 09:21:53 +08:00
{
if (strPath[0] == '/' || strPath.find(_defaultResRootPath) == 0)
2013-04-09 09:21:53 +08:00
{
return true;
}
return false;
}
bool FileUtilsEmscripten::isFileExist(const std::string& strFilePath) const
2013-04-09 09:21:53 +08:00
{
std::string strPath = strFilePath;
if (strPath[0] != '/')
{ // Not absolute path, add the default root path at the beginning.
if (strPath.find(_defaultResRootPath) != 0)
2013-04-09 09:21:53 +08:00
{// Didn't find "assets/" at the beginning of the path, adding it.
strPath.insert(0, _defaultResRootPath);
2013-04-09 09:21:53 +08:00
}
}
return access(strPath.c_str(), F_OK) != -1 ? true : false;
}
NS_CC_END