Skip to content

Commit 41ee83c

Browse files
committed
Issue cocos2d#5892: Polish FileUtils's renameFile for AssetsManager
1 parent d4214ff commit 41ee83c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

cocos/platform/CCFileUtils.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,25 +1126,34 @@ bool FileUtils::removeFile(const std::string &path)
11261126
bool FileUtils::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
11271127
{
11281128
CCASSERT(!path.empty(), "Invalid path");
1129+
std::string oldPath = path + oldname;
1130+
std::string newPath = path + name;
11291131

11301132
// Rename a file
11311133
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
1132-
std::string oldPath = path + oldname;
1133-
std::string newPath = path + name;
1134-
if (rename(oldPath.c_str(), newPath.c_str()) != 0)
1134+
if (0 != rename(oldPath.c_str(), newPath.c_str()))
11351135
{
11361136
CCLOGERROR("Fail to rename file %s to %s !", oldPath.c_str(), newPath.c_str());
11371137
return false;
11381138
}
11391139
return true;
11401140
#else
1141-
std::string command = "ren ";
1142-
// Path may include space.
1143-
command += "\"" + path + oldname + "\" \"" + name + "\"";
1144-
if (WinExec(command.c_str(), SW_HIDE) > 31)
1141+
std::regex pat("\/");
1142+
std::string _old = std::regex_replace(oldfile, pat, "\\");
1143+
std::string _new = std::regex_replace(newfile, pat, "\\");
1144+
1145+
if(FileUtils::getInstance()->isFileExist(_new))
1146+
{
1147+
DeleteFileA(_new.c_str());
1148+
}
1149+
1150+
MoveFileA(_old.c_str(), _new.c_str());
1151+
1152+
if(0 == GetLastError())
11451153
return true;
11461154
else
11471155
return false;
1156+
11481157
#endif
11491158
}
11501159

@@ -1165,7 +1174,7 @@ long FileUtils::getFileSize(const std::string &filepath)
11651174
int result = stat( fullpath.c_str(), &info );
11661175

11671176
// Check if statistics are valid:
1168-
if( result != 0 )
1177+
if( 0 != result )
11691178
{
11701179
// Failed
11711180
return -1;

0 commit comments

Comments
 (0)