cocos从服务器读取文件夹,cocos3——5.js获取文件夹下文件列表

1.C++:

#include

#include

#include

#include

#include "cocos2d.h"

#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32

#include

#include

#else

#include

#endif

namespace fs

{

int readDir( const char *path, vector &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 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://blog.csdn.net/adfansong/article/details/42611449

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值