123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "lua_lsup.h"
- #define check_nsm(L) \
- *(LSUP_NSMap **)luaL_checkudata(L, 1, "LSUP.NSMap")
- /*
- * Factory methods.
- */
- int l_nsmap_new (lua_State *L)
- {
- LSUP_NSMap **nsm_p = lua_newuserdatauv (L, sizeof (*nsm_p), 1);
- luaL_getmetatable (L, "LSUP.NSMap");
- lua_setmetatable (L, -2);
- *nsm_p = LSUP_nsmap_new ();
- if (!*nsm_p) return luaL_error (L, "Error while creating a term!");
- return 1;
- }
- /*
- * Class methods.
- */
- static int l_nsmap_gc (lua_State *L)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- if (nsm) LSUP_nsmap_free (nsm);
- return 0;
- }
- static int l_nsmap_add (lua_State *L)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- const char
- *pfx = luaL_checkstring (L, 2),
- *nsstr = luaL_checkstring (L, 3);
- LUA_PCHECK (
- LSUP_nsmap_add (nsm, pfx, nsstr),
- "Error adding member to NS map");
- return 0;
- }
- static int l_nsmap_remove (lua_State *L)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- const char *pfx = luaL_checkstring (L, 2);
- LUA_PCHECK (
- LSUP_nsmap_remove (nsm, pfx),
- "Error removing member to NS map");
- return 0;
- }
- static int l_nsmap_get_ns (lua_State *L)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- const char *pfx = luaL_checkstring (L, 2);
- const char *ns = LSUP_nsmap_get_ns (nsm, pfx);
- if (ns) lua_pushstring (L, ns);
- else return 0;
- return 1;
- }
- static int l_nsmap_get_pfx (lua_State *L)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- const char *ns = luaL_checkstring (L, 2);
- const char *pfx = LSUP_nsmap_get_pfx (nsm, ns);
- if (pfx) lua_pushstring (L, pfx);
- else lua_pushnil (L);
- return 1;
- }
- static int l_nsmap_normalize_uri (lua_State *L)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- const char *pfx_uri = luaL_checkstring (L, 2);
- char *fq_uri;
- LUA_PCHECK (
- LSUP_nsmap_normalize_uri (nsm, 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)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- const char *fq_uri = luaL_checkstring (L, 2);
- char *pfx_uri;
- LUA_PCHECK (
- LSUP_nsmap_denormalize_uri (nsm, 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 l_nsmap_iter (lua_State *L)
- {
- LSUP_NSMap *nsm = check_nsm (L);
- // TODO
- return 0;
- }
- /*
- * Library setup.
- */
- static const luaL_Reg nsmap_lib_fn [] = {
- {"new", l_nsmap_new},
- {NULL}
- };
- static const luaL_Reg nsmap_lib_meth [] = {
- {"__gc", l_nsmap_gc},
- {"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}
- };
- int luaopen_lsup_namespace (lua_State *L)
- {
- luaL_newmetatable (L, "LSUP.NSMap");
- lua_pushvalue (L, -1);
- lua_setfield (L, -2, "__index");
- luaL_setfuncs (L, nsmap_lib_meth, 0);
- luaL_newlib (L, nsmap_lib_fn);
- return 1;
- }
|