-
Notifications
You must be signed in to change notification settings - Fork 0
/
recurkeys.c
53 lines (42 loc) · 1.01 KB
/
recurkeys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
typedef struct {
int c;
char r[100][100];
} recurse_res;
static void getkeys(lua_State *L, int idx, recurse_res *res) {
lua_pushnil(L);
while(lua_next(L, idx) != 0) {
if (lua_type(L, -2) == LUA_TSTRING) {
strcpy(res->r[res->c], lua_tostring(L, -2));
res->c++;
if (lua_type(L,-1) == LUA_TTABLE)
/*
* as the value is on -1 (the top) we pass recursively
* the positive position of the index on stack
*/
getkeys(L, lua_gettop(L), res);
}
lua_pop(L, 1);
}
}
static int example(lua_State *L) {
recurse_res res = { 0, {{0}}};
int i;
getkeys(L, 1, &res);
for (i=0; i<res.c; i++) lua_pushstring(L, res.r[i]);
return res.c;
}
int luaopen_capi_table_recurkeys(lua_State *L) {
luaL_Reg module[] = {
{ "example", example },
{ NULL, NULL }
};
#if ( LUA_VERSION_NUM < 502 )
luaL_register(L, "", module);
#else
luaL_newlib(L,module);
#endif
return 1;
}