Call to unavailable function 'system': not available on iOS
使用Xcode 9 导入cocos2d-x 项目,报错 Call to unavailable function 'system': not available on iOS
原因很简单,就是ios 11 不支持这个函数调用了,所以报错。
github 上已经给出了相关fix 的方案,传送门:https://github.com/cocos2d/cocos2d-x/pull/17921/files
也可以直接复制下面代码,替换cocos2d_libs.xcodeproj>platform>CCFileUtils.cpp 路径的CCFileUtils.cpp 文件
/****************************************************************************
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/ #include "platform/CCFileUtils.h" #include <stack> #include "base/CCData.h"
#include "base/ccMacros.h"
#include "base/CCDirector.h"
#include "platform/CCSAXParser.h" #include "tinyxml2/tinyxml2.h"
#include "tinydir/tinydir.h" #ifdef MINIZIP_FROM_SYSTEM
#include <minizip/unzip.h>
#else // from our embedded sources
#include "unzip/unzip.h"
#endif
#include <sys/stat.h> NS_CC_BEGIN // Implement DictMaker #if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) typedef enum
{
SAX_NONE = ,
SAX_KEY,
SAX_DICT,
SAX_INT,
SAX_REAL,
SAX_STRING,
SAX_ARRAY
}SAXState; typedef enum
{
SAX_RESULT_NONE = ,
SAX_RESULT_DICT,
SAX_RESULT_ARRAY
}SAXResult; class DictMaker : public SAXDelegator
{
public:
SAXResult _resultType;
ValueMap _rootDict;
ValueVector _rootArray; std::string _curKey; ///< parsed key
std::string _curValue; // parsed value
SAXState _state; ValueMap* _curDict;
ValueVector* _curArray; std::stack<ValueMap*> _dictStack;
std::stack<ValueVector*> _arrayStack;
std::stack<SAXState> _stateStack; public:
DictMaker()
: _resultType(SAX_RESULT_NONE)
{
} ~DictMaker()
{
} ValueMap dictionaryWithContentsOfFile(const std::string& fileName)
{
_resultType = SAX_RESULT_DICT;
SAXParser parser; CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
parser.setDelegator(this); parser.parse(fileName);
return _rootDict;
} ValueMap dictionaryWithDataOfFile(const char* filedata, int filesize)
{
_resultType = SAX_RESULT_DICT;
SAXParser parser; CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
parser.setDelegator(this); parser.parse(filedata, filesize);
return _rootDict;
} ValueVector arrayWithContentsOfFile(const std::string& fileName)
{
_resultType = SAX_RESULT_ARRAY;
SAXParser parser; CCASSERT(parser.init("UTF-8"), "The file format isn't UTF-8");
parser.setDelegator(this); parser.parse(fileName);
return _rootArray;
} void startElement(void *ctx, const char *name, const char **atts)
{
CC_UNUSED_PARAM(ctx);
CC_UNUSED_PARAM(atts);
const std::string sName(name);
if( sName == "dict" )
{
if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
{
_curDict = &_rootDict;
} _state = SAX_DICT; SAXState preState = SAX_NONE;
if (! _stateStack.empty())
{
preState = _stateStack.top();
} if (SAX_ARRAY == preState)
{
// add a new dictionary into the array
_curArray->push_back(Value(ValueMap()));
_curDict = &(_curArray->rbegin())->asValueMap();
}
else if (SAX_DICT == preState)
{
// add a new dictionary into the pre dictionary
CCASSERT(! _dictStack.empty(), "The state is wrong!");
ValueMap* preDict = _dictStack.top();
(*preDict)[_curKey] = Value(ValueMap());
_curDict = &(*preDict)[_curKey].asValueMap();
} // record the dict state
_stateStack.push(_state);
_dictStack.push(_curDict);
}
else if(sName == "key")
{
_state = SAX_KEY;
}
else if(sName == "integer")
{
_state = SAX_INT;
}
else if(sName == "real")
{
_state = SAX_REAL;
}
else if(sName == "string")
{
_state = SAX_STRING;
}
else if (sName == "array")
{
_state = SAX_ARRAY; if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
{
_curArray = &_rootArray;
}
SAXState preState = SAX_NONE;
if (! _stateStack.empty())
{
preState = _stateStack.top();
} if (preState == SAX_DICT)
{
(*_curDict)[_curKey] = Value(ValueVector());
_curArray = &(*_curDict)[_curKey].asValueVector();
}
else if (preState == SAX_ARRAY)
{
CCASSERT(! _arrayStack.empty(), "The state is wrong!");
ValueVector* preArray = _arrayStack.top();
preArray->push_back(Value(ValueVector()));
_curArray = &(_curArray->rbegin())->asValueVector();
}
// record the array state
_stateStack.push(_state);
_arrayStack.push(_curArray);
}
else
{
_state = SAX_NONE;
}
} void endElement(void *ctx, const char *name)
{
CC_UNUSED_PARAM(ctx);
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
const std::string sName((char*)name);
if( sName == "dict" )
{
_stateStack.pop();
_dictStack.pop();
if ( !_dictStack.empty())
{
_curDict = _dictStack.top();
}
}
else if (sName == "array")
{
_stateStack.pop();
_arrayStack.pop();
if (! _arrayStack.empty())
{
_curArray = _arrayStack.top();
}
}
else if (sName == "true")
{
if (SAX_ARRAY == curState)
{
_curArray->push_back(Value(true));
}
else if (SAX_DICT == curState)
{
(*_curDict)[_curKey] = Value(true);
}
}
else if (sName == "false")
{
if (SAX_ARRAY == curState)
{
_curArray->push_back(Value(false));
}
else if (SAX_DICT == curState)
{
(*_curDict)[_curKey] = Value(false);
}
}
else if (sName == "string" || sName == "integer" || sName == "real")
{
if (SAX_ARRAY == curState)
{
if (sName == "string")
_curArray->push_back(Value(_curValue));
else if (sName == "integer")
_curArray->push_back(Value(atoi(_curValue.c_str())));
else
_curArray->push_back(Value(std::atof(_curValue.c_str())));
}
else if (SAX_DICT == curState)
{
if (sName == "string")
(*_curDict)[_curKey] = Value(_curValue);
else if (sName == "integer")
(*_curDict)[_curKey] = Value(atoi(_curValue.c_str()));
else
(*_curDict)[_curKey] = Value(std::atof(_curValue.c_str()));
} _curValue.clear();
} _state = SAX_NONE;
} void textHandler(void *ctx, const char *ch, int len)
{
CC_UNUSED_PARAM(ctx);
if (_state == SAX_NONE)
{
return;
} SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
const std::string text = std::string((char*)ch,len); switch(_state)
{
case SAX_KEY:
_curKey = text;
break;
case SAX_INT:
case SAX_REAL:
case SAX_STRING:
{
if (curState == SAX_DICT)
{
CCASSERT(!_curKey.empty(), "key not found : <integer/real>");
} _curValue.append(text);
}
break;
default:
break;
}
}
}; ValueMap FileUtils::getValueMapFromFile(const std::string& filename)
{
const std::string fullPath = fullPathForFilename(filename);
if (fullPath.empty())
{
ValueMap ret;
return ret;
} DictMaker tMaker;
return tMaker.dictionaryWithContentsOfFile(fullPath);
} ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize)
{
DictMaker tMaker;
return tMaker.dictionaryWithDataOfFile(filedata, filesize);
} ValueVector FileUtils::getValueVectorFromFile(const std::string& filename)
{
const std::string fullPath = fullPathForFilename(filename);
DictMaker tMaker;
return tMaker.arrayWithContentsOfFile(fullPath);
} /*
* forward statement
*/
static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *doc);
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc); /*
* Use tinyxml2 to write plist files
*/
bool FileUtils::writeToFile(const ValueMap& dict, const std::string &fullPath)
{
return writeValueMapToFile(dict, fullPath);
} bool FileUtils::writeValueMapToFile(const ValueMap& dict, const std::string& fullPath)
{
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
if (nullptr == doc)
return false; tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
if (nullptr == declaration)
{
delete doc;
return false;
} doc->LinkEndChild(declaration);
tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
doc->LinkEndChild(docType); tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
rootEle->SetAttribute("version", "1.0");
if (nullptr == rootEle)
{
delete doc;
return false;
}
doc->LinkEndChild(rootEle); tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc);
if (nullptr == innerDict)
{
delete doc;
return false;
}
rootEle->LinkEndChild(innerDict); bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str()); delete doc;
return ret;
} bool FileUtils::writeValueVectorToFile(const ValueVector& vecData, const std::string& fullPath)
{
tinyxml2::XMLDocument *doc = new (std::nothrow)tinyxml2::XMLDocument();
if (nullptr == doc)
return false; tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
if (nullptr == declaration)
{
delete doc;
return false;
} doc->LinkEndChild(declaration);
tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
doc->LinkEndChild(docType); tinyxml2::XMLElement *rootEle = doc->NewElement("plist");
rootEle->SetAttribute("version", "1.0");
if (nullptr == rootEle)
{
delete doc;
return false;
}
doc->LinkEndChild(rootEle); tinyxml2::XMLElement *innerDict = generateElementForArray(vecData, doc);
if (nullptr == innerDict)
{
delete doc;
return false;
}
rootEle->LinkEndChild(innerDict); bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str()); delete doc;
return ret;
} /*
* Generate tinyxml2::XMLElement for Object through a tinyxml2::XMLDocument
*/
static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxml2::XMLDocument *doc)
{
// object is String
if (value.getType() == Value::Type::STRING)
{
tinyxml2::XMLElement* node = doc->NewElement("string");
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
node->LinkEndChild(content);
return node;
} // object is integer
if (value.getType() == Value::Type::INTEGER)
{
tinyxml2::XMLElement* node = doc->NewElement("integer");
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
node->LinkEndChild(content);
return node;
} // object is real
if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE)
{
tinyxml2::XMLElement* node = doc->NewElement("real");
tinyxml2::XMLText* content = doc->NewText(value.asString().c_str());
node->LinkEndChild(content);
return node;
} //object is bool
if (value.getType() == Value::Type::BOOLEAN) {
tinyxml2::XMLElement* node = doc->NewElement(value.asString().c_str());
return node;
} // object is Array
if (value.getType() == Value::Type::VECTOR)
return generateElementForArray(value.asValueVector(), doc); // object is Dictionary
if (value.getType() == Value::Type::MAP)
return generateElementForDict(value.asValueMap(), doc); CCLOG("This type cannot appear in property list");
return nullptr;
} /*
* Generate tinyxml2::XMLElement for Dictionary through a tinyxml2::XMLDocument
*/
static tinyxml2::XMLElement* generateElementForDict(const ValueMap& dict, tinyxml2::XMLDocument *doc)
{
tinyxml2::XMLElement* rootNode = doc->NewElement("dict"); for (const auto &iter : dict)
{
tinyxml2::XMLElement* tmpNode = doc->NewElement("key");
rootNode->LinkEndChild(tmpNode);
tinyxml2::XMLText* content = doc->NewText(iter.first.c_str());
tmpNode->LinkEndChild(content); tinyxml2::XMLElement *element = generateElementForObject(iter.second, doc);
if (element)
rootNode->LinkEndChild(element);
}
return rootNode;
} /*
* Generate tinyxml2::XMLElement for Array through a tinyxml2::XMLDocument
*/
static tinyxml2::XMLElement* generateElementForArray(const ValueVector& array, tinyxml2::XMLDocument *pDoc)
{
tinyxml2::XMLElement* rootNode = pDoc->NewElement("array"); for(const auto &value : array) {
tinyxml2::XMLElement *element = generateElementForObject(value, pDoc);
if (element)
rootNode->LinkEndChild(element);
}
return rootNode;
} #else /* The subclass FileUtilsApple should override these two method. */
ValueMap FileUtils::getValueMapFromFile(const std::string& filename) {return ValueMap();}
ValueMap FileUtils::getValueMapFromData(const char* filedata, int filesize) {return ValueMap();}
ValueVector FileUtils::getValueVectorFromFile(const std::string& filename) {return ValueVector();}
bool FileUtils::writeToFile(const ValueMap& dict, const std::string &fullPath) {return false;} #endif /* (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) */ // Implement FileUtils
FileUtils* FileUtils::s_sharedFileUtils = nullptr; void FileUtils::destroyInstance()
{
CC_SAFE_DELETE(s_sharedFileUtils);
} void FileUtils::setDelegate(FileUtils *delegate)
{
if (s_sharedFileUtils)
delete s_sharedFileUtils; s_sharedFileUtils = delegate;
} FileUtils::FileUtils()
: _writablePath("")
{
} FileUtils::~FileUtils()
{
} bool FileUtils::writeStringToFile(const std::string& dataStr, const std::string& fullPath)
{
Data data;
data.fastSet((unsigned char*)dataStr.c_str(), dataStr.size()); bool rv = writeDataToFile(data, fullPath); data.fastSet(nullptr, );
return rv;
} bool FileUtils::writeDataToFile(const Data& data, const std::string& fullPath)
{
size_t size = ;
const char* mode = "wb"; CCASSERT(!fullPath.empty() && data.getSize() != , "Invalid parameters."); auto fileutils = FileUtils::getInstance();
do
{
// Read the file from hardware
FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode);
CC_BREAK_IF(!fp);
size = data.getSize(); fwrite(data.getBytes(), size, , fp); fclose(fp); return true;
} while (); return false;
} bool FileUtils::init()
{
_searchPathArray.push_back(_defaultResRootPath);
_searchResolutionsOrderArray.push_back("");
return true;
} void FileUtils::purgeCachedEntries()
{
_fullPathCache.clear();
} std::string FileUtils::getStringFromFile(const std::string& filename)
{
std::string s;
getContents(filename, &s);
return s;
} Data FileUtils::getDataFromFile(const std::string& filename)
{
Data d;
getContents(filename, &d);
return d;
} FileUtils::Status FileUtils::getContents(const std::string& filename, ResizableBuffer* buffer)
{
if (filename.empty())
return Status::NotExists; auto fs = FileUtils::getInstance(); std::string fullPath = fs->fullPathForFilename(filename);
if (fullPath.empty())
return Status::NotExists; FILE *fp = fopen(fs->getSuitableFOpen(fullPath).c_str(), "rb");
if (!fp)
return Status::OpenFailed; #if defined(_MSC_VER)
auto descriptor = _fileno(fp);
#else
auto descriptor = fileno(fp);
#endif
struct stat statBuf;
if (fstat(descriptor, &statBuf) == -) {
fclose(fp);
return Status::ReadFailed;
}
size_t size = statBuf.st_size; buffer->resize(size);
size_t readsize = fread(buffer->buffer(), , size, fp);
fclose(fp); if (readsize < size) {
buffer->resize(readsize);
return Status::ReadFailed;
} return Status::OK;
} unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
{
CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
(void)(mode); // mode is unused, as we do not support text mode any more... Data d;
if (getContents(filename, &d) != Status::OK) {
*size = ;
return nullptr;
} return d.takeBuffer(size);
} unsigned char* FileUtils::getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size)
{
unsigned char * buffer = nullptr;
unzFile file = nullptr;
*size = ; do
{
CC_BREAK_IF(zipFilePath.empty()); file = unzOpen(FileUtils::getInstance()->getSuitableFOpen(zipFilePath).c_str());
CC_BREAK_IF(!file); // FIXME: Other platforms should use upstream minizip like mingw-w64
#ifdef MINIZIP_FROM_SYSTEM
int ret = unzLocateFile(file, filename.c_str(), NULL);
#else
int ret = unzLocateFile(file, filename.c_str(), );
#endif
CC_BREAK_IF(UNZ_OK != ret); char filePathA[];
unz_file_info fileInfo;
ret = unzGetCurrentFileInfo(file, &fileInfo, filePathA, sizeof(filePathA), nullptr, , nullptr, );
CC_BREAK_IF(UNZ_OK != ret); ret = unzOpenCurrentFile(file);
CC_BREAK_IF(UNZ_OK != ret); buffer = (unsigned char*)malloc(fileInfo.uncompressed_size);
int CC_UNUSED readedSize = unzReadCurrentFile(file, buffer, static_cast<unsigned>(fileInfo.uncompressed_size));
CCASSERT(readedSize == || readedSize == (int)fileInfo.uncompressed_size, "the file size is wrong"); *size = fileInfo.uncompressed_size;
unzCloseCurrentFile(file);
} while (); if (file)
{
unzClose(file);
} return buffer;
} std::string FileUtils::getNewFilename(const std::string &filename) const
{
std::string newFileName; // in Lookup Filename dictionary ?
auto iter = _filenameLookupDict.find(filename); if (iter == _filenameLookupDict.end())
{
newFileName = filename;
}
else
{
newFileName = iter->second.asString();
}
return newFileName;
} std::string FileUtils::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath) const
{
std::string file = filename;
std::string file_path = "";
size_t pos = filename.find_last_of("/");
if (pos != std::string::npos)
{
file_path = filename.substr(, pos+);
file = filename.substr(pos+);
} // searchPath + file_path + resourceDirectory
std::string path = searchPath;
path += file_path;
path += resolutionDirectory; path = getFullPathForDirectoryAndFilename(path, file); return path;
} std::string FileUtils::fullPathForFilename(const std::string &filename) const
{
if (filename.empty())
{
return "";
} if (isAbsolutePath(filename))
{
return filename;
} // Already Cached ?
auto cacheIter = _fullPathCache.find(filename);
if(cacheIter != _fullPathCache.end())
{
return cacheIter->second;
} // Get the new file name.
const std::string newFilename( getNewFilename(filename) ); std::string fullpath; for (const auto& searchIt : _searchPathArray)
{
for (const auto& resolutionIt : _searchResolutionsOrderArray)
{
fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt); if (!fullpath.empty())
{
// Using the filename passed in as key.
_fullPathCache.insert(std::make_pair(filename, fullpath));
return fullpath;
} }
} if(isPopupNotify()){
CCLOG("cocos2d: fullPathForFilename: No file found at %s. Possible missing file.", filename.c_str());
} // The file wasn't found, return empty string.
return "";
} std::string FileUtils::fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile)
{
return relativeFile.substr(, relativeFile.rfind('/')+) + getNewFilename(filename);
} void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
{
bool existDefault = false;
_fullPathCache.clear();
_searchResolutionsOrderArray.clear();
for(const auto& iter : searchResolutionsOrder)
{
std::string resolutionDirectory = iter;
if (!existDefault && resolutionDirectory == "")
{
existDefault = true;
} if (resolutionDirectory.length() > && resolutionDirectory[resolutionDirectory.length()-] != '/')
{
resolutionDirectory += "/";
} _searchResolutionsOrderArray.push_back(resolutionDirectory);
} if (!existDefault)
{
_searchResolutionsOrderArray.push_back("");
}
} void FileUtils::addSearchResolutionsOrder(const std::string &order,const bool front)
{
std::string resOrder = order;
if (!resOrder.empty() && resOrder[resOrder.length()-] != '/')
resOrder.append("/"); if (front) {
_searchResolutionsOrderArray.insert(_searchResolutionsOrderArray.begin(), resOrder);
} else {
_searchResolutionsOrderArray.push_back(resOrder);
}
} const std::vector<std::string>& FileUtils::getSearchResolutionsOrder() const
{
return _searchResolutionsOrderArray;
} const std::vector<std::string>& FileUtils::getSearchPaths() const
{
return _searchPathArray;
} void FileUtils::setWritablePath(const std::string& writablePath)
{
_writablePath = writablePath;
} void FileUtils::setDefaultResourceRootPath(const std::string& path)
{
_defaultResRootPath = path;
} void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
{
bool existDefaultRootPath = false; _fullPathCache.clear();
_searchPathArray.clear();
for (const auto& iter : searchPaths)
{
std::string prefix;
std::string path; if (!isAbsolutePath(iter))
{ // Not an absolute path
prefix = _defaultResRootPath;
}
path = prefix + (iter);
if (!path.empty() && path[path.length()-] != '/')
{
path += "/";
}
if (!existDefaultRootPath && path == _defaultResRootPath)
{
existDefaultRootPath = true;
}
_searchPathArray.push_back(path);
} if (!existDefaultRootPath)
{
//CCLOG("Default root path doesn't exist, adding it.");
_searchPathArray.push_back(_defaultResRootPath);
}
} void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
{
std::string prefix;
if (!isAbsolutePath(searchpath))
prefix = _defaultResRootPath; std::string path = prefix + searchpath;
if (!path.empty() && path[path.length()-] != '/')
{
path += "/";
}
if (front) {
_searchPathArray.insert(_searchPathArray.begin(), path);
} else {
_searchPathArray.push_back(path);
}
} void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
{
_fullPathCache.clear();
_filenameLookupDict = filenameLookupDict;
} void FileUtils::loadFilenameLookupDictionaryFromFile(const std::string &filename)
{
const std::string fullPath = fullPathForFilename(filename);
if (!fullPath.empty())
{
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
if (!dict.empty())
{
ValueMap& metadata = dict["metadata"].asValueMap();
int version = metadata["version"].asInt();
if (version != )
{
CCLOG("cocos2d: ERROR: Invalid filenameLookup dictionary version: %d. Filename: %s", version, filename.c_str());
return;
}
setFilenameLookupDictionary( dict["filenames"].asValueMap());
}
}
} std::string FileUtils::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const
{
// get directory+filename, safely adding '/' as necessary
std::string ret = directory;
if (directory.size() && directory[directory.size()-] != '/'){
ret += '/';
}
ret += filename; // if the file doesn't exist, return an empty string
if (!isFileExistInternal(ret)) {
ret = "";
}
return ret;
} bool FileUtils::isFileExist(const std::string& filename) const
{
if (isAbsolutePath(filename))
{
return isFileExistInternal(filename);
}
else
{
std::string fullpath = fullPathForFilename(filename);
if (fullpath.empty())
return false;
else
return true;
}
} bool FileUtils::isAbsolutePath(const std::string& path) const
{
return (path[] == '/');
} bool FileUtils::isDirectoryExist(const std::string& dirPath) const
{
CCASSERT(!dirPath.empty(), "Invalid path"); if (isAbsolutePath(dirPath))
{
return isDirectoryExistInternal(dirPath);
} // Already Cached ?
auto cacheIter = _fullPathCache.find(dirPath);
if( cacheIter != _fullPathCache.end() )
{
return isDirectoryExistInternal(cacheIter->second);
} std::string fullpath;
for (const auto& searchIt : _searchPathArray)
{
for (const auto& resolutionIt : _searchResolutionsOrderArray)
{
// searchPath + file_path + resourceDirectory
fullpath = fullPathForFilename(searchIt + dirPath + resolutionIt);
if (isDirectoryExistInternal(fullpath))
{
_fullPathCache.insert(std::make_pair(dirPath, fullpath));
return true;
}
}
}
return false;
} std::vector<std::string> FileUtils::listFiles(const std::string& dirPath) const
{
std::string fullpath = fullPathForFilename(dirPath);
std::vector<std::string> files;
if (isDirectoryExist(fullpath))
{
tinydir_dir dir;
#ifdef UNICODE
unsigned int length = MultiByteToWideChar(CP_UTF8, , &fullpath[], (int)fullpath.size(), NULL, );
if (length != fullpath.size())
{
return files;
}
std::wstring fullpathstr(length, );
MultiByteToWideChar(CP_UTF8, , &fullpath[], (int)fullpath.size(), &fullpathstr[], length);
#else
std::string fullpathstr = fullpath;
#endif
if (tinydir_open(&dir, &fullpathstr[]) != -)
{
while (dir.has_next)
{
tinydir_file file;
if (tinydir_readfile(&dir, &file) == -)
{
// Error getting file
break;
} #ifdef UNICODE
std::wstring path = file.path;
length = WideCharToMultiByte(CP_UTF8, , &path[], (int)path.size(), NULL, , NULL, NULL);
std::string filepath;
if (length > )
{
filepath.resize(length);
WideCharToMultiByte(CP_UTF8, , &path[], (int)path.size(), &filepath[], length, NULL, NULL);
}
#else
std::string filepath = file.path;
#endif
if (file.is_dir)
{
filepath.append("/");
}
files.push_back(filepath); if (tinydir_next(&dir) == -)
{
// Error getting next file
break;
}
}
}
tinydir_close(&dir);
}
return files;
} void FileUtils::listFilesRecursively(const std::string& dirPath, std::vector<std::string> *files) const
{
std::string fullpath = fullPathForFilename(dirPath);
if (isDirectoryExist(fullpath))
{
tinydir_dir dir;
#ifdef UNICODE
unsigned int length = MultiByteToWideChar(CP_UTF8, , &fullpath[], (int)fullpath.size(), NULL, );
if (length != fullpath.size())
{
return;
}
std::wstring fullpathstr(length, );
MultiByteToWideChar(CP_UTF8, , &fullpath[], (int)fullpath.size(), &fullpathstr[], length);
#else
std::string fullpathstr = fullpath;
#endif
if (tinydir_open(&dir, &fullpathstr[]) != -)
{
while (dir.has_next)
{
tinydir_file file;
if (tinydir_readfile(&dir, &file) == -)
{
// Error getting file
break;
} #ifdef UNICODE
std::wstring path = file.path;
length = WideCharToMultiByte(CP_UTF8, , &path[], (int)path.size(), NULL, , NULL, NULL);
std::string filepath;
if (length > )
{
filepath.resize(length);
WideCharToMultiByte(CP_UTF8, , &path[], (int)path.size(), &filepath[], length, NULL, NULL);
}
#else
std::string filepath = file.path;
#endif
if (file.name[] != '.')
{
if (file.is_dir)
{
filepath.append("/");
files->push_back(filepath);
listFilesRecursively(filepath, files);
}
else
{
files->push_back(filepath);
}
} if (tinydir_next(&dir) == -)
{
// Error getting next file
break;
}
}
}
tinydir_close(&dir);
}
} #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
// windows os implement should override in platform specific FileUtiles class 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;
} bool FileUtils::renameFile(const std::string &oldfullpath, const std::string& newfullpath)
{
CCASSERT(false, "FileUtils not support renameFile");
return false;
} 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;
} long FileUtils::getFileSize(const std::string &filepath)
{
CCASSERT(false, "getFileSize should be override by platform FileUtils");
return ;
} #else
// default implements for unix like os
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>
// android doesn't have ftw.h
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
#include <ftw.h>
#endif
bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const
{
struct stat st;
if (stat(dirPath.c_str(), &st) == )
{
return S_ISDIR(st.st_mode);
}
return false;
} bool FileUtils::createDirectory(const std::string& path)
{
CCASSERT(!path.empty(), "Invalid path"); if (isDirectoryExist(path))
return true; // Split the path
size_t start = ;
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 + );
if (!subpath.empty())
dirs.push_back(subpath);
start = found+;
found = path.find_first_of("/\\", start);
if (found == std::string::npos)
{
if (start < path.length())
{
dirs.push_back(path.substr(start));
}
break;
}
}
} DIR *dir = NULL; // Create path recursively
subpath = "";
for (int i = ; i < dirs.size(); ++i)
{
subpath += dirs[i];
dir = opendir(subpath.c_str()); if (!dir)
{
// directory doesn't exist, should create a new one int ret = mkdir(subpath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
if (ret != && (errno != EEXIST))
{
// current directory can not be created, sub directories can not be created too
// should return
return false;
}
}
else
{
// directory exists, should close opened dir
closedir(dir);
}
}
return true;
}
namespace
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
int rv = remove(fpath); if (rv)
perror(fpath); return rv;
}
#endif
}
bool FileUtils::removeDirectory(const std::string& path)
{
#if !defined(CC_TARGET_OS_TVOS) #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
if (nftw(path.c_str(), unlink_cb, , FTW_DEPTH | FTW_PHYS) == -)
return false;
else
return true;
#else
std::string command = "rm -r ";
// Path may include space.
command += "\"" + path + "\"";
if (system(command.c_str()) >= )
return true;
else
return false;
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
#else
return false;
#endif
} bool FileUtils::removeFile(const std::string &path)
{
if (remove(path.c_str())) {
return false;
} else {
return true;
}
} bool FileUtils::renameFile(const std::string &oldfullpath, const std::string &newfullpath)
{
CCASSERT(!oldfullpath.empty(), "Invalid path");
CCASSERT(!newfullpath.empty(), "Invalid path"); int errorCode = rename(oldfullpath.c_str(), newfullpath.c_str()); if ( != errorCode)
{
CCLOGERROR("Fail to rename file %s to %s !Error code is %d", oldfullpath.c_str(), newfullpath.c_str(), errorCode);
return false;
}
return true;
} 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);
} std::string FileUtils::getSuitableFOpen(const std::string& filenameUtf8) const
{
return filenameUtf8;
} long FileUtils::getFileSize(const std::string &filepath)
{
CCASSERT(!filepath.empty(), "Invalid path"); std::string fullpath = filepath;
if (!isAbsolutePath(filepath))
{
fullpath = fullPathForFilename(filepath);
if (fullpath.empty())
return ;
} struct stat info;
// Get data associated with "crt_stat.c":
int result = stat(fullpath.c_str(), &info); // Check if statistics are valid:
if (result != )
{
// Failed
return -;
}
else
{
return (long)(info.st_size);
}
}
#endif //////////////////////////////////////////////////////////////////////////
// Notification support when getFileData from invalid file path.
//////////////////////////////////////////////////////////////////////////
static bool s_popupNotify = true; void FileUtils::setPopupNotify(bool notify)
{
s_popupNotify = notify;
} bool FileUtils::isPopupNotify() const
{
return s_popupNotify;
} 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;
} void FileUtils::valueMapCompact(ValueMap &valueMap)
{
} void FileUtils::valueVectorCompact(ValueVector &valueVector)
{
} NS_CC_END
Call to unavailable function 'system': not available on iOS的更多相关文章
- call to unavailable function system not available on ios 解决方案
编译时报错:call to unavailable function system not available on iOS 原因:iOS11已经将system删除 解决方案:system(comma ...
- PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令
exec 或者 system 都可以调用cmd 的命令 直接上代码: <?php /** 打开windows的计算器 */ exec('start C:WindowsSystem32calc.e ...
- PostgreSQL Reading Ad Writing Files、Execution System Instructions Vul
catalog . postgresql简介 . 文件读取/写入 . 命令执行 . 影响范围 . 恶意代码分析 . 缓解方案 1. postgresql简介 PostgreSQL 是一个自由的对象-关 ...
- Function, Predicate
Function, Predicate的使用 Function用于把一种类型的对象转化为另一种类型的对象.Predicate用于判断某个对象是否符合一定条件. 一.Function和Functions ...
- PHP执行系统外部命令函数:exec()、passthru()、system()、shell_exec()
php提供4种方法执行系统外部命令:exec().passthru().system(). shell_exec().在开始介绍前,先检查下php配置文件php.ini中是有禁止这是个函数.找到 di ...
- Python调用外部程序——os.system()和subprocess.call
通过os.system函数调用其他程序 预备知识:cmd中打开和关闭程序 cmd中打开程序 a.打开系统自带程序 系统自带的程序的路径一般都已加入环境变量之中,只需在cmd窗口中直接输入程序名称即可. ...
- python os.system()和os.popen()
1>python调用Shell脚本,有两种方法:os.system()和os.popen(),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容.>>>hel ...
- HttpWebRequest: Remote server returns error 503 Server Unavailable
I have a client server application written in C# .Net 2.0. I have had the client/server response/r ...
- 17_常用API_第17天(包装类、System、Math、Arrays、大数据运算)_讲义
今日内容介绍 1.基本类型包装类 2.System类 3.Math类 4.Arrays类 5.大数据运算 01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 ...
随机推荐
- Python数据结构:列表、元组和字典
在Python中有三种内建的数据结构——列表list.元组tuple和字典dict 列表中的项目包括在方括号中,项目之间用逗号分割 元组和列表十分类似,只不过元组和字符串一样是不可变的 即你不能修改元 ...
- Windows Thin PC体验 & 语言包更改(win 7 included)
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 简介: Window ...
- Chrome自带恐龙小游戏的源码研究(四)
在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高. ...
- php html_entity_decode使用总结
在处理网页字符串的时候,尤其是做爬虫类的应用时,经常会涉及到要处理的字符串中包含html标签,现在对这类字符串的处理做一个小的总结: 有时候获取到的字符串中有html标签,在入库的时候出于安全的考虑通 ...
- 开源监控软件ganglia
开源监控软件ganglia安装手册 Ganglia是一个监控服务器,集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等 ...
- react build和server start
先到项目目录build项目 npm run build 项目会打包到dist文件夹下 index.html和index.js等 react的项目build后不能直接访问的问题 先执行 npm inst ...
- IDEA下使用Jetty进行Debug模式调试
过程例如以下: (1)找到选项卡中的 –Run– 然后找到 –Edit Configurations (2)点击下图中绿色的plus–找到Maven点进去 (3)依照下边的方式在Command lin ...
- linux jdk 安装另一种方法
linux 7.2 安装 jdk方法: (1). 以root用户登录linux系统, 应用Xmanager把文件拷贝到linux 系统的根目录. (2). 进入opt/software建立jvm文件夹 ...
- rtmp直播拉流客户端EasyRTMPClient设计过程中时间戳问题汇总
EasyRTMPClient 简介 EasyRTMPClient是EasyDarwin流媒体团队开发.提供的一套非常稳定.易用.支持重连接的RTMPClient工具,以SDK形式提供,接口调用非常简单 ...
- sql server charindex函数和patindex函数详解(转)
charindex和patindex函数常常用来在一段字符中搜索字符或字符串.假如被搜索的字符中包含有要搜索的字符,那么这两个函数返回一个非零的整数,这个整数是要搜索的字符在被搜索的字符中的开始位数. ...