talimat.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. --[[
  2. Copyright (c) 2020 Milis Linux
  3. --]]
  4. -- check a variable in an array
  5. function has_value (tab, val)
  6. for index, value in ipairs(tab) do
  7. if value == val then
  8. return true
  9. end
  10. end
  11. return false
  12. end
  13. -- remove leading whitespace from string.
  14. -- http://en.wikipedia.org/wiki/Trim_(programming)
  15. function ltrim(s)
  16. return (s:gsub("^%s*", ""))
  17. end
  18. -- remove trailing whitespace from string.
  19. -- http://en.wikipedia.org/wiki/Trim_(programming)
  20. function rtrim(s)
  21. local n = #s
  22. while n > 0 and s:find("^%s", n) do n = n - 1 end
  23. return s:sub(1, n)
  24. end
  25. local talimat = {};
  26. --- Returns a table containing all the data from the INI file.
  27. --@param fileName The name of the INI file to parse. [string]
  28. --@return The table containing all data from the INI file. [table]
  29. function talimat.load(fileName,nested)
  30. assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  31. local file = assert(io.open(fileName, 'r'), 'Error loading file : ' .. fileName);
  32. local data = {};
  33. local section;
  34. local count =0;
  35. -- if nested param dont't push
  36. if nested == nil then
  37. nested={}
  38. end
  39. for line in file:lines() do
  40. local tempSection = line:match('^%[([^%[%]]+)%]$');
  41. if(tempSection)then
  42. section = tonumber(tempSection) and tonumber(tempSection) or tempSection;
  43. data[section] = data[section] or {};
  44. end
  45. local param, value = line:match('^([%w|_]+)%s-=%s-(.+)$');
  46. if(param and value ~= nil)then
  47. if(tonumber(value))then
  48. value = tonumber(value);
  49. elseif(value == 'true')then
  50. value = true;
  51. elseif(value == 'false')then
  52. value = false;
  53. end
  54. if(tonumber(param))then
  55. param = tonumber(param);
  56. end
  57. -- value trim spaces not all just ltrim,rtrim
  58. -- this is for all,not suit for betik key -> value=value:gsub('%s+', '')
  59. if value ~= nil and type(value) ~= "number" then
  60. value=ltrim(rtrim(value))
  61. end
  62. -- nested parametresiyle derle,pakur gibi kısımlar için
  63. -- indekslenerek array yapı olarak tutulması sağlanıyor.
  64. -- derle,pakur sıralı betik olmak zorundadır.
  65. if has_value(nested,section) then
  66. --count = count +1
  67. --data[section][count] = param .."@@"..value;
  68. table.insert(data[section],param .."@@"..value)
  69. else
  70. data[section][param] = value;
  71. end
  72. end
  73. end
  74. file:close();
  75. return data;
  76. end
  77. return talimat;