diff options
author | onefang | 2021-07-30 04:39:25 +1000 |
---|---|---|
committer | onefang | 2021-07-30 04:39:25 +1000 |
commit | 07d88bf5f16044e8131e7d5aebdb46822d19ff23 (patch) | |
tree | a812dd4e8f047b3d431f2829807034d5e9f8a2a0 /src/sledjchisl/sledjchisl.c | |
parent | Repeat things that newbies tend to ignore, then bug me coz things didn't work. (diff) | |
download | opensim-SC-07d88bf5f16044e8131e7d5aebdb46822d19ff23.zip opensim-SC-07d88bf5f16044e8131e7d5aebdb46822d19ff23.tar.gz opensim-SC-07d88bf5f16044e8131e7d5aebdb46822d19ff23.tar.bz2 opensim-SC-07d88bf5f16044e8131e7d5aebdb46822d19ff23.tar.xz |
Generic Lua table to qlib qtreetbl.
Diffstat (limited to '')
-rw-r--r-- | src/sledjchisl/sledjchisl.c | 115 |
1 files changed, 106 insertions, 9 deletions
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; | |||
86 | #include <lauxlib.h> | 86 | #include <lauxlib.h> |
87 | #include <luajit.h> | 87 | #include <luajit.h> |
88 | 88 | ||
89 | // lua.h has LUA_T* NONE, NIL, BOOLEAN, LIGHTUSERDATA, NUMBER, STRING, TABLE, FUNCTION, USERDATA, THREAD as defines, -1 - 8. | ||
90 | // These are the missing ones. Then later we will have prim, mesh, script, sound, terrain, ... | ||
91 | #define LUA_TGROUP 42 | ||
92 | #define LUA_TINTEGER 43 | ||
93 | #define LUA_TEMAIL 44 | ||
94 | #define LUA_TPASSWORD 45 | ||
95 | #define LUA_TFILE 46 | ||
96 | #define LUA_TIMAGE 47 | ||
97 | |||
89 | #include "lib/fcgi_SC.h" | 98 | #include "lib/fcgi_SC.h" |
90 | #include "lib/handlekeys.h" | 99 | #include "lib/handlekeys.h" |
91 | 100 | ||
@@ -3582,6 +3591,103 @@ t("checkSLOSpassword(%s, %s, %s, ", password, salt, passwordHash, fail); | |||
3582 | } | 3591 | } |
3583 | 3592 | ||
3584 | 3593 | ||
3594 | typedef struct _qLua qLua; | ||
3595 | struct _qLua | ||
3596 | { | ||
3597 | int type; | ||
3598 | union | ||
3599 | { | ||
3600 | boolean b; | ||
3601 | int i; | ||
3602 | float f; | ||
3603 | char *s; | ||
3604 | qtreetbl_t *t; | ||
3605 | } v; | ||
3606 | }; | ||
3607 | |||
3608 | qLua *qLuaGet(qtreetbl_t *tree, char *key) | ||
3609 | { | ||
3610 | return (qLua *) tree->get(tree, key, NULL, false); | ||
3611 | } | ||
3612 | |||
3613 | qtreetbl_t *lua2tree() | ||
3614 | { | ||
3615 | qtreetbl_t *ret = qtreetbl(0); | ||
3616 | |||
3617 | if (NULL != ret) | ||
3618 | { | ||
3619 | qLua q; | ||
3620 | |||
3621 | lua_pushnil(L); // +1 nil, first key | ||
3622 | while(lua_next(L, -2) != 0) // -1 key, +2 next key, value (or 0 if nothing left in table) | ||
3623 | { | ||
3624 | char *n = (char *) lua_tostring(L, -2); // 0 | ||
3625 | |||
3626 | q.type = lua_type(L, -1); // 0 | ||
3627 | // Numbers can convert to strings, so check for numbers before checking for strings. | ||
3628 | // On the other hand, strings that can be converted to numbers also pass lua_isnumber(). sigh | ||
3629 | switch(q.type) | ||
3630 | { | ||
3631 | case LUA_TBOOLEAN : {q.v.b = lua_toboolean(L, -1); break;} | ||
3632 | case LUA_TINTEGER : {q.v.i = lua_tointeger(L, -1); break;} | ||
3633 | case LUA_TNUMBER : {q.v.f = lua_tonumber(L, -1); break;} | ||
3634 | case LUA_TSTRING : {q.v.s = (char *) lua_tostring(L, -1); break;} | ||
3635 | case LUA_TTABLE : {q.v.t = lua2tree(); break;} | ||
3636 | |||
3637 | default : | ||
3638 | { | ||
3639 | q.v.s = (char *) lua_tostring(L, -1); // 0 | ||
3640 | E("Unknown Lua variable type for %s = %s is %d", n, q.v.s, q.type); | ||
3641 | break; | ||
3642 | } | ||
3643 | } | ||
3644 | ret->put(ret, n, &q, sizeof(qLua)); | ||
3645 | lua_pop(L, 1); // -1 value | ||
3646 | } | ||
3647 | } | ||
3648 | else | ||
3649 | { | ||
3650 | D("No memory left."); | ||
3651 | perror_msg("Unable to allocate memory"); | ||
3652 | } | ||
3653 | |||
3654 | return ret; | ||
3655 | } | ||
3656 | |||
3657 | qtreetbl_t *Lua2tree(char *file, char *var) | ||
3658 | { | ||
3659 | qtreetbl_t *ret = NULL; | ||
3660 | struct stat st; | ||
3661 | |||
3662 | if (0 != lstat(file, &st)) | ||
3663 | { | ||
3664 | D("No %s file.", file); | ||
3665 | perror_msg("Unable to stat %s", file); | ||
3666 | } | ||
3667 | else | ||
3668 | { | ||
3669 | 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. | ||
3670 | E("Couldn't load Lua file: %s", lua_tostring(L, -1)); // 0 | ||
3671 | else | ||
3672 | { | ||
3673 | if (lua_pcall(L, 0, LUA_MULTRET, 0)) // +1 result or an error message. Except we get 0 or error? | ||
3674 | { | ||
3675 | E("Failed to run Lua script: %s", lua_tostring(L, -1)); // 0 | ||
3676 | lua_pop(L, 1); // -1 chunk or error message | ||
3677 | } | ||
3678 | else | ||
3679 | { | ||
3680 | lua_getglobal(L, var); // +1 the value of var | ||
3681 | ret = lua2tree(); | ||
3682 | lua_pop(L, 1); // -1 var | ||
3683 | } | ||
3684 | } | ||
3685 | lua_pop(L, 1); // -1 chunk or error message | ||
3686 | } | ||
3687 | return ret; | ||
3688 | } | ||
3689 | |||
3690 | |||
3585 | int LuaToHash(reqData *Rd, char *file, char *var, qhashtbl_t *tnm, int ret, struct stat *st, struct timespec *now, char *type) | 3691 | int LuaToHash(reqData *Rd, char *file, char *var, qhashtbl_t *tnm, int ret, struct stat *st, struct timespec *now, char *type) |
3586 | { | 3692 | { |
3587 | struct timespec then; | 3693 | struct timespec then; |
@@ -4517,15 +4623,6 @@ Maybe - | |||
4517 | DAVEE (Delete, Add, View, Edit, Explore) | 4623 | DAVEE (Delete, Add, View, Edit, Explore) |
4518 | */ | 4624 | */ |
4519 | 4625 | ||
4520 | // lua.h has LUA_T* NONE, NIL, BOOLEAN, LIGHTUSERDATA, NUMBER, STRING, TABLE, FUNCTION, USERDATA, THREAD as defines, -1 - 8. | ||
4521 | // These are the missing ones. Then later we will have prim, mesh, script, sound, terrain, ... | ||
4522 | #define LUA_TGROUP 42 | ||
4523 | #define LUA_TINTEGER 43 | ||
4524 | #define LUA_TEMAIL 44 | ||
4525 | #define LUA_TPASSWORD 45 | ||
4526 | #define LUA_TFILE 46 | ||
4527 | #define LUA_TIMAGE 47 | ||
4528 | |||
4529 | #define FLD_NONE 0 | 4626 | #define FLD_NONE 0 |
4530 | #define FLD_EDITABLE 1 | 4627 | #define FLD_EDITABLE 1 |
4531 | #define FLD_HIDDEN 2 | 4628 | #define FLD_HIDDEN 2 |