axmol/cocos/platform/CCFileUtils.cpp

1322 lines
36 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
#include "CCFileUtils.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include <stack>
2014-04-27 01:35:57 +08:00
#include "base/CCData.h"
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
#include "base/CCDirector.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCSAXParser.h"
2014-07-14 20:45:24 +08:00
#include "base/ccUtils.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "tinyxml2.h"
#ifdef MINIZIP_FROM_SYSTEM
#include <minizip/unzip.h>
#else // from our embedded sources
#include "unzip.h"
#endif
#include <sys/stat.h>
2014-10-10 14:03:20 +08:00
2014-10-15 14:32:11 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include <regex>
#endif
2014-10-10 14:03:20 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#include <ftw.h>
2014-10-10 14:03:20 +08:00
#endif
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>
#endif
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
NS_CC_BEGIN
typedef enum
{
SAX_NONE = 0,
SAX_KEY,
SAX_DICT,
SAX_INT,
SAX_REAL,
SAX_STRING,
SAX_ARRAY
}SAXState;
typedef enum
{
SAX_RESULT_NONE = 0,
SAX_RESULT_DICT,
SAX_RESULT_ARRAY
}SAXResult;
class DictMaker : public SAXDelegator
{
public:
SAXResult _resultType;
ValueMap _rootDict;
ValueVector _rootArray;
std::string _curKey; ///< parsed key
std::string _curValue; // parsed value
SAXState _state;
ValueMap* _curDict;
ValueVector* _curArray;
std::stack<ValueMap*> _dictStack;
std::stack<ValueVector*> _arrayStack;
std::stack<SAXState> _stateStack;
public:
DictMaker()
: _resultType(SAX_RESULT_NONE)
{
}
~DictMaker()
{
}
ValueMap dictionaryWithContentsOfFile(const std::string& fileName)
{
_resultType = SAX_RESULT_DICT;
SAXParser parser;
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
parser.setDelegator(this);
parser.parse(fileName);
return _rootDict;
}
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);
parser.parse(filedata, filesize);
return _rootDict;
}
ValueVector arrayWithContentsOfFile(const std::string& fileName)
{
_resultType = SAX_RESULT_ARRAY;
SAXParser parser;
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
parser.setDelegator(this);
parser.parse(fileName);
return _rootArray;
}
void startElement(void *ctx, const char *name, const char **atts)
{
CC_UNUSED_PARAM(ctx);
CC_UNUSED_PARAM(atts);
const std::string sName(name);
if( sName == "dict" )
{
if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
{
_curDict = &_rootDict;
}
_state = SAX_DICT;
SAXState preState = SAX_NONE;
if (! _stateStack.empty())
{
preState = _stateStack.top();
}
if (SAX_ARRAY == preState)
{
// add a new dictionary into the array
_curArray->push_back(Value(ValueMap()));
_curDict = &(_curArray->rbegin())->asValueMap();
}
else if (SAX_DICT == preState)
{
// add a new dictionary into the pre dictionary
CCASSERT(! _dictStack.empty(), "The state is wrong!");
ValueMap* preDict = _dictStack.top();
(*preDict)[_curKey] = Value(ValueMap());
_curDict = &(*preDict)[_curKey].asValueMap();
}
// record the dict state
_stateStack.push(_state);
_dictStack.push(_curDict);
}
else if(sName == "key")
{
_state = SAX_KEY;
}
else if(sName == "integer")
{
_state = SAX_INT;
}
else if(sName == "real")
{
_state = SAX_REAL;
}
else if(sName == "string")
{
_state = SAX_STRING;
}
else if (sName == "array")
{
_state = SAX_ARRAY;
if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
{
_curArray = &_rootArray;
}
SAXState preState = SAX_NONE;
if (! _stateStack.empty())
{
preState = _stateStack.top();
}
if (preState == SAX_DICT)
{
(*_curDict)[_curKey] = Value(ValueVector());
_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();
}
// record the array state
_stateStack.push(_state);
_arrayStack.push(_curArray);
}
else
{
_state = SAX_NONE;
}
}
void endElement(void *ctx, const char *name)
{
CC_UNUSED_PARAM(ctx);
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
const std::string sName((char*)name);
if( sName == "dict" )
{
_stateStack.pop();
_dictStack.pop();
if ( !_dictStack.empty())
{
_curDict = _dictStack.top();
}
}
else if (sName == "array")
{
_stateStack.pop();
_arrayStack.pop();
if (! _arrayStack.empty())
{
_curArray = _arrayStack.top();
}
}
else if (sName == "true")
{
if (SAX_ARRAY == curState)
{
_curArray->push_back(Value(true));
}
else if (SAX_DICT == curState)
{
(*_curDict)[_curKey] = Value(true);
}
}
else if (sName == "false")
{
if (SAX_ARRAY == curState)
{
_curArray->push_back(Value(false));
}
else if (SAX_DICT == curState)
{
(*_curDict)[_curKey] = Value(false);
}
}
else if (sName == "string" || sName == "integer" || sName == "real")
{
if (SAX_ARRAY == curState)
{
if (sName == "string")
_curArray->push_back(Value(_curValue));
else if (sName == "integer")
_curArray->push_back(Value(atoi(_curValue.c_str())));
else
2014-07-14 20:45:24 +08:00
_curArray->push_back(Value(utils::atof(_curValue.c_str())));
}
else if (SAX_DICT == curState)
{
if (sName == "string")
(*_curDict)[_curKey] = Value(_curValue);
else if (sName == "integer")
(*_curDict)[_curKey] = Value(atoi(_curValue.c_str()));
else
2014-07-14 20:45:24 +08:00
(*_curDict)[_curKey] = Value(utils::atof(_curValue.c_str()));
}
_curValue.clear();
}
_state = SAX_NONE;
}
void textHandler(void *ctx, const char *ch, int len)
{
CC_UNUSED_PARAM(ctx);
if (_state == SAX_NONE)
{
return;
}
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
const std::string text = std::string((char*)ch,len);
switch(_state)
{
case SAX_KEY:
_curKey = text;
break;
case SAX_INT:
case SAX_REAL:
case SAX_STRING:
{
if (curState == SAX_DICT)
{
CCASSERT(!_curKey.empty(), "key not found : <integer/real>");
}
_curValue.append(text);
}
break;
default:
break;
}
}
};
ValueMap FileUtils::getValueMapFromFile(const std::string& filename)
{
const std::string fullPath = fullPathForFilename(filename.c_str());
DictMaker tMaker;
return tMaker.dictionaryWithContentsOfFile(fullPath.c_str());
}
ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize)
{
2014-07-29 17:22:13 +08:00
DictMaker tMaker;
return tMaker.dictionaryWithDataOfFile(filedata, filesize);
}
ValueVector FileUtils::getValueVectorFromFile(const std::string& filename)
{
const std::string fullPath = fullPathForFilename(filename.c_str());
DictMaker tMaker;
return tMaker.arrayWithContentsOfFile(fullPath.c_str());
}
/*
* forward statement
*/
2013-12-20 10:11:41 +08:00
static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *doc);
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc);
/*
* Use tinyxml2 to write plist files
*/
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath)
{
//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;
}
2013-12-18 22:44:03 +08:00
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");
2013-12-18 22:44:03 +08:00
rootEle->SetAttribute("version", "1.0");
if (nullptr == rootEle)
{
delete doc;
return false;
}
2013-12-18 22:44:03 +08:00
doc->LinkEndChild(rootEle);
tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc);
if (nullptr == innerDict )
{
delete doc;
return false;
}
2013-12-18 22:44:03 +08:00
rootEle->LinkEndChild(innerDict);
2013-12-18 22:44:03 +08:00
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(fullPath.c_str());
delete doc;
return ret;
}
/*
* Generate tinyxml2::XMLElement for Object through a tinyxml2::XMLDocument
*/
2013-12-20 10:11:41 +08:00
static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxml2::XMLDocument *doc)
{
// object is String
if (value.getType() == Value::Type::STRING)
{
tinyxml2::XMLElement* node = doc->NewElement("string");
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
node->LinkEndChild(content);
return node;
}
// object is integer
if (value.getType() == Value::Type::INTEGER)
{
tinyxml2::XMLElement* node = doc->NewElement("integer");
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
node->LinkEndChild(content);
return node;
}
// object is real
if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE)
{
tinyxml2::XMLElement* node = doc->NewElement("real");
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
node->LinkEndChild(content);
return node;
}
2014-03-19 16:42:14 +08:00
//object is bool
if (value.getType() == Value::Type::BOOLEAN) {
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;
}
/*
* Generate tinyxml2::XMLElement for Dictionary through a tinyxml2::XMLDocument
*/
2013-12-20 10:11:41 +08:00
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc)
{
2013-12-18 22:44:03 +08:00
tinyxml2::XMLElement* rootNode = doc->NewElement("dict");
for (auto iter = dict.cbegin(); iter != dict.cend(); ++iter)
{
2013-12-18 22:44:03 +08:00
tinyxml2::XMLElement* tmpNode = doc->NewElement("key");
rootNode->LinkEndChild(tmpNode);
tinyxml2::XMLText* content = doc->NewText(iter->first.c_str());
tmpNode->LinkEndChild(content);
2013-12-18 22:44:03 +08:00
tinyxml2::XMLElement *element = generateElementForObject(iter->second, doc);
if (element)
rootNode->LinkEndChild(element);
}
return rootNode;
}
/*
* Generate tinyxml2::XMLElement for Array through a tinyxml2::XMLDocument
*/
2013-12-20 10:11:41 +08:00
static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *pDoc)
{
tinyxml2::XMLElement* rootNode = pDoc->NewElement("array");
for(const auto &value : array) {
tinyxml2::XMLElement *element = generateElementForObject(value, pDoc);
if (element)
rootNode->LinkEndChild(element);
}
return rootNode;
}
#else
NS_CC_BEGIN
/* The subclass FileUtilsApple should override these two method. */
ValueMap FileUtils::getValueMapFromFile(const std::string& filename) {return ValueMap();}
ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize) {return ValueMap();}
ValueVector FileUtils::getValueVectorFromFile(const std::string& filename) {return ValueVector();}
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath) {return false;}
#endif /* (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) */
FileUtils* FileUtils::s_sharedFileUtils = nullptr;
void FileUtils::destroyInstance()
{
CC_SAFE_DELETE(s_sharedFileUtils);
}
FileUtils::FileUtils()
{
}
FileUtils::~FileUtils()
{
}
bool FileUtils::init()
{
_searchPathArray.push_back(_defaultResRootPath);
_searchResolutionsOrderArray.push_back("");
return true;
}
void FileUtils::purgeCachedEntries()
{
_fullPathCache.clear();
}
static Data getData(const std::string& filename, bool forString)
{
2014-04-10 11:07:00 +08:00
if (filename.empty())
{
return Data::Null;
}
Data ret;
unsigned char* buffer = nullptr;
2014-10-28 01:57:57 +08:00
size_t size = 0;
size_t readsize;
const char* mode = nullptr;
if (forString)
mode = "rt";
else
mode = "rb";
do
{
// Read the file from hardware
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
FILE *fp = fopen(fullPath.c_str(), mode);
CC_BREAK_IF(!fp);
fseek(fp,0,SEEK_END);
size = ftell(fp);
fseek(fp,0,SEEK_SET);
if (forString)
{
buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));
buffer[size] = '\0';
}
else
{
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);
2014-05-31 06:13:08 +08:00
if (nullptr == buffer || 0 == readsize)
{
std::string msg = "Get data from file(";
msg.append(filename).append(") failed!");
CCLOG("%s", msg.c_str());
}
else
{
2014-05-31 06:13:08 +08:00
ret.fastSet(buffer, readsize);
}
return ret;
}
std::string FileUtils::getStringFromFile(const std::string& filename)
{
Data data = getData(filename, true);
if (data.isNull())
return "";
2014-04-10 11:07:00 +08:00
std::string ret((const char*)data.getBytes());
return ret;
}
Data FileUtils::getDataFromFile(const std::string& filename)
{
return getData(filename, false);
}
unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
{
unsigned char * buffer = nullptr;
CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
*size = 0;
do
{
// read the file from hardware
const std::string fullPath = fullPathForFilename(filename);
FILE *fp = fopen(fullPath.c_str(), mode);
CC_BREAK_IF(!fp);
fseek(fp,0,SEEK_END);
*size = ftell(fp);
fseek(fp,0,SEEK_SET);
buffer = (unsigned char*)malloc(*size);
*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!");
2013-05-07 14:36:01 +08:00
CCLOG("%s", msg.c_str());
}
return buffer;
}
unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size)
{
unsigned char * buffer = nullptr;
unzFile file = nullptr;
*size = 0;
do
{
CC_BREAK_IF(zipFilePath.empty());
file = unzOpen(zipFilePath.c_str());
CC_BREAK_IF(!file);
2014-10-11 11:39:42 +08:00
// FIXME: Other platforms should use upstream minizip like mingw-w64
#ifdef MINIZIP_FROM_SYSTEM
2014-10-11 11:39:42 +08:00
int ret = unzLocateFile(file, filename.c_str(), NULL);
#else
int ret = unzLocateFile(file, filename.c_str(), 1);
2014-10-11 11:39:42 +08:00
#endif
CC_BREAK_IF(UNZ_OK != ret);
char filePathA[260];
unz_file_info fileInfo;
ret = unzGetCurrentFileInfo(file, &fileInfo, filePathA, sizeof(filePathA), nullptr, 0, nullptr, 0);
CC_BREAK_IF(UNZ_OK != ret);
ret = unzOpenCurrentFile(file);
CC_BREAK_IF(UNZ_OK != ret);
buffer = (unsigned char*)malloc(fileInfo.uncompressed_size);
int CC_UNUSED readedSize = unzReadCurrentFile(file, buffer, static_cast<unsigned>(fileInfo.uncompressed_size));
CCASSERT(readedSize == 0 || readedSize == (int)fileInfo.uncompressed_size, "the file size is wrong");
*size = fileInfo.uncompressed_size;
unzCloseCurrentFile(file);
} while (0);
if (file)
{
unzClose(file);
}
return buffer;
}
std::string FileUtils::getNewFilename(const std::string &filename) const
{
std::string newFileName;
// in Lookup Filename dictionary ?
auto iter = _filenameLookupDict.find(filename);
if (iter == _filenameLookupDict.end())
{
newFileName = filename;
}
else
{
newFileName = iter->second.asString();
}
return newFileName;
}
std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath)
{
std::string file = filename;
std::string file_path = "";
size_t pos = filename.find_last_of("/");
if (pos != std::string::npos)
{
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;
}
std::string FileUtils::fullPathForFilename(const std::string &filename)
{
if (filename.empty())
{
return "";
}
if (isAbsolutePath(filename))
{
return filename;
}
// Already Cached ?
auto cacheIter = _fullPathCache.find(filename);
if( cacheIter != _fullPathCache.end() )
{
return cacheIter->second;
}
// Get the new file name.
const std::string newFilename( getNewFilename(filename) );
std::string fullpath;
for (auto searchIt = _searchPathArray.cbegin(); searchIt != _searchPathArray.cend(); ++searchIt)
{
for (auto resolutionIt = _searchResolutionsOrderArray.cbegin(); resolutionIt != _searchResolutionsOrderArray.cend(); ++resolutionIt)
{
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());
}
// FIXME: Should it return nullptr ? or an empty string ?
// The file wasn't found, return the file name passed in.
return filename;
}
std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile)
{
return relativeFile.substr(0, relativeFile.rfind('/')+1) + getNewFilename(filename);
}
void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
{
bool existDefault = false;
2013-07-10 04:21:43 +08:00
_fullPathCache.clear();
_searchResolutionsOrderArray.clear();
for(auto iter = searchResolutionsOrder.cbegin(); iter != searchResolutionsOrder.cend(); ++iter)
{
std::string resolutionDirectory = *iter;
if (!existDefault && resolutionDirectory == "")
{
existDefault = true;
}
if (resolutionDirectory.length() > 0 && resolutionDirectory[resolutionDirectory.length()-1] != '/')
{
resolutionDirectory += "/";
}
_searchResolutionsOrderArray.push_back(resolutionDirectory);
}
if (!existDefault)
{
_searchResolutionsOrderArray.push_back("");
}
}
void FileUtils::addSearchResolutionsOrder(const std::string &order,const bool front)
2013-02-04 12:41:24 +08:00
{
std::string resOrder = order;
if (!resOrder.empty() && resOrder[resOrder.length()-1] != '/')
resOrder.append("/");
if (front) {
_searchResolutionsOrderArray.insert(_searchResolutionsOrderArray.begin(), resOrder);
} else {
_searchResolutionsOrderArray.push_back(resOrder);
}
2013-02-04 12:41:24 +08:00
}
const std::vector<std::string>& FileUtils::getSearchResolutionsOrder()
{
return _searchResolutionsOrderArray;
}
const std::vector<std::string>& FileUtils::getSearchPaths() const
{
return _searchPathArray;
}
void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
{
bool existDefaultRootPath = false;
2013-07-10 04:21:43 +08:00
_fullPathCache.clear();
_searchPathArray.clear();
for (auto iter = searchPaths.cbegin(); iter != searchPaths.cend(); ++iter)
{
std::string prefix;
std::string path;
if (!isAbsolutePath(*iter))
{ // Not an absolute path
prefix = _defaultResRootPath;
}
path = prefix + (*iter);
if (path.length() > 0 && path[path.length()-1] != '/')
{
path += "/";
}
if (!existDefaultRootPath && path == _defaultResRootPath)
{
existDefaultRootPath = true;
}
_searchPathArray.push_back(path);
}
if (!existDefaultRootPath)
{
//CCLOG("Default root path doesn't exist, adding it.");
_searchPathArray.push_back(_defaultResRootPath);
}
}
void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
2013-02-04 12:41:24 +08:00
{
std::string prefix;
if (!isAbsolutePath(searchpath))
prefix = _defaultResRootPath;
std::string path = prefix + searchpath;
2013-02-04 12:41:24 +08:00
if (path.length() > 0 && path[path.length()-1] != '/')
{
path += "/";
}
if (front) {
_searchPathArray.insert(_searchPathArray.begin(), path);
} else {
_searchPathArray.push_back(path);
}
2013-02-04 12:41:24 +08:00
}
void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
{
2013-07-10 04:21:43 +08:00
_fullPathCache.clear();
_filenameLookupDict = filenameLookupDict;
}
void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename)
{
const std::string fullPath = fullPathForFilename(filename);
if (fullPath.length() > 0)
{
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
if (!dict.empty())
{
ValueMap& metadata = dict["metadata"].asValueMap();
int version = metadata["version"].asInt();
if (version != 1)
{
2013-12-05 17:19:01 +08:00
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %d. Filename: %s", version, filename.c_str());
return;
}
setFilenameLookupDictionary( dict["filenames"].asValueMap());
}
}
}
std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename)
{
// 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 = "";
}
return ret;
}
std::string FileUtils::searchFullPathForFilename(const std::string& filename) const
{
if (isAbsolutePath(filename))
{
return filename;
}
std::string path = const_cast<FileUtils*>(this)->fullPathForFilename(filename);
if (0 == path.compare(filename))
{
return "";
}
else
{
return path;
}
}
bool FileUtils::isFileExist(const std::string& filename) const
{
if (isAbsolutePath(filename))
{
return isFileExistInternal(filename);
}
else
{
std::string fullpath = searchFullPathForFilename(filename);
if (fullpath.empty())
return false;
else
return true;
}
}
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_WP8) || (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))
2014-08-07 11:56:28 +08:00
{
return true;
}
return false;
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-08-07 11:56:28 +08:00
unsigned long fAttrib = GetFileAttributesA(dirPath.c_str());
if (fAttrib != INVALID_FILE_ATTRIBUTES &&
(fAttrib & FILE_ATTRIBUTE_DIRECTORY))
{
return true;
}
return false;
#else
struct stat st;
if (stat(dirPath.c_str(), &st) == 0)
{
return S_ISDIR(st.st_mode);
}
return false;
#endif
}
bool FileUtils::isDirectoryExist(const std::string& dirPath)
{
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;
for (auto searchIt = _searchPathArray.cbegin(); searchIt != _searchPathArray.cend(); ++searchIt)
{
for (auto resolutionIt = _searchResolutionsOrderArray.cbegin(); resolutionIt != _searchResolutionsOrderArray.cend(); ++resolutionIt)
{
// searchPath + file_path + resourceDirectory
fullpath = *searchIt + dirPath + *resolutionIt;
if (isDirectoryExistInternal(fullpath))
{
const_cast<FileUtils*>(this)->_fullPathCache.insert(std::make_pair(dirPath, fullpath));
return true;
}
}
}
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)
{
subpath = path.substr(start, found - start + 1);
if (!subpath.empty())
dirs.push_back(subpath);
start = found+1;
found = path.find_first_of("/\\", start);
if (found == std::string::npos)
{
if (start < path.length())
{
dirs.push_back(path.substr(start));
}
break;
}
}
}
2014-08-08 10:47:14 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
2014-08-07 14:09:22 +08:00
WIN32_FILE_ATTRIBUTE_DATA wfad;
std::wstring wpath(path.begin(), path.end());
if (!(GetFileAttributesEx(wpath.c_str(), GetFileExInfoStandard, &wfad)))
2014-08-07 11:56:28 +08:00
{
subpath = "";
2014-10-28 01:57:57 +08:00
for(unsigned int i = 0 ; i < dirs.size() ; ++i)
2014-08-07 11:56:28 +08:00
{
subpath += dirs[i];
2014-09-19 15:54:18 +08:00
if (i > 0 && !isDirectoryExist(subpath))
2014-08-07 11:56:28 +08:00
{
std::wstring wsubpath(subpath.begin(), subpath.end());
BOOL ret = CreateDirectory(wsubpath.c_str(), NULL);
2014-09-19 15:54:18 +08:00
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
2014-08-07 11:56:28 +08:00
}
}
}
return true;
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
if ((GetFileAttributesA(path.c_str())) == INVALID_FILE_ATTRIBUTES)
{
subpath = "";
2014-09-19 15:54:18 +08:00
for (int i = 0; i < dirs.size(); ++i)
{
subpath += dirs[i];
2014-09-19 15:54:18 +08:00
if (!isDirectoryExist(subpath))
{
BOOL ret = CreateDirectoryA(subpath.c_str(), NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
}
}
}
return true;
#else
DIR *dir = NULL;
// Create path recursively
subpath = "";
for (int i = 0; i < dirs.size(); ++i) {
subpath += dirs[i];
dir = opendir(subpath.c_str());
if (!dir)
{
int ret = mkdir(subpath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
if (ret != 0 && (errno != EEXIST))
{
return false;
}
}
}
return true;
#endif
}
2014-10-10 14:03:20 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
2014-10-10 15:13:05 +08:00
static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
auto ret = remove(fpath);
if (ret) {
log("Fail to remove:%s ",fpath);
}
return ret;
}
2014-10-10 14:03:20 +08:00
#endif
bool FileUtils::removeDirectory(const std::string& path)
{
if (path.size() > 0 && path[path.size() - 1] != '/')
{
2014-06-24 14:51:01 +08:00
CCLOGERROR("Fail to remove directory, path must termniate with '/': %s", path.c_str());
return false;
}
// Remove downloaded files
2014-08-08 10:47:14 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
std::wstring wpath = std::wstring(path.begin(), path.end());
std::wstring files = wpath + L"*.*";
2014-09-19 16:52:11 +08:00
WIN32_FIND_DATA wfd;
HANDLE search = FindFirstFileEx(files.c_str(), FindExInfoStandard, &wfd, FindExSearchNameMatch, NULL, 0);
2014-09-19 16:52:11 +08:00
bool ret=true;
if (search!=INVALID_HANDLE_VALUE)
2014-09-19 15:54:18 +08:00
{
2014-10-28 01:57:57 +08:00
BOOL find=true;
2014-09-19 16:52:11 +08:00
while (find)
2014-09-19 15:54:18 +08:00
{
//. ..
if(wfd.cFileName[0]!='.')
{
std::wstring temp = wpath + wfd.cFileName;
2014-09-19 15:54:18 +08:00
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
temp += '/';
ret = ret && this->removeDirectory(std::string(temp.begin(), temp.end()));
2014-09-19 15:54:18 +08:00
}
else
{
SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
ret = ret && DeleteFile(temp.c_str());
2014-09-19 15:54:18 +08:00
}
}
find = FindNextFile(search, &wfd);
2014-09-19 15:54:18 +08:00
}
FindClose(search);
2014-08-07 11:56:28 +08:00
}
2014-10-28 01:57:57 +08:00
if (ret && RemoveDirectory(wpath.c_str()))
{
return true;
}
2014-08-07 11:56:28 +08:00
return false;
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-09-19 15:54:18 +08:00
std::string command = "cmd /c rd /s /q ";
// Path may include space.
command += "\"" + path + "\"";
if (WinExec(command.c_str(), SW_HIDE) > 31)
2014-09-19 15:54:18 +08:00
return true;
else
return false;
2014-10-10 14:03:20 +08:00
#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;
2014-10-10 14:03:20 +08:00
#else
std::string command = "rm -r ";
// Path may include space.
command += "\"" + path + "\"";
if (system(command.c_str()) >= 0)
return true;
else
return false;
2014-08-08 10:47:14 +08:00
#endif
}
bool FileUtils::removeFile(const std::string &path)
{
// Remove downloaded file
2014-08-08 10:47:14 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
std::wstring wpath(path.begin(), path.end());
if (DeleteFile(wpath.c_str()))
2014-08-07 11:56:28 +08:00
{
return true;
}
return false;
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-09-19 15:54:18 +08:00
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)
2014-09-19 15:54:18 +08:00
return true;
else
return false;
#else
if (remove(path.c_str())) {
return false;
} else {
return true;
}
#endif
}
bool FileUtils::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;
// Rename a file
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
int errorCode = rename(oldPath.c_str(), newPath.c_str());
if (0 != errorCode)
{
CCLOGERROR("Fail to rename file %s to %s !Error code is %d", oldPath.c_str(), newPath.c_str(), errorCode);
return false;
}
return true;
#else
std::regex pat("\/");
std::string _old = std::regex_replace(oldPath, pat, "\\");
std::string _new = std::regex_replace(newPath, pat, "\\");
if(FileUtils::getInstance()->isFileExist(_new))
{
DeleteFileA(_new.c_str());
}
MoveFileA(_old.c_str(), _new.c_str());
if(0 == GetLastError())
return true;
else
return false;
#endif
}
long FileUtils::getFileSize(const std::string &filepath)
{
CCASSERT(!filepath.empty(), "Invalid path");
std::string fullpath = filepath;
if (!isAbsolutePath(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 )
{
// Failed
return -1;
}
else
{
return (long)(info.st_size);
}
}
//////////////////////////////////////////////////////////////////////////
// Notification support when getFileData from invalid file path.
//////////////////////////////////////////////////////////////////////////
static bool s_popupNotify = true;
void FileUtils::setPopupNotify(bool notify)
{
s_popupNotify = notify;
}
bool FileUtils::isPopupNotify()
{
return s_popupNotify;
}
NS_CC_END