mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 07:45:54 +01:00
ADD redist folder and logo
This commit is contained in:
parent
d15e1f28c9
commit
adb83594bc
BIN
docs/delphimvcframework.png
Normal file
BIN
docs/delphimvcframework.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
18
redist/Lua/boot.lua
Normal file
18
redist/Lua/boot.lua
Normal file
@ -0,0 +1,18 @@
|
||||
require "Lua.helper.view"
|
||||
--[[require "Lua.delphi.datasets"]]
|
||||
dump = require "Lua.inspect"
|
||||
json = require "dkjson"
|
||||
|
||||
function htmlize(s)
|
||||
s = s:gsub('&', '&')
|
||||
s = s:gsub('<', '<')
|
||||
s = s:gsub('>', '>')
|
||||
if s == ' ' then
|
||||
s = ' '
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
function dumppre(value)
|
||||
return '<pre style="padding: 10px; border: thin #a0a0a0 solid; background-color: #f0f0f0">' .. htmlize(dump(value)) .. '</pre>'
|
||||
end
|
29
redist/Lua/delphi/datasets.lua
Normal file
29
redist/Lua/delphi/datasets.lua
Normal file
@ -0,0 +1,29 @@
|
||||
function open_dataset(ud)
|
||||
_dataset_open(ud);
|
||||
m = {
|
||||
pointer = ud,
|
||||
eof = function()
|
||||
return _dataset_eof(ud)
|
||||
end,
|
||||
movenext = function()
|
||||
_dataset_move_next(ud)
|
||||
end,
|
||||
get = function(fieldname)
|
||||
return _dataset_field_by_name(ud, fieldname)
|
||||
end,
|
||||
close = function()
|
||||
_dataset_close(ud)
|
||||
end,
|
||||
first = function()
|
||||
_dataset_first(ud)
|
||||
end,
|
||||
is_open = function()
|
||||
return _dataset_is_open(ud)
|
||||
end,
|
||||
open = function()
|
||||
_dataset_open(ud)
|
||||
end
|
||||
};
|
||||
|
||||
return m;
|
||||
end
|
42
redist/Lua/delphi/delphiobjects.lua
Normal file
42
redist/Lua/delphi/delphiobjects.lua
Normal file
@ -0,0 +1,42 @@
|
||||
function connect_to_delphi_object(ud)
|
||||
m = {
|
||||
pointer = ud,
|
||||
};
|
||||
|
||||
meta = {
|
||||
__index = function (self, name)
|
||||
if _delphi_is_a_property(ud, name) then
|
||||
return _delphi_call_method(ud,name,{maxindex = -1})
|
||||
else
|
||||
return function(...)
|
||||
local parent_table = self
|
||||
local method_name = name
|
||||
local methodargs = {}
|
||||
local index = 0
|
||||
for k,v in ipairs(arg) do
|
||||
methodargs['_'..tostring(index)] = v
|
||||
index = index + 1
|
||||
end
|
||||
methodargs['maxindex'] = index - 1
|
||||
logger:debug("Ecco i parametri:")
|
||||
logger:debug(methodargs)
|
||||
return _delphi_call_method(ud,method_name,methodargs)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
__newindex = function (self, key, value)
|
||||
logger:debug("setting "..key.." to "..tostring(value))
|
||||
return _delphi_set_property(ud, key, value)
|
||||
end
|
||||
}
|
||||
|
||||
setmetatable(m, meta);
|
||||
|
||||
return m;
|
||||
end
|
||||
|
||||
function create_delphi_object(ClassName, ...)
|
||||
return connect_to_delphi_object(_delphi_create_object(ClassName, ...))
|
||||
end
|
||||
|
803
redist/Lua/dkjson.lua
Normal file
803
redist/Lua/dkjson.lua
Normal file
@ -0,0 +1,803 @@
|
||||
-- Module options:
|
||||
local always_try_using_lpeg = false
|
||||
local register_global_module_table = false
|
||||
local global_module_name = 'json'
|
||||
|
||||
--[==[
|
||||
|
||||
David Kolf's JSON module for Lua 5.1/5.2
|
||||
========================================
|
||||
|
||||
*Version 2.3*
|
||||
|
||||
In the default configuration this module writes no global values, not even
|
||||
the module table. Import it using
|
||||
|
||||
json = require ("dkjson")
|
||||
|
||||
In environments where `require` or a similiar function are not available
|
||||
and you cannot receive the return value of the module, you can set the
|
||||
option `register_global_module_table` to `true`. The module table will
|
||||
then be saved in the global variable with the name given by the option
|
||||
`global_module_name`.
|
||||
|
||||
Exported functions and values:
|
||||
|
||||
`json.encode (object [, state])`
|
||||
--------------------------------
|
||||
|
||||
Create a string representing the object. `Object` can be a table,
|
||||
a string, a number, a boolean, `nil`, `json.null` or any object with
|
||||
a function `__tojson` in its metatable. A table can only use strings
|
||||
and numbers as keys and its values have to be valid objects as
|
||||
well. It raises an error for any invalid data types or reference
|
||||
cycles.
|
||||
|
||||
`state` is an optional table with the following fields:
|
||||
|
||||
- `indent`
|
||||
When `indent` (a boolean) is set, the created string will contain
|
||||
newlines and indentations. Otherwise it will be one long line.
|
||||
- `keyorder`
|
||||
`keyorder` is an array to specify the ordering of keys in the
|
||||
encoded output. If an object has keys which are not in this array
|
||||
they are written after the sorted keys.
|
||||
- `level`
|
||||
This is the initial level of indentation used when `indent` is
|
||||
set. For each level two spaces are added. When absent it is set
|
||||
to 0.
|
||||
- `buffer`
|
||||
`buffer` is an array to store the strings for the result so they
|
||||
can be concatenated at once. When it isn't given, the encode
|
||||
function will create it temporary and will return the
|
||||
concatenated result.
|
||||
- `bufferlen`
|
||||
When `bufferlen` is set, it has to be the index of the last
|
||||
element of `buffer`.
|
||||
- `tables`
|
||||
`tables` is a set to detect reference cycles. It is created
|
||||
temporary when absent. Every table that is currently processed
|
||||
is used as key, the value is `true`.
|
||||
|
||||
When `state.buffer` was set, the return value will be `true` on
|
||||
success. Without `state.buffer` the return value will be a string.
|
||||
|
||||
`json.decode (string [, position [, null]])`
|
||||
--------------------------------------------
|
||||
|
||||
Decode `string` starting at `position` or at 1 if `position` was
|
||||
omitted.
|
||||
|
||||
`null` is an optional value to be returned for null values. The
|
||||
default is `nil`, but you could set it to `json.null` or any other
|
||||
value.
|
||||
|
||||
The return values are the object or `nil`, the position of the next
|
||||
character that doesn't belong to the object, and in case of errors
|
||||
an error message.
|
||||
|
||||
Two metatables are created. Every array or object that is decoded gets
|
||||
a metatable with the `__jsontype` field set to either `array` or
|
||||
`object`. If you want to provide your own metatables use the syntax
|
||||
|
||||
json.decode (string, position, null, objectmeta, arraymeta)
|
||||
|
||||
To prevent the assigning of metatables pass `nil`:
|
||||
|
||||
json.decode (string, position, null, nil)
|
||||
|
||||
`<metatable>.__jsonorder`
|
||||
-------------------------
|
||||
|
||||
`__jsonorder` can overwrite the `keyorder` for a specific table.
|
||||
|
||||
`<metatable>.__jsontype`
|
||||
------------------------
|
||||
|
||||
`__jsontype` can be either `"array"` or `"object"`. This value is only
|
||||
checked for empty tables. (The default for empty tables is `"array"`).
|
||||
|
||||
`<metatable>.__tojson (self, state)`
|
||||
------------------------------------
|
||||
|
||||
You can provide your own `__tojson` function in a metatable. In this
|
||||
function you can either add directly to the buffer and return true,
|
||||
or you can return a string. On errors nil and a message should be
|
||||
returned.
|
||||
|
||||
`json.null`
|
||||
-----------
|
||||
|
||||
You can use this value for setting explicit `null` values.
|
||||
|
||||
`json.version`
|
||||
--------------
|
||||
|
||||
Set to `"dkjson 2.3"`.
|
||||
|
||||
`json.quotestring (string)`
|
||||
---------------------------
|
||||
|
||||
Quote a UTF-8 string and escape critical characters using JSON
|
||||
escape sequences. This function is only necessary when you build
|
||||
your own `__tojson` functions.
|
||||
|
||||
`json.addnewline (state)`
|
||||
-------------------------
|
||||
|
||||
When `state.indent` is set, add a newline to `state.buffer` and spaces
|
||||
according to `state.level`.
|
||||
|
||||
LPeg support
|
||||
------------
|
||||
|
||||
When the local configuration variable `always_try_using_lpeg` is set,
|
||||
this module tries to load LPeg to replace the `decode` function. The
|
||||
speed increase is significant. You can get the LPeg module at
|
||||
<http://www.inf.puc-rio.br/~roberto/lpeg/>.
|
||||
When LPeg couldn't be loaded, the pure Lua functions stay active.
|
||||
|
||||
In case you don't want this module to require LPeg on its own,
|
||||
disable the option `always_try_using_lpeg` in the options section at
|
||||
the top of the module.
|
||||
|
||||
In this case you can later load LPeg support using
|
||||
|
||||
### `json.use_lpeg ()`
|
||||
|
||||
Require the LPeg module and replace the functions `quotestring` and
|
||||
and `decode` with functions that use LPeg patterns.
|
||||
This function returns the module table, so you can load the module
|
||||
using:
|
||||
|
||||
json = require "dkjson".use_lpeg()
|
||||
|
||||
Alternatively you can use `pcall` so the JSON module still works when
|
||||
LPeg isn't found.
|
||||
|
||||
json = require "dkjson"
|
||||
pcall (json.use_lpeg)
|
||||
|
||||
### `json.using_lpeg`
|
||||
|
||||
This variable is set to `true` when LPeg was loaded successfully.
|
||||
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Contact
|
||||
-------
|
||||
|
||||
You can contact the author by sending an e-mail to 'david' at the
|
||||
domain 'dkolf.de'.
|
||||
|
||||
---------------------------------------------------------------------
|
||||
|
||||
*Copyright (C) 2010-2013 David Heiko Kolf*
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
<!-- This documentation can be parsed using Markdown to generate HTML.
|
||||
The source code is enclosed in a HTML comment so it won't be displayed
|
||||
by browsers, but it should be removed from the final HTML file as
|
||||
it isn't a valid HTML comment (and wastes space).
|
||||
-->
|
||||
|
||||
<!--]==]
|
||||
|
||||
-- global dependencies:
|
||||
local pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset =
|
||||
pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset
|
||||
local error, require, pcall, select = error, require, pcall, select
|
||||
local floor, huge = math.floor, math.huge
|
||||
local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat =
|
||||
string.rep, string.gsub, string.sub, string.byte, string.char,
|
||||
string.find, string.len, string.format
|
||||
local concat = table.concat
|
||||
|
||||
local json = { version = "dkjson 2.3" }
|
||||
|
||||
if register_global_module_table then
|
||||
_G[global_module_name] = json
|
||||
end
|
||||
|
||||
local _ENV = nil -- blocking globals in Lua 5.2
|
||||
|
||||
pcall (function()
|
||||
-- Enable access to blocked metatables.
|
||||
-- Don't worry, this module doesn't change anything in them.
|
||||
local debmeta = require "debug".getmetatable
|
||||
if debmeta then getmetatable = debmeta end
|
||||
end)
|
||||
|
||||
json.null = setmetatable ({}, {
|
||||
__tojson = function () return "null" end
|
||||
})
|
||||
|
||||
local function isarray (tbl)
|
||||
local max, n, arraylen = 0, 0, 0
|
||||
for k,v in pairs (tbl) do
|
||||
if k == 'n' and type(v) == 'number' then
|
||||
arraylen = v
|
||||
if v > max then
|
||||
max = v
|
||||
end
|
||||
else
|
||||
if type(k) ~= 'number' or k < 1 or floor(k) ~= k then
|
||||
return false
|
||||
end
|
||||
if k > max then
|
||||
max = k
|
||||
end
|
||||
n = n + 1
|
||||
end
|
||||
end
|
||||
if max > 10 and max > arraylen and max > n * 2 then
|
||||
return false -- don't create an array with too many holes
|
||||
end
|
||||
return true, max
|
||||
end
|
||||
|
||||
local escapecodes = {
|
||||
["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", ["\f"] = "\\f",
|
||||
["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"
|
||||
}
|
||||
|
||||
local function escapeutf8 (uchar)
|
||||
local value = escapecodes[uchar]
|
||||
if value then
|
||||
return value
|
||||
end
|
||||
local a, b, c, d = strbyte (uchar, 1, 4)
|
||||
a, b, c, d = a or 0, b or 0, c or 0, d or 0
|
||||
if a <= 0x7f then
|
||||
value = a
|
||||
elseif 0xc0 <= a and a <= 0xdf and b >= 0x80 then
|
||||
value = (a - 0xc0) * 0x40 + b - 0x80
|
||||
elseif 0xe0 <= a and a <= 0xef and b >= 0x80 and c >= 0x80 then
|
||||
value = ((a - 0xe0) * 0x40 + b - 0x80) * 0x40 + c - 0x80
|
||||
elseif 0xf0 <= a and a <= 0xf7 and b >= 0x80 and c >= 0x80 and d >= 0x80 then
|
||||
value = (((a - 0xf0) * 0x40 + b - 0x80) * 0x40 + c - 0x80) * 0x40 + d - 0x80
|
||||
else
|
||||
return ""
|
||||
end
|
||||
if value <= 0xffff then
|
||||
return strformat ("\\u%.4x", value)
|
||||
elseif value <= 0x10ffff then
|
||||
-- encode as UTF-16 surrogate pair
|
||||
value = value - 0x10000
|
||||
local highsur, lowsur = 0xD800 + floor (value/0x400), 0xDC00 + (value % 0x400)
|
||||
return strformat ("\\u%.4x\\u%.4x", highsur, lowsur)
|
||||
else
|
||||
return ""
|
||||
end
|
||||
end
|
||||
|
||||
local function fsub (str, pattern, repl)
|
||||
-- gsub always builds a new string in a buffer, even when no match
|
||||
-- exists. First using find should be more efficient when most strings
|
||||
-- don't contain the pattern.
|
||||
if strfind (str, pattern) then
|
||||
return gsub (str, pattern, repl)
|
||||
else
|
||||
return str
|
||||
end
|
||||
end
|
||||
|
||||
local function quotestring (value)
|
||||
-- based on the regexp "escapable" in https://github.com/douglascrockford/JSON-js
|
||||
value = fsub (value, "[%z\1-\31\"\\\127]", escapeutf8)
|
||||
if strfind (value, "[\194\216\220\225\226\239]") then
|
||||
value = fsub (value, "\194[\128-\159\173]", escapeutf8)
|
||||
value = fsub (value, "\216[\128-\132]", escapeutf8)
|
||||
value = fsub (value, "\220\143", escapeutf8)
|
||||
value = fsub (value, "\225\158[\180\181]", escapeutf8)
|
||||
value = fsub (value, "\226\128[\140-\143\168-\175]", escapeutf8)
|
||||
value = fsub (value, "\226\129[\160-\175]", escapeutf8)
|
||||
value = fsub (value, "\239\187\191", escapeutf8)
|
||||
value = fsub (value, "\239\191[\176-\191]", escapeutf8)
|
||||
end
|
||||
return "\"" .. value .. "\""
|
||||
end
|
||||
json.quotestring = quotestring
|
||||
|
||||
local function addnewline2 (level, buffer, buflen)
|
||||
buffer[buflen+1] = "\n"
|
||||
buffer[buflen+2] = strrep (" ", level)
|
||||
buflen = buflen + 2
|
||||
return buflen
|
||||
end
|
||||
|
||||
function json.addnewline (state)
|
||||
if state.indent then
|
||||
state.bufferlen = addnewline2 (state.level or 0,
|
||||
state.buffer, state.bufferlen or #(state.buffer))
|
||||
end
|
||||
end
|
||||
|
||||
local encode2 -- forward declaration
|
||||
|
||||
local function addpair (key, value, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||
local kt = type (key)
|
||||
if kt ~= 'string' and kt ~= 'number' then
|
||||
return nil, "type '" .. kt .. "' is not supported as a key by JSON."
|
||||
end
|
||||
if prev then
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = ","
|
||||
end
|
||||
if indent then
|
||||
buflen = addnewline2 (level, buffer, buflen)
|
||||
end
|
||||
buffer[buflen+1] = quotestring (key)
|
||||
buffer[buflen+2] = ":"
|
||||
return encode2 (value, indent, level, buffer, buflen + 2, tables, globalorder)
|
||||
end
|
||||
|
||||
encode2 = function (value, indent, level, buffer, buflen, tables, globalorder)
|
||||
local valtype = type (value)
|
||||
local valmeta = getmetatable (value)
|
||||
valmeta = type (valmeta) == 'table' and valmeta -- only tables
|
||||
local valtojson = valmeta and valmeta.__tojson
|
||||
if valtojson then
|
||||
if tables[value] then
|
||||
return nil, "reference cycle"
|
||||
end
|
||||
tables[value] = true
|
||||
local state = {
|
||||
indent = indent, level = level, buffer = buffer,
|
||||
bufferlen = buflen, tables = tables, keyorder = globalorder
|
||||
}
|
||||
local ret, msg = valtojson (value, state)
|
||||
if not ret then return nil, msg end
|
||||
tables[value] = nil
|
||||
buflen = state.bufferlen
|
||||
if type (ret) == 'string' then
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = ret
|
||||
end
|
||||
elseif value == nil then
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = "null"
|
||||
elseif valtype == 'number' then
|
||||
local s
|
||||
if value ~= value or value >= huge or -value >= huge then
|
||||
-- This is the behaviour of the original JSON implementation.
|
||||
s = "null"
|
||||
else
|
||||
s = tostring (value)
|
||||
end
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = s
|
||||
elseif valtype == 'boolean' then
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = value and "true" or "false"
|
||||
elseif valtype == 'string' then
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = quotestring (value)
|
||||
elseif valtype == 'table' then
|
||||
if tables[value] then
|
||||
return nil, "reference cycle"
|
||||
end
|
||||
tables[value] = true
|
||||
level = level + 1
|
||||
local isa, n = isarray (value)
|
||||
if n == 0 and valmeta and valmeta.__jsontype == 'object' then
|
||||
isa = false
|
||||
end
|
||||
local msg
|
||||
if isa then -- JSON array
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = "["
|
||||
for i = 1, n do
|
||||
buflen, msg = encode2 (value[i], indent, level, buffer, buflen, tables, globalorder)
|
||||
if not buflen then return nil, msg end
|
||||
if i < n then
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = ","
|
||||
end
|
||||
end
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = "]"
|
||||
else -- JSON object
|
||||
local prev = false
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = "{"
|
||||
local order = valmeta and valmeta.__jsonorder or globalorder
|
||||
if order then
|
||||
local used = {}
|
||||
n = #order
|
||||
for i = 1, n do
|
||||
local k = order[i]
|
||||
local v = value[k]
|
||||
if v then
|
||||
used[k] = true
|
||||
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||
prev = true -- add a seperator before the next element
|
||||
end
|
||||
end
|
||||
for k,v in pairs (value) do
|
||||
if not used[k] then
|
||||
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||
if not buflen then return nil, msg end
|
||||
prev = true -- add a seperator before the next element
|
||||
end
|
||||
end
|
||||
else -- unordered
|
||||
for k,v in pairs (value) do
|
||||
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||
if not buflen then return nil, msg end
|
||||
prev = true -- add a seperator before the next element
|
||||
end
|
||||
end
|
||||
if indent then
|
||||
buflen = addnewline2 (level - 1, buffer, buflen)
|
||||
end
|
||||
buflen = buflen + 1
|
||||
buffer[buflen] = "}"
|
||||
end
|
||||
tables[value] = nil
|
||||
else
|
||||
return nil, "type '" .. valtype .. "' is not supported by JSON."
|
||||
end
|
||||
return buflen
|
||||
end
|
||||
|
||||
function json.encode (value, state)
|
||||
state = state or {}
|
||||
local oldbuffer = state.buffer
|
||||
local buffer = oldbuffer or {}
|
||||
local ret, msg = encode2 (value, state.indent, state.level or 0,
|
||||
buffer, state.bufferlen or 0, state.tables or {}, state.keyorder)
|
||||
if not ret then
|
||||
error (msg, 2)
|
||||
elseif oldbuffer then
|
||||
state.bufferlen = ret
|
||||
return true
|
||||
else
|
||||
return concat (buffer)
|
||||
end
|
||||
end
|
||||
|
||||
local function loc (str, where)
|
||||
local line, pos, linepos = 1, 1, 0
|
||||
while true do
|
||||
pos = strfind (str, "\n", pos, true)
|
||||
if pos and pos < where then
|
||||
line = line + 1
|
||||
linepos = pos
|
||||
pos = pos + 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
return "line " .. line .. ", column " .. (where - linepos)
|
||||
end
|
||||
|
||||
local function unterminated (str, what, where)
|
||||
return nil, strlen (str) + 1, "unterminated " .. what .. " at " .. loc (str, where)
|
||||
end
|
||||
|
||||
local function scanwhite (str, pos)
|
||||
while true do
|
||||
pos = strfind (str, "%S", pos)
|
||||
if not pos then return nil end
|
||||
if strsub (str, pos, pos + 2) == "\239\187\191" then
|
||||
-- UTF-8 Byte Order Mark
|
||||
pos = pos + 3
|
||||
else
|
||||
return pos
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local escapechars = {
|
||||
["\""] = "\"", ["\\"] = "\\", ["/"] = "/", ["b"] = "\b", ["f"] = "\f",
|
||||
["n"] = "\n", ["r"] = "\r", ["t"] = "\t"
|
||||
}
|
||||
|
||||
local function unichar (value)
|
||||
if value < 0 then
|
||||
return nil
|
||||
elseif value <= 0x007f then
|
||||
return strchar (value)
|
||||
elseif value <= 0x07ff then
|
||||
return strchar (0xc0 + floor(value/0x40),
|
||||
0x80 + (floor(value) % 0x40))
|
||||
elseif value <= 0xffff then
|
||||
return strchar (0xe0 + floor(value/0x1000),
|
||||
0x80 + (floor(value/0x40) % 0x40),
|
||||
0x80 + (floor(value) % 0x40))
|
||||
elseif value <= 0x10ffff then
|
||||
return strchar (0xf0 + floor(value/0x40000),
|
||||
0x80 + (floor(value/0x1000) % 0x40),
|
||||
0x80 + (floor(value/0x40) % 0x40),
|
||||
0x80 + (floor(value) % 0x40))
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
local function scanstring (str, pos)
|
||||
local lastpos = pos + 1
|
||||
local buffer, n = {}, 0
|
||||
while true do
|
||||
local nextpos = strfind (str, "[\"\\]", lastpos)
|
||||
if not nextpos then
|
||||
return unterminated (str, "string", pos)
|
||||
end
|
||||
if nextpos > lastpos then
|
||||
n = n + 1
|
||||
buffer[n] = strsub (str, lastpos, nextpos - 1)
|
||||
end
|
||||
if strsub (str, nextpos, nextpos) == "\"" then
|
||||
lastpos = nextpos + 1
|
||||
break
|
||||
else
|
||||
local escchar = strsub (str, nextpos + 1, nextpos + 1)
|
||||
local value
|
||||
if escchar == "u" then
|
||||
value = tonumber (strsub (str, nextpos + 2, nextpos + 5), 16)
|
||||
if value then
|
||||
local value2
|
||||
if 0xD800 <= value and value <= 0xDBff then
|
||||
-- we have the high surrogate of UTF-16. Check if there is a
|
||||
-- low surrogate escaped nearby to combine them.
|
||||
if strsub (str, nextpos + 6, nextpos + 7) == "\\u" then
|
||||
value2 = tonumber (strsub (str, nextpos + 8, nextpos + 11), 16)
|
||||
if value2 and 0xDC00 <= value2 and value2 <= 0xDFFF then
|
||||
value = (value - 0xD800) * 0x400 + (value2 - 0xDC00) + 0x10000
|
||||
else
|
||||
value2 = nil -- in case it was out of range for a low surrogate
|
||||
end
|
||||
end
|
||||
end
|
||||
value = value and unichar (value)
|
||||
if value then
|
||||
if value2 then
|
||||
lastpos = nextpos + 12
|
||||
else
|
||||
lastpos = nextpos + 6
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if not value then
|
||||
value = escapechars[escchar] or escchar
|
||||
lastpos = nextpos + 2
|
||||
end
|
||||
n = n + 1
|
||||
buffer[n] = value
|
||||
end
|
||||
end
|
||||
if n == 1 then
|
||||
return buffer[1], lastpos
|
||||
elseif n > 1 then
|
||||
return concat (buffer), lastpos
|
||||
else
|
||||
return "", lastpos
|
||||
end
|
||||
end
|
||||
|
||||
local scanvalue -- forward declaration
|
||||
|
||||
local function scantable (what, closechar, str, startpos, nullval, objectmeta, arraymeta)
|
||||
local len = strlen (str)
|
||||
local tbl, n = {}, 0
|
||||
local pos = startpos + 1
|
||||
if what == 'object' then
|
||||
setmetatable (tbl, objectmeta)
|
||||
else
|
||||
setmetatable (tbl, arraymeta)
|
||||
end
|
||||
while true do
|
||||
pos = scanwhite (str, pos)
|
||||
if not pos then return unterminated (str, what, startpos) end
|
||||
local char = strsub (str, pos, pos)
|
||||
if char == closechar then
|
||||
return tbl, pos + 1
|
||||
end
|
||||
local val1, err
|
||||
val1, pos, err = scanvalue (str, pos, nullval, objectmeta, arraymeta)
|
||||
if err then return nil, pos, err end
|
||||
pos = scanwhite (str, pos)
|
||||
if not pos then return unterminated (str, what, startpos) end
|
||||
char = strsub (str, pos, pos)
|
||||
if char == ":" then
|
||||
if val1 == nil then
|
||||
return nil, pos, "cannot use nil as table index (at " .. loc (str, pos) .. ")"
|
||||
end
|
||||
pos = scanwhite (str, pos + 1)
|
||||
if not pos then return unterminated (str, what, startpos) end
|
||||
local val2
|
||||
val2, pos, err = scanvalue (str, pos, nullval, objectmeta, arraymeta)
|
||||
if err then return nil, pos, err end
|
||||
tbl[val1] = val2
|
||||
pos = scanwhite (str, pos)
|
||||
if not pos then return unterminated (str, what, startpos) end
|
||||
char = strsub (str, pos, pos)
|
||||
else
|
||||
n = n + 1
|
||||
tbl[n] = val1
|
||||
end
|
||||
if char == "," then
|
||||
pos = pos + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
scanvalue = function (str, pos, nullval, objectmeta, arraymeta)
|
||||
pos = pos or 1
|
||||
pos = scanwhite (str, pos)
|
||||
if not pos then
|
||||
return nil, strlen (str) + 1, "no valid JSON value (reached the end)"
|
||||
end
|
||||
local char = strsub (str, pos, pos)
|
||||
if char == "{" then
|
||||
return scantable ('object', "}", str, pos, nullval, objectmeta, arraymeta)
|
||||
elseif char == "[" then
|
||||
return scantable ('array', "]", str, pos, nullval, objectmeta, arraymeta)
|
||||
elseif char == "\"" then
|
||||
return scanstring (str, pos)
|
||||
else
|
||||
local pstart, pend = strfind (str, "^%-?[%d%.]+[eE]?[%+%-]?%d*", pos)
|
||||
if pstart then
|
||||
local number = tonumber (strsub (str, pstart, pend))
|
||||
if number then
|
||||
return number, pend + 1
|
||||
end
|
||||
end
|
||||
pstart, pend = strfind (str, "^%a%w*", pos)
|
||||
if pstart then
|
||||
local name = strsub (str, pstart, pend)
|
||||
if name == "true" then
|
||||
return true, pend + 1
|
||||
elseif name == "false" then
|
||||
return false, pend + 1
|
||||
elseif name == "null" then
|
||||
return nullval, pend + 1
|
||||
end
|
||||
end
|
||||
return nil, pos, "no valid JSON value at " .. loc (str, pos)
|
||||
end
|
||||
end
|
||||
|
||||
local function optionalmetatables(...)
|
||||
if select("#", ...) > 0 then
|
||||
return ...
|
||||
else
|
||||
return {__jsontype = 'object'}, {__jsontype = 'array'}
|
||||
end
|
||||
end
|
||||
|
||||
function json.decode (str, pos, nullval, ...)
|
||||
local objectmeta, arraymeta = optionalmetatables(...)
|
||||
return scanvalue (str, pos, nullval, objectmeta, arraymeta)
|
||||
end
|
||||
|
||||
function json.use_lpeg ()
|
||||
local g = require ("lpeg")
|
||||
local pegmatch = g.match
|
||||
local P, S, R, V = g.P, g.S, g.R, g.V
|
||||
|
||||
local function ErrorCall (str, pos, msg, state)
|
||||
if not state.msg then
|
||||
state.msg = msg .. " at " .. loc (str, pos)
|
||||
state.pos = pos
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function Err (msg)
|
||||
return g.Cmt (g.Cc (msg) * g.Carg (2), ErrorCall)
|
||||
end
|
||||
|
||||
local Space = (S" \n\r\t" + P"\239\187\191")^0
|
||||
|
||||
local PlainChar = 1 - S"\"\\\n\r"
|
||||
local EscapeSequence = (P"\\" * g.C (S"\"\\/bfnrt" + Err "unsupported escape sequence")) / escapechars
|
||||
local HexDigit = R("09", "af", "AF")
|
||||
local function UTF16Surrogate (match, pos, high, low)
|
||||
high, low = tonumber (high, 16), tonumber (low, 16)
|
||||
if 0xD800 <= high and high <= 0xDBff and 0xDC00 <= low and low <= 0xDFFF then
|
||||
return true, unichar ((high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000)
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
local function UTF16BMP (hex)
|
||||
return unichar (tonumber (hex, 16))
|
||||
end
|
||||
local U16Sequence = (P"\\u" * g.C (HexDigit * HexDigit * HexDigit * HexDigit))
|
||||
local UnicodeEscape = g.Cmt (U16Sequence * U16Sequence, UTF16Surrogate) + U16Sequence/UTF16BMP
|
||||
local Char = UnicodeEscape + EscapeSequence + PlainChar
|
||||
local String = P"\"" * g.Cs (Char ^ 0) * (P"\"" + Err "unterminated string")
|
||||
local Integer = P"-"^(-1) * (P"0" + (R"19" * R"09"^0))
|
||||
local Fractal = P"." * R"09"^0
|
||||
local Exponent = (S"eE") * (S"+-")^(-1) * R"09"^1
|
||||
local Number = (Integer * Fractal^(-1) * Exponent^(-1))/tonumber
|
||||
local Constant = P"true" * g.Cc (true) + P"false" * g.Cc (false) + P"null" * g.Carg (1)
|
||||
local SimpleValue = Number + String + Constant
|
||||
local ArrayContent, ObjectContent
|
||||
|
||||
-- The functions parsearray and parseobject parse only a single value/pair
|
||||
-- at a time and store them directly to avoid hitting the LPeg limits.
|
||||
local function parsearray (str, pos, nullval, state)
|
||||
local obj, cont
|
||||
local npos
|
||||
local t, nt = {}, 0
|
||||
repeat
|
||||
obj, cont, npos = pegmatch (ArrayContent, str, pos, nullval, state)
|
||||
if not npos then break end
|
||||
pos = npos
|
||||
nt = nt + 1
|
||||
t[nt] = obj
|
||||
until cont == 'last'
|
||||
return pos, setmetatable (t, state.arraymeta)
|
||||
end
|
||||
|
||||
local function parseobject (str, pos, nullval, state)
|
||||
local obj, key, cont
|
||||
local npos
|
||||
local t = {}
|
||||
repeat
|
||||
key, obj, cont, npos = pegmatch (ObjectContent, str, pos, nullval, state)
|
||||
if not npos then break end
|
||||
pos = npos
|
||||
t[key] = obj
|
||||
until cont == 'last'
|
||||
return pos, setmetatable (t, state.objectmeta)
|
||||
end
|
||||
|
||||
local Array = P"[" * g.Cmt (g.Carg(1) * g.Carg(2), parsearray) * Space * (P"]" + Err "']' expected")
|
||||
local Object = P"{" * g.Cmt (g.Carg(1) * g.Carg(2), parseobject) * Space * (P"}" + Err "'}' expected")
|
||||
local Value = Space * (Array + Object + SimpleValue)
|
||||
local ExpectedValue = Value + Space * Err "value expected"
|
||||
ArrayContent = Value * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp()
|
||||
local Pair = g.Cg (Space * String * Space * (P":" + Err "colon expected") * ExpectedValue)
|
||||
ObjectContent = Pair * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp()
|
||||
local DecodeValue = ExpectedValue * g.Cp ()
|
||||
|
||||
function json.decode (str, pos, nullval, ...)
|
||||
local state = {}
|
||||
state.objectmeta, state.arraymeta = optionalmetatables(...)
|
||||
local obj, retpos = pegmatch (DecodeValue, str, pos, nullval, state)
|
||||
if state.msg then
|
||||
return nil, state.pos, state.msg
|
||||
else
|
||||
return obj, retpos
|
||||
end
|
||||
end
|
||||
|
||||
-- use this function only once:
|
||||
json.use_lpeg = function () return json end
|
||||
|
||||
json.using_lpeg = true
|
||||
|
||||
return json -- so you can get the module using json = require "dkjson".use_lpeg()
|
||||
end
|
||||
|
||||
if always_try_using_lpeg then
|
||||
pcall (json.use_lpeg)
|
||||
end
|
||||
|
||||
return json
|
||||
|
||||
-->
|
233
redist/Lua/dumper.lua
Normal file
233
redist/Lua/dumper.lua
Normal file
@ -0,0 +1,233 @@
|
||||
--[[ DataDumper.lua
|
||||
Copyright (c) 2007 Olivetti-Engineering SA
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
]]
|
||||
|
||||
local dumplua_closure = [[
|
||||
local closures = {}
|
||||
local function closure(t)
|
||||
closures[#closures+1] = t
|
||||
t[1] = assert(loadstring(t[1]))
|
||||
return t[1]
|
||||
end
|
||||
|
||||
for _,t in pairs(closures) do
|
||||
for i = 2,#t do
|
||||
debug.setupvalue(t[1], i-1, t[i])
|
||||
end
|
||||
end
|
||||
]]
|
||||
|
||||
local lua_reserved_keywords = {
|
||||
'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for',
|
||||
'function', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat',
|
||||
'return', 'then', 'true', 'until', 'while' }
|
||||
|
||||
local function keys(t)
|
||||
local res = {}
|
||||
local oktypes = { stringstring = true, numbernumber = true }
|
||||
local function cmpfct(a,b)
|
||||
if oktypes[type(a)..type(b)] then
|
||||
return a < b
|
||||
else
|
||||
return type(a) < type(b)
|
||||
end
|
||||
end
|
||||
for k in pairs(t) do
|
||||
res[#res+1] = k
|
||||
end
|
||||
table.sort(res, cmpfct)
|
||||
return res
|
||||
end
|
||||
|
||||
local c_functions = {}
|
||||
for _,lib in pairs{'_G', 'string', 'table', 'math',
|
||||
'io', 'os', 'coroutine', 'package', 'debug'} do
|
||||
local t = _G[lib] or {}
|
||||
lib = lib .. "."
|
||||
if lib == "_G." then lib = "" end
|
||||
for k,v in pairs(t) do
|
||||
if type(v) == 'function' and not pcall(string.dump, v) then
|
||||
c_functions[v] = lib..k
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function DataDumper(value, varname, fastmode, ident)
|
||||
local defined, dumplua = {}
|
||||
-- Local variables for speed optimization
|
||||
local string_format, type, string_dump, string_rep =
|
||||
string.format, type, string.dump, string.rep
|
||||
local tostring, pairs, table_concat =
|
||||
tostring, pairs, table.concat
|
||||
local keycache, strvalcache, out, closure_cnt = {}, {}, {}, 0
|
||||
setmetatable(strvalcache, {__index = function(t,value)
|
||||
local res = string_format('%q', value)
|
||||
t[value] = res
|
||||
return res
|
||||
end})
|
||||
local fcts = {
|
||||
string = function(value) return strvalcache[value] end,
|
||||
number = function(value) return value end,
|
||||
boolean = function(value) return tostring(value) end,
|
||||
['nil'] = function(value) return 'nil' end,
|
||||
['function'] = function(value)
|
||||
return string_format("loadstring(%q)", string_dump(value))
|
||||
end,
|
||||
userdata = function() error("Cannot dump userdata") end,
|
||||
thread = function() error("Cannot dump threads") end,
|
||||
}
|
||||
local function test_defined(value, path)
|
||||
if defined[value] then
|
||||
if path:match("^getmetatable.*%)$") then
|
||||
out[#out+1] = string_format("s%s, %s)\n", path:sub(2,-2), defined[value])
|
||||
else
|
||||
out[#out+1] = path .. " = " .. defined[value] .. "\n"
|
||||
end
|
||||
return true
|
||||
end
|
||||
defined[value] = path
|
||||
end
|
||||
local function make_key(t, key)
|
||||
local s
|
||||
if type(key) == 'string' and key:match('^[_%a][_%w]*$') then
|
||||
s = key .. "="
|
||||
else
|
||||
s = "[" .. dumplua(key, 0) .. "]="
|
||||
end
|
||||
t[key] = s
|
||||
return s
|
||||
end
|
||||
for _,k in ipairs(lua_reserved_keywords) do
|
||||
keycache[k] = '["'..k..'"] = '
|
||||
end
|
||||
if fastmode then
|
||||
fcts.table = function (value)
|
||||
-- Table value
|
||||
local numidx = 1
|
||||
out[#out+1] = "{"
|
||||
for key,val in pairs(value) do
|
||||
if key == numidx then
|
||||
numidx = numidx + 1
|
||||
else
|
||||
out[#out+1] = keycache[key]
|
||||
end
|
||||
local str = dumplua(val)
|
||||
out[#out+1] = str..","
|
||||
end
|
||||
if string.sub(out[#out], -1) == "," then
|
||||
out[#out] = string.sub(out[#out], 1, -2);
|
||||
end
|
||||
out[#out+1] = "}"
|
||||
return ""
|
||||
end
|
||||
else
|
||||
fcts.table = function (value, ident, path)
|
||||
if test_defined(value, path) then return "nil" end
|
||||
-- Table value
|
||||
local sep, str, numidx, totallen = " ", {}, 1, 0
|
||||
local meta, metastr = (debug or getfenv()).getmetatable(value)
|
||||
if meta then
|
||||
ident = ident + 1
|
||||
metastr = dumplua(meta, ident, "getmetatable("..path..")")
|
||||
totallen = totallen + #metastr + 16
|
||||
end
|
||||
for _,key in pairs(keys(value)) do
|
||||
local val = value[key]
|
||||
local s = ""
|
||||
local subpath = path
|
||||
if key == numidx then
|
||||
subpath = subpath .. "[" .. numidx .. "]"
|
||||
numidx = numidx + 1
|
||||
else
|
||||
s = keycache[key]
|
||||
if not s:match "^%[" then subpath = subpath .. "." end
|
||||
subpath = subpath .. s:gsub("%s*=%s*$","")
|
||||
end
|
||||
s = s .. dumplua(val, ident+1, subpath)
|
||||
str[#str+1] = s
|
||||
totallen = totallen + #s + 2
|
||||
end
|
||||
if totallen > 80 then
|
||||
sep = "\n" .. string_rep(" ", ident+1)
|
||||
end
|
||||
str = "{"..sep..table_concat(str, ","..sep).." "..sep:sub(1,-3).."}"
|
||||
if meta then
|
||||
sep = sep:sub(1,-3)
|
||||
return "setmetatable("..sep..str..","..sep..metastr..sep:sub(1,-3)..")"
|
||||
end
|
||||
return str
|
||||
end
|
||||
fcts['function'] = function (value, ident, path)
|
||||
if test_defined(value, path) then return "nil" end
|
||||
if c_functions[value] then
|
||||
return c_functions[value]
|
||||
elseif debug == nil or debug.getupvalue(value, 1) == nil then
|
||||
return string_format("loadstring(%q)", string_dump(value))
|
||||
end
|
||||
closure_cnt = closure_cnt + 1
|
||||
local res = {string.dump(value)}
|
||||
for i = 1,math.huge do
|
||||
local name, v = debug.getupvalue(value,i)
|
||||
if name == nil then break end
|
||||
res[i+1] = v
|
||||
end
|
||||
return "closure " .. dumplua(res, ident, "closures["..closure_cnt.."]")
|
||||
end
|
||||
end
|
||||
function dumplua(value, ident, path)
|
||||
return fcts[type(value)](value, ident, path)
|
||||
end
|
||||
if varname == nil then
|
||||
varname = "return "
|
||||
elseif varname:match("^[%a_][%w_]*$") then
|
||||
varname = varname .. " = "
|
||||
end
|
||||
if fastmode then
|
||||
setmetatable(keycache, {__index = make_key })
|
||||
out[1] = varname
|
||||
table.insert(out,dumplua(value, 0))
|
||||
return table.concat(out)
|
||||
else
|
||||
setmetatable(keycache, {__index = make_key })
|
||||
local items = {}
|
||||
for i=1,10 do items[i] = '' end
|
||||
items[3] = dumplua(value, ident or 0, "t")
|
||||
if closure_cnt > 0 then
|
||||
items[1], items[6] = dumplua_closure:match("(.*\n)\n(.*)")
|
||||
out[#out+1] = ""
|
||||
end
|
||||
if #out > 0 then
|
||||
items[2], items[4] = "local t = ", "\n"
|
||||
items[5] = table.concat(out)
|
||||
items[7] = varname .. "t"
|
||||
else
|
||||
items[2] = varname
|
||||
end
|
||||
return table.concat(items)
|
||||
end
|
||||
end
|
||||
|
||||
function dump(value)
|
||||
return '<pre>'..DataDumper(value)..'</pre>'
|
||||
end
|
5
redist/Lua/helper/view.lua
Normal file
5
redist/Lua/helper/view.lua
Normal file
@ -0,0 +1,5 @@
|
||||
require "Lua.logger"
|
||||
|
||||
function _out(value)
|
||||
response:out(tostring(value))
|
||||
end
|
5
redist/Lua/html/form.lua
Normal file
5
redist/Lua/html/form.lua
Normal file
@ -0,0 +1,5 @@
|
||||
html = {}
|
||||
|
||||
function html:input(type, value)
|
||||
return string.format('<input type="%s" value="%s"/>', type, value)
|
||||
end
|
2
redist/Lua/html/test.lua
Normal file
2
redist/Lua/html/test.lua
Normal file
@ -0,0 +1,2 @@
|
||||
require "..\\helper\\view"
|
||||
require "form"
|
243
redist/Lua/inspect.lua
Normal file
243
redist/Lua/inspect.lua
Normal file
@ -0,0 +1,243 @@
|
||||
-----------------------------------------------------------------------------------------------------------------------
|
||||
-- inspect.lua - v1.2.1 (2013-01)
|
||||
-- Enrique García Cota - enrique.garcia.cota [AT] gmail [DOT] com
|
||||
-- human-readable representations of tables.
|
||||
-- inspired by http://lua-users.org/wiki/TableSerialization
|
||||
-----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
local inspect ={}
|
||||
inspect.__VERSION = '1.2.0'
|
||||
|
||||
-- Apostrophizes the string if it has quotes, but not aphostrophes
|
||||
-- Otherwise, it returns a regular quoted string
|
||||
local function smartQuote(str)
|
||||
if string.match( string.gsub(str,"[^'\"]",""), '^"+$' ) then
|
||||
return "'" .. str .. "'"
|
||||
end
|
||||
return string.format("%q", str )
|
||||
end
|
||||
|
||||
local controlCharsTranslation = {
|
||||
["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
|
||||
["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\\"] = "\\\\"
|
||||
}
|
||||
|
||||
local function unescapeChar(c) return controlCharsTranslation[c] end
|
||||
|
||||
local function unescape(str)
|
||||
local result, _ = string.gsub( str, "(%c)", unescapeChar )
|
||||
return result
|
||||
end
|
||||
|
||||
local function isIdentifier(str)
|
||||
return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
|
||||
end
|
||||
|
||||
local function isArrayKey(k, length)
|
||||
return type(k)=='number' and 1 <= k and k <= length
|
||||
end
|
||||
|
||||
local function isDictionaryKey(k, length)
|
||||
return not isArrayKey(k, length)
|
||||
end
|
||||
|
||||
local sortOrdersByType = {
|
||||
['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4,
|
||||
['function'] = 5, ['userdata'] = 6, ['thread'] = 7
|
||||
}
|
||||
|
||||
local function sortKeys(a,b)
|
||||
local ta, tb = type(a), type(b)
|
||||
if ta ~= tb then return sortOrdersByType[ta] < sortOrdersByType[tb] end
|
||||
if ta == 'string' or ta == 'number' then return a < b end
|
||||
return false
|
||||
end
|
||||
|
||||
local function getDictionaryKeys(t)
|
||||
local length = #t
|
||||
local keys = {}
|
||||
for k,_ in pairs(t) do
|
||||
if isDictionaryKey(k, length) then table.insert(keys,k) end
|
||||
end
|
||||
table.sort(keys, sortKeys)
|
||||
return keys
|
||||
end
|
||||
|
||||
local function getToStringResultSafely(t, mt)
|
||||
local __tostring = type(mt) == 'table' and rawget(mt, '__tostring')
|
||||
local string, status
|
||||
if type(__tostring) == 'function' then
|
||||
status, string = pcall(__tostring, t)
|
||||
string = status and string or 'error: ' .. tostring(string)
|
||||
end
|
||||
return string
|
||||
end
|
||||
|
||||
local Inspector = {}
|
||||
|
||||
function Inspector:new(t, depth)
|
||||
local inspector = {
|
||||
buffer = {},
|
||||
depth = depth,
|
||||
level = 0,
|
||||
maxIds = {
|
||||
['function'] = 0,
|
||||
['userdata'] = 0,
|
||||
['thread'] = 0,
|
||||
['table'] = 0
|
||||
},
|
||||
ids = {
|
||||
['function'] = setmetatable({}, {__mode = "kv"}),
|
||||
['userdata'] = setmetatable({}, {__mode = "kv"}),
|
||||
['thread'] = setmetatable({}, {__mode = "kv"}),
|
||||
['table'] = setmetatable({}, {__mode = "kv"})
|
||||
},
|
||||
tableAppearances = setmetatable({}, {__mode = "k"})
|
||||
}
|
||||
|
||||
setmetatable(inspector, {__index = Inspector})
|
||||
|
||||
inspector:countTableAppearances(t)
|
||||
|
||||
return inspector:putValue(t)
|
||||
end
|
||||
|
||||
function Inspector:countTableAppearances(t)
|
||||
if type(t) == 'table' then
|
||||
if not self.tableAppearances[t] then
|
||||
self.tableAppearances[t] = 1
|
||||
for k,v in pairs(t) do
|
||||
self:countTableAppearances(k)
|
||||
self:countTableAppearances(v)
|
||||
end
|
||||
self:countTableAppearances(getmetatable(t))
|
||||
else
|
||||
self.tableAppearances[t] = self.tableAppearances[t] + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Inspector:tabify()
|
||||
self:puts("\n", string.rep(" ", self.level))
|
||||
return self
|
||||
end
|
||||
|
||||
function Inspector:up()
|
||||
self.level = self.level - 1
|
||||
end
|
||||
|
||||
function Inspector:down()
|
||||
self.level = self.level + 1
|
||||
end
|
||||
|
||||
function Inspector:puts(...)
|
||||
local args = {...}
|
||||
local len = #self.buffer
|
||||
for i=1, #args do
|
||||
len = len + 1
|
||||
self.buffer[len] = tostring(args[i])
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Inspector:commaControl(comma)
|
||||
if comma then self:puts(',') end
|
||||
return true
|
||||
end
|
||||
|
||||
function Inspector:putTable(t)
|
||||
if self:alreadyVisited(t) then
|
||||
self:puts('<table ', self:getId(t), '>')
|
||||
elseif self.depth and self.level >= self.depth then
|
||||
self:puts('{...}')
|
||||
else
|
||||
if self.tableAppearances[t] > 1 then
|
||||
self:puts('<',self:getId(t),'>')
|
||||
end
|
||||
self:puts('{')
|
||||
self:down()
|
||||
|
||||
local length = #t
|
||||
local mt = getmetatable(t)
|
||||
|
||||
local string = getToStringResultSafely(t, mt)
|
||||
if type(string) == 'string' and #string > 0 then
|
||||
self:puts(' -- ', unescape(string))
|
||||
if length >= 1 then self:tabify() end -- tabify the array values
|
||||
end
|
||||
|
||||
local comma = false
|
||||
for i=1, length do
|
||||
comma = self:commaControl(comma)
|
||||
self:puts(' '):putValue(t[i])
|
||||
end
|
||||
|
||||
local dictKeys = getDictionaryKeys(t)
|
||||
|
||||
for _,k in ipairs(dictKeys) do
|
||||
comma = self:commaControl(comma)
|
||||
self:tabify():putKey(k):puts(' = '):putValue(t[k])
|
||||
end
|
||||
|
||||
if mt then
|
||||
comma = self:commaControl(comma)
|
||||
self:tabify():puts('<metatable> = '):putValue(mt)
|
||||
end
|
||||
|
||||
self:up()
|
||||
|
||||
if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
|
||||
self:tabify()
|
||||
elseif length > 0 then -- array tables have one extra space before closing }
|
||||
self:puts(' ')
|
||||
end
|
||||
self:puts('}')
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Inspector:alreadyVisited(v)
|
||||
return self.ids[type(v)][v] ~= nil
|
||||
end
|
||||
|
||||
function Inspector:getId(v)
|
||||
local tv = type(v)
|
||||
local id = self.ids[tv][v]
|
||||
if not id then
|
||||
id = self.maxIds[tv] + 1
|
||||
self.maxIds[tv] = id
|
||||
self.ids[tv][v] = id
|
||||
end
|
||||
return id
|
||||
end
|
||||
|
||||
function Inspector:putValue(v)
|
||||
local tv = type(v)
|
||||
|
||||
if tv == 'string' then
|
||||
self:puts(smartQuote(unescape(v)))
|
||||
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
|
||||
self:puts(tostring(v))
|
||||
elseif tv == 'table' then
|
||||
self:putTable(v)
|
||||
else
|
||||
self:puts('<',tv,' ',self:getId(v),'>')
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Inspector:putKey(k)
|
||||
if isIdentifier(k) then return self:puts(k) end
|
||||
return self:puts( "[" ):putValue(k):puts("]")
|
||||
end
|
||||
|
||||
function Inspector:tostring()
|
||||
return table.concat(self.buffer)
|
||||
end
|
||||
|
||||
setmetatable(inspect, { __call = function(_,t,depth)
|
||||
return Inspector:new(t, depth):tostring()
|
||||
end })
|
||||
|
||||
return inspect
|
||||
|
2
redist/Lua/logger.lua
Normal file
2
redist/Lua/logger.lua
Normal file
@ -0,0 +1,2 @@
|
||||
require "Lua.logging.file"
|
||||
logger = logging.file(__log_file, "%Y-%m-%d")
|
189
redist/Lua/logging.lua
Normal file
189
redist/Lua/logging.lua
Normal file
@ -0,0 +1,189 @@
|
||||
-------------------------------------------------------------------------------
|
||||
-- includes a new tostring function that handles tables recursively
|
||||
--
|
||||
-- @author Danilo Tuler (tuler@ideais.com.br)
|
||||
-- @author Andre Carregal (info@keplerproject.org)
|
||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
||||
--
|
||||
-- @copyright 2004-2011 Kepler Project
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
local type, table, string, _tostring, tonumber = type, table, string, tostring, tonumber
|
||||
local select = select
|
||||
local error = error
|
||||
local format = string.format
|
||||
|
||||
module("logging")
|
||||
|
||||
-- Meta information
|
||||
_COPYRIGHT = "Copyright (C) 2004-2011 Kepler Project"
|
||||
_DESCRIPTION = "A simple API to use logging features in Lua"
|
||||
_VERSION = "LuaLogging 1.1.4"
|
||||
|
||||
-- The DEBUG Level designates fine-grained instring.formational events that are most
|
||||
-- useful to debug an application
|
||||
DEBUG = "DEBUG"
|
||||
|
||||
-- The INFO level designates instring.formational messages that highlight the
|
||||
-- progress of the application at coarse-grained level
|
||||
INFO = "INFO"
|
||||
|
||||
-- The WARN level designates potentially harmful situations
|
||||
WARN = "WARN"
|
||||
|
||||
-- The ERROR level designates error events that might still allow the
|
||||
-- application to continue running
|
||||
ERROR = "ERROR"
|
||||
|
||||
-- The FATAL level designates very severe error events that will presumably
|
||||
-- lead the application to abort
|
||||
FATAL = "FATAL"
|
||||
|
||||
local LEVEL = {"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
|
||||
local MAX_LEVELS = #LEVEL
|
||||
-- make level names to order
|
||||
for i=1,MAX_LEVELS do
|
||||
LEVEL[LEVEL[i]] = i
|
||||
end
|
||||
|
||||
-- private log function, with support for formating a complex log message.
|
||||
local function LOG_MSG(self, level, fmt, ...)
|
||||
local f_type = type(fmt)
|
||||
if f_type == 'string' then
|
||||
if select('#', ...) > 0 then
|
||||
return self:append(level, format(fmt, ...))
|
||||
else
|
||||
-- only a single string, no formating needed.
|
||||
return self:append(level, fmt)
|
||||
end
|
||||
elseif f_type == 'function' then
|
||||
-- fmt should be a callable function which returns the message to log
|
||||
return self:append(level, fmt(...))
|
||||
end
|
||||
-- fmt is not a string and not a function, just call tostring() on it.
|
||||
return self:append(level, tostring(fmt))
|
||||
end
|
||||
|
||||
-- create the proxy functions for each log level.
|
||||
local LEVEL_FUNCS = {}
|
||||
for i=1,MAX_LEVELS do
|
||||
local level = LEVEL[i]
|
||||
LEVEL_FUNCS[i] = function(self, ...)
|
||||
-- no level checking needed here, this function will only be called if it's level is active.
|
||||
return LOG_MSG(self, level, ...)
|
||||
end
|
||||
end
|
||||
|
||||
-- do nothing function for disabled levels.
|
||||
local function disable_level() end
|
||||
|
||||
-- improved assertion funciton.
|
||||
local function assert(exp, ...)
|
||||
-- if exp is true, we are finished so don't do any processing of the parameters
|
||||
if exp then return exp, ... end
|
||||
-- assertion failed, raise error
|
||||
error(format(...), 2)
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Creates a new logger object
|
||||
-- @param append Function used by the logger to append a message with a
|
||||
-- log-level to the log stream.
|
||||
-- @return Table representing the new logger object.
|
||||
-------------------------------------------------------------------------------
|
||||
function new(append)
|
||||
|
||||
if type(append) ~= "function" then
|
||||
return nil, "Appender must be a function."
|
||||
end
|
||||
|
||||
local logger = {}
|
||||
logger.append = append
|
||||
|
||||
logger.setLevel = function (self, level)
|
||||
local order = LEVEL[level]
|
||||
assert(order, "undefined level `%s'", _tostring(level))
|
||||
self.level = level
|
||||
self.level_order = order
|
||||
-- enable/disable levels
|
||||
for i=1,MAX_LEVELS do
|
||||
local name = LEVEL[i]:lower()
|
||||
if i >= order then
|
||||
self[name] = LEVEL_FUNCS[i]
|
||||
else
|
||||
self[name] = disable_level
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- generic log function.
|
||||
logger.log = function (self, level, ...)
|
||||
local order = LEVEL[level]
|
||||
assert(order, "undefined level `%s'", _tostring(level))
|
||||
if order < self.level_order then
|
||||
return
|
||||
end
|
||||
return LOG_MSG(self, level, ...)
|
||||
end
|
||||
|
||||
-- initialize log level.
|
||||
logger:setLevel(DEBUG)
|
||||
return logger
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Prepares the log message
|
||||
-------------------------------------------------------------------------------
|
||||
function prepareLogMsg(pattern, dt, level, message)
|
||||
|
||||
local logMsg = pattern or "%date %level %message\n"
|
||||
message = string.gsub(message, "%%", "%%%%")
|
||||
logMsg = string.gsub(logMsg, "%%date", dt)
|
||||
logMsg = string.gsub(logMsg, "%%level", level)
|
||||
logMsg = string.gsub(logMsg, "%%message", message)
|
||||
return logMsg
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Converts a Lua value to a string
|
||||
--
|
||||
-- Converts Table fields in alphabetical order
|
||||
-------------------------------------------------------------------------------
|
||||
function tostring(value)
|
||||
local str = ''
|
||||
|
||||
if (type(value) ~= 'table') then
|
||||
if (type(value) == 'string') then
|
||||
str = string.format("%q", value)
|
||||
else
|
||||
str = _tostring(value)
|
||||
end
|
||||
else
|
||||
local auxTable = {}
|
||||
table.foreach(value, function(i, v)
|
||||
if (tonumber(i) ~= i) then
|
||||
table.insert(auxTable, i)
|
||||
else
|
||||
table.insert(auxTable, tostring(i))
|
||||
end
|
||||
end)
|
||||
table.sort(auxTable)
|
||||
|
||||
str = str..'{'
|
||||
local separator = ""
|
||||
local entry = ""
|
||||
table.foreachi (auxTable, function (i, fieldName)
|
||||
if ((tonumber(fieldName)) and (tonumber(fieldName) > 0)) then
|
||||
entry = tostring(value[tonumber(fieldName)])
|
||||
else
|
||||
entry = fieldName.." = "..tostring(value[fieldName])
|
||||
end
|
||||
str = str..separator..entry
|
||||
separator = ", "
|
||||
end)
|
||||
str = str..'}'
|
||||
end
|
||||
return str
|
||||
end
|
22
redist/Lua/logging/console.lua
Normal file
22
redist/Lua/logging/console.lua
Normal file
@ -0,0 +1,22 @@
|
||||
-------------------------------------------------------------------------------
|
||||
-- Prints logging information to console
|
||||
--
|
||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
||||
--
|
||||
-- @copyright 2004-2011 Kepler Project
|
||||
--
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
require"logging"
|
||||
|
||||
function logging.console(logPattern)
|
||||
|
||||
return logging.new( function(self, level, message)
|
||||
io.stdout:write(logging.prepareLogMsg(logPattern, os.date(), level, message))
|
||||
return true
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
return logging.console
|
||||
|
44
redist/Lua/logging/email.lua
Normal file
44
redist/Lua/logging/email.lua
Normal file
@ -0,0 +1,44 @@
|
||||
-------------------------------------------------------------------------------
|
||||
-- Emails logging information to the given recipient
|
||||
--
|
||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
||||
--
|
||||
-- @copyright 2004-2011 Kepler Project
|
||||
--
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
require"logging"
|
||||
local smtp = require"socket.smtp"
|
||||
|
||||
function logging.email(params)
|
||||
|
||||
params = params or {}
|
||||
params.headers = params.headers or {}
|
||||
|
||||
if params.from == nil then
|
||||
return nil, "'from' parameter is required"
|
||||
end
|
||||
if params.rcpt == nil then
|
||||
return nil, "'rcpt' parameter is required"
|
||||
end
|
||||
|
||||
return logging.new( function(self, level, message)
|
||||
local s = logging.prepareLogMsg(params.logPattern, os.date(), level, message)
|
||||
if params.headers.subject then
|
||||
params.headers.subject = logging.prepareLogMsg(params.headers.subject, os.date(), level, message)
|
||||
end
|
||||
local msg = { headers = params.headers, body = s }
|
||||
params.source = smtp.message(msg)
|
||||
|
||||
local r, e = smtp.send(params)
|
||||
if not r then
|
||||
return nil, e
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
return logging.email
|
||||
|
55
redist/Lua/logging/file.lua
Normal file
55
redist/Lua/logging/file.lua
Normal file
@ -0,0 +1,55 @@
|
||||
-------------------------------------------------------------------------------
|
||||
-- Saves logging information in a file
|
||||
--
|
||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
||||
--
|
||||
-- @copyright 2004-2011 Kepler Project
|
||||
--
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
require"logging"
|
||||
|
||||
local lastFileNameDatePattern
|
||||
local lastFileHandler
|
||||
|
||||
local openFileLogger = function (filename, datePattern)
|
||||
|
||||
local filename = string.format(filename, os.date(datePattern))
|
||||
if (lastFileNameDatePattern ~= filename) then
|
||||
|
||||
local f = io.open(filename, "a")
|
||||
if (f) then
|
||||
f:setvbuf ("line")
|
||||
lastFileNameDatePattern = filename
|
||||
lastFileHandler = f
|
||||
return f
|
||||
else
|
||||
return nil, string.format("file `%s' could not be opened for writing", filename)
|
||||
end
|
||||
else
|
||||
return lastFileHandler
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function logging.file(filename, datePattern, logPattern)
|
||||
|
||||
if type(filename) ~= "string" then
|
||||
filename = "lualogging.log"
|
||||
end
|
||||
|
||||
return logging.new( function(self, level, message)
|
||||
|
||||
local f, msg = openFileLogger(filename, datePattern)
|
||||
if not f then
|
||||
return nil, msg
|
||||
end
|
||||
local s = logging.prepareLogMsg(logPattern, os.date(), level, message)
|
||||
f:write(s)
|
||||
return true
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
return logging.file
|
||||
|
81
redist/Lua/logging/rolling_file.lua
Normal file
81
redist/Lua/logging/rolling_file.lua
Normal file
@ -0,0 +1,81 @@
|
||||
---------------------------------------------------------------------------
|
||||
-- RollingFileAppender is a FileAppender that rolls over the logfile
|
||||
-- once it has reached a certain size limit. It also mantains a
|
||||
-- maximum number of log files.
|
||||
--
|
||||
-- @author Tiago Cesar Katcipis (tiagokatcipis@gmail.com)
|
||||
--
|
||||
-- @copyright 2004-2007 Kepler Project
|
||||
---------------------------------------------------------------------------
|
||||
|
||||
require"logging"
|
||||
|
||||
|
||||
local function openFile(self)
|
||||
self.file = io.open(self.filename, "a")
|
||||
if not self.file then
|
||||
return nil, string.format("file `%s' could not be opened for writing", self.filename)
|
||||
end
|
||||
self.file:setvbuf ("line")
|
||||
return self.file
|
||||
end
|
||||
|
||||
local rollOver = function (self)
|
||||
|
||||
for i = self.maxIndex - 1, 1, -1 do
|
||||
-- files may not exist yet, lets ignore the possible errors.
|
||||
os.rename(self.filename.."."..i, self.filename.."."..i+1)
|
||||
end
|
||||
|
||||
self.file:close()
|
||||
self.file = nil
|
||||
|
||||
local _, msg = os.rename(self.filename, self.filename..".".."1")
|
||||
|
||||
if msg then
|
||||
return nil, string.format("error %s on log rollover", msg)
|
||||
end
|
||||
|
||||
return openFile(self)
|
||||
end
|
||||
|
||||
|
||||
local openRollingFileLogger = function (self)
|
||||
|
||||
if not self.file then
|
||||
return openFile(self)
|
||||
end
|
||||
|
||||
local filesize = self.file:seek("end", 0)
|
||||
|
||||
if (filesize < self.maxSize) then
|
||||
return self.file
|
||||
end
|
||||
|
||||
return rollOver(self)
|
||||
end
|
||||
|
||||
|
||||
function logging.rolling_file(filename, maxFileSize, maxBackupIndex, logPattern)
|
||||
|
||||
if type(filename) ~= "string" then
|
||||
filename = "lualogging.log"
|
||||
end
|
||||
|
||||
local obj = { filename = filename,
|
||||
maxSize = maxFileSize,
|
||||
maxIndex = maxBackupIndex or 1
|
||||
}
|
||||
|
||||
return logging.new( function(self, level, message)
|
||||
|
||||
local f, msg = openRollingFileLogger(obj)
|
||||
if not f then
|
||||
return nil, msg
|
||||
end
|
||||
local s = logging.prepareLogMsg(logPattern, os.date(), level, message)
|
||||
f:write(s)
|
||||
return true
|
||||
end
|
||||
)
|
||||
end
|
35
redist/Lua/logging/socket.lua
Normal file
35
redist/Lua/logging/socket.lua
Normal file
@ -0,0 +1,35 @@
|
||||
-------------------------------------------------------------------------------
|
||||
-- Sends the logging information through a socket using luasocket
|
||||
--
|
||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
||||
--
|
||||
-- @copyright 2004-2011 Kepler Project
|
||||
--
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
require"logging"
|
||||
local socket = require"socket"
|
||||
|
||||
function logging.socket(address, port, logPattern)
|
||||
|
||||
return logging.new( function(self, level, message)
|
||||
local s = logging.prepareLogMsg(logPattern, os.date(), level, message)
|
||||
|
||||
local socket, err = socket.connect(address, port)
|
||||
if not socket then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
local cond, err = socket:send(s)
|
||||
if not cond then
|
||||
return nil, err
|
||||
end
|
||||
socket:close()
|
||||
|
||||
return true
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
return logging.socket
|
||||
|
64
redist/Lua/logging/sql.lua
Normal file
64
redist/Lua/logging/sql.lua
Normal file
@ -0,0 +1,64 @@
|
||||
-------------------------------------------------------------------------------
|
||||
-- Saves the logging information in a table using luasql
|
||||
--
|
||||
-- @author Thiago Costa Ponte (thiago@ideais.com.br)
|
||||
--
|
||||
-- @copyright 2004-2011 Kepler Project
|
||||
--
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
require"logging"
|
||||
|
||||
function logging.sql(params)
|
||||
|
||||
params = params or {}
|
||||
params.tablename = params.tablename or "LogTable"
|
||||
params.logdatefield = params.logdatefield or "LogDate"
|
||||
params.loglevelfield = params.loglevelfield or "LogLevel"
|
||||
params.logmessagefield = params.logmessagefield or "LogMessage"
|
||||
|
||||
if params.connectionfactory == nil or type(params.connectionfactory) ~= "function" then
|
||||
return nil, "No specified connection factory function"
|
||||
end
|
||||
|
||||
local con, err
|
||||
if params.keepalive then
|
||||
con, err = params.connectionfactory()
|
||||
end
|
||||
|
||||
return logging.new( function(self, level, message)
|
||||
if (not params.keepalive) or (con == nil) then
|
||||
con, err = params.connectionfactory()
|
||||
if not con then
|
||||
return nil, err
|
||||
end
|
||||
end
|
||||
|
||||
local logDate = os.date("%Y-%m-%d %H:%M:%S")
|
||||
local insert = string.format("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')",
|
||||
params.tablename, params.logdatefield, params.loglevelfield,
|
||||
params.logmessagefield, logDate, level, string.gsub(message, "'", "''"))
|
||||
|
||||
local ret, err = pcall(con.execute, con, insert)
|
||||
if not ret then
|
||||
con, err = params.connectionfactory()
|
||||
if not con then
|
||||
return nil, err
|
||||
end
|
||||
ret, err = con:execute(insert)
|
||||
if not ret then
|
||||
return nil, err
|
||||
end
|
||||
end
|
||||
|
||||
if not params.keepalive then
|
||||
con:close()
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
return logging.sql
|
||||
|
149
redist/Lua/userlib/commons.lua
Normal file
149
redist/Lua/userlib/commons.lua
Normal file
@ -0,0 +1,149 @@
|
||||
function comma_value(amount)
|
||||
local formatted = amount
|
||||
while true do
|
||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
|
||||
if (k==0) then
|
||||
break
|
||||
end
|
||||
end
|
||||
return formatted
|
||||
end
|
||||
|
||||
function htmltable_row(tt)
|
||||
local s = '<tr>'
|
||||
for k,v in pairs(tt) do
|
||||
s = s ..'<td>' .. htmlize(tostring(v)) .. '</td>'
|
||||
end
|
||||
return s .. '</tr>'
|
||||
end
|
||||
|
||||
function keys(t)
|
||||
local tt = {}
|
||||
for k,v in pairs(t) do
|
||||
table.insert(tt, k)
|
||||
end
|
||||
return tt
|
||||
end
|
||||
|
||||
function round(val, decimal)
|
||||
if (decimal) then
|
||||
return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
|
||||
else
|
||||
return math.floor(val+0.5)
|
||||
end
|
||||
end
|
||||
|
||||
function format_num(amount, decimal, prefix, neg_prefix)
|
||||
local str_amount, formatted, famount, remain
|
||||
|
||||
decimal = decimal or 2 -- default 2 decimal places
|
||||
neg_prefix = neg_prefix or "-" -- default negative sign
|
||||
|
||||
famount = math.abs(round(amount,decimal))
|
||||
famount = math.floor(famount)
|
||||
|
||||
remain = round(math.abs(amount) - famount, decimal)
|
||||
|
||||
-- comma to separate the thousands
|
||||
formatted = comma_value(famount)
|
||||
|
||||
-- attach the decimal portion
|
||||
if (decimal > 0) then
|
||||
remain = string.sub(tostring(remain),3)
|
||||
formatted = formatted .. "." .. remain ..
|
||||
string.rep("0", decimal - string.len(remain))
|
||||
end
|
||||
|
||||
-- attach prefix string e.g '$'
|
||||
formatted = (prefix or "") .. formatted
|
||||
|
||||
-- if value is negative then format accordingly
|
||||
if (amount<0) then
|
||||
if (neg_prefix=="()") then
|
||||
formatted = "("..formatted ..")"
|
||||
else
|
||||
formatted = neg_prefix .. formatted
|
||||
end
|
||||
end
|
||||
|
||||
return formatted
|
||||
end
|
||||
|
||||
|
||||
function table_size(t)
|
||||
local c = 0
|
||||
for i in pairs(t) do
|
||||
c=c+1
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
function html_table(t, headers, celldumpers, table_attributes)
|
||||
local headers = headers or {}
|
||||
local celldumpers = celldumpers or {}
|
||||
local html = {}
|
||||
|
||||
if table_size(headers) == 0 then --[[doesn't work]]
|
||||
local firstobj = t[1]
|
||||
for k in pairs(firstobj) do
|
||||
headers[k] = k
|
||||
end
|
||||
end
|
||||
|
||||
--[[calculate table attributes]]
|
||||
local table_attributes = table_attributes or {cellpadding="2", cellspacing="10"}
|
||||
local attrs = ""
|
||||
for k,v in pairs(table_attributes) do
|
||||
attrs = attrs .. tostring(k) .. '="' .. tostring(v) .. '" '
|
||||
end
|
||||
|
||||
table.insert(html, '<table ' .. attrs .. ' ><thead><tr>')
|
||||
|
||||
for k,v in ipairs(headers) do
|
||||
table.insert(html, '<th>'..v.coltitle..'</th>')
|
||||
end
|
||||
|
||||
table.insert(html, '</tr></thead><tbody>')
|
||||
local cell
|
||||
for k, a in pairs(t) do
|
||||
table.insert(html, "<tr>")
|
||||
for idx, obj in ipairs(headers) do
|
||||
if celldumpers[obj.fieldname] then
|
||||
cell = tostring(celldumpers[obj.fieldname](obj.fieldname, a))
|
||||
else
|
||||
if obj.fieldname == nil then
|
||||
cell = ' '
|
||||
else
|
||||
cell = tostring(a[obj.fieldname])
|
||||
end
|
||||
end
|
||||
table.insert(html, "<td>".. cell .."</td>")
|
||||
end
|
||||
table.insert(html, "</tr>")
|
||||
end
|
||||
table.insert(html, '</tbody></table>')
|
||||
return table.concat(html)
|
||||
end
|
||||
|
||||
function lookup(fieldvalue, lookuptable, keyfield, descfield)
|
||||
for k,v in pairs(lookuptable) do
|
||||
if fieldvalue == v[keyfield] then
|
||||
return v[descfield]
|
||||
end
|
||||
end
|
||||
return ' '
|
||||
end
|
||||
|
||||
function fmtfloat(n)
|
||||
return string.format("%2.2f", n)
|
||||
end
|
||||
|
||||
|
||||
--- Pads str to length len with char from left
|
||||
string.lpad = function(str, len, char)
|
||||
if char == nil then
|
||||
char = ' '
|
||||
end
|
||||
return string.rep(char, len - #str) .. str
|
||||
end
|
||||
|
6
redist/eLua/index.elua
Normal file
6
redist/eLua/index.elua
Normal file
@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
<h3>This is an eLua View</h3>
|
||||
<h3>This is an <?lua="Hello World"?> from Lua</h3>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user