and cnt with the current count of saved configurations. It writes two text
lines that should look like this:
cnt = 1
cur = CONF1.CFG
To be safe, the new file is first written to file CONFSW.INI.NEW. If this is suc-
cessful, the old INI file is renamed to CONFSW.INI.BAK (existing files of that
name are deleted first), and file CONFSW.INI.NEW is renamed to CONFSW.INI.
This is done in function _activate_file(). Working in this way, we always
have a valid INI file. Even a battery going flat when writing the INI file can-
not lead to a corrupt file:
function write_ini(name, cnt)
local new = ininame..".NEW"
local file,msg =
io.open (new, "w")
if not file then error(msg) end
local ret,msg = file:write(
if ret then
ret,msg = file:write (
end
file:close()
if ret then
ret,msg =
_activate_file(new, ininame)
end
if not ret then error(msg) end
end
function _activate_file(new, to)
local bak = to..".BAK"
os.remove(bak)
os.rename(to,bak)
local ret,msg =
if not ret then
os.rename(bak,to)
end
return ret,msg
end
"cnt="..cnt.."\n")
"cur="..name.."\n")
os.rename(new,to)
5.7 Example scripts
187