summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/configuration_file.lua
diff options
context:
space:
mode:
Diffstat (limited to 'misc/freeswitch/scripts/common/configuration_file.lua')
-rw-r--r--misc/freeswitch/scripts/common/configuration_file.lua70
1 files changed, 70 insertions, 0 deletions
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