From: Florian Forster Date: Sun, 28 Nov 2010 09:11:02 +0000 (+0100) Subject: src/utils_lua.[ch]: Implement functions to export C types to Lua. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=fec2a717231d2ace78e52a709f50ca0055a52736;p=collectd.git src/utils_lua.[ch]: Implement functions to export C types to Lua. --- diff --git a/src/utils_lua.c b/src/utils_lua.c index c01e5949..1c5f3e25 100644 --- a/src/utils_lua.c +++ b/src/utils_lua.c @@ -95,6 +95,53 @@ static int ltoc_table_values (lua_State *l, int idx, /* {{{ */ return (status); } /* }}} int ltoc_table_values */ +static int luaC_pushvalues (lua_State *l, const data_set_t *ds, const value_list_t *vl) /* {{{ */ +{ + int i; + + assert (vl->values_len == ds->ds_num); + + lua_newtable (l); + for (i = 0; i < vl->values_len; i++) + { + lua_pushinteger (l, (lua_Integer) i); + luaC_pushvalue (l, vl->values[i], ds->ds[i].type); + lua_settable (l, /* idx = */ -3); + } + + return (0); +} /* }}} int luaC_pushvalues */ + +static int luaC_pushdstypes (lua_State *l, const data_set_t *ds) /* {{{ */ +{ + int i; + + lua_newtable (l); + for (i = 0; i < ds->ds_num; i++) + { + lua_pushinteger (l, (lua_Integer) i); + lua_pushstring (l, DS_TYPE_TO_STRING (ds->ds[i].type)); + lua_settable (l, /* idx = */ -3); + } + + return (0); +} /* }}} int luaC_pushdstypes */ + +static int luaC_pushdsnames (lua_State *l, const data_set_t *ds) /* {{{ */ +{ + int i; + + lua_newtable (l); + for (i = 0; i < ds->ds_num; i++) + { + lua_pushinteger (l, (lua_Integer) i); + lua_pushstring (l, ds->ds[i].name); + lua_settable (l, /* idx = */ -3); + } + + return (0); +} /* }}} int luaC_pushdsnames */ + /* * Public functions */ @@ -236,4 +283,62 @@ value_list_t *luaC_tovaluelist (lua_State *l, int idx) /* {{{ */ return (vl); } /* }}} value_list_t *luaC_tovaluelist */ +int luaC_pushcdtime (lua_State *l, cdtime_t t) /* {{{ */ +{ + double d = CDTIME_T_TO_DOUBLE (t); + + lua_pushnumber (l, (lua_Number) d); + return (0); +} /* }}} int luaC_pushcdtime */ + +int luaC_pushvalue (lua_State *l, value_t v, int ds_type) /* {{{ */ +{ + if (ds_type == DS_TYPE_GAUGE) + lua_pushnumber (l, (lua_Number) v.gauge); + else if (ds_type == DS_TYPE_DERIVE) + lua_pushinteger (l, (lua_Integer) v.derive); + else if (ds_type == DS_TYPE_COUNTER) + lua_pushinteger (l, (lua_Integer) v.counter); + else if (ds_type == DS_TYPE_ABSOLUTE) + lua_pushinteger (l, (lua_Integer) v.absolute); + else + return (-1); + return (0); +} /* }}} int luaC_pushvalue */ + +int luaC_pushvaluelist (lua_State *l, const data_set_t *ds, const value_list_t *vl) /* {{{ */ +{ + lua_newtable (l); + + lua_pushstring (l, vl->host); + lua_setfield (l, /* idx = */ -2, "host"); + + lua_pushstring (l, vl->plugin); + lua_setfield (l, /* idx = */ -2, "plugin"); + lua_pushstring (l, vl->plugin_instance); + lua_setfield (l, /* idx = */ -2, "plugin_instance"); + + lua_pushstring (l, vl->type); + lua_setfield (l, /* idx = */ -2, "type"); + lua_pushstring (l, vl->type_instance); + lua_setfield (l, /* idx = */ -2, "type_instance"); + + luaC_pushvalues (l, ds, vl); + lua_setfield (l, /* idx = */ -2, "values"); + + luaC_pushdstypes (l, ds); + lua_setfield (l, /* idx = */ -2, "dstypes"); + + luaC_pushdsnames (l, ds); + lua_setfield (l, /* idx = */ -2, "dsnames"); + + luaC_pushcdtime (l, vl->time); + lua_setfield (l, /* idx = */ -2, "time"); + + luaC_pushcdtime (l, vl->interval); + lua_setfield (l, /* idx = */ -2, "interval"); + + return (0); +} /* }}} int luaC_pushvaluelist */ + /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_lua.h b/src/utils_lua.h index bb97d858..366dd0ff 100644 --- a/src/utils_lua.h +++ b/src/utils_lua.h @@ -35,5 +35,12 @@ int luaC_tostringbuffer (lua_State *l, int idx, char *buffer, size_t b value_t luaC_tovalue (lua_State *l, int idx, int ds_type); value_list_t *luaC_tovaluelist (lua_State *l, int idx); +/* + * push functions (C -> stack) + */ +int luaC_pushcdtime (lua_State *l, cdtime_t t); +int luaC_pushvalue (lua_State *l, value_t v, int ds_type); +int luaC_pushvaluelist (lua_State *l, const data_set_t *ds, const value_list_t *vl); + #endif /* UTILS_LUA_H */ /* vim: set sw=2 sts=2 et fdm=marker : */