diff options
author | Ted Trask <ttrask01@yahoo.com> | 2008-05-07 17:37:37 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2008-05-07 17:37:37 +0000 |
commit | edfd85ee749aebacddee51c143902f2a2918b817 (patch) | |
tree | 046e5f511f5f33060977212f770a0a1cd789e2a6 | |
parent | e871208433b3be5e2261292fb0ba041a2a7e5418 (diff) | |
download | acf-core-edfd85ee749aebacddee51c143902f2a2918b817.tar.bz2 acf-core-edfd85ee749aebacddee51c143902f2a2918b817.tar.xz |
Rewrote getopts.getoptsfromfile function to handle more complicated config files and removed getopts.getoptsfromfile_onperline function
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@1111 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r-- | lib/getopts.lua | 134 |
1 files changed, 66 insertions, 68 deletions
diff --git a/lib/getopts.lua b/lib/getopts.lua index ef74b49..e2de912 100644 --- a/lib/getopts.lua +++ b/lib/getopts.lua @@ -1,6 +1,22 @@ module (..., package.seeall) require("fs") +-- Search the option string for separate options (-x or --xyz) and put them in a table +local opts_to_table = function ( optstring, filter ) + local optsparams + if optstring then + local optstr = " " .. optstring .. " " + for o in string.gmatch(optstr, "%s%-%-?%a+%s+%a*") do + local option = string.match(o, "%-%-?%a+") + if not filter or filter == option then + if not optsparams then optsparams = {} end + optsparams[option] = string.match(o, "%a*$") + end + end + end + return optsparams +end + function setoptsinfile (file, search, option, value) local opts = {} local newfilecontent = nil @@ -37,87 +53,69 @@ function setoptsinfile (file, search, option, value) return true, "File '" .. file .. "' has been modifyed!", nil end -function getoptsfromfile (file, search, filter) +-- Parse file for options returned in a table +-- If search_section is defined, only report options in matching section +-- If search_option is defined, only report matching options +-- If to_table is true, attempt to convert option string to array of options +-- If filter is defined (and table is true), only list option matching filter +function getoptsfromfile (file, search_section, search_option, to_table, filter) local opts = nil if not (fs.is_file(file)) then return nil end local conf_file = fs.read_file_as_array ( file ) - for i=1,table.maxn(conf_file) do - local l = conf_file[i] - if not string.find ( l, "^[;#].*" ) then - local a = string.match ( l, "^%s*(%S*)=" ) - if (a) then - if not (search) or (search == a) then - local b = string.match ( l, '^%s*%S*%s*%=%s*%"?(.-)%s*%"?%s*$' ) - local optstable = getopts.opts_to_table(b,filter) - if (optstable) or not (filter) then - if not (opts) then - opts = {} - end + local section = "" + for i,l in ipairs(conf_file) do + -- check if comment line + if not string.find ( l, "^%s*#" ) then + -- first, concat lines + local j = 1 + while string.find ( l, "\\%s*$" ) and conf_file[i+j] do + l = string.match ( l, "^(.*)\\%s*$" ) .. " " .. conf_file[i+j] + j = j+1 + end + -- find section name + local a = string.match ( l, "^%s*%[%s*(%S+)%s*%]" ) + if a then + section = a + elseif not (search_section) or (search_section == section) then + -- find option name + a = string.match ( l, "^%s*(%S+)%s*=" ) + if a and (not (search_option) or (search_option == a)) then + -- Figure out the value + local b = string.match ( l, '^%s*%S+%s*%=%s*(.*)$' ) or "" + -- remove comments from end of line + if string.find ( b, '#' ) then + b = string.match ( b, '^(.*)#.*$' ) or "" + end + -- remove spaces from front and back + b = string.match ( b, '^%s*(.*%S)%s*$' ) or "" + -- finally, remove quotes + if #b > 1 and string.sub(b,1,1) == '"' and string.sub(b,-1) == '"' then + b = string.sub(b,2,-2) or "" + end + if to_table == true then + local optstable = opts_to_table(b,filter) if (optstable) then - opts[a] = optstable + if not (opts) then opts = {} end + if not (opts[section]) then opts[section] = {} end + opts[section][a] = optstable ---[[ Next line is DEBUG info. Should be commented out! --opts[a]["debug"] = b -- End debug info. --]] - else - opts[a] = b end + else + if not (opts) then opts = {} end + if not (opts[section]) then opts[section] = {} end + opts[section][a] = b end end end end end - return opts -end -function opts_to_table ( optstring, filter ) - local optsparams = nil - local optstr = optstring - if optstr then - local option = "" - for j = 1, string.len(optstr) do - if (string.find(string.sub(optstr, j, string.len(optstr)), "^-%a%s*")) then - option=string.sub(optstr, j, j+1) - if not (filter) or (filter == option) then - for k = j+1, string.len(optstr) do - if not (optsparams) then - optsparams = {} - end - if (string.sub(optstr, k, k) == "-") then - optsparams[option] = string.match(string.sub(optstr, j+2, k-1),"^%s*(.-)%s*$") - break - end - if (k == string.len(optstr)) then - optsparams[option] = string.match(string.sub(optstr, j+2, k),"^%s*(.-)%s*$") - break - end - end - end - end - end - end - return optsparams -end -function getoptsfromfile_onperline (file, search, filter) - local opts = nil - if not (fs.is_file(file)) then return nil end - local conf_file = fs.read_file_as_array ( file ) - for i=1,table.maxn(conf_file) do - local l = conf_file[i] - if not string.find ( l, "^[;#].*" ) then - local a = string.match ( l, "^%s*(%S*)=" ) - if (a) then - if not (search) or (search == a) then - local b = string.match ( l, '^%s*%S*%s*%=%s*%"?(.-)%s*%"?%s*$' ) --- local optstable = getopts.opts_to_table(b,filter) - if not (filter) then - if not (opts) then - opts = {} - end - opts[a] = b - end - end - end - end - end + if opts and search_section and search_option then + return opts[search_section][search_option] + elseif opts and search_section then + return opts[search_section] + end return opts end |