diff options
author | Mika Havela <mika.havela@gmail.com> | 2008-01-29 16:21:52 +0000 |
---|---|---|
committer | Mika Havela <mika.havela@gmail.com> | 2008-01-29 16:21:52 +0000 |
commit | f03106343fdacd4caedfa4315a8cd75ab27a84ac (patch) | |
tree | c9eac3c66ede53aead627c6ed6e8e94eac857b81 /lib | |
parent | 473fc0b6fa7d2b9626eb63344c55c5c4a0994ba3 (diff) | |
download | acf-core-f03106343fdacd4caedfa4315a8cd75ab27a84ac.tar.bz2 acf-core-f03106343fdacd4caedfa4315a8cd75ab27a84ac.tar.xz |
Added functionallity to modify options in a file
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@661 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lib')
-rw-r--r-- | lib/getopts.lua | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/lib/getopts.lua b/lib/getopts.lua index d5ac795..9940f53 100644 --- a/lib/getopts.lua +++ b/lib/getopts.lua @@ -1,11 +1,40 @@ module (..., package.seeall) require("fs") ---local conf_file - function setoptsinfile (file, search, option, value) - local opts = getoptsfromfile(file) - return opts + local opts = {} + local newfilecontent = nil + local filecontent = nil + opts = getoptsfromfile(file) or {} + filecontent = fs.read_file(file) or "" + if (filecontent == "") or (opts[search] == "") or (opts[search] == nil) then + opts[search] = {} + end + if not (search) or not (option) then + return nil, "Invalid usage of function getopts.setoptsinfile()" + end + + --Change to new value + opts[search][option] = value + + local optstr = "" + for k,v in pairs(opts) do + if (k == search) then + optstr = optstr.. k .. "=\"" + for kk,vv in pairs(v) do + optstr = optstr .. kk .. " " .. vv .. " " + end + optstr = string.match(optstr, "(.-)%s*$") .. "\"" + end + end + + newfilecontent = string.gsub(filecontent, "%s*[;#]?" .. search .. "%s*=.-\n?$", "\n" .. optstr .. "\n") or "" + if (string.find(newfilecontent, search .. "%s*=" ) == nil) or (newfilecontent == "") then + fs.write_file(file,string.match(filecontent, "(.-)\n*$") .. "\n" .. optstr .. "\n") + else + fs.write_file(file,string.match(newfilecontent, "(.-)\n*$")) + end + return "File '" .. file .. "'has been modifyed!" end function getoptsfromfile (file, search, filter) |