2013-02-01 11:20:46 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-01-31 08:51:43 +08:00
|
|
|
|
2014-09-10 08:41:49 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-01-31 08:51:43 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/win32/CCFileUtils-win32.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCCommon.h"
|
2013-02-01 11:20:46 +08:00
|
|
|
#include <Shlobj.h>
|
2015-02-06 20:12:02 +08:00
|
|
|
#include <cstdlib>
|
2015-07-13 17:06:01 +08:00
|
|
|
#include <regex>
|
2016-04-26 13:37:22 +08:00
|
|
|
#include <sstream>
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
#define CC_MAX_PATH 512
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
// The root path of resources, the character encoding is UTF-8.
|
|
|
|
// UTF-8 is the only encoding supported by cocos2d-x API.
|
|
|
|
static std::string s_resourcePath = "";
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
// D:\aaa\bbb\ccc\ddd\abc.txt --> D:/aaa/bbb/ccc/ddd/abc.txt
|
|
|
|
static inline std::string convertPathFormatToUnixStyle(const std::string& path)
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
|
|
|
std::string ret = path;
|
|
|
|
int len = ret.length();
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
if (ret[i] == '\\')
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
ret[i] = '/';
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
static std::wstring StringUtf8ToWideChar(const std::string& strUtf8)
|
|
|
|
{
|
|
|
|
std::wstring ret;
|
|
|
|
if (!strUtf8.empty())
|
|
|
|
{
|
|
|
|
int nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, nullptr, 0);
|
|
|
|
if (nNum)
|
|
|
|
{
|
|
|
|
WCHAR* wideCharString = new WCHAR[nNum + 1];
|
|
|
|
wideCharString[0] = 0;
|
|
|
|
|
|
|
|
nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, wideCharString, nNum + 1);
|
|
|
|
|
|
|
|
ret = wideCharString;
|
|
|
|
delete[] wideCharString;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Wrong convert to WideChar code:0x%x", GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string StringWideCharToUtf8(const std::wstring& strWideChar)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
if (!strWideChar.empty())
|
|
|
|
{
|
|
|
|
int nNum = WideCharToMultiByte(CP_UTF8, 0, strWideChar.c_str(), -1, nullptr, 0, nullptr, FALSE);
|
|
|
|
if (nNum)
|
|
|
|
{
|
|
|
|
char* utf8String = new char[nNum + 1];
|
|
|
|
utf8String[0] = 0;
|
|
|
|
|
|
|
|
nNum = WideCharToMultiByte(CP_UTF8, 0, strWideChar.c_str(), -1, utf8String, nNum + 1, nullptr, FALSE);
|
|
|
|
|
|
|
|
ret = utf8String;
|
|
|
|
delete[] utf8String;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Wrong convert to Utf8 code:0x%x", GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string UTF8StringToMultiByte(const std::string& strUtf8)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
if (!strUtf8.empty())
|
|
|
|
{
|
|
|
|
std::wstring strWideChar = StringUtf8ToWideChar(strUtf8);
|
|
|
|
int nNum = WideCharToMultiByte(CP_ACP, 0, strWideChar.c_str(), -1, nullptr, 0, nullptr, FALSE);
|
|
|
|
if (nNum)
|
|
|
|
{
|
|
|
|
char* ansiString = new char[nNum + 1];
|
|
|
|
ansiString[0] = 0;
|
|
|
|
|
|
|
|
nNum = WideCharToMultiByte(CP_ACP, 0, strWideChar.c_str(), -1, ansiString, nNum + 1, nullptr, FALSE);
|
|
|
|
|
|
|
|
ret = ansiString;
|
|
|
|
delete[] ansiString;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Wrong convert to Ansi code:0x%x", GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
static void _checkPath()
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2015-09-01 11:26:09 +08:00
|
|
|
if (s_resourcePath.empty())
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2015-11-19 15:21:14 +08:00
|
|
|
WCHAR utf16Path[CC_MAX_PATH] = { 0 };
|
|
|
|
GetModuleFileNameW(NULL, utf16Path, CC_MAX_PATH - 1);
|
2015-11-25 09:49:19 +08:00
|
|
|
WCHAR *pUtf16ExePath = &(utf16Path[0]);
|
2015-02-06 20:12:02 +08:00
|
|
|
|
|
|
|
// We need only directory part without exe
|
|
|
|
WCHAR *pUtf16DirEnd = wcsrchr(pUtf16ExePath, L'\\');
|
|
|
|
|
|
|
|
char utf8ExeDir[CC_MAX_PATH] = { 0 };
|
|
|
|
int nNum = WideCharToMultiByte(CP_UTF8, 0, pUtf16ExePath, pUtf16DirEnd-pUtf16ExePath+1, utf8ExeDir, sizeof(utf8ExeDir), nullptr, nullptr);
|
|
|
|
|
|
|
|
s_resourcePath = convertPathFormatToUnixStyle(utf8ExeDir);
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:03:39 +08:00
|
|
|
FileUtils* FileUtils::getInstance()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if (s_sharedFileUtils == nullptr)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_sharedFileUtils = new FileUtilsWin32();
|
2013-07-23 20:44:08 +08:00
|
|
|
if(!s_sharedFileUtils->init())
|
|
|
|
{
|
|
|
|
delete s_sharedFileUtils;
|
2014-07-10 00:45:27 +08:00
|
|
|
s_sharedFileUtils = nullptr;
|
2013-07-23 20:44:08 +08:00
|
|
|
CCLOG("ERROR: Could not init CCFileUtilsWin32");
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
return s_sharedFileUtils;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtilsWin32::FileUtilsWin32()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsWin32::init()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
_checkPath();
|
2013-08-02 17:31:19 +08:00
|
|
|
_defaultResRootPath = s_resourcePath;
|
2013-06-20 14:13:12 +08:00
|
|
|
return FileUtils::init();
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
bool FileUtilsWin32::isDirectoryExistInternal(const std::string& dirPath) const
|
|
|
|
{
|
|
|
|
unsigned long fAttrib = GetFileAttributes(StringUtf8ToWideChar(dirPath).c_str());
|
|
|
|
if (fAttrib != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(fAttrib & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtilsWin32::getSuitableFOpen(const std::string& filenameUtf8) const
|
|
|
|
{
|
|
|
|
return UTF8StringToMultiByte(filenameUtf8);
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:50:20 +08:00
|
|
|
long FileUtilsWin32::getFileSize(const std::string &filepath)
|
|
|
|
{
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fad;
|
|
|
|
if (!GetFileAttributesEx(StringUtf8ToWideChar(filepath).c_str(), GetFileExInfoStandard, &fad))
|
|
|
|
{
|
|
|
|
return 0; // error condition, could call GetLastError to find out more
|
|
|
|
}
|
|
|
|
LARGE_INTEGER size;
|
|
|
|
size.HighPart = fad.nFileSizeHigh;
|
|
|
|
size.LowPart = fad.nFileSizeLow;
|
|
|
|
return (long)size.QuadPart;
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:35:09 +08:00
|
|
|
bool FileUtilsWin32::isFileExistInternal(const std::string& strFilePath) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2015-09-01 11:26:09 +08:00
|
|
|
if (strFilePath.empty())
|
2013-04-25 21:51:13 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 22:19:58 +08:00
|
|
|
std::string strPath = strFilePath;
|
|
|
|
if (!isAbsolutePath(strPath))
|
|
|
|
{ // Not absolute path, add the default root path at the beginning.
|
2013-06-15 14:03:30 +08:00
|
|
|
strPath.insert(0, _defaultResRootPath);
|
2013-02-01 22:19:58 +08:00
|
|
|
}
|
2013-07-29 18:27:26 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
DWORD attr = GetFileAttributesW(StringUtf8ToWideChar(strPath).c_str());
|
2014-04-08 15:24:47 +08:00
|
|
|
if(attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
return false; // not a file
|
|
|
|
return true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
bool FileUtilsWin32::isAbsolutePath(const std::string& strPath) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2015-07-07 14:06:59 +08:00
|
|
|
if ( (strPath.length() > 2
|
2013-02-01 11:20:46 +08:00
|
|
|
&& ( (strPath[0] >= 'a' && strPath[0] <= 'z') || (strPath[0] >= 'A' && strPath[0] <= 'Z') )
|
2015-02-12 16:31:12 +08:00
|
|
|
&& strPath[1] == ':') || (strPath[0] == '/' && strPath[1] == '/'))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-16 17:24:54 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
FileUtils::Status FileUtilsWin32::getContents(const std::string& filename, ResizableBuffer* buffer)
|
2013-12-18 14:58:17 +08:00
|
|
|
{
|
2014-04-10 11:07:00 +08:00
|
|
|
if (filename.empty())
|
2016-04-26 13:37:22 +08:00
|
|
|
return FileUtils::Status::NotExists;
|
|
|
|
|
|
|
|
// read the file from hardware
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
|
|
|
|
|
|
|
|
HANDLE fileHandle = ::CreateFile(StringUtf8ToWideChar(fullPath).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
|
|
|
if (fileHandle == INVALID_HANDLE_VALUE)
|
|
|
|
return FileUtils::Status::OpenFailed;
|
|
|
|
|
|
|
|
DWORD hi;
|
|
|
|
auto size = ::GetFileSize(fileHandle, &hi);
|
|
|
|
if (hi > 0)
|
|
|
|
{
|
|
|
|
::CloseHandle(fileHandle);
|
|
|
|
return FileUtils::Status::TooLarge;
|
|
|
|
}
|
2016-07-05 14:27:28 +08:00
|
|
|
// don't read file content if it is empty
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
::CloseHandle(fileHandle);
|
|
|
|
return FileUtils::Status::OK;
|
|
|
|
}
|
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
buffer->resize(size);
|
|
|
|
DWORD sizeRead = 0;
|
|
|
|
BOOL successed = ::ReadFile(fileHandle, buffer->buffer(), size, &sizeRead, nullptr);
|
|
|
|
::CloseHandle(fileHandle);
|
|
|
|
|
|
|
|
if (!successed) {
|
|
|
|
CCLOG("Get data from file(%s) failed, error code is %s", filename.data(), std::to_string(::GetLastError()).data());
|
|
|
|
buffer->resize(sizeRead);
|
2016-06-25 11:58:55 +08:00
|
|
|
return FileUtils::Status::ReadFailed;
|
2014-04-10 11:07:00 +08:00
|
|
|
}
|
2016-04-26 13:37:22 +08:00
|
|
|
return FileUtils::Status::OK;
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
std::string FileUtilsWin32::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
|
|
|
std::string unixFileName = convertPathFormatToUnixStyle(filename);
|
2013-08-02 17:31:19 +08:00
|
|
|
std::string unixResolutionDirectory = convertPathFormatToUnixStyle(resolutionDirectory);
|
2013-07-29 18:27:26 +08:00
|
|
|
std::string unixSearchPath = convertPathFormatToUnixStyle(searchPath);
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
return FileUtils::getPathForFilename(unixFileName, unixResolutionDirectory, unixSearchPath);
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
std::string FileUtilsWin32::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename) const
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
std::string unixDirectory = convertPathFormatToUnixStyle(strDirectory);
|
|
|
|
std::string unixFilename = convertPathFormatToUnixStyle(strFilename);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
return FileUtils::getFullPathForDirectoryAndFilename(unixDirectory, unixFilename);
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
string FileUtilsWin32::getWritablePath() const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2015-02-28 18:21:57 +08:00
|
|
|
if (_writablePath.length())
|
|
|
|
{
|
|
|
|
return _writablePath;
|
|
|
|
}
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
// Get full path of executable, e.g. c:\Program Files (x86)\My Game Folder\MyGame.exe
|
2015-07-13 17:06:01 +08:00
|
|
|
WCHAR full_path[CC_MAX_PATH + 1] = { 0 };
|
|
|
|
::GetModuleFileName(nullptr, full_path, CC_MAX_PATH + 1);
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
// Debug app uses executable directory; Non-debug app uses local app data directory
|
2014-08-29 17:27:55 +08:00
|
|
|
//#ifndef _DEBUG
|
2015-07-13 17:06:01 +08:00
|
|
|
// Get filename of executable only, e.g. MyGame.exe
|
|
|
|
WCHAR *base_name = wcsrchr(full_path, '\\');
|
|
|
|
wstring retPath;
|
|
|
|
if(base_name)
|
|
|
|
{
|
|
|
|
WCHAR app_data_path[CC_MAX_PATH + 1];
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
// Get local app data directory, e.g. C:\Documents and Settings\username\Local Settings\Application Data
|
|
|
|
if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, app_data_path)))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2015-07-13 17:06:01 +08:00
|
|
|
wstring ret(app_data_path);
|
|
|
|
|
|
|
|
// Adding executable filename, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame.exe
|
|
|
|
ret += base_name;
|
|
|
|
|
|
|
|
// Remove ".exe" extension, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame
|
|
|
|
ret = ret.substr(0, ret.rfind(L"."));
|
|
|
|
|
|
|
|
ret += L"\\";
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
// Create directory
|
|
|
|
if (SUCCEEDED(SHCreateDirectoryEx(nullptr, ret.c_str(), nullptr)))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2015-07-13 17:06:01 +08:00
|
|
|
retPath = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (retPath.empty())
|
|
|
|
//#endif // not defined _DEBUG
|
|
|
|
{
|
|
|
|
// If fetching of local app data directory fails, use the executable one
|
|
|
|
retPath = full_path;
|
|
|
|
|
|
|
|
// remove xxx.exe
|
|
|
|
retPath = retPath.substr(0, retPath.rfind(L"\\") + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertPathFormatToUnixStyle(StringWideCharToUtf8(retPath));
|
|
|
|
}
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
bool FileUtilsWin32::renameFile(const std::string &oldfullpath, const std::string& newfullpath)
|
2015-07-13 17:06:01 +08:00
|
|
|
{
|
2015-08-13 15:14:10 +08:00
|
|
|
CCASSERT(!oldfullpath.empty(), "Invalid path");
|
|
|
|
CCASSERT(!newfullpath.empty(), "Invalid path");
|
2015-07-13 17:06:01 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
std::wstring _wNew = StringUtf8ToWideChar(newfullpath);
|
|
|
|
std::wstring _wOld = StringUtf8ToWideChar(oldfullpath);
|
2015-07-13 17:06:01 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(newfullpath))
|
2015-07-13 17:06:01 +08:00
|
|
|
{
|
|
|
|
if (!DeleteFile(_wNew.c_str()))
|
|
|
|
{
|
2015-08-13 15:14:10 +08:00
|
|
|
CCLOGERROR("Fail to delete file %s !Error code is 0x%x", newfullpath.c_str(), GetLastError());
|
2015-07-13 17:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
if (MoveFile(_wOld.c_str(), _wNew.c_str()))
|
2015-07-13 17:06:01 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-13 15:14:10 +08:00
|
|
|
CCLOGERROR("Fail to rename file %s to %s !Error code is 0x%x", oldfullpath.c_str(), newfullpath.c_str(), GetLastError());
|
2015-07-13 17:06:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
bool FileUtilsWin32::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
|
|
|
|
{
|
|
|
|
CCASSERT(!path.empty(), "Invalid path");
|
|
|
|
std::string oldPath = path + oldname;
|
|
|
|
std::string newPath = path + name;
|
|
|
|
|
|
|
|
std::regex pat("\\/");
|
|
|
|
std::string _old = std::regex_replace(oldPath, pat, "\\");
|
|
|
|
std::string _new = std::regex_replace(newPath, pat, "\\");
|
|
|
|
|
|
|
|
return renameFile(_old, _new);
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
bool FileUtilsWin32::createDirectory(const std::string& dirPath)
|
|
|
|
{
|
|
|
|
CCASSERT(!dirPath.empty(), "Invalid path");
|
|
|
|
|
|
|
|
if (isDirectoryExist(dirPath))
|
|
|
|
return true;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
std::wstring path = StringUtf8ToWideChar(dirPath);
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
// Split the path
|
|
|
|
size_t start = 0;
|
|
|
|
size_t found = path.find_first_of(L"/\\", start);
|
|
|
|
std::wstring subpath;
|
|
|
|
std::vector<std::wstring> dirs;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
if (found != std::wstring::npos)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
subpath = path.substr(start, found - start + 1);
|
|
|
|
if (!subpath.empty())
|
|
|
|
dirs.push_back(subpath);
|
|
|
|
start = found + 1;
|
|
|
|
found = path.find_first_of(L"/\\", start);
|
|
|
|
if (found == std::wstring::npos)
|
|
|
|
{
|
|
|
|
if (start < path.length())
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2015-07-13 17:06:01 +08:00
|
|
|
dirs.push_back(path.substr(start));
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2015-07-13 17:06:01 +08:00
|
|
|
break;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 17:06:01 +08:00
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
if ((GetFileAttributes(path.c_str())) == INVALID_FILE_ATTRIBUTES)
|
|
|
|
{
|
|
|
|
subpath = L"";
|
|
|
|
for (unsigned int i = 0; i < dirs.size(); ++i)
|
|
|
|
{
|
|
|
|
subpath += dirs[i];
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
std::string utf8Path = StringWideCharToUtf8(subpath);
|
|
|
|
if (!isDirectoryExist(utf8Path))
|
|
|
|
{
|
|
|
|
BOOL ret = CreateDirectory(subpath.c_str(), NULL);
|
|
|
|
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
|
|
|
|
{
|
|
|
|
CCLOGERROR("Fail create directory %s !Error code is 0x%x", utf8Path.c_str(), GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
bool FileUtilsWin32::removeFile(const std::string &filepath)
|
|
|
|
{
|
|
|
|
std::regex pat("\\/");
|
|
|
|
std::string win32path = std::regex_replace(filepath, pat, "\\");
|
2013-08-02 17:31:19 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
if (DeleteFile(StringUtf8ToWideChar(win32path).c_str()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOGERROR("Fail remove file %s !Error code is 0x%x", filepath.c_str(), GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtilsWin32::removeDirectory(const std::string& dirPath)
|
|
|
|
{
|
|
|
|
std::wstring wpath = StringUtf8ToWideChar(dirPath);
|
|
|
|
std::wstring files = wpath + L"*.*";
|
|
|
|
WIN32_FIND_DATA wfd;
|
|
|
|
HANDLE search = FindFirstFileEx(files.c_str(), FindExInfoStandard, &wfd, FindExSearchNameMatch, NULL, 0);
|
|
|
|
bool ret = true;
|
|
|
|
if (search != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
BOOL find = true;
|
|
|
|
while (find)
|
|
|
|
{
|
2015-09-18 03:36:43 +08:00
|
|
|
// Need check string . and .. for delete folders and files begin name.
|
|
|
|
std::wstring fileName = wfd.cFileName;
|
|
|
|
if (fileName != L"." && fileName != L"..")
|
2015-07-13 17:06:01 +08:00
|
|
|
{
|
|
|
|
std::wstring temp = wpath + wfd.cFileName;
|
|
|
|
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
{
|
|
|
|
temp += '/';
|
|
|
|
ret = ret && this->removeDirectory(StringWideCharToUtf8(temp));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
|
|
|
|
ret = ret && DeleteFile(temp.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
find = FindNextFile(search, &wfd);
|
|
|
|
}
|
|
|
|
FindClose(search);
|
|
|
|
}
|
|
|
|
if (ret && RemoveDirectory(wpath.c_str()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
2014-01-31 08:51:43 +08:00
|
|
|
|
|
|
|
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|