Cocos3--5.js getting a list of files under a folder

Source: Internet
Author: User
Tags strcmp

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_win32win32_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 (SzD Ir.c_str (), &AMP;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);d Werror = GetLastError (); if (dwerror! = error_no_more_files) {Cclogwarn ("FindFirstFile error", path); return 0;} FindClose (hfind); #elseDIR *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 ()); #endifreturn names.size ();}}

2. Bind JS:

BOOL Js_fs_readdir (Jscontext *cx, uint32_t argc, Jsval *vp) {if (argc = = 1) {//Get the native object from the second objec T to the first objectjsval *ARGV = JS_ARGV (CX, VP); string path;jsval_to_std_string (CX, argv[0], &path); Vector<stri Ng> 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 Call:

var files = fs.readdir (Full_path);
PS: Here to pass the full path, it seems that Android to copy files to the writable path to get to the file list.

Cocos3--5.js getting a list of files under a folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.