Recurse directory in squirrel

352 views Asked by At

Is it possible to examine the contents of a directory in squirrel? I need a list of file names, including their paths, in a given directory and its subdirectories.

I'm writing a script for use in Code::Blocks, which uses squirrel as a scripting language. I've had a look at the squirrel standard library, but couldn't find any file related operations. It might also be possible to outsource this task to an external script (bash or whatever), but I'd prefer not to do that.

2

There are 2 answers

0
Kelvin Shadewing On

Squirrel doesn't have any built-in I/O functions. You'll have to write one on the C++ side and expose that function to Squirrel.

0
Jimmy On
  1. Download tinydir form : https://github.com/cxong/tinydir

  2. Add system api for squrriel:

static SQInteger _system_getfiles(HSQUIRRELVM v)
{
    const SQChar *dirPath;
    sq_getstring(v, 2, &dirPath);
    sq_newarray(v,0);

    printf("Get dir %s;\r\n", dirPath);
    tinydir_dir dir;
    tinydir_open(&dir, dirPath);

    while (dir.has_next)
    {
        tinydir_file file;
        tinydir_readfile(&dir, &file);

//        printf("%s\r\n", file.name);
//        if (file.is_dir)
//        {
//            printf("/");
//        }
//        printf("\n");

        if (!file.is_dir)
        {
            sq_pushstring(v, file.name, -1);
            sq_arrayappend(v, -2);
        }

        tinydir_next(&dir);
    }

    tinydir_close(&dir);

//    sq_newarray(v,0);
//    sq_pushstring(v, "test_001.c", -1);
//    sq_arrayappend(v, -2);
//    sq_pushstring(v, "test_002.c", -1);
//    sq_arrayappend(v, -2);
//    sq_pushstring(v, "test_003.c", -1);
//    sq_arrayappend(v, -2);

    return 1;
}
#define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
static const SQRegFunction systemlib_funcs[]={
    _DECL_FUNC(getenv,2,_SC(".s")),
    _DECL_FUNC(system,2,_SC(".s")),
    _DECL_FUNC(clock,0,NULL),
    _DECL_FUNC(time,1,NULL),
    _DECL_FUNC(date,-1,_SC(".nn")),
    _DECL_FUNC(remove,2,_SC(".s")),
    _DECL_FUNC(rename,3,_SC(".ss")),
    _DECL_FUNC(getfiles,2,_SC(".s")),
    {NULL,(SQFUNCTION)0,0,NULL}
};
#undef _DECL_FUNC

  1. The test result:

sq>local files=getfiles("c:");foreach(file in files){print(file + "\r\n");}
Get dir c:;
devcon64.exe
espacePlugin.log
java14224.reg
MyProject.smp
ns_fp.ocx
pagefile.sys
ScanResult.log
test.log
test1.log
UpdateDocPermission.log
UpdateVMLog.txt
UserAgentData.log
UserData.log

sq>