2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2013-02-01 11:20:46 +08:00
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCFileUtils.h"
|
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"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCSAXParser.h"
|
2014-07-14 20:45:24 +08:00
|
|
|
#include "base/ccUtils.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "tinyxml2.h"
|
2014-11-17 03:41:37 +08:00
|
|
|
#ifdef MINIZIP_FROM_SYSTEM
|
|
|
|
#include <minizip/unzip.h>
|
|
|
|
#else // from our embedded sources
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "unzip.h"
|
2014-11-17 03:41:37 +08:00
|
|
|
#endif
|
2014-06-20 18:01:34 +08:00
|
|
|
#include <sys/stat.h>
|
2014-10-10 14:03:20 +08:00
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-10-15 14:32:11 +08:00
|
|
|
#include <regex>
|
|
|
|
#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 11:59:35 +08:00
|
|
|
#include <ftw.h>
|
2014-10-10 14:03:20 +08:00
|
|
|
#endif
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-06-20 18:01:34 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SAX_NONE = 0,
|
|
|
|
SAX_KEY,
|
|
|
|
SAX_DICT,
|
|
|
|
SAX_INT,
|
|
|
|
SAX_REAL,
|
|
|
|
SAX_STRING,
|
|
|
|
SAX_ARRAY
|
2013-06-20 14:13:12 +08:00
|
|
|
}SAXState;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SAX_RESULT_NONE = 0,
|
|
|
|
SAX_RESULT_DICT,
|
|
|
|
SAX_RESULT_ARRAY
|
2013-06-20 14:13:12 +08:00
|
|
|
}SAXResult;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class DictMaker : public SAXDelegator
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXResult _resultType;
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap _rootDict;
|
|
|
|
ValueVector _rootArray;
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _curKey; ///< parsed key
|
|
|
|
std::string _curValue; // parsed value
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState _state;
|
2013-06-15 14:03:30 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap* _curDict;
|
|
|
|
ValueVector* _curArray;
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
std::stack<ValueMap*> _dictStack;
|
|
|
|
std::stack<ValueVector*> _arrayStack;
|
2013-06-20 14:13:12 +08:00
|
|
|
std::stack<SAXState> _stateStack;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker()
|
2013-12-04 10:33:27 +08:00
|
|
|
: _resultType(SAX_RESULT_NONE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
~DictMaker()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
ValueMap dictionaryWithContentsOfFile(const std::string& fileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resultType = SAX_RESULT_DICT;
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXParser parser;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
|
2012-04-19 14:35:52 +08:00
|
|
|
parser.setDelegator(this);
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
parser.parse(fileName);
|
2013-12-10 16:06:05 +08:00
|
|
|
return _rootDict;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 17:06:43 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
ValueVector arrayWithContentsOfFile(const std::string& fileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_resultType = SAX_RESULT_ARRAY;
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXParser parser;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
|
2012-04-19 14:35:52 +08:00
|
|
|
parser.setDelegator(this);
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
parser.parse(fileName);
|
2013-12-10 16:06:05 +08:00
|
|
|
return _rootArray;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void startElement(void *ctx, const char *name, const char **atts)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
|
|
|
CC_UNUSED_PARAM(atts);
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string sName(name);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( sName == "dict" )
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curDict = &_rootDict;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_DICT;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState preState = SAX_NONE;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _stateStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
preState = _stateStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SAX_ARRAY == preState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
// add a new dictionary into the array
|
2013-12-04 17:46:57 +08:00
|
|
|
_curArray->push_back(Value(ValueMap()));
|
|
|
|
_curDict = &(_curArray->rbegin())->asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == preState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
// add a new dictionary into the pre dictionary
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! _dictStack.empty(), "The state is wrong!");
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap* preDict = _dictStack.top();
|
|
|
|
(*preDict)[_curKey] = Value(ValueMap());
|
|
|
|
_curDict = &(*preDict)[_curKey].asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// record the dict state
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.push(_state);
|
|
|
|
_dictStack.push(_curDict);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "key")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_KEY;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "integer")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_INT;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "real")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_REAL;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if(sName == "string")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_STRING;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (sName == "array")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_ARRAY;
|
2013-12-04 10:33:27 +08:00
|
|
|
|
2013-12-04 15:34:05 +08:00
|
|
|
if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray = &_rootArray;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState preState = SAX_NONE;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _stateStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
preState = _stateStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preState == SAX_DICT)
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
(*_curDict)[_curKey] = Value(ValueVector());
|
|
|
|
_curArray = &(*_curDict)[_curKey].asValueVector();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (preState == SAX_ARRAY)
|
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! _arrayStack.empty(), "The state is wrong!");
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector* preArray = _arrayStack.top();
|
|
|
|
preArray->push_back(Value(ValueVector()));
|
|
|
|
_curArray = &(_curArray->rbegin())->asValueVector();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// record the array state
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.push(_state);
|
2013-12-04 15:34:05 +08:00
|
|
|
_arrayStack.push(_curArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_NONE;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void endElement(void *ctx, const char *name)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string sName((char*)name);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( sName == "dict" )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.pop();
|
|
|
|
_dictStack.pop();
|
|
|
|
if ( !_dictStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_curDict = _dictStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "array")
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stateStack.pop();
|
|
|
|
_arrayStack.pop();
|
|
|
|
if (! _arrayStack.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray = _arrayStack.top();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "true")
|
|
|
|
{
|
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(true));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sName == "false")
|
|
|
|
{
|
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(false));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-25 10:55:59 +08:00
|
|
|
else if (sName == "string" || sName == "integer" || sName == "real")
|
|
|
|
{
|
|
|
|
if (SAX_ARRAY == curState)
|
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
if (sName == "string")
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(_curValue));
|
2013-12-04 10:33:27 +08:00
|
|
|
else if (sName == "integer")
|
2013-12-04 15:34:05 +08:00
|
|
|
_curArray->push_back(Value(atoi(_curValue.c_str())));
|
2013-12-04 10:33:27 +08:00
|
|
|
else
|
2014-07-14 20:45:24 +08:00
|
|
|
_curArray->push_back(Value(utils::atof(_curValue.c_str())));
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
else if (SAX_DICT == curState)
|
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
if (sName == "string")
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(_curValue);
|
2013-12-04 10:33:27 +08:00
|
|
|
else if (sName == "integer")
|
2013-12-04 15:34:05 +08:00
|
|
|
(*_curDict)[_curKey] = Value(atoi(_curValue.c_str()));
|
2013-12-04 10:33:27 +08:00
|
|
|
else
|
2014-07-14 20:45:24 +08:00
|
|
|
(*_curDict)[_curKey] = Value(utils::atof(_curValue.c_str()));
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_curValue.clear();
|
2012-04-25 10:55:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = SAX_NONE;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void textHandler(void *ctx, const char *ch, int len)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_state == SAX_NONE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
|
2014-11-12 07:06:30 +08:00
|
|
|
const std::string text = std::string((char*)ch,len);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
switch(_state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
case SAX_KEY:
|
2013-12-04 10:33:27 +08:00
|
|
|
_curKey = text;
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
case SAX_INT:
|
|
|
|
case SAX_REAL:
|
|
|
|
case SAX_STRING:
|
|
|
|
{
|
2012-04-25 10:55:59 +08:00
|
|
|
if (curState == SAX_DICT)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(!_curKey.empty(), "key not found : <integer/real>");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-25 10:55:59 +08:00
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
_curValue.append(text);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-25 10:55:59 +08:00
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap FileUtils::getValueMapFromFile(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename.c_str());
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2013-12-10 16:06:05 +08:00
|
|
|
return tMaker.dictionaryWithContentsOfFile(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 17:06:43 +08:00
|
|
|
ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize)
|
|
|
|
{
|
2014-07-29 17:22:13 +08:00
|
|
|
DictMaker tMaker;
|
|
|
|
return tMaker.dictionaryWithDataOfFile(filedata, filesize);
|
2014-07-29 17:06:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueVector FileUtils::getValueVectorFromFile(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename.c_str());
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2013-12-10 16:06:05 +08:00
|
|
|
return tMaker.arrayWithContentsOfFile(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
/*
|
|
|
|
* Use tinyxml2 to write plist files
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
//CCLOG("tinyxml2 Dictionary %d writeToFile %s", dict->_ID, fullPath.c_str());
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
|
|
|
|
if (nullptr == doc)
|
2013-04-13 16:55:24 +08:00
|
|
|
return false;
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
|
|
|
if (nullptr == declaration)
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
2013-04-16 12:34:09 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 22:44:03 +08:00
|
|
|
doc->LinkEndChild(declaration);
|
2013-12-18 17:47:20 +08:00
|
|
|
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);
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
|
2013-12-18 22:44:03 +08:00
|
|
|
rootEle->SetAttribute("version", "1.0");
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr == rootEle)
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
2013-04-16 12:34:09 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-12-18 22:44:03 +08:00
|
|
|
doc->LinkEndChild(rootEle);
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc);
|
2013-12-04 10:33:27 +08:00
|
|
|
if (nullptr == innerDict )
|
2013-04-28 00:36:37 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
2013-04-13 16:55:24 +08:00
|
|
|
return false;
|
2013-04-28 00:36:37 +08:00
|
|
|
}
|
2013-12-18 22:44:03 +08:00
|
|
|
rootEle->LinkEndChild(innerDict);
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2015-04-19 19:00:27 +08:00
|
|
|
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
delete doc;
|
|
|
|
return ret;
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* Generate tinyxml2::XMLElement for Object through a tinyxml2::XMLDocument
|
2013-04-16 12:34:09 +08:00
|
|
|
*/
|
2013-12-20 10:11:41 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxml2::XMLDocument *doc)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is String
|
2013-12-04 10:33:27 +08:00
|
|
|
if (value.getType() == Value::Type::STRING)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* node = doc->NewElement("string");
|
|
|
|
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
|
2013-04-28 00:36:37 +08:00
|
|
|
node->LinkEndChild(content);
|
|
|
|
return node;
|
2013-04-16 12:34:09 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
// object is integer
|
|
|
|
if (value.getType() == Value::Type::INTEGER)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* node = doc->NewElement("integer");
|
|
|
|
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
|
2013-12-04 10:33:27 +08:00
|
|
|
node->LinkEndChild(content);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
// object is real
|
|
|
|
if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement* node = doc->NewElement("real");
|
|
|
|
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
|
2013-12-04 10:33:27 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-12-04 10:33:27 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is Array
|
2013-12-05 11:44:37 +08:00
|
|
|
if (value.getType() == Value::Type::VECTOR)
|
2013-12-18 17:47:20 +08:00
|
|
|
return generateElementForArray(value.asValueVector(), doc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// object is Dictionary
|
2013-12-05 11:44:37 +08:00
|
|
|
if (value.getType() == Value::Type::MAP)
|
2013-12-18 17:47:20 +08:00
|
|
|
return generateElementForDict(value.asValueMap(), doc);
|
2013-04-16 12:34:09 +08:00
|
|
|
|
2013-04-28 00:36:37 +08:00
|
|
|
CCLOG("This type cannot appear in property list");
|
2013-12-04 10:33:27 +08:00
|
|
|
return nullptr;
|
2013-04-16 12:34:09 +08:00
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* Generate tinyxml2::XMLElement for Dictionary through a tinyxml2::XMLDocument
|
2013-04-13 16:55:24 +08:00
|
|
|
*/
|
2013-12-20 10:11:41 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-18 22:44:03 +08:00
|
|
|
tinyxml2::XMLElement* rootNode = doc->NewElement("dict");
|
2013-04-13 16:55:24 +08:00
|
|
|
|
2014-11-22 06:52:13 +08:00
|
|
|
for (const auto &iter : dict)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-18 22:44:03 +08:00
|
|
|
tinyxml2::XMLElement* tmpNode = doc->NewElement("key");
|
2013-04-13 16:55:24 +08:00
|
|
|
rootNode->LinkEndChild(tmpNode);
|
2014-11-22 06:52:13 +08:00
|
|
|
tinyxml2::XMLText* content = doc->NewText(iter.first.c_str());
|
2013-04-13 16:55:24 +08:00
|
|
|
tmpNode->LinkEndChild(content);
|
|
|
|
|
2014-11-22 06:52:13 +08:00
|
|
|
tinyxml2::XMLElement *element = generateElementForObject(iter.second, doc);
|
2013-04-16 12:34:09 +08:00
|
|
|
if (element)
|
|
|
|
rootNode->LinkEndChild(element);
|
|
|
|
}
|
|
|
|
return rootNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* Generate tinyxml2::XMLElement for Array through a tinyxml2::XMLDocument
|
2013-04-16 12:34:09 +08:00
|
|
|
*/
|
2013-12-20 10:11:41 +08:00
|
|
|
static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *pDoc)
|
2013-04-16 12:34:09 +08:00
|
|
|
{
|
|
|
|
tinyxml2::XMLElement* rootNode = pDoc->NewElement("array");
|
2013-12-20 05:34:41 +08:00
|
|
|
|
|
|
|
for(const auto &value : array) {
|
2013-12-04 10:33:27 +08:00
|
|
|
tinyxml2::XMLElement *element = generateElementForObject(value, pDoc);
|
2013-04-16 12:34:09 +08:00
|
|
|
if (element)
|
|
|
|
rootNode->LinkEndChild(element);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-04-13 16:55:24 +08:00
|
|
|
return rootNode;
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:34:09 +08:00
|
|
|
#else
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
/* The subclass FileUtilsApple should override these two method. */
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap FileUtils::getValueMapFromFile(const std::string& filename) {return ValueMap();}
|
2014-07-29 17:06:43 +08:00
|
|
|
ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize) {return ValueMap();}
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueVector FileUtils::getValueVectorFromFile(const std::string& filename) {return ValueVector();}
|
2013-12-04 17:46:57 +08:00
|
|
|
bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath) {return false;}
|
2013-04-16 12:34:09 +08:00
|
|
|
|
|
|
|
#endif /* (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) */
|
|
|
|
|
2013-12-04 10:33:27 +08:00
|
|
|
FileUtils* FileUtils::s_sharedFileUtils = nullptr;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
void FileUtils::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(s_sharedFileUtils);
|
|
|
|
}
|
|
|
|
|
2014-12-26 14:25:55 +08:00
|
|
|
void FileUtils::setDelegate(FileUtils *delegate)
|
2014-12-26 11:34:31 +08:00
|
|
|
{
|
2014-12-26 14:25:55 +08:00
|
|
|
if (s_sharedFileUtils)
|
|
|
|
delete s_sharedFileUtils;
|
|
|
|
|
2014-12-26 11:34:31 +08:00
|
|
|
s_sharedFileUtils = delegate;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtils::FileUtils()
|
2014-12-25 20:33:47 +08:00
|
|
|
: _writablePath("")
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtils::~FileUtils()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-26 11:34:31 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtils::init()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(_defaultResRootPath);
|
|
|
|
_searchResolutionsOrderArray.push_back("");
|
2013-02-01 15:41:41 +08:00
|
|
|
return true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::purgeCachedEntries()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_fullPathCache.clear();
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
static Data getData(const std::string& filename, bool forString)
|
|
|
|
{
|
2014-04-10 11:07:00 +08:00
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
return Data::Null;
|
|
|
|
}
|
2013-12-18 14:58:17 +08:00
|
|
|
|
|
|
|
Data ret;
|
|
|
|
unsigned char* buffer = nullptr;
|
2014-10-28 01:57:57 +08:00
|
|
|
size_t size = 0;
|
2014-05-31 06:06:56 +08:00
|
|
|
size_t readsize;
|
2013-12-18 14:58:17 +08:00
|
|
|
const char* mode = nullptr;
|
2014-11-22 03:12:55 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
if (forString)
|
|
|
|
mode = "rt";
|
|
|
|
else
|
|
|
|
mode = "rb";
|
2015-05-15 08:24:27 +08:00
|
|
|
|
|
|
|
auto fileutils = FileUtils::getInstance();
|
2013-12-18 14:58:17 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// Read the file from hardware
|
2015-05-15 08:24:27 +08:00
|
|
|
std::string fullPath = fileutils->fullPathForFilename(filename);
|
|
|
|
FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode);
|
2013-12-18 14:58:17 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:06:56 +08:00
|
|
|
readsize = fread(buffer, sizeof(unsigned char), size, fp);
|
2013-12-18 14:58:17 +08:00
|
|
|
fclose(fp);
|
2014-05-31 06:06:56 +08:00
|
|
|
|
|
|
|
if (forString && readsize < size)
|
|
|
|
{
|
|
|
|
buffer[readsize] = '\0';
|
|
|
|
}
|
2013-12-18 14:58:17 +08:00
|
|
|
} while (0);
|
|
|
|
|
2014-05-31 06:13:08 +08:00
|
|
|
if (nullptr == buffer || 0 == readsize)
|
2013-12-18 14:58:17 +08:00
|
|
|
{
|
2015-05-15 08:24:27 +08:00
|
|
|
CCLOG("Get data from file %s failed", filename.c_str());
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-31 06:13:08 +08:00
|
|
|
ret.fastSet(buffer, readsize);
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtils::getStringFromFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
Data data = getData(filename, true);
|
2014-01-16 10:09:04 +08:00
|
|
|
if (data.isNull())
|
2014-01-15 12:39:56 +08:00
|
|
|
return "";
|
2014-04-10 11:07:00 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
std::string ret((const char*)data.getBytes());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data FileUtils::getDataFromFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
return getData(filename, false);
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
unsigned char * buffer = nullptr;
|
2013-12-24 10:51:47 +08:00
|
|
|
CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = 0;
|
2013-02-01 11:20:46 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// read the file from hardware
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
2015-04-19 19:00:27 +08:00
|
|
|
FILE *fp = fopen(getSuitableFOpen(fullPath).c_str(), mode);
|
2013-02-01 11:20:46 +08:00
|
|
|
CC_BREAK_IF(!fp);
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = ftell(fp);
|
2013-02-01 11:20:46 +08:00
|
|
|
fseek(fp,0,SEEK_SET);
|
2013-11-12 10:09:47 +08:00
|
|
|
buffer = (unsigned char*)malloc(*size);
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = fread(buffer,sizeof(unsigned char), *size,fp);
|
2013-02-01 11:20:46 +08:00
|
|
|
fclose(fp);
|
|
|
|
} while (0);
|
|
|
|
|
2014-11-22 03:12:55 +08:00
|
|
|
if (!buffer)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
std::string msg = "Get data from file(";
|
2013-07-26 06:53:24 +08:00
|
|
|
msg.append(filename).append(") failed!");
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-05-07 14:36:01 +08:00
|
|
|
CCLOG("%s", msg.c_str());
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
return buffer;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 10:33:27 +08:00
|
|
|
unsigned char * buffer = nullptr;
|
2013-12-18 17:47:20 +08:00
|
|
|
unzFile file = nullptr;
|
2013-09-07 09:46:33 +08:00
|
|
|
*size = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-12-24 10:51:47 +08:00
|
|
|
CC_BREAK_IF(zipFilePath.empty());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
file = unzOpen(zipFilePath.c_str());
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(!file);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-10-11 11:39:42 +08:00
|
|
|
// FIXME: Other platforms should use upstream minizip like mingw-w64
|
2014-11-17 03:41:37 +08:00
|
|
|
#ifdef MINIZIP_FROM_SYSTEM
|
2014-10-11 11:39:42 +08:00
|
|
|
int ret = unzLocateFile(file, filename.c_str(), NULL);
|
|
|
|
#else
|
2013-12-24 10:51:47 +08:00
|
|
|
int ret = unzLocateFile(file, filename.c_str(), 1);
|
2014-10-11 11:39:42 +08:00
|
|
|
#endif
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(UNZ_OK != ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
char filePathA[260];
|
|
|
|
unz_file_info fileInfo;
|
|
|
|
ret = unzGetCurrentFileInfo(file, &fileInfo, filePathA, sizeof(filePathA), nullptr, 0, nullptr, 0);
|
|
|
|
CC_BREAK_IF(UNZ_OK != ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = unzOpenCurrentFile(file);
|
|
|
|
CC_BREAK_IF(UNZ_OK != ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
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");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
*size = fileInfo.uncompressed_size;
|
|
|
|
unzCloseCurrentFile(file);
|
2012-04-19 14:35:52 +08:00
|
|
|
} while (0);
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (file)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
unzClose(file);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
return buffer;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 15:35:09 +08:00
|
|
|
std::string FileUtils::getNewFilename(const std::string &filename) const
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-09-07 13:54:08 +08:00
|
|
|
std::string newFileName;
|
2013-09-07 06:33:28 +08:00
|
|
|
|
2013-01-24 00:32:22 +08:00
|
|
|
// in Lookup Filename dictionary ?
|
2013-12-03 14:47:35 +08:00
|
|
|
auto iter = _filenameLookupDict.find(filename);
|
|
|
|
|
|
|
|
if (iter == _filenameLookupDict.end())
|
|
|
|
{
|
2013-09-07 06:33:28 +08:00
|
|
|
newFileName = filename;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
newFileName = iter->second.asString();
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
2013-09-07 06:33:28 +08:00
|
|
|
return newFileName;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2013-02-01 11:53:05 +08:00
|
|
|
//CCLOG("getPathForFilename, fullPath = %s", path.c_str());
|
2013-02-01 11:20:46 +08:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
std::string FileUtils::fullPathForFilename(const std::string &filename) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-04-28 14:53:30 +08:00
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
if (isAbsolutePath(filename))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
return filename;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2014-04-02 16:33:05 +08:00
|
|
|
|
|
|
|
// Already Cached ?
|
|
|
|
auto cacheIter = _fullPathCache.find(filename);
|
2014-11-22 03:12:55 +08:00
|
|
|
if(cacheIter != _fullPathCache.end())
|
2014-04-02 16:33:05 +08:00
|
|
|
{
|
|
|
|
return cacheIter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the new file name.
|
|
|
|
const std::string newFilename( getNewFilename(filename) );
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2014-01-08 05:02:31 +08:00
|
|
|
std::string fullpath;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2014-11-22 03:12:55 +08:00
|
|
|
for (const auto& searchIt : _searchPathArray)
|
2014-04-02 15:35:09 +08:00
|
|
|
{
|
2014-11-22 03:12:55 +08:00
|
|
|
for (const auto& resolutionIt : _searchResolutionsOrderArray)
|
2014-04-02 16:33:05 +08:00
|
|
|
{
|
2014-11-22 03:12:55 +08:00
|
|
|
fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt);
|
2014-04-02 16:33:05 +08:00
|
|
|
|
|
|
|
if (fullpath.length() > 0)
|
|
|
|
{
|
|
|
|
// Using the filename passed in as key.
|
|
|
|
_fullPathCache.insert(std::make_pair(filename, fullpath));
|
|
|
|
return fullpath;
|
|
|
|
}
|
2014-11-22 03:12:55 +08:00
|
|
|
|
2014-04-02 16:33:05 +08:00
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2014-09-15 14:06:04 +08:00
|
|
|
if(isPopupNotify()){
|
|
|
|
CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", filename.c_str());
|
|
|
|
}
|
2013-05-13 21:48:25 +08:00
|
|
|
|
2015-01-10 18:30:42 +08:00
|
|
|
// The file wasn't found, return empty string.
|
|
|
|
return "";
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
return relativeFile.substr(0, relativeFile.rfind('/')+1) + getNewFilename(filename);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
bool existDefault = false;
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.clear();
|
2014-11-22 03:12:55 +08:00
|
|
|
for(const auto& iter : searchResolutionsOrder)
|
2013-01-28 17:44:53 +08:00
|
|
|
{
|
2014-11-22 03:12:55 +08:00
|
|
|
std::string resolutionDirectory = iter;
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefault && resolutionDirectory == "")
|
2013-01-28 17:44:53 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
existDefault = true;
|
2013-01-28 17:44:53 +08:00
|
|
|
}
|
2013-02-01 18:48:44 +08:00
|
|
|
|
|
|
|
if (resolutionDirectory.length() > 0 && resolutionDirectory[resolutionDirectory.length()-1] != '/')
|
|
|
|
{
|
|
|
|
resolutionDirectory += "/";
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.push_back(resolutionDirectory);
|
2013-01-28 17:44:53 +08:00
|
|
|
}
|
2014-11-22 03:12:55 +08:00
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefault)
|
2013-01-28 17:44:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchResolutionsOrderArray.push_back("");
|
2013-01-28 17:44:53 +08:00
|
|
|
}
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-02 10:29:09 +08:00
|
|
|
void FileUtils::addSearchResolutionsOrder(const std::string &order,const bool front)
|
2013-02-04 12:41:24 +08:00
|
|
|
{
|
2014-03-19 17:56:29 +08:00
|
|
|
std::string resOrder = order;
|
|
|
|
if (!resOrder.empty() && resOrder[resOrder.length()-1] != '/')
|
|
|
|
resOrder.append("/");
|
2014-11-22 03:12:55 +08:00
|
|
|
|
2014-07-02 10:29:09 +08:00
|
|
|
if (front) {
|
|
|
|
_searchResolutionsOrderArray.insert(_searchResolutionsOrderArray.begin(), resOrder);
|
|
|
|
} else {
|
|
|
|
_searchResolutionsOrderArray.push_back(resOrder);
|
|
|
|
}
|
2013-02-04 12:41:24 +08:00
|
|
|
}
|
|
|
|
|
2014-11-22 03:12:55 +08:00
|
|
|
const std::vector<std::string>& FileUtils::getSearchResolutionsOrder() const
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _searchResolutionsOrderArray;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
const std::vector<std::string>& FileUtils::getSearchPaths() const
|
2013-01-24 00:32:22 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _searchPathArray;
|
2013-01-24 00:32:22 +08:00
|
|
|
}
|
|
|
|
|
2014-12-25 20:33:47 +08:00
|
|
|
void FileUtils::setWritablePath(const std::string& writablePath)
|
|
|
|
{
|
|
|
|
_writablePath = writablePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::setDefaultResourceRootPath(const std::string& path)
|
|
|
|
{
|
|
|
|
_defaultResRootPath = path;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
|
2012-08-09 12:49:33 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
bool existDefaultRootPath = false;
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.clear();
|
2014-11-22 06:52:13 +08:00
|
|
|
for (const auto& iter : searchPaths)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string prefix;
|
2013-02-01 11:20:46 +08:00
|
|
|
std::string path;
|
2013-09-07 09:46:33 +08:00
|
|
|
|
2014-11-22 03:12:55 +08:00
|
|
|
if (!isAbsolutePath(iter))
|
2013-02-01 11:20:46 +08:00
|
|
|
{ // Not an absolute path
|
2013-12-18 17:47:20 +08:00
|
|
|
prefix = _defaultResRootPath;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2014-11-22 03:12:55 +08:00
|
|
|
path = prefix + (iter);
|
2013-02-01 11:20:46 +08:00
|
|
|
if (path.length() > 0 && path[path.length()-1] != '/')
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefaultRootPath && path == _defaultResRootPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
existDefaultRootPath = true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(path);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
if (!existDefaultRootPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-02-01 11:53:05 +08:00
|
|
|
//CCLOG("Default root path doesn't exist, adding it.");
|
2013-06-15 14:03:30 +08:00
|
|
|
_searchPathArray.push_back(_defaultResRootPath);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2012-08-09 12:49:33 +08:00
|
|
|
}
|
|
|
|
|
2014-07-02 10:29:09 +08:00
|
|
|
void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
|
2013-02-04 12:41:24 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string prefix;
|
2013-09-07 13:54:08 +08:00
|
|
|
if (!isAbsolutePath(searchpath))
|
2013-12-18 17:47:20 +08:00
|
|
|
prefix = _defaultResRootPath;
|
2013-09-07 13:54:08 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string path = prefix + searchpath;
|
2013-02-04 12:41:24 +08:00
|
|
|
if (path.length() > 0 && path[path.length()-1] != '/')
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
2014-07-02 10:29:09 +08:00
|
|
|
if (front) {
|
|
|
|
_searchPathArray.insert(_searchPathArray.begin(), path);
|
|
|
|
} else {
|
|
|
|
_searchPathArray.push_back(path);
|
|
|
|
}
|
2013-02-04 12:41:24 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
|
2013-01-18 18:05:32 +08:00
|
|
|
{
|
2013-07-10 04:21:43 +08:00
|
|
|
_fullPathCache.clear();
|
2013-12-03 14:47:35 +08:00
|
|
|
_filenameLookupDict = filenameLookupDict;
|
2013-01-18 18:05:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:54:08 +08:00
|
|
|
void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename)
|
2013-01-25 20:51:52 +08:00
|
|
|
{
|
2014-01-08 05:02:31 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
2013-01-25 20:51:52 +08:00
|
|
|
if (fullPath.length() > 0)
|
|
|
|
{
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2013-12-03 14:47:35 +08:00
|
|
|
if (!dict.empty())
|
2013-01-25 20:51:52 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap& metadata = dict["metadata"].asValueMap();
|
2013-12-03 14:47:35 +08:00
|
|
|
int version = metadata["version"].asInt();
|
2013-01-25 20:51:52 +08:00
|
|
|
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());
|
2013-01-25 20:51:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-12-04 17:46:57 +08:00
|
|
|
setFilenameLookupDictionary( dict["filenames"].asValueMap());
|
2013-01-25 20:51:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-08-21 01:22:44 +08:00
|
|
|
// get directory+filename, safely adding '/' as necessary
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string ret = directory;
|
|
|
|
if (directory.size() && directory[directory.size()-1] != '/'){
|
2013-08-21 01:22:44 +08:00
|
|
|
ret += '/';
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
ret += filename;
|
2013-08-21 01:22:44 +08:00
|
|
|
|
|
|
|
// if the file doesn't exist, return an empty string
|
2014-04-02 15:35:09 +08:00
|
|
|
if (!isFileExistInternal(ret)) {
|
2013-02-01 11:20:46 +08:00
|
|
|
ret = "";
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
bool FileUtils::isFileExist(const std::string& filename) const
|
|
|
|
{
|
|
|
|
if (isAbsolutePath(filename))
|
|
|
|
{
|
|
|
|
return isFileExistInternal(filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-25 09:50:48 +08:00
|
|
|
std::string fullpath = fullPathForFilename(filename);
|
2014-06-20 18:01:34 +08:00
|
|
|
if (fullpath.empty())
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-02 15:35:09 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
bool FileUtils::isAbsolutePath(const std::string& path) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return (path[0] == '/');
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 18:26:11 +08:00
|
|
|
bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const
|
2014-06-20 18:01:34 +08:00
|
|
|
{
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-09-27 02:38:17 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-06-20 18:01:34 +08:00
|
|
|
return false;
|
2014-09-27 02:38:17 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2014-08-07 11:56:28 +08:00
|
|
|
unsigned long fAttrib = GetFileAttributesA(dirPath.c_str());
|
2014-06-23 11:13:33 +08:00
|
|
|
if (fAttrib != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(fAttrib & FILE_ATTRIBUTE_DIRECTORY))
|
2014-06-20 18:01:34 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-09-27 02:38:17 +08:00
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
if (stat(dirPath.c_str(), &st) == 0)
|
|
|
|
{
|
|
|
|
return S_ISDIR(st.st_mode);
|
|
|
|
}
|
|
|
|
return false;
|
2014-06-20 18:01:34 +08:00
|
|
|
#endif
|
2014-09-27 02:38:17 +08:00
|
|
|
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
bool FileUtils::isDirectoryExist(const std::string& dirPath) const
|
2014-07-08 18:26:11 +08:00
|
|
|
{
|
|
|
|
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;
|
2014-11-22 03:12:55 +08:00
|
|
|
for (const auto& searchIt : _searchPathArray)
|
2014-07-08 18:26:11 +08:00
|
|
|
{
|
2014-11-22 03:12:55 +08:00
|
|
|
for (const auto& resolutionIt : _searchResolutionsOrderArray)
|
2014-07-08 18:26:11 +08:00
|
|
|
{
|
|
|
|
// searchPath + file_path + resourceDirectory
|
2014-11-22 03:12:55 +08:00
|
|
|
fullpath = searchIt + dirPath + resolutionIt;
|
2014-07-08 18:26:11 +08:00
|
|
|
if (isDirectoryExistInternal(fullpath))
|
|
|
|
{
|
2015-04-07 22:15:15 +08:00
|
|
|
_fullPathCache.insert(std::make_pair(dirPath, fullpath));
|
2014-07-08 18:26:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-21 16:51:48 +08:00
|
|
|
bool FileUtils::createDirectory(const std::string& path)
|
2014-06-20 18:01:34 +08:00
|
|
|
{
|
2014-07-18 15:09:00 +08:00
|
|
|
CCASSERT(!path.empty(), "Invalid path");
|
|
|
|
|
|
|
|
if (isDirectoryExist(path))
|
|
|
|
return true;
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
// 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-09-27 02:38:17 +08:00
|
|
|
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-08-07 14:09:22 +08:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA wfad;
|
2014-09-27 02:38:17 +08:00
|
|
|
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
|
|
|
{
|
2014-09-27 02:38:17 +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;
|
2014-09-27 02:38:17 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2014-07-21 16:51:48 +08:00
|
|
|
if ((GetFileAttributesA(path.c_str())) == INVALID_FILE_ATTRIBUTES)
|
|
|
|
{
|
|
|
|
subpath = "";
|
2015-01-23 09:02:33 +08:00
|
|
|
for (unsigned int i = 0; i < dirs.size(); ++i)
|
2014-07-21 16:51:48 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 16:51:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2014-09-27 02:38:17 +08:00
|
|
|
#else
|
|
|
|
DIR *dir = NULL;
|
|
|
|
|
|
|
|
// Create path recursively
|
|
|
|
subpath = "";
|
2014-12-08 09:58:58 +08:00
|
|
|
for (int i = 0; i < dirs.size(); ++i)
|
|
|
|
{
|
2014-09-27 02:38:17 +08:00
|
|
|
subpath += dirs[i];
|
|
|
|
dir = opendir(subpath.c_str());
|
2014-12-08 11:36:11 +08:00
|
|
|
|
2014-09-27 02:38:17 +08:00
|
|
|
if (!dir)
|
|
|
|
{
|
2014-12-08 11:36:11 +08:00
|
|
|
// directory doesn't exist, should create a new one
|
2014-12-08 09:58:58 +08:00
|
|
|
|
2014-09-27 02:38:17 +08:00
|
|
|
int ret = mkdir(subpath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
if (ret != 0 && (errno != EEXIST))
|
|
|
|
{
|
2014-12-08 11:36:11 +08:00
|
|
|
// current directory can not be created, sub directories can not be created too
|
|
|
|
// should return
|
2014-09-27 02:38:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-12-08 11:36:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// directory exists, should close opened dir
|
|
|
|
closedir(dir);
|
|
|
|
}
|
2014-09-27 02:38:17 +08:00
|
|
|
}
|
|
|
|
return true;
|
2014-07-21 16:51:48 +08:00
|
|
|
#endif
|
2014-06-20 18:01:34 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2014-10-10 11:59:35 +08:00
|
|
|
{
|
|
|
|
auto ret = remove(fpath);
|
2014-12-08 09:58:58 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
log("Fail to remove: %s ",fpath);
|
2014-10-10 11:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-10-10 14:03:20 +08:00
|
|
|
#endif
|
2014-10-10 11:59:35 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
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());
|
2014-06-20 18:01:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove downloaded files
|
2014-09-27 02:38:17 +08:00
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-09-27 02:38:17 +08:00
|
|
|
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;
|
2014-09-27 02:38:17 +08:00
|
|
|
HANDLE search = FindFirstFileEx(files.c_str(), FindExInfoStandard, &wfd, FindExSearchNameMatch, NULL, 0);
|
2014-09-19 16:52:11 +08:00
|
|
|
bool ret=true;
|
2014-09-22 09:42:33 +08:00
|
|
|
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]!='.')
|
|
|
|
{
|
2014-09-27 02:38:17 +08:00
|
|
|
std::wstring temp = wpath + wfd.cFileName;
|
2014-09-19 15:54:18 +08:00
|
|
|
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
{
|
2014-09-27 02:38:17 +08:00
|
|
|
temp += '/';
|
|
|
|
ret = ret && this->removeDirectory(std::string(temp.begin(), temp.end()));
|
2014-09-19 15:54:18 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-27 02:38:17 +08:00
|
|
|
SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
|
|
|
|
ret = ret && DeleteFile(temp.c_str());
|
2014-09-19 15:54:18 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 09:42:33 +08:00
|
|
|
find = FindNextFile(search, &wfd);
|
2014-09-19 15:54:18 +08:00
|
|
|
}
|
2014-09-22 09:42:33 +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;
|
2014-09-27 02:38:17 +08:00
|
|
|
#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 + "\"";
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
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)
|
2014-10-10 11:59:35 +08:00
|
|
|
if (nftw(path.c_str(),unlink_cb, 64, FTW_DEPTH | FTW_PHYS))
|
2014-06-20 18:01:34 +08:00
|
|
|
return false;
|
2014-10-10 11:59:35 +08:00
|
|
|
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
|
2014-09-27 02:38:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::removeFile(const std::string &path)
|
|
|
|
{
|
|
|
|
// Remove downloaded file
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-09-27 02:38:17 +08:00
|
|
|
std::wstring wpath(path.begin(), path.end());
|
|
|
|
if (DeleteFile(wpath.c_str()))
|
2014-08-07 11:56:28 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-09-27 02:38:17 +08:00
|
|
|
#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;
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
if (WinExec(command.c_str(), SW_HIDE) > 31)
|
2014-09-19 15:54:18 +08:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2014-09-27 02:38:17 +08:00
|
|
|
#else
|
2014-10-10 11:59:35 +08:00
|
|
|
if (remove(path.c_str())) {
|
2014-09-27 02:38:17 +08:00
|
|
|
return false;
|
2014-10-10 11:59:35 +08:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-20 18:01:34 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
|
|
|
|
{
|
|
|
|
CCASSERT(!path.empty(), "Invalid path");
|
2014-10-16 10:28:52 +08:00
|
|
|
std::string oldPath = path + oldname;
|
|
|
|
std::string newPath = path + name;
|
2014-12-06 10:12:25 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
// Rename a file
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-12-06 10:12:25 +08:00
|
|
|
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(),
|
|
|
|
std::wstring(_new.begin(), _new.end()).c_str(),
|
|
|
|
MOVEFILE_REPLACE_EXISTING & MOVEFILE_WRITE_THROUGH))
|
2014-06-20 18:01:34 +08:00
|
|
|
{
|
2014-12-06 10:12:25 +08:00
|
|
|
return true;
|
2014-06-20 18:01:34 +08:00
|
|
|
}
|
2014-12-06 10:12:25 +08:00
|
|
|
return false;
|
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2014-11-25 03:52:44 +08:00
|
|
|
std::regex pat("\\/");
|
2014-10-16 10:28:52 +08:00
|
|
|
std::string _old = std::regex_replace(oldPath, pat, "\\");
|
|
|
|
std::string _new = std::regex_replace(newPath, pat, "\\");
|
2014-12-06 10:12:25 +08:00
|
|
|
|
2014-10-16 10:28:52 +08:00
|
|
|
if(FileUtils::getInstance()->isFileExist(_new))
|
|
|
|
{
|
2015-03-31 18:52:40 +08:00
|
|
|
if (!DeleteFileA(_new.c_str()))
|
|
|
|
{
|
|
|
|
CCLOGERROR("Fail to delete file %s !Error code is 0x%x", newPath.c_str(), GetLastError());
|
|
|
|
}
|
2014-10-16 10:28:52 +08:00
|
|
|
}
|
2014-12-06 10:12:25 +08:00
|
|
|
|
2015-03-31 18:52:40 +08:00
|
|
|
if (MoveFileA(_old.c_str(), _new.c_str()))
|
|
|
|
{
|
2014-10-16 10:28:52 +08:00
|
|
|
return true;
|
2015-03-31 18:52:40 +08:00
|
|
|
}
|
2014-10-16 10:28:52 +08:00
|
|
|
else
|
2015-03-31 18:52:40 +08:00
|
|
|
{
|
|
|
|
CCLOGERROR("Fail to rename file %s to %s !Error code is 0x%x", oldPath.c_str(), newPath.c_str(), GetLastError());
|
2014-10-16 10:28:52 +08:00
|
|
|
return false;
|
2015-03-31 18:52:40 +08:00
|
|
|
}
|
2014-12-06 10:12:25 +08:00
|
|
|
#else
|
|
|
|
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;
|
2014-06-20 18:01:34 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
long FileUtils::getFileSize(const std::string &filepath)
|
|
|
|
{
|
|
|
|
CCASSERT(!filepath.empty(), "Invalid path");
|
|
|
|
|
|
|
|
std::string fullpath = filepath;
|
|
|
|
if (!isAbsolutePath(filepath))
|
|
|
|
{
|
2015-05-25 09:50:48 +08:00
|
|
|
fullpath = fullPathForFilename(filepath);
|
2014-06-20 18:01:34 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Notification support when getFileData from invalid file path.
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-09-07 09:46:33 +08:00
|
|
|
static bool s_popupNotify = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-07 09:46:33 +08:00
|
|
|
void FileUtils::setPopupNotify(bool notify)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
s_popupNotify = notify;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
bool FileUtils::isPopupNotify() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-07 09:46:33 +08:00
|
|
|
return s_popupNotify;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2015-05-04 16:06:04 +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 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
|
|
|
|
{
|
|
|
|
return UTF8StringToMultiByte(filenameUtf8);
|
|
|
|
}
|
|
|
|
#else
|
2015-04-19 19:00:27 +08:00
|
|
|
std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
|
|
|
|
{
|
|
|
|
return filenameUtf8;
|
|
|
|
}
|
2015-05-04 16:06:04 +08:00
|
|
|
#endif
|
2015-04-19 19:00:27 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|