2022-03-27 06:49:41 +00:00
|
|
|
-- the iterator of QList object
|
|
|
|
local qlist_iterator = function(list, n)
|
|
|
|
if n < list:length() - 1 then
|
|
|
|
return n + 1, list:at(n + 1) -- the next element of list
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
function fk.qlist(list)
|
2022-03-27 06:49:41 +00:00
|
|
|
return qlist_iterator, list, -1
|
|
|
|
end
|
|
|
|
|
|
|
|
function table:contains(element)
|
|
|
|
if #self == 0 or type(self[1]) ~= type(element) then return false end
|
|
|
|
for _, e in ipairs(self) do
|
|
|
|
if e == element then return true end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function table:shuffle()
|
|
|
|
for i = #self, 2, -1 do
|
|
|
|
local j = math.random(i)
|
|
|
|
self[i], self[j] = self[j], self[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
function table:insertTable(list)
|
|
|
|
for _, e in ipairs(list) do
|
|
|
|
table.insert(self, e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
function table:indexOf(value, from)
|
|
|
|
from = from or 1
|
|
|
|
for i = from, #self do
|
|
|
|
if self[i] == value then return i end
|
|
|
|
end
|
|
|
|
return -1
|
|
|
|
end
|
|
|
|
|
|
|
|
function table:removeOne(element)
|
|
|
|
if #self == 0 or type(self[1]) ~= type(element) then return false end
|
|
|
|
|
|
|
|
for i = 1, #self do
|
|
|
|
if self[i] == element then
|
|
|
|
table.remove(self, i)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-04-14 10:22:00 +00:00
|
|
|
-- Note: only clone key and value, no metatable
|
|
|
|
-- so dont use for class or instance
|
|
|
|
---@generic T
|
|
|
|
---@param self T
|
|
|
|
---@return T
|
|
|
|
function table.clone(self)
|
|
|
|
local ret = {}
|
|
|
|
for k, v in pairs(self) do
|
|
|
|
if type(v) == "table" then
|
|
|
|
ret[k] = table.clone(v)
|
|
|
|
else
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@class Sql
|
2022-03-27 06:49:41 +00:00
|
|
|
Sql = {
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param filename string
|
2022-03-27 06:49:41 +00:00
|
|
|
open = function(filename)
|
2022-03-31 05:29:23 +00:00
|
|
|
return fk.OpenDatabase(filename)
|
2022-03-27 06:49:41 +00:00
|
|
|
end,
|
2022-03-30 08:33:56 +00:00
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param db fk.SQLite3
|
2022-03-27 06:49:41 +00:00
|
|
|
close = function(db)
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.CloseDatabase(db)
|
2022-03-27 06:49:41 +00:00
|
|
|
end,
|
2022-03-30 08:33:56 +00:00
|
|
|
|
|
|
|
--- Execute an SQL statement.
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param db fk.SQLite3
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param sql string
|
2022-03-27 06:49:41 +00:00
|
|
|
exec = function(db, sql)
|
2022-03-31 05:29:23 +00:00
|
|
|
fk.ExecSQL(db, sql)
|
2022-03-27 06:49:41 +00:00
|
|
|
end,
|
2022-03-30 08:33:56 +00:00
|
|
|
|
|
|
|
--- Execute a `SELECT` SQL statement.
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param db fk.SQLite3
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param sql string
|
2022-04-02 13:39:44 +00:00
|
|
|
---@return table @ { [columnName] --> result : string[] }
|
2022-03-27 06:49:41 +00:00
|
|
|
exec_select = function(db, sql)
|
2022-03-31 05:29:23 +00:00
|
|
|
return json.decode(fk.SelectFromDb(db, sql))
|
2022-03-27 06:49:41 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
FileIO = {
|
2022-03-31 05:29:23 +00:00
|
|
|
pwd = fk.QmlBackend_pwd,
|
2022-03-28 14:24:30 +00:00
|
|
|
ls = function(filename)
|
|
|
|
if filename == nil then
|
2022-03-31 05:29:23 +00:00
|
|
|
return fk.QmlBackend_ls(".")
|
2022-03-28 14:24:30 +00:00
|
|
|
else
|
2022-03-31 05:29:23 +00:00
|
|
|
return fk.QmlBackend_ls(filename)
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
end,
|
2022-03-31 05:29:23 +00:00
|
|
|
cd = fk.QmlBackend_cd,
|
|
|
|
exists = fk.QmlBackend_exists,
|
|
|
|
isDir = fk.QmlBackend_isDir
|
2022-03-28 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
os.getms = fk.GetMicroSecond
|
2022-03-30 06:14:40 +00:00
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
---@class Stack : Object
|
2022-03-28 14:24:30 +00:00
|
|
|
Stack = class("Stack")
|
|
|
|
function Stack:initialize()
|
|
|
|
self.t = {}
|
|
|
|
self.p = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function Stack:push(e)
|
|
|
|
self.p = self.p + 1
|
|
|
|
self.t[self.p] = e
|
|
|
|
end
|
|
|
|
|
|
|
|
function Stack:isEmpty()
|
|
|
|
return self.p == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function Stack:pop()
|
|
|
|
if self.p == 0 then return nil end
|
|
|
|
self.p = self.p - 1
|
|
|
|
return self.t[self.p + 1]
|
|
|
|
end
|
|
|
|
|
2022-03-27 06:49:41 +00:00
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
--- useful function to create enums
|
|
|
|
---
|
|
|
|
--- only use it in a terminal
|
|
|
|
---@param table string
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param enum string[]
|
2022-04-01 12:51:01 +00:00
|
|
|
function CreateEnum(table, enum)
|
|
|
|
local enum_format = "%s.%s = %d"
|
2022-03-31 05:29:23 +00:00
|
|
|
for i, v in ipairs(enum) do
|
2022-04-01 12:51:01 +00:00
|
|
|
print(string.format(enum_format, table, v, i))
|
2022-03-31 05:29:23 +00:00
|
|
|
end
|
2022-03-27 06:49:41 +00:00
|
|
|
end
|
2022-04-02 13:39:44 +00:00
|
|
|
|
|
|
|
function switch(param, case_table)
|
|
|
|
local case = case_table[param]
|
|
|
|
if case then return case() end
|
|
|
|
local def = case_table["default"]
|
|
|
|
return def and def() or nil
|
|
|
|
end
|