2010-10-08 17:53:04 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-06 15:31:51 +08:00
|
|
|
#include "windows.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2011-03-26 13:52:33 +08:00
|
|
|
NS_CC_BEGIN;
|
2010-10-08 17:53:04 +08:00
|
|
|
|
|
|
|
// record the resource path
|
|
|
|
static char s_pszResourcePath[MAX_PATH] = {0};
|
|
|
|
|
|
|
|
void _CheckPath()
|
|
|
|
{
|
|
|
|
if (! s_pszResourcePath[0])
|
|
|
|
{
|
|
|
|
WCHAR wszPath[MAX_PATH];
|
2010-10-09 17:41:42 +08:00
|
|
|
int nNum = WideCharToMultiByte(CP_ACP, 0, wszPath,
|
2011-05-19 21:11:29 +08:00
|
|
|
GetCurrentDirectoryW(sizeof(wszPath), wszPath),
|
2010-10-08 17:53:04 +08:00
|
|
|
s_pszResourcePath, MAX_PATH, NULL, NULL);
|
2011-05-13 14:13:30 +08:00
|
|
|
s_pszResourcePath[nNum] = '\\';
|
2010-10-08 17:53:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCFileUtils::setResourcePath(const char *pszResourcePath)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path");
|
|
|
|
CCAssert(strlen(pszResourcePath) <= MAX_PATH, "[FileUtils setResourcePath] -- resource path too long");
|
2010-10-08 17:53:04 +08:00
|
|
|
|
|
|
|
strcpy(s_pszResourcePath, pszResourcePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
|
|
|
|
{
|
|
|
|
_CheckPath();
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCString * pRet = new CCString();
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':'))
|
|
|
|
{
|
2011-05-19 21:11:29 +08:00
|
|
|
// path start with "x:", is absolute path
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->m_sString = pszRelativePath;
|
|
|
|
}
|
2011-05-19 21:11:29 +08:00
|
|
|
else if (strlen(pszRelativePath) > 0
|
|
|
|
&& ('/' == pszRelativePath[0] || '\\' == pszRelativePath[0]))
|
2010-10-08 17:53:04 +08:00
|
|
|
{
|
2011-05-19 21:11:29 +08:00
|
|
|
// path start with '/' or '\', is absolute path without driver name
|
2010-10-08 17:53:04 +08:00
|
|
|
char szDriver[3] = {s_pszResourcePath[0], s_pszResourcePath[1], 0};
|
|
|
|
pRet->m_sString = szDriver;
|
|
|
|
pRet->m_sString += pszRelativePath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pRet->m_sString = s_pszResourcePath;
|
|
|
|
pRet->m_sString += pszRelativePath;
|
|
|
|
}
|
2011-02-18 18:30:22 +08:00
|
|
|
#if (CC_IS_RETINA_DISPLAY_SUPPORTED)
|
|
|
|
if (CC_CONTENT_SCALE_FACTOR() != 1.0f)
|
|
|
|
{
|
2011-03-09 12:00:04 +08:00
|
|
|
std::string hiRes = pRet->m_sString.c_str();
|
|
|
|
std::string::size_type pos = hiRes.find_last_of("/\\");
|
|
|
|
std::string::size_type dotPos = hiRes.find_last_of(".");
|
2011-02-18 18:30:22 +08:00
|
|
|
|
2011-03-09 12:00:04 +08:00
|
|
|
if (std::string::npos != dotPos && dotPos > pos)
|
2011-02-18 18:30:22 +08:00
|
|
|
{
|
|
|
|
hiRes.insert(dotPos, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hiRes.append(CC_RETINA_DISPLAY_FILENAME_SUFFIX);
|
|
|
|
}
|
|
|
|
DWORD attrib = GetFileAttributesA(hiRes.c_str());
|
|
|
|
|
|
|
|
if (attrib != INVALID_FILE_ATTRIBUTES && ! (FILE_ATTRIBUTE_DIRECTORY & attrib))
|
|
|
|
{
|
|
|
|
pRet->m_sString.swap(hiRes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-10-08 17:53:04 +08:00
|
|
|
return pRet->m_sString.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
|
|
|
|
{
|
|
|
|
_CheckPath();
|
2011-04-26 14:53:57 +08:00
|
|
|
// std::string relativeFile = fullPathFromRelativePath(pszRelativeFile);
|
|
|
|
std::string relativeFile = pszRelativeFile;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCString *pRet = new CCString();
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->autorelease();
|
2011-05-16 17:13:15 +08:00
|
|
|
pRet->m_sString = relativeFile.substr(0, relativeFile.find_last_of("/\\") + 1);
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->m_sString += pszFilename;
|
|
|
|
return pRet->m_sString.c_str();
|
|
|
|
}
|
|
|
|
|
2011-03-26 13:52:33 +08:00
|
|
|
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
|
|
|
|
{
|
2011-03-29 15:05:53 +08:00
|
|
|
unsigned char * pBuffer = NULL;
|
2011-03-26 13:52:33 +08:00
|
|
|
|
2011-04-26 14:53:57 +08:00
|
|
|
do
|
|
|
|
{
|
2011-03-26 13:52:33 +08:00
|
|
|
// 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:05:53 +08:00
|
|
|
pBuffer = new unsigned char[*pSize];
|
|
|
|
*pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
|
2011-04-26 14:53:57 +08:00
|
|
|
fclose(fp);
|
2011-03-26 13:52:33 +08:00
|
|
|
} while (0);
|
|
|
|
|
2011-03-29 15:05:53 +08:00
|
|
|
if (! pBuffer && getIsPopupNotify())
|
2011-03-29 11:41:44 +08:00
|
|
|
{
|
|
|
|
std::string title = "Notification";
|
|
|
|
std::string msg = "Get data from file(";
|
|
|
|
msg.append(pszFileName).append(") failed!");
|
|
|
|
|
|
|
|
CCMessageBox(msg.c_str(), title.c_str());
|
|
|
|
}
|
2011-03-29 15:05:53 +08:00
|
|
|
return pBuffer;
|
2011-03-26 13:52:33 +08:00
|
|
|
}
|
|
|
|
|
2011-04-25 09:40:10 +08:00
|
|
|
void CCFileUtils::setResource(const char* pszZipFileName)
|
2011-03-26 13:52:33 +08:00
|
|
|
{
|
|
|
|
CCAssert(0, "Have not implement!");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCFileUtils::getResourcePath(void)
|
|
|
|
{
|
|
|
|
CCAssert(0, "Have not implement!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCFileUtils::setRelativePath(const char* pszRelativePath)
|
2010-10-08 17:53:04 +08:00
|
|
|
{
|
2011-03-26 13:52:33 +08:00
|
|
|
CCAssert(0, "Have not implement!");
|
2010-10-08 17:53:04 +08:00
|
|
|
}
|
|
|
|
|
2011-05-06 17:46:19 +08:00
|
|
|
string CCFileUtils::getWriteablePath()
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
// return the path that the exe file saved in
|
|
|
|
|
|
|
|
char full_path[_MAX_PATH + 1];
|
|
|
|
::GetModuleFileNameA(NULL, full_path, _MAX_PATH + 1);
|
|
|
|
|
|
|
|
string ret((char*)full_path);
|
|
|
|
|
|
|
|
// remove xxx.exe
|
|
|
|
ret = ret.substr(0, ret.rfind("\\") + 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-26 13:52:33 +08:00
|
|
|
NS_CC_END;
|