展会信息港展会大全

cocos3——5.js获取文件夹下文件列表,cocos35.js
来源:互联网   发布日期:2015-09-28 13:53:27   浏览:2172次  

导读: cocos3——5.js获取文件夹下文件列表,cocos35.js 1.C++: #include <iostream> #include <fstream> #include <vecto...

cocos3——5.js获取文件夹下文件列表,cocos35.js

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>

#endif

namespace 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要把文件拷到可写路径去才能获取到文件列表。。

http://www.bkjia.com/Androidjc/942205.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/942205.htmlTechArticlecocos3——5.js获取文件夹下文件列表,cocos35.js 1.C: #include iostream#include fstream#include vector#include string#include cocos2d.h#if CC_TARGET_PLATFORM == CC_PLA...

赞助本站

人工智能实验室

相关热词: android开发 应用开发

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港