summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-02-26 17:13:11 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-02-26 17:13:11 (GMT)
commit15e91f62a93c1cba6b7a36828837c69759ea8d46 (patch)
tree9ca79c23e348c56196da922f80579f0e7f48e9c9
parentf2af88dc39cc0b785be0ef294a4ba2fddbf4401a (diff)
downloadswift-15e91f62a93c1cba6b7a36828837c69759ea8d46.zip
swift-15e91f62a93c1cba6b7a36828837c69759ea8d46.tar.bz2
Added built-in table print function to sluift.
-rw-r--r--Sluift/linit.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/Sluift/linit.c b/Sluift/linit.c
index 4a3bd24..0114c12 100644
--- a/Sluift/linit.c
+++ b/Sluift/linit.c
@@ -1,19 +1,35 @@
-/*
-** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $
-** Initialization of libraries for lua.c
-** See Copyright Notice in lua.h
-*/
-
-
-#define linit_c
-#define LUA_LIB
-
#include "lua.h"
-
#include "lualib.h"
#include "lauxlib.h"
#include "sluift.h"
+// A built-in table print function
+// From: http://lua-users.org/wiki/TableSerialization
+static const char tprint[] =
+ "function tprint (tt, indent, done)\n"
+ " done = done or {}\n"
+ " indent = indent or 0\n"
+ " if type(tt) == \"table\" then\n"
+ " for key, value in pairs (tt) do\n"
+ " io.write(string.rep (\" \", indent)) -- indent it\n"
+ " if type (value) == \"table\" and not done [value] then\n"
+ " done [value] = true\n"
+ " io.write(string.format(\"[%s] => table\\n\", tostring (key)));\n"
+ " io.write(string.rep (\" \", indent+4)) -- indent it\n"
+ " io.write(\"(\\n\");\n"
+ " tprint (value, indent + 7, done)\n"
+ " io.write(string.rep (\" \", indent+4)) -- indent it\n"
+ " io.write(\")\\n\");\n"
+ " else\n"
+ " io.write(string.format(\"[%s] => %s\\n\",\n"
+ " tostring (key), tostring(value)))\n"
+ " end\n"
+ " end\n"
+ " else\n"
+ " io.write(tt .. \"\\n\")\n"
+ " end\n"
+ "end\n";
+
static const luaL_Reg lualibs[] = {
{"", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
@@ -35,5 +51,9 @@ LUALIB_API void luaL_openlibs (lua_State *L) {
lua_pushstring(L, lib->name);
lua_call(L, 1, 0);
}
+ int result = luaL_loadstring(L, tprint) || lua_pcall(L, 0, 0, 0);
+ if (result != 0) {
+ fprintf(stderr, "%s\n", lua_tostring(L, -1));
+ lua_pop(L, 1);
+ }
}
-