From b80bd744ad873f6fc43018bc4bfb90677de167bd Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Mon, 17 Dec 2012 12:01:45 +0100 Subject: Start of GS5. --- .../scripts/common/configuration_file.lua | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 misc/freeswitch/scripts/common/configuration_file.lua (limited to 'misc/freeswitch/scripts/common/configuration_file.lua') diff --git a/misc/freeswitch/scripts/common/configuration_file.lua b/misc/freeswitch/scripts/common/configuration_file.lua new file mode 100644 index 0000000..67e1f3b --- /dev/null +++ b/misc/freeswitch/scripts/common/configuration_file.lua @@ -0,0 +1,70 @@ +-- Gemeinschaft 5 module: configuration file +-- (c) AMOOMA GmbH 2012 +-- + +module(...,package.seeall) + +function ignore_comments(line) + return line:gsub(';+([^;]*)', function(entry) + return ''; + end); +end + +-- parse configuration +function parse(lines, filter_section_name) + require 'common.str' + local section = {} + local root = { [true] = section } + + for line in lines do + if line then + local ignore_line = false; + line = ignore_comments(line); + + line:gsub('^%s*%[(.-)%]%s*$', function(section_name) + if tostring(section_name):match('%=false$') then + section = {} + else + root[common.str.strip(section_name)] = {}; + section = root[common.str.strip(section_name)]; + end + ignore_line = true; + end); + + if not ignore_line then + key, value = common.str.partition(line, '='); + if value and key and not common.str.strip(key):match('%s') then + section[common.str.strip(key)] = common.str.strip(value); + else + line = common.str.strip(line); + if not common.str.blank(line) then + if line:match(',') then + table.insert(section, common.str.strip_to_a(line, ',')); + else + table.insert(section, line); + end + end + end + end + end + end + + if filter_section_name == false then + root[true] = nil; + elseif filter_section_name then + return root[filter_section_name]; + end + + return root; +end + +-- retrieve configuration from file +function get(file_name, filter_section_name) + local file = io.open(file_name); + + if file then + local result = parse(file:lines(), filter_section_name); + file:close(); + return result; + end +end -- cgit v1.2.3