From f03106343fdacd4caedfa4315a8cd75ab27a84ac Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Tue, 29 Jan 2008 16:21:52 +0000 Subject: Added functionallity to modify options in a file git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@661 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/getopts.lua | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'lib') 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) -- cgit v1.2.3