From 15e91f62a93c1cba6b7a36828837c69759ea8d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be> Date: Sat, 26 Feb 2011 18:13:11 +0100 Subject: Added built-in table print function to sluift. 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); + } } - -- cgit v0.10.2-6-g49f6