mirror of https://github.com/axmolengine/axmol.git
Merge pr11866: FileUtilsWin32 correct use all unicode version winapi.
This commit is contained in:
commit
06bf6ffa90
|
@ -59,7 +59,7 @@ THE SOFTWARE.
|
|||
|
||||
NS_CC_BEGIN
|
||||
|
||||
typedef enum
|
||||
typedef enum
|
||||
{
|
||||
SAX_NONE = 0,
|
||||
SAX_KEY,
|
||||
|
@ -81,22 +81,22 @@ class DictMaker : public SAXDelegator
|
|||
{
|
||||
public:
|
||||
SAXResult _resultType;
|
||||
ValueMap _rootDict;
|
||||
ValueVector _rootArray;
|
||||
ValueMap _rootDict;
|
||||
ValueVector _rootArray;
|
||||
|
||||
std::string _curKey; ///< parsed key
|
||||
std::string _curValue; // parsed value
|
||||
SAXState _state;
|
||||
|
||||
ValueMap* _curDict;
|
||||
ValueMap* _curDict;
|
||||
ValueVector* _curArray;
|
||||
|
||||
std::stack<ValueMap*> _dictStack;
|
||||
std::stack<ValueMap*> _dictStack;
|
||||
std::stack<ValueVector*> _arrayStack;
|
||||
std::stack<SAXState> _stateStack;
|
||||
|
||||
public:
|
||||
DictMaker()
|
||||
DictMaker()
|
||||
: _resultType(SAX_RESULT_NONE)
|
||||
{
|
||||
}
|
||||
|
@ -114,20 +114,20 @@ public:
|
|||
parser.setDelegator(this);
|
||||
|
||||
parser.parse(fileName);
|
||||
return _rootDict;
|
||||
return _rootDict;
|
||||
}
|
||||
|
||||
ValueMap dictionaryWithDataOfFile(const char* filedata, int filesize)
|
||||
{
|
||||
_resultType = SAX_RESULT_DICT;
|
||||
SAXParser parser;
|
||||
ValueMap dictionaryWithDataOfFile(const char* filedata, int filesize)
|
||||
{
|
||||
_resultType = SAX_RESULT_DICT;
|
||||
SAXParser parser;
|
||||
|
||||
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
|
||||
parser.setDelegator(this);
|
||||
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
|
||||
parser.setDelegator(this);
|
||||
|
||||
parser.parse(filedata, filesize);
|
||||
return _rootDict;
|
||||
}
|
||||
parser.parse(filedata, filesize);
|
||||
return _rootDict;
|
||||
}
|
||||
|
||||
ValueVector arrayWithContentsOfFile(const std::string& fileName)
|
||||
{
|
||||
|
@ -138,7 +138,7 @@ public:
|
|||
parser.setDelegator(this);
|
||||
|
||||
parser.parse(fileName);
|
||||
return _rootArray;
|
||||
return _rootArray;
|
||||
}
|
||||
|
||||
void startElement(void *ctx, const char *name, const char **atts)
|
||||
|
@ -148,7 +148,7 @@ public:
|
|||
const std::string sName(name);
|
||||
if( sName == "dict" )
|
||||
{
|
||||
if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
|
||||
if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
|
||||
{
|
||||
_curDict = &_rootDict;
|
||||
}
|
||||
|
@ -165,7 +165,7 @@ public:
|
|||
{
|
||||
// add a new dictionary into the array
|
||||
_curArray->push_back(Value(ValueMap()));
|
||||
_curDict = &(_curArray->rbegin())->asValueMap();
|
||||
_curDict = &(_curArray->rbegin())->asValueMap();
|
||||
}
|
||||
else if (SAX_DICT == preState)
|
||||
{
|
||||
|
@ -173,7 +173,7 @@ public:
|
|||
CCASSERT(! _dictStack.empty(), "The state is wrong!");
|
||||
ValueMap* preDict = _dictStack.top();
|
||||
(*preDict)[_curKey] = Value(ValueMap());
|
||||
_curDict = &(*preDict)[_curKey].asValueMap();
|
||||
_curDict = &(*preDict)[_curKey].asValueMap();
|
||||
}
|
||||
|
||||
// record the dict state
|
||||
|
@ -200,9 +200,9 @@ public:
|
|||
{
|
||||
_state = SAX_ARRAY;
|
||||
|
||||
if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
|
||||
if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
|
||||
{
|
||||
_curArray = &_rootArray;
|
||||
_curArray = &_rootArray;
|
||||
}
|
||||
SAXState preState = SAX_NONE;
|
||||
if (! _stateStack.empty())
|
||||
|
@ -213,14 +213,14 @@ public:
|
|||
if (preState == SAX_DICT)
|
||||
{
|
||||
(*_curDict)[_curKey] = Value(ValueVector());
|
||||
_curArray = &(*_curDict)[_curKey].asValueVector();
|
||||
_curArray = &(*_curDict)[_curKey].asValueVector();
|
||||
}
|
||||
else if (preState == SAX_ARRAY)
|
||||
{
|
||||
CCASSERT(! _arrayStack.empty(), "The state is wrong!");
|
||||
ValueVector* preArray = _arrayStack.top();
|
||||
preArray->push_back(Value(ValueVector()));
|
||||
_curArray = &(_curArray->rbegin())->asValueVector();
|
||||
_curArray = &(_curArray->rbegin())->asValueVector();
|
||||
}
|
||||
// record the array state
|
||||
_stateStack.push(_state);
|
||||
|
@ -300,7 +300,7 @@ public:
|
|||
|
||||
_curValue.clear();
|
||||
}
|
||||
|
||||
|
||||
_state = SAX_NONE;
|
||||
}
|
||||
|
||||
|
@ -328,7 +328,7 @@ public:
|
|||
{
|
||||
CCASSERT(!_curKey.empty(), "key not found : <integer/real>");
|
||||
}
|
||||
|
||||
|
||||
_curValue.append(text);
|
||||
}
|
||||
break;
|
||||
|
@ -370,26 +370,22 @@ static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxm
|
|||
*/
|
||||
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath)
|
||||
{
|
||||
return writeValueMapToFile(dict, fullPath);
|
||||
}
|
||||
|
||||
bool FileUtils::writeValueMapToFile(ValueMap& dict, const std::string& fullPath)
|
||||
{
|
||||
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
|
||||
//CCLOG("tinyxml2 Dictionary %d writeToFile %s", dict->_ID, fullPath.c_str());
|
||||
tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
|
||||
if (nullptr == doc)
|
||||
return false;
|
||||
|
||||
|
||||
tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
||||
if (nullptr == declaration)
|
||||
{
|
||||
delete doc;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
doc->LinkEndChild(declaration);
|
||||
tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
|
||||
doc->LinkEndChild(docType);
|
||||
|
||||
|
||||
tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
|
||||
rootEle->SetAttribute("version", "1.0");
|
||||
if (nullptr == rootEle)
|
||||
|
@ -398,57 +394,17 @@ bool FileUtils::writeValueMapToFile(ValueMap& dict, const std::string& fullPath)
|
|||
return false;
|
||||
}
|
||||
doc->LinkEndChild(rootEle);
|
||||
|
||||
|
||||
tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc);
|
||||
if (nullptr == innerDict)
|
||||
if (nullptr == innerDict )
|
||||
{
|
||||
delete doc;
|
||||
return false;
|
||||
}
|
||||
rootEle->LinkEndChild(innerDict);
|
||||
|
||||
|
||||
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
|
||||
|
||||
delete doc;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool FileUtils::writeValueVectorToFile(ValueVector vecData, const std::string& fullPath)
|
||||
{
|
||||
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
|
||||
if (nullptr == doc)
|
||||
return false;
|
||||
|
||||
tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
||||
if (nullptr == declaration)
|
||||
{
|
||||
delete doc;
|
||||
return false;
|
||||
}
|
||||
|
||||
doc->LinkEndChild(declaration);
|
||||
tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
|
||||
doc->LinkEndChild(docType);
|
||||
|
||||
tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
|
||||
rootEle->SetAttribute("version", "1.0");
|
||||
if (nullptr == rootEle)
|
||||
{
|
||||
delete doc;
|
||||
return false;
|
||||
}
|
||||
doc->LinkEndChild(rootEle);
|
||||
|
||||
tinyxml2::XMLElement *innerDict = generateElementForArray(vecData, doc);
|
||||
if (nullptr == innerDict)
|
||||
{
|
||||
delete doc;
|
||||
return false;
|
||||
}
|
||||
rootEle->LinkEndChild(innerDict);
|
||||
|
||||
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
|
||||
|
||||
|
||||
delete doc;
|
||||
return ret;
|
||||
}
|
||||
|
@ -466,7 +422,7 @@ static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxm
|
|||
node->LinkEndChild(content);
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
// object is integer
|
||||
if (value.getType() == Value::Type::INTEGER)
|
||||
{
|
||||
|
@ -484,21 +440,21 @@ static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxm
|
|||
node->LinkEndChild(content);
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
//object is bool
|
||||
if (value.getType() == Value::Type::BOOLEAN) {
|
||||
tinyxml2::XMLElement* node = doc->NewElement(value.asString().c_str());
|
||||
return node;
|
||||
tinyxml2::XMLElement* node = doc->NewElement(value.asString().c_str());
|
||||
return node;
|
||||
}
|
||||
|
||||
// object is Array
|
||||
if (value.getType() == Value::Type::VECTOR)
|
||||
return generateElementForArray(value.asValueVector(), doc);
|
||||
|
||||
|
||||
// object is Dictionary
|
||||
if (value.getType() == Value::Type::MAP)
|
||||
return generateElementForDict(value.asValueMap(), doc);
|
||||
|
||||
|
||||
CCLOG("This type cannot appear in property list");
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -509,14 +465,14 @@ static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxm
|
|||
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc)
|
||||
{
|
||||
tinyxml2::XMLElement* rootNode = doc->NewElement("dict");
|
||||
|
||||
|
||||
for (const auto &iter : dict)
|
||||
{
|
||||
tinyxml2::XMLElement* tmpNode = doc->NewElement("key");
|
||||
rootNode->LinkEndChild(tmpNode);
|
||||
tinyxml2::XMLText* content = doc->NewText(iter.first.c_str());
|
||||
tmpNode->LinkEndChild(content);
|
||||
|
||||
|
||||
tinyxml2::XMLElement *element = generateElementForObject(iter.second, doc);
|
||||
if (element)
|
||||
rootNode->LinkEndChild(element);
|
||||
|
@ -561,7 +517,7 @@ void FileUtils::setDelegate(FileUtils *delegate)
|
|||
{
|
||||
if (s_sharedFileUtils)
|
||||
delete s_sharedFileUtils;
|
||||
|
||||
|
||||
s_sharedFileUtils = delegate;
|
||||
}
|
||||
|
||||
|
@ -574,38 +530,7 @@ FileUtils::~FileUtils()
|
|||
{
|
||||
}
|
||||
|
||||
bool FileUtils::writeStringToFile(std::string dataStr, const std::string& fullPath)
|
||||
{
|
||||
Data retData;
|
||||
retData.copy((unsigned char*)dataStr.c_str(), dataStr.size());
|
||||
|
||||
return writeDataToFile(retData, fullPath);
|
||||
}
|
||||
|
||||
bool FileUtils::writeDataToFile(Data retData, const std::string& fullPath)
|
||||
{
|
||||
size_t size = 0;
|
||||
const char* mode = "wb";
|
||||
|
||||
CCASSERT(!fullPath.empty() && retData.getSize() != 0, "Invalid parameters.");
|
||||
|
||||
auto fileutils = FileUtils::getInstance();
|
||||
do
|
||||
{
|
||||
// Read the file from hardware
|
||||
FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode);
|
||||
CC_BREAK_IF(!fp);
|
||||
size = retData.getSize();
|
||||
|
||||
fwrite(retData.getBytes(), size, 1, fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return true;
|
||||
} while (0);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileUtils::init()
|
||||
{
|
||||
|
@ -625,13 +550,13 @@ static Data getData(const std::string& filename, bool forString)
|
|||
{
|
||||
return Data::Null;
|
||||
}
|
||||
|
||||
|
||||
Data ret;
|
||||
unsigned char* buffer = nullptr;
|
||||
size_t size = 0;
|
||||
size_t readsize;
|
||||
const char* mode = nullptr;
|
||||
|
||||
|
||||
if (forString)
|
||||
mode = "rt";
|
||||
else
|
||||
|
@ -647,7 +572,7 @@ static Data getData(const std::string& filename, bool forString)
|
|||
fseek(fp,0,SEEK_END);
|
||||
size = ftell(fp);
|
||||
fseek(fp,0,SEEK_SET);
|
||||
|
||||
|
||||
if (forString)
|
||||
{
|
||||
buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));
|
||||
|
@ -657,16 +582,16 @@ static Data getData(const std::string& filename, bool forString)
|
|||
{
|
||||
buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);
|
||||
}
|
||||
|
||||
|
||||
readsize = fread(buffer, sizeof(unsigned char), size, fp);
|
||||
fclose(fp);
|
||||
|
||||
|
||||
if (forString && readsize < size)
|
||||
{
|
||||
buffer[readsize] = '\0';
|
||||
}
|
||||
} while (0);
|
||||
|
||||
|
||||
if (nullptr == buffer || 0 == readsize)
|
||||
{
|
||||
CCLOG("Get data from file %s failed", filename.c_str());
|
||||
|
@ -675,7 +600,7 @@ static Data getData(const std::string& filename, bool forString)
|
|||
{
|
||||
ret.fastSet(buffer, readsize);
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -683,8 +608,8 @@ std::string FileUtils::getStringFromFile(const std::string& filename)
|
|||
{
|
||||
Data data = getData(filename, true);
|
||||
if (data.isNull())
|
||||
return "";
|
||||
|
||||
return "";
|
||||
|
||||
std::string ret((const char*)data.getBytes());
|
||||
return ret;
|
||||
}
|
||||
|
@ -705,7 +630,7 @@ unsigned char* FileUtils::getFileData(const std::string& filename, const char* m
|
|||
const std::string fullPath = fullPathForFilename(filename);
|
||||
FILE *fp = fopen(getSuitableFOpen(fullPath).c_str(), mode);
|
||||
CC_BREAK_IF(!fp);
|
||||
|
||||
|
||||
fseek(fp,0,SEEK_END);
|
||||
*size = ftell(fp);
|
||||
fseek(fp,0,SEEK_SET);
|
||||
|
@ -713,12 +638,12 @@ unsigned char* FileUtils::getFileData(const std::string& filename, const char* m
|
|||
*size = fread(buffer,sizeof(unsigned char), *size,fp);
|
||||
fclose(fp);
|
||||
} while (0);
|
||||
|
||||
|
||||
if (!buffer)
|
||||
{
|
||||
std::string msg = "Get data from file(";
|
||||
msg.append(filename).append(") failed!");
|
||||
|
||||
|
||||
CCLOG("%s", msg.c_str());
|
||||
}
|
||||
return buffer;
|
||||
|
@ -730,14 +655,14 @@ unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, con
|
|||
unzFile file = nullptr;
|
||||
*size = 0;
|
||||
|
||||
do
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(zipFilePath.empty());
|
||||
|
||||
file = unzOpen(zipFilePath.c_str());
|
||||
CC_BREAK_IF(!file);
|
||||
|
||||
// FIXME: Other platforms should use upstream minizip like mingw-w64
|
||||
// FIXME: Other platforms should use upstream minizip like mingw-w64
|
||||
#ifdef MINIZIP_FROM_SYSTEM
|
||||
int ret = unzLocateFile(file, filename.c_str(), NULL);
|
||||
#else
|
||||
|
@ -772,7 +697,7 @@ unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, con
|
|||
std::string FileUtils::getNewFilename(const std::string &filename) const
|
||||
{
|
||||
std::string newFileName;
|
||||
|
||||
|
||||
// in Lookup Filename dictionary ?
|
||||
auto iter = _filenameLookupDict.find(filename);
|
||||
|
||||
|
@ -797,14 +722,14 @@ std::string FileUtils::getPathForFilename(const std::string& filename, const std
|
|||
file_path = filename.substr(0, pos+1);
|
||||
file = filename.substr(pos+1);
|
||||
}
|
||||
|
||||
|
||||
// searchPath + file_path + resourceDirectory
|
||||
std::string path = searchPath;
|
||||
path += file_path;
|
||||
path += resolutionDirectory;
|
||||
|
||||
|
||||
path = getFullPathForDirectoryAndFilename(path, file);
|
||||
|
||||
|
||||
//CCLOG("getPathForFilename, fullPath = %s", path.c_str());
|
||||
return path;
|
||||
}
|
||||
|
@ -815,7 +740,7 @@ std::string FileUtils::fullPathForFilename(const std::string &filename) const
|
|||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
if (isAbsolutePath(filename))
|
||||
{
|
||||
return filename;
|
||||
|
@ -827,28 +752,28 @@ std::string FileUtils::fullPathForFilename(const std::string &filename) const
|
|||
{
|
||||
return cacheIter->second;
|
||||
}
|
||||
|
||||
|
||||
// Get the new file name.
|
||||
const std::string newFilename( getNewFilename(filename) );
|
||||
|
||||
std::string fullpath;
|
||||
|
||||
|
||||
std::string fullpath;
|
||||
|
||||
for (const auto& searchIt : _searchPathArray)
|
||||
{
|
||||
for (const auto& resolutionIt : _searchResolutionsOrderArray)
|
||||
{
|
||||
fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt);
|
||||
|
||||
|
||||
if (fullpath.length() > 0)
|
||||
{
|
||||
// Using the filename passed in as key.
|
||||
_fullPathCache.insert(std::make_pair(filename, fullpath));
|
||||
return fullpath;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(isPopupNotify()){
|
||||
CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", filename.c_str());
|
||||
}
|
||||
|
@ -874,12 +799,12 @@ void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& search
|
|||
{
|
||||
existDefault = true;
|
||||
}
|
||||
|
||||
|
||||
if (resolutionDirectory.length() > 0 && resolutionDirectory[resolutionDirectory.length()-1] != '/')
|
||||
{
|
||||
resolutionDirectory += "/";
|
||||
}
|
||||
|
||||
|
||||
_searchResolutionsOrderArray.push_back(resolutionDirectory);
|
||||
}
|
||||
|
||||
|
@ -894,7 +819,7 @@ void FileUtils::addSearchResolutionsOrder(const std::string &order,const bool fr
|
|||
std::string resOrder = order;
|
||||
if (!resOrder.empty() && resOrder[resOrder.length()-1] != '/')
|
||||
resOrder.append("/");
|
||||
|
||||
|
||||
if (front) {
|
||||
_searchResolutionsOrderArray.insert(_searchResolutionsOrderArray.begin(), resOrder);
|
||||
} else {
|
||||
|
@ -925,14 +850,14 @@ void FileUtils::setDefaultResourceRootPath(const std::string& path)
|
|||
void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
|
||||
{
|
||||
bool existDefaultRootPath = false;
|
||||
|
||||
|
||||
_fullPathCache.clear();
|
||||
_searchPathArray.clear();
|
||||
for (const auto& iter : searchPaths)
|
||||
{
|
||||
std::string prefix;
|
||||
std::string path;
|
||||
|
||||
|
||||
if (!isAbsolutePath(iter))
|
||||
{ // Not an absolute path
|
||||
prefix = _defaultResRootPath;
|
||||
|
@ -948,7 +873,7 @@ void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
|
|||
}
|
||||
_searchPathArray.push_back(path);
|
||||
}
|
||||
|
||||
|
||||
if (!existDefaultRootPath)
|
||||
{
|
||||
//CCLOG("Default root path doesn't exist, adding it.");
|
||||
|
@ -967,16 +892,6 @@ void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
|
|||
{
|
||||
path += "/";
|
||||
}
|
||||
|
||||
auto iter = std::find(_searchPathArray.begin(), _searchPathArray.end(), searchpath);
|
||||
if (iter != _searchPathArray.cend())
|
||||
{
|
||||
if (front)
|
||||
_searchPathArray.erase(iter);
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
if (front) {
|
||||
_searchPathArray.insert(_searchPathArray.begin(), path);
|
||||
} else {
|
||||
|
@ -986,7 +901,7 @@ void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
|
|||
|
||||
void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
|
||||
{
|
||||
_fullPathCache.clear();
|
||||
_fullPathCache.clear();
|
||||
_filenameLookupDict = filenameLookupDict;
|
||||
}
|
||||
|
||||
|
@ -1012,13 +927,13 @@ void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename
|
|||
|
||||
std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const
|
||||
{
|
||||
// get directory+filename, safely adding '/' as necessary
|
||||
// get directory+filename, safely adding '/' as necessary
|
||||
std::string ret = directory;
|
||||
if (directory.size() && directory[directory.size()-1] != '/'){
|
||||
ret += '/';
|
||||
}
|
||||
ret += filename;
|
||||
|
||||
|
||||
// if the file doesn't exist, return an empty string
|
||||
if (!isFileExistInternal(ret)) {
|
||||
ret = "";
|
||||
|
@ -1026,6 +941,23 @@ std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& dir
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::string FileUtils::searchFullPathForFilename(const std::string& filename) const
|
||||
{
|
||||
if (isAbsolutePath(filename))
|
||||
{
|
||||
return filename;
|
||||
}
|
||||
std::string path = fullPathForFilename(filename);
|
||||
if (0 == path.compare(filename))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
else
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileUtils::isFileExist(const std::string& filename) const
|
||||
{
|
||||
if (isAbsolutePath(filename))
|
||||
|
@ -1034,7 +966,7 @@ bool FileUtils::isFileExist(const std::string& filename) const
|
|||
}
|
||||
else
|
||||
{
|
||||
std::string fullpath = fullPathForFilename(filename);
|
||||
std::string fullpath = searchFullPathForFilename(filename);
|
||||
if (fullpath.empty())
|
||||
return false;
|
||||
else
|
||||
|
@ -1047,23 +979,46 @@ bool FileUtils::isAbsolutePath(const std::string& path) const
|
|||
return (path[0] == '/');
|
||||
}
|
||||
|
||||
bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const
|
||||
{
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||
WIN32_FILE_ATTRIBUTE_DATA wfad;
|
||||
std::wstring wdirPath(dirPath.begin(), dirPath.end());
|
||||
if (GetFileAttributesEx(wdirPath.c_str(), GetFileExInfoStandard, &wfad))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#elif (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
||||
struct stat st;
|
||||
if (stat(dirPath.c_str(), &st) == 0)
|
||||
{
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
return false;
|
||||
#else
|
||||
CCASSERT(false, "FileUtils not support isDirectoryExistInternal");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool FileUtils::isDirectoryExist(const std::string& dirPath) const
|
||||
{
|
||||
CCASSERT(!dirPath.empty(), "Invalid path");
|
||||
|
||||
|
||||
if (isAbsolutePath(dirPath))
|
||||
{
|
||||
return isDirectoryExistInternal(dirPath);
|
||||
}
|
||||
|
||||
|
||||
// Already Cached ?
|
||||
auto cacheIter = _fullPathCache.find(dirPath);
|
||||
if( cacheIter != _fullPathCache.end() )
|
||||
{
|
||||
return isDirectoryExistInternal(cacheIter->second);
|
||||
}
|
||||
|
||||
std::string fullpath;
|
||||
|
||||
std::string fullpath;
|
||||
for (const auto& searchIt : _searchPathArray)
|
||||
{
|
||||
for (const auto& resolutionIt : _searchResolutionsOrderArray)
|
||||
|
@ -1077,22 +1032,23 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileUtils::createDirectory(const std::string& path)
|
||||
{
|
||||
CCASSERT(!path.empty(), "Invalid path");
|
||||
|
||||
|
||||
if (isDirectoryExist(path))
|
||||
return true;
|
||||
|
||||
|
||||
// Split the path
|
||||
size_t start = 0;
|
||||
size_t found = path.find_first_of("/\\", start);
|
||||
std::string subpath;
|
||||
std::vector<std::string> dirs;
|
||||
|
||||
|
||||
if (found != std::string::npos)
|
||||
{
|
||||
while (true)
|
||||
|
@ -1115,45 +1071,27 @@ bool FileUtils::createDirectory(const std::string& path)
|
|||
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||
WIN32_FILE_ATTRIBUTE_DATA wfad;
|
||||
WIN32_FILE_ATTRIBUTE_DATA wfad;
|
||||
std::wstring wpath(path.begin(), path.end());
|
||||
if (!(GetFileAttributesEx(wpath.c_str(), GetFileExInfoStandard, &wfad)))
|
||||
{
|
||||
subpath = "";
|
||||
for(unsigned int i = 0 ; i < dirs.size() ; ++i)
|
||||
{
|
||||
subpath += dirs[i];
|
||||
if (i > 0 && !isDirectoryExist(subpath))
|
||||
{
|
||||
{
|
||||
subpath = "";
|
||||
for(unsigned int i = 0 ; i < dirs.size() ; ++i)
|
||||
{
|
||||
subpath += dirs[i];
|
||||
if (i > 0 && !isDirectoryExist(subpath))
|
||||
{
|
||||
std::wstring wsubpath(subpath.begin(), subpath.end());
|
||||
BOOL ret = CreateDirectory(wsubpath.c_str(), NULL);
|
||||
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
||||
if ((GetFileAttributesA(path.c_str())) == INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
subpath = "";
|
||||
for (unsigned int i = 0; i < dirs.size(); ++i)
|
||||
{
|
||||
subpath += dirs[i];
|
||||
if (!isDirectoryExist(subpath))
|
||||
{
|
||||
BOOL ret = CreateDirectoryA(subpath.c_str(), NULL);
|
||||
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
#elif (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
||||
DIR *dir = NULL;
|
||||
|
||||
// Create path recursively
|
||||
|
@ -1162,11 +1100,11 @@ bool FileUtils::createDirectory(const std::string& path)
|
|||
{
|
||||
subpath += dirs[i];
|
||||
dir = opendir(subpath.c_str());
|
||||
|
||||
|
||||
if (!dir)
|
||||
{
|
||||
// directory doesn't exist, should create a new one
|
||||
|
||||
|
||||
int ret = mkdir(subpath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
if (ret != 0 && (errno != EEXIST))
|
||||
{
|
||||
|
@ -1182,6 +1120,9 @@ bool FileUtils::createDirectory(const std::string& path)
|
|||
}
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
CCASSERT(false, "FileUtils not support createDirectory");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -1193,7 +1134,7 @@ static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, str
|
|||
{
|
||||
log("Fail to remove: %s ",fpath);
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
@ -1205,59 +1146,50 @@ bool FileUtils::removeDirectory(const std::string& path)
|
|||
CCLOGERROR("Fail to remove directory, path must termniate with '/': %s", path.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Remove downloaded files
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||
std::wstring wpath = std::wstring(path.begin(), path.end());
|
||||
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)
|
||||
{
|
||||
//. ..
|
||||
if(wfd.cFileName[0]!='.')
|
||||
{
|
||||
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)
|
||||
{
|
||||
//. ..
|
||||
if(wfd.cFileName[0]!='.')
|
||||
{
|
||||
std::wstring temp = wpath + wfd.cFileName;
|
||||
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
{
|
||||
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
{
|
||||
temp += '/';
|
||||
ret = ret && this->removeDirectory(std::string(temp.begin(), temp.end()));
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
|
||||
ret = ret && DeleteFile(temp.c_str());
|
||||
}
|
||||
}
|
||||
find = FindNextFile(search, &wfd);
|
||||
}
|
||||
FindClose(search);
|
||||
}
|
||||
}
|
||||
}
|
||||
find = FindNextFile(search, &wfd);
|
||||
}
|
||||
FindClose(search);
|
||||
}
|
||||
if (ret && RemoveDirectory(wpath.c_str()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
||||
std::string command = "cmd /c rd /s /q ";
|
||||
// Path may include space.
|
||||
command += "\"" + path + "\"";
|
||||
|
||||
if (WinExec(command.c_str(), SW_HIDE) > 31)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
return false;
|
||||
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
||||
if (nftw(path.c_str(),unlink_cb, 64, FTW_DEPTH | FTW_PHYS))
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
#else
|
||||
#elif (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
||||
std::string command = "rm -r ";
|
||||
// Path may include space.
|
||||
command += "\"" + path + "\"";
|
||||
|
@ -1265,6 +1197,9 @@ bool FileUtils::removeDirectory(const std::string& path)
|
|||
return true;
|
||||
else
|
||||
return false;
|
||||
#else
|
||||
CCASSERT(false, "FileUtils not support removeDirectory");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -1275,33 +1210,19 @@ bool FileUtils::removeFile(const std::string &path)
|
|||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||
std::wstring wpath(path.begin(), path.end());
|
||||
if (DeleteFile(wpath.c_str()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
||||
std::string command = "cmd /c del /q ";
|
||||
std::string win32path = path;
|
||||
int len = win32path.length();
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
if (win32path[i] == '/')
|
||||
{
|
||||
win32path[i] = '\\';
|
||||
}
|
||||
}
|
||||
command += win32path;
|
||||
|
||||
if (WinExec(command.c_str(), SW_HIDE) > 31)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
#else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#elif (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
||||
if (remove(path.c_str())) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
CCASSERT(false, "FileUtils not support removeFile");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -1310,42 +1231,20 @@ bool FileUtils::renameFile(const std::string &path, const std::string &oldname,
|
|||
CCASSERT(!path.empty(), "Invalid path");
|
||||
std::string oldPath = path + oldname;
|
||||
std::string newPath = path + name;
|
||||
|
||||
|
||||
// Rename a file
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||
std::regex pat("\\/");
|
||||
std::string _old = std::regex_replace(oldPath, pat, "\\");
|
||||
std::string _new = std::regex_replace(newPath, pat, "\\");
|
||||
if (MoveFileEx(std::wstring(_old.begin(), _old.end()).c_str(),
|
||||
if (MoveFileEx(std::wstring(_old.begin(), _old.end()).c_str(),
|
||||
std::wstring(_new.begin(), _new.end()).c_str(),
|
||||
MOVEFILE_REPLACE_EXISTING & MOVEFILE_WRITE_THROUGH))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
||||
std::regex pat("\\/");
|
||||
std::string _old = std::regex_replace(oldPath, pat, "\\");
|
||||
std::string _new = std::regex_replace(newPath, pat, "\\");
|
||||
|
||||
if(FileUtils::getInstance()->isFileExist(_new))
|
||||
{
|
||||
if (!DeleteFileA(_new.c_str()))
|
||||
{
|
||||
CCLOGERROR("Fail to delete file %s !Error code is 0x%x", newPath.c_str(), GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
if (MoveFileA(_old.c_str(), _new.c_str()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("Fail to rename file %s to %s !Error code is 0x%x", oldPath.c_str(), newPath.c_str(), GetLastError());
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
#elif (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
||||
int errorCode = rename(oldPath.c_str(), newPath.c_str());
|
||||
|
||||
if (0 != errorCode)
|
||||
|
@ -1354,25 +1253,28 @@ bool FileUtils::renameFile(const std::string &path, const std::string &oldname,
|
|||
return false;
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
CCASSERT(false, "FileUtils not support renameFile");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
long FileUtils::getFileSize(const std::string &filepath)
|
||||
{
|
||||
CCASSERT(!filepath.empty(), "Invalid path");
|
||||
|
||||
|
||||
std::string fullpath = filepath;
|
||||
if (!isAbsolutePath(filepath))
|
||||
{
|
||||
fullpath = fullPathForFilename(filepath);
|
||||
fullpath = searchFullPathForFilename(filepath);
|
||||
if (fullpath.empty())
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct stat info;
|
||||
// Get data associated with "crt_stat.c":
|
||||
int result = stat( fullpath.c_str(), &info );
|
||||
|
||||
|
||||
// Check if statistics are valid:
|
||||
if( result != 0 )
|
||||
{
|
||||
|
@ -1460,7 +1362,6 @@ std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
|
|||
{
|
||||
return filenameUtf8;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -30,6 +30,7 @@ THE SOFTWARE.
|
|||
#include "platform/CCCommon.h"
|
||||
#include <Shlobj.h>
|
||||
#include <cstdlib>
|
||||
#include <regex>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -56,6 +57,55 @@ static inline std::string convertPathFormatToUnixStyle(const std::string& path)
|
|||
return ret;
|
||||
}
|
||||
|
||||
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 void _checkPath()
|
||||
{
|
||||
if (0 == s_resourcePath.length())
|
||||
|
@ -99,6 +149,17 @@ bool FileUtilsWin32::init()
|
|||
return FileUtils::init();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::isFileExistInternal(const std::string& strFilePath) const
|
||||
{
|
||||
if (0 == strFilePath.length())
|
||||
|
@ -112,10 +173,7 @@ bool FileUtilsWin32::isFileExistInternal(const std::string& strFilePath) const
|
|||
strPath.insert(0, _defaultResRootPath);
|
||||
}
|
||||
|
||||
WCHAR utf16Buf[CC_MAX_PATH] = {0};
|
||||
MultiByteToWideChar(CP_UTF8, 0, strPath.c_str(), -1, utf16Buf, sizeof(utf16Buf)/sizeof(utf16Buf[0]));
|
||||
|
||||
DWORD attr = GetFileAttributesW(utf16Buf);
|
||||
DWORD attr = GetFileAttributesW(StringUtf8ToWideChar(strPath).c_str());
|
||||
if(attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
|
||||
return false; // not a file
|
||||
return true;
|
||||
|
@ -206,10 +264,7 @@ static Data getData(const std::string& filename, bool forString)
|
|||
// check if the filename uses correct case characters
|
||||
CC_BREAK_IF(!checkFileName(fullPath, filename));
|
||||
|
||||
WCHAR wszBuf[CC_MAX_PATH] = {0};
|
||||
MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)/sizeof(wszBuf[0]));
|
||||
|
||||
HANDLE fileHandle = ::CreateFileW(wszBuf, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
||||
HANDLE fileHandle = ::CreateFile(StringUtf8ToWideChar(fullPath).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
||||
CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE);
|
||||
|
||||
size = ::GetFileSize(fileHandle, nullptr);
|
||||
|
@ -291,10 +346,7 @@ unsigned char* FileUtilsWin32::getFileData(const std::string& filename, const ch
|
|||
// check if the filename uses correct case characters
|
||||
CC_BREAK_IF(!checkFileName(fullPath, filename));
|
||||
|
||||
WCHAR wszBuf[CC_MAX_PATH] = {0};
|
||||
MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)/sizeof(wszBuf[0]));
|
||||
|
||||
HANDLE fileHandle = ::CreateFileW(wszBuf, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
||||
HANDLE fileHandle = ::CreateFile(StringUtf8ToWideChar(fullPath).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
||||
CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE);
|
||||
|
||||
*size = ::GetFileSize(fileHandle, nullptr);
|
||||
|
@ -351,49 +403,191 @@ string FileUtilsWin32::getWritablePath() const
|
|||
}
|
||||
|
||||
// Get full path of executable, e.g. c:\Program Files (x86)\My Game Folder\MyGame.exe
|
||||
char full_path[CC_MAX_PATH + 1];
|
||||
::GetModuleFileNameA(nullptr, full_path, CC_MAX_PATH + 1);
|
||||
WCHAR full_path[CC_MAX_PATH + 1] = { 0 };
|
||||
::GetModuleFileName(nullptr, full_path, CC_MAX_PATH + 1);
|
||||
|
||||
// Debug app uses executable directory; Non-debug app uses local app data directory
|
||||
//#ifndef _DEBUG
|
||||
// Get filename of executable only, e.g. MyGame.exe
|
||||
char *base_name = strrchr(full_path, '\\');
|
||||
// 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];
|
||||
|
||||
if(base_name)
|
||||
// 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)))
|
||||
{
|
||||
char app_data_path[CC_MAX_PATH + 1];
|
||||
wstring ret(app_data_path);
|
||||
|
||||
// Get local app data directory, e.g. C:\Documents and Settings\username\Local Settings\Application Data
|
||||
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, 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"\\";
|
||||
|
||||
// Create directory
|
||||
if (SUCCEEDED(SHCreateDirectoryEx(nullptr, ret.c_str(), nullptr)))
|
||||
{
|
||||
string ret((char*)app_data_path);
|
||||
retPath = ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (retPath.empty())
|
||||
//#endif // not defined _DEBUG
|
||||
{
|
||||
// If fetching of local app data directory fails, use the executable one
|
||||
retPath = full_path;
|
||||
|
||||
// Adding executable filename, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame.exe
|
||||
ret += base_name;
|
||||
// remove xxx.exe
|
||||
retPath = retPath.substr(0, retPath.rfind(L"\\") + 1);
|
||||
}
|
||||
|
||||
// Remove ".exe" extension, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame
|
||||
ret = ret.substr(0, ret.rfind("."));
|
||||
return convertPathFormatToUnixStyle(StringWideCharToUtf8(retPath));
|
||||
}
|
||||
|
||||
ret += "\\";
|
||||
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;
|
||||
|
||||
// Create directory
|
||||
if (SUCCEEDED(SHCreateDirectoryExA(nullptr, ret.c_str(), nullptr)))
|
||||
std::regex pat("\\/");
|
||||
std::string _old = std::regex_replace(oldPath, pat, "\\");
|
||||
std::string _new = std::regex_replace(newPath, pat, "\\");
|
||||
|
||||
std::wstring _wNew = StringUtf8ToWideChar(_new);
|
||||
|
||||
if (FileUtils::getInstance()->isFileExist(_new))
|
||||
{
|
||||
if (!DeleteFile(_wNew.c_str()))
|
||||
{
|
||||
CCLOGERROR("Fail to delete file %s !Error code is 0x%x", newPath.c_str(), GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
if (MoveFile(StringUtf8ToWideChar(_old).c_str(), _wNew.c_str()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOGERROR("Fail to rename file %s to %s !Error code is 0x%x", oldPath.c_str(), newPath.c_str(), GetLastError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::createDirectory(const std::string& dirPath)
|
||||
{
|
||||
CCASSERT(!dirPath.empty(), "Invalid path");
|
||||
|
||||
if (isDirectoryExist(dirPath))
|
||||
return true;
|
||||
|
||||
std::wstring path = StringUtf8ToWideChar(dirPath);
|
||||
|
||||
// Split the path
|
||||
size_t start = 0;
|
||||
size_t found = path.find_first_of(L"/\\", start);
|
||||
std::wstring subpath;
|
||||
std::vector<std::wstring> dirs;
|
||||
|
||||
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())
|
||||
{
|
||||
return convertPathFormatToUnixStyle(ret);
|
||||
dirs.push_back(path.substr(start));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((GetFileAttributes(path.c_str())) == INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
subpath = L"";
|
||||
for (unsigned int i = 0; i < dirs.size(); ++i)
|
||||
{
|
||||
subpath += dirs[i];
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
//#endif // not defined _DEBUG
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// If fetching of local app data directory fails, use the executable one
|
||||
string ret((char*)full_path);
|
||||
bool FileUtilsWin32::removeFile(const std::string &filepath)
|
||||
{
|
||||
std::regex pat("\\/");
|
||||
std::string win32path = std::regex_replace(filepath, pat, "\\");
|
||||
|
||||
// remove xxx.exe
|
||||
ret = ret.substr(0, ret.rfind("\\") + 1);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
ret = convertPathFormatToUnixStyle(ret);
|
||||
|
||||
return ret;
|
||||
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)
|
||||
{
|
||||
//. ..
|
||||
if (wfd.cFileName[0] != '.')
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -49,12 +49,53 @@ class CC_DLL FileUtilsWin32 : public FileUtils
|
|||
public:
|
||||
/* override funtions */
|
||||
bool init();
|
||||
virtual std::string getWritablePath() const;
|
||||
virtual bool isAbsolutePath(const std::string& strPath) const;
|
||||
virtual std::string getWritablePath() const override;
|
||||
virtual bool isAbsolutePath(const std::string& strPath) const override;
|
||||
protected:
|
||||
|
||||
virtual bool isFileExistInternal(const std::string& strFilePath) const override;
|
||||
|
||||
/**
|
||||
* Renames a file under the given directory.
|
||||
*
|
||||
* @param path The parent directory path of the file, it must be an absolute path.
|
||||
* @param oldname The current name of the file.
|
||||
* @param name The new name of the file.
|
||||
* @return True if the file have been renamed successfully, false if not.
|
||||
*/
|
||||
virtual bool renameFile(const std::string &path, const std::string &oldname, const std::string &name) override;
|
||||
|
||||
/**
|
||||
* Checks whether a directory exists without considering search paths and resolution orders.
|
||||
* @param dirPath The directory (with absolute path) to look up for
|
||||
* @return Returns true if the directory found at the given absolute path, otherwise returns false
|
||||
*/
|
||||
virtual bool isDirectoryExistInternal(const std::string& dirPath) const override;
|
||||
|
||||
/**
|
||||
* Removes a file.
|
||||
*
|
||||
* @param filepath The full path of the file, it must be an absolute path.
|
||||
* @return True if the file have been removed successfully, false if not.
|
||||
*/
|
||||
virtual bool removeFile(const std::string &filepath) override;
|
||||
|
||||
/**
|
||||
* Creates a directory.
|
||||
*
|
||||
* @param dirPath The path of the directory, it must be an absolute path.
|
||||
* @return True if the directory have been created successfully, false if not.
|
||||
*/
|
||||
virtual bool createDirectory(const std::string& dirPath) override;
|
||||
|
||||
/**
|
||||
* Removes a directory.
|
||||
*
|
||||
* @param dirPath The full path of the directory, it must be an absolute path.
|
||||
* @return True if the directory have been removed successfully, false if not.
|
||||
*/
|
||||
virtual bool removeDirectory(const std::string& dirPath) override;
|
||||
|
||||
/**
|
||||
* Gets resource file data
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue