Skip to content

Commit

Permalink
Added Lua wrapper of the function dtree_next()
Browse files Browse the repository at this point in the history
It constructs a new Lua table for each device.
  • Loading branch information
Jan Viktorin committed Jun 12, 2012
1 parent 5fb7673 commit d4ac6d7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lua_dtree.c
Expand Up @@ -69,6 +69,49 @@ int l_dtree_close(lua_State *l)
return 0;
}

static
int l_dtree_next(lua_State *l) {
if(state == L_DTREE_CLOSE)
luaL_error(l, "Invalid call to dtree.next(), call dtree.open() first");

struct dtree_dev_t *dev = dtree_next();
if(dev == NULL) {
if(dtree_iserror())
luaL_error(l, dtree_errstr());

lua_pushboolean(l, 0);
return 1;
}

lua_createtable(l, 0, 4);

lua_pushstring(l, "name");
lua_pushstring(l, dtree_dev_name(dev));
lua_rawset(l, -3);

lua_pushstring(l, "base");
lua_pushinteger(l, dtree_dev_base(dev));
lua_rawset(l, -3);

lua_pushstring(l, "high");
lua_pushinteger(l, dtree_dev_high(dev));
lua_rawset(l, -3);

lua_pushstring(l, "compat");
lua_newtable(l);
const char **compat = dtree_dev_compat(dev);
int i;

for(i = 0; compat[i] != NULL; ++i) {
lua_pushstring(l, compat[i]);
lua_rawseti(l, -2, i);
}

lua_rawset(l, -3);

dtree_dev_free(dev);
return 1;
}

//
// Register in Lua
Expand All @@ -79,6 +122,7 @@ struct luaL_reg dtreelib[] =
{
{"open", l_dtree_open},
{"close", l_dtree_close},
{"next", l_dtree_next},
{NULL, NULL}
};

Expand Down

0 comments on commit d4ac6d7

Please sign in to comment.