From af43f4566cf43ab0a1a700685c0286840c124d0a Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Mon, 4 Aug 2008 20:58:15 +0000 Subject: Modified getopts to allow writing to blank files and adding in a missing section. git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@1371 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/getopts.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/getopts.lua b/lib/getopts.lua index 59333db..68271b9 100644 --- a/lib/getopts.lua +++ b/lib/getopts.lua @@ -76,7 +76,7 @@ end -- If the search_section is not found, we'll add it at the end of the file -- If the search_name is not found, we'll add it at the end of the section function setoptsinfile (file, search_section, search_name, value, to_table, optionvalue) - if not file or file == "" or not search_name or search_name == "" or (to_table == true and not value) then + if not file or not search_name or search_name == "" or (to_table == true and not value) then return false, nil, "Invalid input for getopts.setoptsinfile()", file end search_section = search_section or "" @@ -268,7 +268,7 @@ function getsection (file, search_section) end function setsection (file, search_section, section_content) - if not file or file == "" then + if not file then return false, nil, "Invalid input for getopts.setoptsinfile()", file end search_section = search_section or "" @@ -307,6 +307,11 @@ function setsection (file, search_section, section_content) new_conf_file[#new_conf_file + 1] = l end + if not done then + -- we didn't find the section, add it now + new_conf_file[#new_conf_file + 1] = '[' .. search_section .. ']\n' .. (section_content or "") + end + if (fs.is_file(file)) then fs.write_file(file, table.concat(new_conf_file, '\n')) else -- cgit v1.2.3