lua 获取指定目录下指定后缀文件名

lfs库是很好的选择,可惜不会编译,无奈只能自己写个简单的lua库。代码如下:

 1 #include <io.h>
 2 #include <stdio.h>
 3 
 4 #include "lua.h"
 5 #include "lauxlib.h"
 6 #include "lualib.h"
 7 
 8 static int
 9 DirFiles(lua_State *L){
10     long Handle;
11     struct _finddata_t FileInfo;
12     size_t l;
13     int index = 0;
14     const char* path = luaL_checklstring(L, 1, &l);
15     lua_newtable(L);
16     if ((Handle = _findfirst(path, &FileInfo)) != -1L)
17     {
18         lua_pushstring(L, FileInfo.name);
19         lua_seti(L, -2, ++index);
20         while(_findnext(Handle, &FileInfo) == 0)
21         {
22             lua_pushstring(L, FileInfo.name);
23             lua_seti(L, -2, ++index);
24         }
25         _findclose(Handle);
26     }
27     
28     return 1;
29 }
30 
31 int 
32 luaopen_lfs(lua_State *L){
33     luaL_checkversion(L);
34     
35     luaL_Reg methods[] = {
36         {"DirFiles", DirFiles},
37         {NULL, NULL}
38         };
39     luaL_newlib(L, methods);
40     
41     return 1;
42 }

简单编译成链接库供lua使用,lua调用方式如下:

1 local lfs = require "lfs"
2 local files = lfs.DirFiles("./*.c")

make这东西真的需要好好学学,经常会碰到git下来的项目没法编译,都是泪啊...推荐陈硕的博客跟我一起写Makefile

原文地址:https://www.cnblogs.com/ruofengzhishang/p/4585611.html