|
@@ -300,39 +300,47 @@ static int l_graph_lookup (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int l_graph_encode (lua_State *L)
|
|
|
+static int graph_encode_iter_next (lua_State *L)
|
|
|
+{
|
|
|
+ void *it = lua_touserdata (L, lua_upvalueindex (1));
|
|
|
+ const VOLK_Codec *codec = lua_touserdata (L, lua_upvalueindex (2));
|
|
|
+
|
|
|
+ char *out = NULL;
|
|
|
+ VOLK_rc rc = codec->encode_graph_iter (it, &out);
|
|
|
+ LUA_PCHECK (rc, "Encoding failed");
|
|
|
+ //LOG_DEBUG ("Serialized fragment: %s", out);
|
|
|
+ if (rc == VOLK_END) {
|
|
|
+ codec->encode_graph_done (it);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ lua_pushstring (L, out);
|
|
|
+ free (out);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static int l_graph_encode_iter (lua_State *L)
|
|
|
{
|
|
|
const VOLK_Graph *gr = check_graph (L, 1);
|
|
|
const char *codec_str = lua_tostring (L, 2);
|
|
|
|
|
|
- VOLK_Codec codec;
|
|
|
+ const VOLK_Codec *codec_p;
|
|
|
if (strcmp(codec_str, "nt") == 0)
|
|
|
- codec = nt_codec;
|
|
|
+ codec_p = &nt_codec;
|
|
|
else if (strcmp(codec_str, "ttl") == 0)
|
|
|
- codec = ttl_codec;
|
|
|
+ codec_p = &ttl_codec;
|
|
|
else
|
|
|
return luaL_error(L, "Invalid encoding format: %s", codec_str);
|
|
|
|
|
|
- char *out = calloc (1, 1);
|
|
|
- LUA_NLCHECK (out, "Error allocating codec iterator.");
|
|
|
- void *it = codec.encode_graph_init (gr);
|
|
|
+ void *it = codec_p->encode_graph_init (gr);
|
|
|
LUA_NLCHECK (it, "Error creating codec iterator.");
|
|
|
|
|
|
- char *tmp = NULL;
|
|
|
- VOLK_rc rc;
|
|
|
- while ((rc = codec.encode_graph_iter (it, &tmp)) != VOLK_END) {
|
|
|
- //log_debug ("Serialized fragment: %s\n", tmp);
|
|
|
- LUA_PCHECK (rc, "Encoding failed");
|
|
|
- out = realloc (out, strlen(out) + strlen (tmp) + 1);
|
|
|
- LUA_NLCHECK (out, "Error reallocating serialization buffer.");
|
|
|
- out = strcat (out, tmp);
|
|
|
- LUA_NLCHECK (out, "Error filling serialization buffer.");
|
|
|
- }
|
|
|
- codec.encode_graph_done (it);
|
|
|
-
|
|
|
- lua_pushstring (L, out);
|
|
|
- free (tmp);
|
|
|
- free (out);
|
|
|
+ lua_pushlightuserdata (L, it);
|
|
|
+ lua_pushlightuserdata (L, (void *)codec_p);
|
|
|
+ lua_pushcclosure (L, graph_encode_iter_next, 2);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -507,7 +515,7 @@ static const luaL_Reg graph_lib_meth [] = {
|
|
|
{"term_set", l_graph_term_set},
|
|
|
{"unique_terms", l_graph_unique_terms},
|
|
|
{"attr", l_graph_attr},
|
|
|
- {"encode", l_graph_encode},
|
|
|
+ {"encode", l_graph_encode_iter},
|
|
|
|
|
|
{NULL}
|
|
|
};
|