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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/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"
|
2016-05-03 15:47:43 +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-07-13 17:06:01 +08:00
|
|
|
NS_CC_BEGIN
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
// Implement DictMaker
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
|
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
typedef enum
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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;
|
2015-07-07 14:06:59 +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
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
ValueMap* _curDict;
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector* _curArray;
|
2013-12-04 15:34:05 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
std::stack<ValueMap*> _dictStack;
|
2013-12-04 17:46:57 +08:00
|
|
|
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:
|
2015-07-07 14:06:59 +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);
|
2015-07-07 14:06:59 +08:00
|
|
|
return _rootDict;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
ValueMap dictionaryWithDataOfFile(const char* filedata, int filesize)
|
|
|
|
{
|
|
|
|
_resultType = SAX_RESULT_DICT;
|
|
|
|
SAXParser parser;
|
2014-07-29 17:06:43 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
|
|
|
|
parser.setDelegator(this);
|
2014-07-29 17:06:43 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
parser.parse(filedata, filesize);
|
|
|
|
return _rootDict;
|
|
|
|
}
|
2014-07-29 17:06:43 +08:00
|
|
|
|
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);
|
2015-07-07 14:06:59 +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" )
|
|
|
|
{
|
2015-07-07 14:06:59 +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()));
|
2015-07-07 14:06:59 +08:00
|
|
|
_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());
|
2015-07-07 14:06:59 +08:00
|
|
|
_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
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-07 14:06:59 +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());
|
2015-07-07 14:06:59 +08:00
|
|
|
_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()));
|
2015-07-07 14:06:59 +08:00
|
|
|
_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
|
2016-05-03 15:47:43 +08:00
|
|
|
_curArray->push_back(Value(std::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
|
2016-05-03 15:47:43 +08:00
|
|
|
(*_curDict)[_curKey] = Value(std::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
|
|
|
}
|
2015-07-07 14:06: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
|
|
|
}
|
2015-07-07 14:06: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
|
|
|
{
|
2016-02-03 23:12:37 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2016-02-03 23:12:37 +08:00
|
|
|
return tMaker.dictionaryWithContentsOfFile(fullPath);
|
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
|
|
|
{
|
2016-02-03 23:12:37 +08:00
|
|
|
const std::string fullPath = fullPathForFilename(filename);
|
2013-06-20 14:13:12 +08:00
|
|
|
DictMaker tMaker;
|
2016-02-03 23:12:37 +08:00
|
|
|
return tMaker.arrayWithContentsOfFile(fullPath);
|
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
|
|
|
|
*/
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtils::writeToFile(const ValueMap& dict, const std::string &fullPath)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2015-07-07 14:06:59 +08:00
|
|
|
return writeValueMapToFile(dict, fullPath);
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtils::writeValueMapToFile(const ValueMap& dict, const std::string& fullPath)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2015-07-08 15:19:17 +08:00
|
|
|
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr == doc)
|
2013-04-13 16:55:24 +08:00
|
|
|
return false;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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
|
|
|
}
|
2015-07-07 14:06:59 +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);
|
2015-07-07 14:06:59 +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);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc);
|
2015-07-07 14:06:59 +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);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-04-19 19:00:27 +08:00
|
|
|
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
delete doc;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtils::writeValueVectorToFile(const ValueVector& vecData, const std::string& fullPath)
|
2015-07-07 14:06:59 +08:00
|
|
|
{
|
2015-07-08 15:19:17 +08:00
|
|
|
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
|
2015-07-07 14:06:59 +08:00
|
|
|
if (nullptr == doc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
|
|
|
if (nullptr == declaration)
|
|
|
|
{
|
|
|
|
delete doc;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
doc->LinkEndChild(declaration);
|
|
|
|
tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
|
|
|
|
doc->LinkEndChild(docType);
|
|
|
|
|
|
|
|
tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
|
|
|
|
rootEle->SetAttribute("version", "1.0");
|
|
|
|
if (nullptr == rootEle)
|
|
|
|
{
|
|
|
|
delete doc;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
doc->LinkEndChild(rootEle);
|
|
|
|
|
|
|
|
tinyxml2::XMLElement *innerDict = generateElementForArray(vecData, doc);
|
|
|
|
if (nullptr == innerDict)
|
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);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-04-19 19:00:27 +08:00
|
|
|
bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str());
|
2015-07-07 14:06:59 +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
|
|
|
}
|
2015-07-07 14:06:59 +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;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-03-19 16:42:14 +08:00
|
|
|
//object is bool
|
|
|
|
if (value.getType() == Value::Type::BOOLEAN) {
|
2015-07-07 14:06:59 +08:00
|
|
|
tinyxml2::XMLElement* node = doc->NewElement(value.asString().c_str());
|
|
|
|
return node;
|
2014-03-19 16:42:14 +08:00
|
|
|
}
|
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);
|
2015-07-07 14:06:59 +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);
|
2015-07-07 14:06:59 +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");
|
2015-07-07 14:06:59 +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);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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
|
|
|
|
|
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();}
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtils::writeToFile(const 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) */
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
// Implement FileUtils
|
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;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtils::writeStringToFile(const std::string& dataStr, const std::string& fullPath)
|
2015-07-07 14:06:59 +08:00
|
|
|
{
|
2016-01-27 13:39:31 +08:00
|
|
|
Data data;
|
2016-01-27 13:40:19 +08:00
|
|
|
data.fastSet((unsigned char*)dataStr.c_str(), dataStr.size());
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-01-27 13:40:19 +08:00
|
|
|
bool rv = writeDataToFile(data, fullPath);
|
|
|
|
|
|
|
|
data.fastSet(nullptr, 0);
|
2016-01-27 13:43:55 +08:00
|
|
|
return rv;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-01-27 13:39:31 +08:00
|
|
|
bool FileUtils::writeDataToFile(const Data& data, const std::string& fullPath)
|
2015-07-07 14:06:59 +08:00
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
const char* mode = "wb";
|
2014-12-26 11:34:31 +08:00
|
|
|
|
2016-01-27 13:39:31 +08:00
|
|
|
CCASSERT(!fullPath.empty() && data.getSize() != 0, "Invalid parameters.");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
auto fileutils = FileUtils::getInstance();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Read the file from hardware
|
|
|
|
FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode);
|
|
|
|
CC_BREAK_IF(!fp);
|
2016-01-27 13:39:31 +08:00
|
|
|
size = data.getSize();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-01-27 13:39:31 +08:00
|
|
|
fwrite(data.getBytes(), size, 1, fp);
|
2014-12-26 11:34:31 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
std::string FileUtils::getStringFromFile(const std::string& filename)
|
2013-12-18 14:58:17 +08:00
|
|
|
{
|
2016-04-26 13:37:22 +08:00
|
|
|
std::string s;
|
|
|
|
getContents(filename, &s);
|
|
|
|
return s;
|
|
|
|
}
|
2015-05-15 08:24:27 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
Data FileUtils::getDataFromFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
Data d;
|
|
|
|
getContents(filename, &d);
|
|
|
|
return d;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
FileUtils::Status FileUtils::getContents(const std::string& filename, ResizableBuffer* buffer)
|
|
|
|
{
|
|
|
|
if (filename.empty())
|
|
|
|
return Status::NotExists;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
auto fs = FileUtils::getInstance();
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
std::string fullPath = fs->fullPathForFilename(filename);
|
|
|
|
if (fullPath.empty())
|
|
|
|
return Status::NotExists;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
FILE *fp = fopen(fs->getSuitableFOpen(fullPath).c_str(), "rb");
|
|
|
|
if (!fp)
|
|
|
|
return Status::OpenFailed;
|
2013-12-18 14:58:17 +08:00
|
|
|
|
2016-07-14 00:18:44 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
auto descriptor = _fileno(fp);
|
|
|
|
#else
|
|
|
|
auto descriptor = fileno(fp);
|
|
|
|
#endif
|
|
|
|
struct stat statBuf;
|
|
|
|
if (fstat(descriptor, &statBuf) == -1) {
|
2016-07-15 16:04:14 +08:00
|
|
|
fclose(fp);
|
2016-07-14 00:18:44 +08:00
|
|
|
return Status::ReadFailed;
|
|
|
|
}
|
|
|
|
size_t size = statBuf.st_size;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
buffer->resize(size);
|
|
|
|
size_t readsize = fread(buffer->buffer(), 1, size, fp);
|
|
|
|
fclose(fp);
|
2013-12-18 14:58:17 +08:00
|
|
|
|
2016-04-26 13:37:22 +08:00
|
|
|
if (readsize < size) {
|
|
|
|
buffer->resize(readsize);
|
2016-06-25 11:58:55 +08:00
|
|
|
return Status::ReadFailed;
|
2016-04-26 13:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK;
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
|
|
|
|
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-24 10:51:47 +08:00
|
|
|
CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
|
2016-04-26 13:37:22 +08:00
|
|
|
(void)(mode); // mode is unused, as we do not support text mode any more...
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2016-04-29 09:29:54 +08:00
|
|
|
Data d;
|
|
|
|
if (getContents(filename, &d) != Status::OK) {
|
|
|
|
*size = 0;
|
2016-04-26 13:37:22 +08:00
|
|
|
return nullptr;
|
2016-04-29 09:29:54 +08:00
|
|
|
}
|
2016-04-26 13:37:22 +08:00
|
|
|
|
2016-04-29 09:29:54 +08:00
|
|
|
return d.takeBuffer(size);
|
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
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-24 10:51:47 +08:00
|
|
|
CC_BREAK_IF(zipFilePath.empty());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-09-18 04:30:32 +08:00
|
|
|
file = unzOpen(FileUtils::getInstance()->getSuitableFOpen(zipFilePath).c_str());
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(!file);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
// FIXME: Other platforms should use upstream minizip like mingw-w64
|
2015-12-29 07:59:36 +08:00
|
|
|
#ifdef MINIZIP_FROM_SYSTEM
|
2014-10-11 11:39:42 +08:00
|
|
|
int ret = unzLocateFile(file, filename.c_str(), NULL);
|
2015-12-29 07:59:36 +08:00
|
|
|
#else
|
2013-12-24 10:51:47 +08:00
|
|
|
int ret = unzLocateFile(file, filename.c_str(), 1);
|
2015-12-29 07:59:36 +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;
|
2015-07-07 14:06:59 +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);
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
// searchPath + file_path + resourceDirectory
|
|
|
|
std::string path = searchPath;
|
|
|
|
path += file_path;
|
|
|
|
path += resolutionDirectory;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
path = getFullPathForDirectoryAndFilename(path, file);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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 "";
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-04-02 16:33:05 +08:00
|
|
|
// Get the new file name.
|
|
|
|
const std::string newFilename( getNewFilename(filename) );
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
std::string fullpath;
|
|
|
|
|
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);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!fullpath.empty())
|
2014-04-02 16:33:05 +08:00
|
|
|
{
|
|
|
|
// Using the filename passed in as key.
|
|
|
|
_fullPathCache.insert(std::make_pair(filename, fullpath));
|
|
|
|
return fullpath;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-04-02 16:33:05 +08:00
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2015-07-07 14:06:59 +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
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-02-01 18:48:44 +08:00
|
|
|
if (resolutionDirectory.length() > 0 && resolutionDirectory[resolutionDirectory.length()-1] != '/')
|
|
|
|
{
|
|
|
|
resolutionDirectory += "/";
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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("/");
|
2015-07-07 14:06:59 +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;
|
2015-07-07 14:06:59 +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;
|
2015-07-07 14:06:59 +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);
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!path.empty() && path[path.length()-1] != '/')
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2015-07-07 14:06:59 +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;
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!path.empty() && path[path.length()-1] != '/')
|
2013-02-04 12:41:24 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2015-07-07 14:06:59 +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);
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!fullPath.empty())
|
2013-01-25 20:51:52 +08:00
|
|
|
{
|
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
|
|
|
{
|
2015-07-07 14:06:59 +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;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-08 18:26:11 +08:00
|
|
|
if (isAbsolutePath(dirPath))
|
|
|
|
{
|
|
|
|
return isDirectoryExistInternal(dirPath);
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-08 18:26:11 +08:00
|
|
|
// Already Cached ?
|
|
|
|
auto cacheIter = _fullPathCache.find(dirPath);
|
|
|
|
if( cacheIter != _fullPathCache.end() )
|
|
|
|
{
|
|
|
|
return isDirectoryExistInternal(cacheIter->second);
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
|
|
|
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
|
2016-06-22 10:19:35 +08:00
|
|
|
fullpath = fullPathForFilename(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;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2015-07-13 22:44:40 +08:00
|
|
|
// windows os implement should override in platform specific FileUtiles class
|
2015-07-13 17:06:01 +08:00
|
|
|
bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const
|
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support isDirectoryExistInternal");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::createDirectory(const std::string& path)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support createDirectory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::removeDirectory(const std::string& path)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support removeDirectory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::removeFile(const std::string &path)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support removeFile");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
bool FileUtils::renameFile(const std::string &oldfullpath, const std::string& newfullpath)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support renameFile");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
bool FileUtils::renameFile(const std::string &path, const std::string &oldname, const std::string &name)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "FileUtils not support renameFile");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
|
|
|
|
{
|
|
|
|
CCASSERT(false, "getSuitableFOpen should be override by platform FileUtils");
|
|
|
|
return filenameUtf8;
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:50:20 +08:00
|
|
|
long FileUtils::getFileSize(const std::string &filepath)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "getFileSize should be override by platform FileUtils");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
#else
|
2015-07-13 22:44:40 +08:00
|
|
|
// default implements for unix like os
|
2015-07-13 17:06:01 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(dirPath.c_str(), &st) == 0)
|
|
|
|
{
|
|
|
|
return S_ISDIR(st.st_mode);
|
|
|
|
}
|
|
|
|
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");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-18 15:09:00 +08:00
|
|
|
if (isDirectoryExist(path))
|
|
|
|
return true;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
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
|
|
|
|
|
|
|
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());
|
2015-07-07 14:06:59 +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
|
2015-07-07 14:06:59 +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-10-10 11:59:35 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
bool FileUtils::removeDirectory(const std::string& path)
|
|
|
|
{
|
2014-10-10 14:03:20 +08:00
|
|
|
std::string command = "rm -r ";
|
|
|
|
// Path may include space.
|
|
|
|
command += "\"" + path + "\"";
|
|
|
|
if (system(command.c_str()) >= 0)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2014-09-27 02:38:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtils::removeFile(const std::string &path)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
bool FileUtils::renameFile(const std::string &oldfullpath, const std::string &newfullpath)
|
2014-06-20 18:01:34 +08:00
|
|
|
{
|
2015-08-13 15:14:10 +08:00
|
|
|
CCASSERT(!oldfullpath.empty(), "Invalid path");
|
|
|
|
CCASSERT(!newfullpath.empty(), "Invalid path");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
int errorCode = rename(oldfullpath.c_str(), newfullpath.c_str());
|
2014-12-06 10:12:25 +08:00
|
|
|
|
|
|
|
if (0 != errorCode)
|
|
|
|
{
|
2015-08-13 15:14:10 +08:00
|
|
|
CCLOGERROR("Fail to rename file %s to %s !Error code is %d", oldfullpath.c_str(), newfullpath.c_str(), errorCode);
|
2014-12-06 10:12:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-06-20 18:01:34 +08:00
|
|
|
}
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
return this->renameFile(oldPath, newPath);
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
|
|
|
|
{
|
|
|
|
return filenameUtf8;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:44:40 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
long FileUtils::getFileSize(const std::string &filepath)
|
|
|
|
{
|
|
|
|
CCASSERT(!filepath.empty(), "Invalid path");
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
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;
|
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
struct stat info;
|
|
|
|
// Get data associated with "crt_stat.c":
|
2015-09-25 14:50:20 +08:00
|
|
|
int result = stat(fullpath.c_str(), &info);
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-06-20 18:01:34 +08:00
|
|
|
// Check if statistics are valid:
|
2015-09-25 14:50:20 +08:00
|
|
|
if (result != 0)
|
2014-06-20 18:01:34 +08:00
|
|
|
{
|
|
|
|
// Failed
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (long)(info.st_size);
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 14:50:20 +08:00
|
|
|
#endif
|
2014-06-20 18:01:34 +08:00
|
|
|
|
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-08-06 03:21:16 +08:00
|
|
|
std::string FileUtils::getFileExtension(const std::string& filePath) const
|
|
|
|
{
|
|
|
|
std::string fileExtension;
|
|
|
|
size_t pos = filePath.find_last_of('.');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
fileExtension = filePath.substr(pos, filePath.length());
|
|
|
|
|
|
|
|
std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileExtension;
|
|
|
|
}
|
|
|
|
|
2016-06-10 13:52:35 +08:00
|
|
|
void FileUtils::valueMapCompact(ValueMap& valueMap)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtils::valueVectorCompact(ValueVector& valueVector)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|