1

是否可以在 squirrel 中检查目录的内容?我需要给定目录及其子目录中的文件名列表,包括它们的路径。

我正在编写一个用于 Code::Blocks 的脚本,它使用 squirrel 作为脚本语言。我查看了松鼠标准库,但找不到任何与文件相关的操作。也可以将此任务外包给外部脚本(bash 或其他),但我不希望这样做。

4

2 回答 2

1
  1. 下载 tinydir 表格:https ://github.com/cxong/tinydir

  2. 为松鼠添加系统api:

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. 测试结果:

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>

于 2017-06-06T14:18:15.780 回答
1

Squirrel 没有任何内置的 I/O 功能。您必须在 C++ 端编写一个并将该函数公开给 Squirrel。

于 2015-09-12T21:51:26.040 回答