1. -- movie script
  2.  
  3. -- Usage:
  4. -- ======
  5. --
  6. -- data = inifile_to_proplist(_movie.path & "projector.ini")
  7. -- if voidP(data["Settings"]) then data["Settings"] = [:]
  8. -- data["Settings"]["EscapeOK"] = 0
  9. -- ok = proplist_to_inifile(data, _movie.path & "projector.ini")
  10.  
  11. ----------------------------------------
  12. -- Loads INI file, returns nested propList.
  13. -- Per default strings are used as properties, keeping the original case.
  14. -- Pass TRUE as second parameter to use symbols as properties instead.
  15. -- If INI file does not exist (yet), an empty propList is returned.
  16. -- @requires Director 11.5/12, fileIO xtra
  17. -- @param {string} ini_file
  18. -- @param {bool} [symbol_props] - Optional, defaults to FALSE
  19. -- @param {string} [charset] - Optional, default is "utf-8"
  20. -- @returns {propList}
  21. ----------------------------------------
  22. on inifile_to_proplist(ini_file, symbol_props, charset)
  23.     res = [:]
  24.     fio = xtra("fileio").new()
  25.     fio.openFile(ini_file, 1)
  26.     if fio.status() then return res
  27.     if not voidP(charset) then fio.setCharSet(charset)
  28.     od = _player.itemDelimiter
  29.     _player.itemDelimiter = "="
  30.     tmp = res
  31.     repeat while TRUE
  32.         ini_line = fio.readLine()
  33.         if not ini_line.length then
  34.             exit repeat
  35.         end if
  36.         ini_line = ini_line.word[1..ini_line.word.count] -- trim white space
  37.         if ini_line = EMPTY or ini_line starts ";" then -- skip empty and comment lines
  38.             next repeat
  39.         else if ini_line starts "[" then
  40.             prop = ini_line.char[2..ini_line.char.count - 1]
  41.             if symbol_props then
  42.                 prop = symbol(prop)
  43.             end if
  44.             res[prop] = [:]
  45.             tmp = res[prop]
  46.         else if ini_line.item.count > 1 then
  47.             prop = ini_line.item[1]
  48.             val = ini_line.item[2..ini_line.item.count]
  49.             prop = prop.word[1..prop.word.count] -- trim white space
  50.             if symbol_props then
  51.                 prop = symbol(prop)
  52.             end if
  53.             val = val.word[1..val.word.count] -- trim white space
  54.             tmp.addProp(prop, val)
  55.         end if
  56.     end repeat
  57.     fio.closeFile()
  58.     _player.itemDelimiter = od
  59.     return res
  60. end
  61.  
  62. ----------------------------------------
  63. -- Saves nested propList as INI file.
  64. -- An existing file is overwritten.
  65. -- @requires Director 11.5/12, fileIO xtra
  66. -- @param {propList} prop_list
  67. -- @param {string} ini_file
  68. -- @param {string} [charset] - Optional, default is "utf-8"
  69. -- @returns {bool} success
  70. ----------------------------------------
  71. on proplist_to_inifile(prop_list, ini_file, charset)
  72.     fio = xtra("fileio").new()
  73.     fio.openFile(ini_file, 2)
  74.     if not fio.status() then fio.delete()
  75.     fio.createFile(ini_file)
  76.     if fio.status() then return FALSE
  77.     fio.openFile(ini_file, 2)
  78.     if not voidP(charset) then fio.setCharSet(charset)
  79.     eol = numtochar(13) & numtochar(10) -- CRLF
  80.     cnt_i = prop_list.count
  81.     repeat with i = 1 to cnt_i
  82.         if i > 1 then
  83.             -- for better human readability add empty line before new section
  84.             fio.writeString(eol)
  85.         end if
  86.         fio.writeString("[" & prop_list.getPropAt(i) & "]" & eol)
  87.         cnt_j = prop_list[i].count
  88.         repeat with j = 1 to cnt_j
  89.             fio.writeString(prop_list[i].getPropAt(j) & "=" & prop_list[i][j] & eol)
  90.         end repeat
  91.     end repeat
  92.     fio.closeFile()
  93.     return TRUE
  94. end
  95.  
[raw code]