Browse Source

Add namespace module.

scossu 3 weeks ago
parent
commit
198213201c
3 changed files with 132 additions and 22 deletions
  1. 12 0
      lua/lua_lsup.h
  2. 119 15
      lua/lua_namespace.c
  3. 1 7
      lua/lua_term.c

+ 12 - 0
lua/lua_lsup.h

@@ -0,0 +1,12 @@
+#include <lua.h>
+//#include <lualib.h>
+#include <lauxlib.h>
+
+#include "lsup_rdf.h"
+
+
+/// Raise Lua error including LSUP error message on negative rc.
+#define LUA_PCHECK(exp, message) do {\
+    if ((exp) < LSUP_OK) \
+        luaL_error (L, "%s: %s", message, LSUP_strerror (exp)); \
+} while (0)

+ 119 - 15
lua/lua_namespace.c

@@ -1,20 +1,14 @@
-#include <lua.h>
-//#include <lualib.h>
-#include <lauxlib.h>
-#include <namespace.h>
+#include "lua_lsup.h"
 
 #define check_nsm(L) \
     *(LSUP_NSMap **)luaL_checkudata(L, 1, "LSUP.NSMap")
 
 
-lua_State *L;
-
-
 /*
  * Factory methods.
  */
 
-static int l_nsm_new (lua_State *L)
+static int l_nsmap_new (lua_State *L)
 {
     LSUP_NSMap **nsm_p = lua_newuserdatauv (L, sizeof (*nsm_p), 1);
     luaL_getmetatable (L, "LSUP.NSMap");
@@ -31,7 +25,7 @@ static int l_nsm_new (lua_State *L)
  * Class methods.
  */
 
-static int l_nsm_gc (lua_State *L)
+static int l_nsmap_gc (lua_State *L)
 {
     LSUP_NSMap *nsm = check_nsm (L);
     if (nsm) LSUP_nsmap_free (nsm);
@@ -40,18 +34,128 @@ static int l_nsm_gc (lua_State *L)
 }
 
 
+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_dump (lua_State *L)
+{
+    LSUP_NSMap *nsm = check_nsm (L);
+
+    // TODO
+
+    return 0;
+}
+
+
 /*
  * Library setup.
  */
 
-static const struct luaL_Reg nsm_lib_fn [] = {
-    {"nsm_new", l_nsm_new},
+static const struct luaL_Reg nsmap_lib_fn [] = {
+    {"new", l_nsmap_new},
     {NULL}
 };
 
 
-static const struct luaL_Reg nsm_lib_meth [] = {
-    {"__gc", l_nsm_gc},
+static const struct 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},
+    //{"dump", l_nsmap_dump},
 
     {NULL}
 };
@@ -62,8 +166,8 @@ int luaopen_namespace (lua_State *L)
     luaL_newmetatable (L, "LSUP.NSMap");
     lua_pushvalue (L, -1);
     lua_setfield (L, -2, "__index");
-    luaL_setfuncs (L, nsm_lib_meth, 0);
-    luaL_newlib (L, nsm_lib_fn);
+    luaL_setfuncs (L, nsmap_lib_meth, 0);
+    luaL_newlib (L, nsmap_lib_fn);
 
     return 1;
 }

+ 1 - 7
lua/lua_term.c

@@ -1,15 +1,9 @@
-#include <lua.h>
-//#include <lualib.h>
-#include <lauxlib.h>
-#include <term.h>
+#include "lua_lsup.h"
 
 #define check_term(L) \
     *(LSUP_Term **)luaL_checkudata(L, 1, "LSUP.Term")
 
 
-lua_State *L;
-
-
 static LSUP_Term **allocate_term (lua_State *L)
 {
     LSUP_Term **tp = lua_newuserdatauv (L, sizeof (*tp), 1);