#include "lua_volksdata.h" /* * Class methods. */ static int l_nsmap_add (lua_State *L) { const char *pfx = luaL_checkstring (L, 1), *nsstr = luaL_checkstring (L, 2); LUA_PCHECK ( VOLK_nsmap_add (pfx, nsstr), "Error adding member to NS map"); return 0; } static int l_nsmap_remove (lua_State *L) { const char *pfx = luaL_checkstring (L, 1); LUA_PCHECK ( VOLK_nsmap_remove (pfx), "Error removing member to NS map"); return 0; } static int l_nsmap_get_ns (lua_State *L) { const char *pfx = luaL_checkstring (L, 1); const char *ns = VOLK_nsmap_get_ns (pfx); if (ns) lua_pushstring (L, ns); else return 0; return 1; } static int l_nsmap_get_pfx (lua_State *L) { const char *ns = luaL_checkstring (L, 1); const char *pfx = VOLK_nsmap_get_pfx (ns); if (pfx) lua_pushstring (L, pfx); else lua_pushnil (L); return 1; } static int l_nsmap_normalize_uri (lua_State *L) { const char *pfx_uri = luaL_checkstring (L, 1); char *fq_uri; LUA_PCHECK ( VOLK_nsmap_normalize_uri (pfx_uri, &fq_uri), "Error normalizing URI"); if (fq_uri) lua_pushstring (L, fq_uri); else return 0; free (fq_uri); return 1; } static int l_nsmap_denormalize_uri (lua_State *L) { const char *fq_uri = luaL_checkstring (L, 1); char *pfx_uri; LUA_PCHECK ( VOLK_nsmap_denormalize_uri (fq_uri, &pfx_uri), "Error denormalizing URI"); if (pfx_uri) lua_pushstring (L, pfx_uri); else return 0; free (pfx_uri); return 1; } static int nsmap_iter_next (lua_State *L) { const char ****data_p = lua_touserdata (L, lua_upvalueindex (1)); const char ***data = *data_p; size_t *i = lua_touserdata (L, lua_upvalueindex (2)); if ((data)[*i]) { lua_pushstring (L, data[*i][0]); lua_pushstring (L, data[*i][1]); (*i)++; return 2; } else { free (data); *data_p = NULL; free (i); return 0; } } static int l_nsmap_iter (lua_State *L) { const char ****data_p = lua_newuserdata (L, sizeof (*data_p)); //luaL_getmetatable (L, "VOLK.String2DIterator"); //lua_setmetatable (L, -2); *data_p = VOLK_nsmap_dump (); LUA_NLCHECK (*data_p, "Error creating NS map iterator."); size_t *i = malloc (sizeof (*i)); LUA_NLCHECK (i, "Memory error in NS map iterator."); *i = 0; lua_pushlightuserdata (L, i); lua_pushcclosure (L, nsmap_iter_next, 2); return 1; } /* * Library setup. */ static const luaL_Reg nsmap_lib_fn [] = { {"add", l_nsmap_add}, {"remove", l_nsmap_remove}, {"get_ns", l_nsmap_get_ns}, {"get_pfx", l_nsmap_get_pfx}, {"normalize_uri", l_nsmap_normalize_uri}, {"denormalize_uri", l_nsmap_denormalize_uri}, {"iter", l_nsmap_iter}, {NULL} }; /* static const luaL_Reg nsmap_lib_meth [] = { {NULL} }; */ int luaopen_volksdata_namespace (lua_State *L) { luaL_newmetatable (L, "VOLK.NSMap"); lua_pushvalue (L, -1); //lua_setfield (L, -2, "__index"); //luaL_setfuncs (L, nsmap_lib_meth, 0); luaL_newlib (L, nsmap_lib_fn); //luaL_newmetatable (L, "VOLK.String2DIterator"); return 1; }