diff options
author | Peter Kozak <spag@golwen.net> | 2013-03-11 05:56:54 -0400 |
---|---|---|
committer | Peter Kozak <spag@golwen.net> | 2013-03-11 05:56:54 -0400 |
commit | 46c987b67be71aa77f484929c7c7323d8c25b0c2 (patch) | |
tree | cb24e4eda4c34f541cdccd995eb4b0c2a9cb6674 /misc/freeswitch/scripts | |
parent | 2a95c9611ea922921eef5fb8e414a8b9262f9c06 (diff) |
array module added
Diffstat (limited to 'misc/freeswitch/scripts')
-rw-r--r-- | misc/freeswitch/scripts/common/array.lua | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/array.lua b/misc/freeswitch/scripts/common/array.lua new file mode 100644 index 0000000..b93ef69 --- /dev/null +++ b/misc/freeswitch/scripts/common/array.lua @@ -0,0 +1,54 @@ +-- Gemeinschaft 5 module: array functions +-- (c) AMOOMA GmbH 2013 +-- + +module(...,package.seeall) + +function try(array, arguments) + if type(arguments) ~= 'string' or type(array) ~= 'table' then + return nil; + end + + local result = array; + + arguments:gsub('([^%.]+)', function(entry) + local success, result = pcall(function() result = (result[tonumber(entry) or entry]); end); + end); + + return result; +end + + +function set(array, arguments, value) + local nop, arguments_count = arguments:gsub('%.', ''); + local structure = array; + arguments:gsub('([^%.]+)', function(entry) + if arguments_count <= 0 then + structure[entry] = value; + elseif type(structure[entry]) == 'table' then + structure = structure[entry]; + else + structure[entry] = {}; + structure = structure[entry]; + end + arguments_count = arguments_count - 1; + end); +end + + +function expand_variable(variable_path, variable_sets) + for index=1, #variable_sets do + local result = try(variable_sets[index], variable_path); + if result then + return result; + end + end +end + + +function expand_variables(line, ...) + local variable_sets = {...}; + return (line:gsub('{([%a%d%._]+)}', function(captured) + return expand_variable(captured, variable_sets); + end)) +end |