-- movie script
-- Usage:
-- ======
--
-- data = inifile_to_proplist(_movie.path & "projector.ini")
-- if voidP(data["Settings"]) then data["Settings"] = [:]
-- data["Settings"]["EscapeOK"] = 0
-- ok = proplist_to_inifile(data, _movie.path & "projector.ini")
----------------------------------------
-- Loads INI file, returns nested propList.
-- Per default strings are used as properties, keeping the original case.
-- Pass TRUE as second parameter to use symbols as properties instead.
-- If INI file does not exist (yet), an empty propList is returned.
-- @requires Director 11.5/12, fileIO xtra
-- @param {string} ini_file
-- @param {bool} [symbol_props] - Optional, defaults to FALSE
-- @param {string} [charset] - Optional, default is "utf-8"
-- @returns {propList}
----------------------------------------
on inifile_to_proplist(ini_file, symbol_props, charset)
res = [:]
fio = xtra("fileio").new()
fio.openFile(ini_file, 1)
if fio.status() then return res
if not voidP(charset) then fio.setCharSet(charset)
od = _player.itemDelimiter
_player.itemDelimiter = "="
tmp = res
repeat while TRUE
ini_line = fio.readLine()
if not ini_line.length then
exit repeat
end if
ini_line = ini_line.word[1..ini_line.word.count] -- trim white space
if ini_line = EMPTY or ini_line starts ";" then -- skip empty and comment lines
next repeat
else if ini_line starts "[" then
prop = ini_line.char[2..ini_line.char.count - 1]
if symbol_props then
prop = symbol(prop)
end if
res[prop] = [:]
tmp = res[prop]
else if ini_line.item.count > 1 then
prop = ini_line.item[1]
val = ini_line.item[2..ini_line.item.count]
prop = prop.word[1..prop.word.count] -- trim white space
if symbol_props then
prop = symbol(prop)
end if
val = val.word[1..val.word.count] -- trim white space
tmp.addProp(prop, val)
end if
end repeat
fio.closeFile()
_player.itemDelimiter = od
return res
end
----------------------------------------
-- Saves nested propList as INI file.
-- An existing file is overwritten.
-- @requires Director 11.5/12, fileIO xtra
-- @param {propList} prop_list
-- @param {string} ini_file
-- @param {string} [charset] - Optional, default is "utf-8"
-- @returns {bool} success
----------------------------------------
on proplist_to_inifile(prop_list, ini_file, charset)
fio = xtra("fileio").new()
fio.openFile(ini_file, 2)
if not fio.status() then fio.delete()
fio.createFile(ini_file)
if fio.status() then return FALSE
fio.openFile(ini_file, 2)
if not voidP(charset) then fio.setCharSet(charset)
eol = numtochar(13) & numtochar(10) -- CRLF
cnt_i = prop_list.count
repeat with i = 1 to cnt_i
if i > 1 then
-- for better human readability add empty line before new section
fio.writeString(eol)
end if
fio.writeString("[" & prop_list.getPropAt(i) & "]" & eol)
cnt_j = prop_list[i].count
repeat with j = 1 to cnt_j
fio.writeString(prop_list[i].getPropAt(j) & "=" & prop_list[i][j] & eol)
end repeat
end repeat
fio.closeFile()
return TRUE
end