diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-06-13 13:05:11 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-12-13 15:02:55 +0100 |
commit | 6834df19a326c47d394b6c0b41e499c595464767 (patch) | |
tree | 3b73faafefffc77756589d035295aad7040d263a /ctl-lib | |
parent | 3d004cbc1b01bcfc805d157906a0873612529171 (diff) |
Add 2 lua utilities function
Sleep function and a recursive table
equality function
Change-Id: I4cb3a0f0909674f03caeadeebd8da35f16391198
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'ctl-lib')
-rw-r--r-- | ctl-lib/ctl-lua-utils.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/ctl-lib/ctl-lua-utils.c b/ctl-lib/ctl-lua-utils.c index da22b7a..98dcebd 100644 --- a/ctl-lib/ctl-lua-utils.c +++ b/ctl-lib/ctl-lua-utils.c @@ -82,6 +82,56 @@ printf = function(s,...)\n\ return\n\ end\n\ \n\ +function sleep(n)\n\ + os.execute(\"sleep \" .. tonumber(n))\n\ +end\n\ +\n\ +function table_eq(table1, table2)\n\ + local avoid_loops = {}\n\ + local function recurse(t1, t2)\n\ + -- compare value types\n\ + if type(t1) ~= type(t2) then return false end\n\ + -- Base case: compare simple values\n\ + if type(t1) ~= \"table\" then return t1 == t2 end\n\ + -- Now, on to tables.\n\ + -- First, let's avoid looping forever.\n\ + if avoid_loops[t1] then return avoid_loops[t1] == t2 end\n\ + avoid_loops[t1] = t2\n\ + -- Copy keys from t2\n\ + local t2keys = {}\n\ + local t2tablekeys = {}\n\ + for k, _ in pairs(t2) do\n\ + if type(k) == \"table\" then table.insert(t2tablekeys, k) end\n\ + t2keys[k] = true\n\ + end\n\ + -- Let's iterate keys from t1\n\ + for k1, v1 in pairs(t1) do\n\ + local v2 = t2[k1]\n\ + if type(k1) == \"table\" then\n\ + -- if key is a table, we need to find an equivalent one.\n\ + local ok = false\n\ + for i, tk in ipairs(t2tablekeys) do\n\ + if table_eq(k1, tk) and recurse(v1, t2[tk]) then\n\ + table.remove(t2tablekeys, i)\n\ + t2keys[tk] = nil\n\ + ok = true\n\ + break\n\ + end\n\ + end\n\ + if not ok then return false end\n\ + else\n\ + -- t1 has a key which t2 doesn't have, fail.\n\ + if v2 == nil then return false end\n\ + t2keys[k1] = nil\n\ + if not recurse(v1, v2) then return false end\n\ + end\n\ + end\n\ + -- if t2 has a key which t1 doesn't have, fail.\n\ + if next(t2keys) then return false end\n\ + return true\n\ + end\n\ + return recurse(table1, table2)\n\ +end\n\ -- lock global variable\n\ GLOBAL_lock(_G)\n\ "; |