From 07d88bf5f16044e8131e7d5aebdb46822d19ff23 Mon Sep 17 00:00:00 2001 From: onefang Date: Fri, 30 Jul 2021 04:39:25 +1000 Subject: Generic Lua table to qlib qtreetbl. --- src/sledjchisl/sledjchisl.c | 115 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 9 deletions(-) (limited to 'src/sledjchisl') diff --git a/src/sledjchisl/sledjchisl.c b/src/sledjchisl/sledjchisl.c index 9a8d50c..fb2d9bc 100644 --- a/src/sledjchisl/sledjchisl.c +++ b/src/sledjchisl/sledjchisl.c @@ -86,6 +86,15 @@ extern char **environ; #include #include +// lua.h has LUA_T* NONE, NIL, BOOLEAN, LIGHTUSERDATA, NUMBER, STRING, TABLE, FUNCTION, USERDATA, THREAD as defines, -1 - 8. +// These are the missing ones. Then later we will have prim, mesh, script, sound, terrain, ... +#define LUA_TGROUP 42 +#define LUA_TINTEGER 43 +#define LUA_TEMAIL 44 +#define LUA_TPASSWORD 45 +#define LUA_TFILE 46 +#define LUA_TIMAGE 47 + #include "lib/fcgi_SC.h" #include "lib/handlekeys.h" @@ -3582,6 +3591,103 @@ t("checkSLOSpassword(%s, %s, %s, ", password, salt, passwordHash, fail); } +typedef struct _qLua qLua; +struct _qLua +{ + int type; + union + { + boolean b; + int i; + float f; + char *s; + qtreetbl_t *t; + } v; +}; + +qLua *qLuaGet(qtreetbl_t *tree, char *key) +{ + return (qLua *) tree->get(tree, key, NULL, false); +} + +qtreetbl_t *lua2tree() +{ + qtreetbl_t *ret = qtreetbl(0); + + if (NULL != ret) + { + qLua q; + + lua_pushnil(L); // +1 nil, first key + while(lua_next(L, -2) != 0) // -1 key, +2 next key, value (or 0 if nothing left in table) + { + char *n = (char *) lua_tostring(L, -2); // 0 + + q.type = lua_type(L, -1); // 0 + // Numbers can convert to strings, so check for numbers before checking for strings. + // On the other hand, strings that can be converted to numbers also pass lua_isnumber(). sigh + switch(q.type) + { + case LUA_TBOOLEAN : {q.v.b = lua_toboolean(L, -1); break;} + case LUA_TINTEGER : {q.v.i = lua_tointeger(L, -1); break;} + case LUA_TNUMBER : {q.v.f = lua_tonumber(L, -1); break;} + case LUA_TSTRING : {q.v.s = (char *) lua_tostring(L, -1); break;} + case LUA_TTABLE : {q.v.t = lua2tree(); break;} + + default : + { + q.v.s = (char *) lua_tostring(L, -1); // 0 + E("Unknown Lua variable type for %s = %s is %d", n, q.v.s, q.type); + break; + } + } + ret->put(ret, n, &q, sizeof(qLua)); + lua_pop(L, 1); // -1 value + } + } + else + { + D("No memory left."); + perror_msg("Unable to allocate memory"); + } + + return ret; +} + +qtreetbl_t *Lua2tree(char *file, char *var) +{ + qtreetbl_t *ret = NULL; + struct stat st; + + if (0 != lstat(file, &st)) + { + D("No %s file.", file); + perror_msg("Unable to stat %s", file); + } + else + { + if (luaL_loadfile(L, file)) // +1 the chunk or an error message. If something went wrong, error message is at the top of the stack. + E("Couldn't load Lua file: %s", lua_tostring(L, -1)); // 0 + else + { + if (lua_pcall(L, 0, LUA_MULTRET, 0)) // +1 result or an error message. Except we get 0 or error? + { + E("Failed to run Lua script: %s", lua_tostring(L, -1)); // 0 + lua_pop(L, 1); // -1 chunk or error message + } + else + { + lua_getglobal(L, var); // +1 the value of var + ret = lua2tree(); + lua_pop(L, 1); // -1 var + } + } + lua_pop(L, 1); // -1 chunk or error message + } + return ret; +} + + int LuaToHash(reqData *Rd, char *file, char *var, qhashtbl_t *tnm, int ret, struct stat *st, struct timespec *now, char *type) { struct timespec then; @@ -4517,15 +4623,6 @@ Maybe - DAVEE (Delete, Add, View, Edit, Explore) */ -// lua.h has LUA_T* NONE, NIL, BOOLEAN, LIGHTUSERDATA, NUMBER, STRING, TABLE, FUNCTION, USERDATA, THREAD as defines, -1 - 8. -// These are the missing ones. Then later we will have prim, mesh, script, sound, terrain, ... -#define LUA_TGROUP 42 -#define LUA_TINTEGER 43 -#define LUA_TEMAIL 44 -#define LUA_TPASSWORD 45 -#define LUA_TFILE 46 -#define LUA_TIMAGE 47 - #define FLD_NONE 0 #define FLD_EDITABLE 1 #define FLD_HIDDEN 2 -- cgit v1.1