博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2dx-2.x CCFileUtils文件管理类分析(3)
阅读量:4217 次
发布时间:2019-05-26

本文共 3111 字,大约阅读时间需要 10 分钟。

在2中,我们分析了几个函数,在这一篇中我们继续分析其他一些函数。1、在2中,多次用到了m_searchPathArray(搜索路径),那这个搜索路径怎么来的呢?我们可以通过setSearchPaths这个函数来设置搜索路径void CCFileUtils::setSearchPaths(const std::vector
& searchPaths){ bool bExistDefaultRootPath = false; //先把以前的清空,包括缓存路径 m_fullPathCache.clear(); m_searchPathArray.clear(); //逐个加入到m_searchPathArray for (std::vector
::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter) { std::string strPrefix; std::string path; //如果不是绝对路径,android的则加上"assets/"前缀,表明需要去安装包里找 if (!isAbsolutePath(*iter)) { // Not an absolute path strPrefix = m_strDefaultResRootPath; } //如果路径不是以'/'结尾,则在结尾加上'/' path = strPrefix+(*iter); if (path.length() > 0 && path[path.length()-1] != '/') { path += "/"; } if (!bExistDefaultRootPath && path == m_strDefaultResRootPath) { bExistDefaultRootPath = true; } m_searchPathArray.push_back(path); } if (!bExistDefaultRootPath) { //如果m_strDefaultResRootPath默认路径不在m_searchPathArray,则加入进来 //CCLOG("Default root path doesn't exist, adding it."); m_searchPathArray.push_back(m_strDefaultResRootPath); }}-->>void CCFileUtils::addSearchPath(const char* path_){ std::string strPrefix; std::string path(path_); if (!isAbsolutePath(path)) { // Not an absolute path strPrefix = m_strDefaultResRootPath; } path = strPrefix + path; if (path.length() > 0 && path[path.length()-1] != '/') { path += "/"; } m_searchPathArray.push_back(path);}//移除一个搜索路径:void CCFileUtils::removeSearchPath(const char *path_){ std::string strPrefix; std::string path(path_); if (!isAbsolutePath(path)) { // Not an absolute path strPrefix = m_strDefaultResRootPath; } path = strPrefix + path; if (path.length() > 0 && path[path.length()-1] != '/') { path += "/"; } std::vector
::iterator iter = std::find(m_searchPathArray.begin(), m_searchPathArray.end(), path); m_searchPathArray.erase(iter);}//移除全部void CCFileUtils::removeAllPaths(){ m_searchPathArray.clear();}2、m_searchResolutionsOrderArray资源路径和上面的一样处理方式,就不说了。3、从pszRelativeFile这个文件的相对路径中,得到pszFilename文件的全路径,其实就是找到pszRelativeFile文件的最后一个'/',然后去这个'/'前的所有字符 + pszFilename即可。const char* CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile){ std::string relativeFile = pszRelativeFile; CCString *pRet = CCString::create(""); pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1); pRet->m_sString += getNewFilename(pszFilename); return pRet->getCString();}4、//android下的可读写路径string CCFileUtilsAndroid::getWritablePath(){ // Fix for Nexus 10 (Android 4.2 multi-user environment) // the path is retrieved through Java Context.getCacheDir() method string dir(""); //pContext.getFilesDir().getAbsolutePath() java端 string tmp = getFileDirectoryJNI(); //pContext.getFilesDir().getAbsolutePath() if (tmp.length() > 0) { dir.append(tmp).append("/"); return dir; } else { return ""; }}

转载地址:http://dxsmi.baihongyu.com/

你可能感兴趣的文章
【屌丝程序的口才逆袭演讲稿50篇】第十二篇:世界上最快的捷径【张振华.Jack】
查看>>
Android中Java代码和XML布局效率问题
查看>>
android TextView属性大全(转)
查看>>
Conclusion for Resource Management
查看>>
Conclusion for Constructors,Destructors,and Assignment Operators
查看>>
Conclusion for Accustoming Yourself to C++
查看>>
面试题1:赋值运算函数(offer)
查看>>
Mark : MessagePack简介及使用
查看>>
Mark : hive文件存储格式
查看>>
mark : hadoop 四种压缩格式
查看>>
All Things OpenTSDB
查看>>
单例模式(singleton),工厂方法模式(factory),门面模式(facade)
查看>>
抽象模式,适配器模式(Adapter),模板方法模式(Template method)
查看>>
建造者模式(builder),桥梁模式(bridge mode),命令模式(Command mode)
查看>>
装饰模式(Decorator),迭代器模式(Iterator),组合模式(composite)
查看>>
观察者模式(Observer),责任链模式,访问者模式(Visitor)
查看>>
状态模式(State)
查看>>
快速排序
查看>>
插入算法
查看>>
希尔排序
查看>>