1.C++:
#include <iostream>#include <fstream>#include <vector>#include <string>#include "cocos2d.h"#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32#include <windows.h>#include <strsafe.h>#else#include <dirent.h>#endifnamespace fs { int readDir( const char *path,vector<string> &names ) { names.clear();#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 WIN32_FIND_DATAA ffd; //LARGE_INTEGER filesize; string szDir; //size_t length_of_arg; HANDLE hFind = INVALID_HANDLE_VALUE; DWORD DWError=0; string strDir = path; szDir = strDir + "\*"; hFind = FindFirstfileA(szDir.c_str(),&ffd); if (INVALID_HANDLE_VALUE == hFind) { //cclOGWARN("get file name error",path); return 0; } do { if (!(ffd.DWfileAttributes & file_ATTRIBUTE_DIRECTORY)) { string filename=ffd.cfilename;//(const char*) //string filedir=strDir+"\"+filename; string filedir = filename; names.push_back(filedir); } }while (FindNextfileA(hFind,&ffd) != 0); DWError = GetLastError(); if (DWError != ERROR_NO_MORE_fileS) { //cclOGWARN("FindFirstfile error",path); return 0; } FindClose(hFind);#else DIR *dp = opendir(path); if (!dp) { //cclOGWARN("open dir error: %s.",path); return 0; } struct dirent *dirp = readdir(dp); while (dirp) { if (!strcmp(dirp->d_name,".") || !strcmp(dirp->d_name,"..")) continue; names.push_back(dirp->d_name); } closedir(dp); sort(names.begin(),names.end());#endif return names.size(); }}
2.绑定Js:
bool Js_fs_readDir(jscontext *cx,uint32_t argc,Jsval *vp){ if (argc == 1) { // get the native object from the second object to the first object Jsval *argv = Js_ARGV(cx,vp); string path; Jsval_to_std_string(cx,argv[0],&path); vector<string> names; fs::readDir(path.c_str(),names); Jsval Jsret = JsVAL_NulL; Jsret = std_vector_string_to_Jsval(cx,names); Js_SET_RVAL(cx,vp,Jsret); return true; } return false;}
3.Js调用:
var files = fs.readdir(full_path);PS: 这里要传全路径,貌似androID要把文件拷到可写路径去才能获取到文件列表。。 总结
以上是内存溢出为你收集整理的cocos3——5.js获取文件夹下文件列表全部内容,希望文章能够帮你解决cocos3——5.js获取文件夹下文件列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)