Cocos2dx基础 | [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类

前端之家收集整理的这篇文章主要介绍了Cocos2dx基础 | [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面看一下Data类

01@H_404_16@ classCC_DLL Data@H_404_16@
02@H_404_16@ @H_301_34@ {@H_404_16@
03@H_404_16@ public:@H_404_16@
04@H_404_16@ @H_301_34@ staticconstData Null;@H_404_16@
05@H_404_16@ @H_301_34@//构造函数@H_404_16@
06@H_404_16@ @H_301_34@Data();@H_404_16@
07@H_404_16@ @H_301_34@Data(Data& other);@H_404_16@
08@H_404_16@ @H_301_34@Data(Data&& other);@H_404_16@
09@H_404_16@ @H_301_34@~Data();@H_404_16@
10@H_404_16@ // 重载符号@H_404_16@
11@H_404_16@ @H_301_34@Data& operator= (12@H_404_16@ @H_301_34@Data& operator= (Data&& other);@H_404_16@
13@H_404_16@ @H_301_34@@H_404_16@
14@H_404_16@ @H_301_34@unsignedchar* getBytes();//获取数据@H_404_16@
15@H_404_16@ @H_301_34@ssize_t getSize()//尺寸@H_404_16@
16@H_404_16@ voidcopy(unsigned* bytes,ssize_t size);//从bytes复制@H_404_16@
17@H_404_16@ @H_301_34@fastSet(unsigned//从bytes快速set,使用后bytes将不能在外部使用@H_404_16@
18@H_404_16@ @H_301_34@clear();//清除@H_404_16@
19@H_404_16@ @H_301_34@boolisNull()//判空@H_404_16@
20@H_404_16@ private:@H_404_16@
21@H_404_16@ @H_301_34@move(Data& other);@H_404_16@
22@H_404_16@ 23@H_404_16@ @H_301_34@* _bytes;@H_404_16@
24@H_404_16@ @H_301_34@ssize_t _size;@H_404_16@
25@H_404_16@ @H_301_34@};@H_404_16@

unsigned char* getFileDataFromZip(const std::string& zipFilePath,const std::string& filename,ssize_t *size);//读取压缩文件数据(zip格式)

如果读取成功size中会返回文件的大小,否则返回0。

std::string fullPathForFilename(const std::string &filename);//获取文件的完整路径

如果我们通过setSearchPaths()设置搜索路径("/mnt/sdcard/","internal_dir/"),然后通过setSearchResolutionsOrder()设置子区分路径("resources-ipadhd/","resources-ipad/","resources-iphonehd")。如果搜索文件名为'sprite.png' 那么会先在文件查找字典中查找key: sprite.png -> value: sprite.pvr.gz,然后搜索文件'sprite.pvr.gz'如下顺序:

1@H_404_16@ @H_301_34@/mnt/sdcard/resources-ipadhd/sprite.pvr.gz (ifnot found,search next)@H_404_16@
2@H_404_16@ @H_301_34@/mnt/sdcard/resources-ipad/sprite.pvr.gz (404_16@
3@H_404_16@ @H_301_34@/mnt/sdcard/resources-iphonehd/sprite.pvr.gz (4@H_404_16@ @H_301_34@/mnt/sdcard/sprite.pvr.gz (5@H_404_16@ @H_301_34@internal_dir/resources-ipadhd/sprite.pvr.gz (6@H_404_16@ @H_301_34@internal_dir/resources-ipad/sprite.pvr.gz (7@H_404_16@ @H_301_34@internal_dir/resources-iphonehd/sprite.pvr.gz (8@H_404_16@ @H_301_34@internal_dir/sprite.pvr.gz (return"sprite.png")@H_404_16@

如果找到返回完整路径,没找到返回'sprite.png'。

void loadFilenameLookupDictionaryFromFile(const std::string &filename);//从文件导入文件名查找字典

文件为plist格式如下:

<?xml version="1.0"encoding="UTF-8"?>@H_404_16@
<!DOCTYPE plist PUBLIC"-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">@H_404_16@
<plist version=>@H_404_16@
<dict>@H_404_16@
<key>filenames</key>@H_404_16@
<key>sounds/click.wav</key>@H_404_16@
<string>sounds/click.caf</string>@H_404_16@
<key>sounds/endgame.wav</key>@H_404_16@
<string>sounds/endgame.caf</string>@H_404_16@
<key>sounds/gem-0.wav</key>@H_404_16@
<string>sounds/gem-0.caf</string>@H_404_16@
</dict>@H_404_16@
<key>Metadata</key>@H_404_16@
<dict>@H_404_16@
<key>version</key>@H_404_16@
<integer>1</integer>@H_404_16@
</dict>@H_404_16@
</plist>@H_404_16@

key对应string

void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);//从ValueMap中设置文件名查找字典

ValueMap的定义:

typedefstd::unordered_map<std::string,Value> ValueMap;@H_404_16@

std::string fullPathFromRelativeFile(const std::string &filename,const std::string &relativeFile);//获取相对应文件的完整路径

e.g. filename: hello.png,pszRelativeFile: /User/path1/path2/hello.plist Return: /User/path1/path2/hello.pvr (If there a a key(hello.png)-value(hello.pvr) in FilenameLookup dictionary. )

void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);//设置子搜索区分路径

见fullPathForFilename()。

void addSearchResolutionsOrder(const std::string &order);//增加搜索路径

const std::vector<std::string>& getSearchResolutionsOrder();//获取搜索区分路径

void setSearchPaths(const std::vector<std::string>& searchPaths);//设置搜索路径

void addSearchPath(const std::string & path);//增加搜索路径

const std::vector<std::string>& getSearchPaths() const;//获取搜索路径

std::string getWritablePath();//获取一个可写入文件的路径

经过测试在win32平台上,debug版本返回的是程序文件所在的路径,release返回的是“我的文档”路径。

bool isFileExist(const std::string& filePath);//判断文件是否存在

经过测试在win32平台上,如果路径中包含中文字符会找不到文件。所以可以自己写个

wFileIO::isFileExist(std::string& pFileName)@H_404_16@
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)@H_404_16@
returnCCFileUtils::getInstance()->isFileExist(pFileName);@H_404_16@
#else@H_404_16@
(GetFileAttributesA(pFileName.c_str()) == INVALID_FILE_ATTRIBUTES)@H_404_16@
returnfalse;@H_404_16@
true;@H_404_16@
#endif@H_404_16@
}@H_404_16@

bool isAbsolutePath(const std::string& path);判断是否为绝对路径


void setPopupNotify(bool notify);

bool isPopupNotify();

Sets/Gets 当文件加载失败时弹出messageBox.

ValueMap getValueMapFromFile(const std::string& filename);//从文件获取ValueMap

bool writeToFile(ValueMap& dict,const std::string& fullPath);//写入一个ValueMap数据到plist格式文件

ValueVector getValueVectorFromFile(const std::string& filename);//从文件获取ValueVector

ValueVector定义:

std::vector<Value> ValueVector;@H_404_16@

函数就这么多了,就在这里记录下,到时要用再来看看奋斗

因为没发现有直接写文件函数,所以我这里自己写了下,虽然不知道再其他平台会怎样,再windows上用着再说大笑

再win32上realse版本getWritablePath()会获取“我的文档”,还是改成当前路径吧

std::string wFileIO::getWritablePath()@H_404_16@
CCFileUtils::getInstance()->getWritablePath();@H_404_16@
full_path[MAX_PATH + 1];@H_404_16@
::GetModuleFileNameA(NULL,full_path,MAX_PATH + 1);@H_404_16@
std::string ret((*)full_path);@H_404_16@
// remove xxx.exe@H_404_16@
ret = ret.substr(0,ret.rfind("\\") + 1);@H_404_16@
ret = convertPathFormatToUnixStyle(ret);@H_404_16@
ret;@H_404_16@
下面是保存文件:

wFileIO::saveFile(const* pContentString,monospace!important; font-size:10pt!important; min-height:inherit!important">std::string fn=convertPathFormatToUnixStyle(pFileName);@H_404_16@
intnp=fn.rfind('/');@H_404_16@
(np!=std::string::npos)@H_404_16@
(!mkDirM(fn.substr(0,np)))@H_404_16@
@H_404_16@
std::string path = getWritablePath()+fn;@H_404_16@
FILE* file =fopen(path.c_str(),"w"(file)@H_404_16@
fputs(pContentString,file);@H_404_16@
fclose(file);@H_404_16@
log("save file [%s]",path.c_str());@H_404_16@
}@H_404_16@
else@H_404_16@
"fail to save file [%s]!" //检测各级文件夹,不存在则创建@H_404_16@
wFileIO::mkDirM(std::string& pDirName)@H_404_16@
std::string path = getWritablePath();@H_404_16@
26@H_404_16@ @H_301_34@np=pDirName.find(404_16@
27@H_404_16@ while28@H_404_16@ 29@H_404_16@ @H_301_34@(!mkDir(path+pDirName.substr(0,np)))@H_404_16@
30@H_404_16@ 31@H_404_16@ 404_16@
32@H_404_16@ @H_301_34@}@H_404_16@
33@H_404_16@ @H_301_34@mkDir(path+pDirName);@H_404_16@
34@H_404_16@ 35@H_404_16@ 36@H_404_16@ //创建文件夹@H_404_16@
37@H_404_16@ @H_301_34@wFileIO::mkDir(38@H_404_16@ 39@H_404_16@ 40@H_404_16@ @H_301_34@DIR *pDir = NULL;@H_404_16@
41@H_404_16@ //打开该路径@H_404_16@
42@H_404_16@ @H_301_34@pDir = opendir (pDirName.c_str());@H_404_16@
43@H_404_16@ @H_301_34@(! pDir)@H_404_16@
44@H_404_16@ 45@H_404_16@ //创建该路径@H_404_16@
46@H_404_16@ @H_301_34@(!mkdir(pDirName.c_str(),S_IRWXU | S_IRWXG | S_IRWXO))@H_404_16@
47@H_404_16@ @H_301_34@{@H_404_16@
48@H_404_16@ "fail to create dir [%s]"404_16@
49@H_404_16@ 50@H_404_16@ 51@H_404_16@ "create dir [%s]"404_16@
52@H_404_16@ 53@H_404_16@ 54@H_404_16@ @H_301_34@((GetFileAttributesA(pDirName.c_str())) == INVALID_FILE_ATTRIBUTES)@H_404_16@
55@H_404_16@ 56@H_404_16@ @H_301_34@(!CreateDirectoryA(pDirName.c_str(),0))@H_404_16@
57@H_404_16@ 58@H_404_16@ 59@H_404_16@ 60@H_404_16@ 61@H_404_16@ 62@H_404_16@ 63@H_404_16@ 64@H_404_16@ 65@H_404_16@ 66@H_404_16@ 67@H_404_16@ //路径格式转为UnixStyle,"c:\xxx.txt" --> "c:/xxx.txt"@H_404_16@
68@H_404_16@ inlinestd::string convertPathFormatToUnixStyle(std::string& path)@H_404_16@
69@H_404_16@ 70@H_404_16@ @H_301_34@std::string ret = path;len = ret.length();@H_404_16@
71@H_404_16@ for(i = 0; i < len; ++i)@H_404_16@
72@H_404_16@ 73@H_404_16@ @H_301_34@(ret[i] =='\\')@H_404_16@
74@H_404_16@ 75@H_404_16@ @H_301_34@ret[i] =76@H_404_16@ 77@H_404_16@ 78@H_404_16@ 79@H_404_16@ @H_301_34@}@H_404_16@

猜你在找的Cocos2d-x相关文章