LIP.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. --[[
  2. Copyright (c) 2012 Carreras Nicolas
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. --]]
  19. --- Lua INI Parser.
  20. -- It has never been that simple to use INI files with Lua.
  21. --@author Dynodzzo
  22. local LIP = {};
  23. --- Returns a table containing all the data from the INI file.
  24. --@param fileName The name of the INI file to parse. [string]
  25. --@return The table containing all data from the INI file. [table]
  26. function LIP.load(fileName,nested)
  27. assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  28. local file = assert(io.open(fileName, 'r'), 'Error loading file : ' .. fileName);
  29. local data = {};
  30. local section;
  31. local count =0;
  32. -- if nested param dont't push
  33. if nested == nil then
  34. nested={}
  35. end
  36. for line in file:lines() do
  37. local tempSection = line:match('^%[([^%[%]]+)%]$');
  38. if(tempSection)then
  39. section = tonumber(tempSection) and tonumber(tempSection) or tempSection;
  40. data[section] = data[section] or {};
  41. end
  42. local param, value = line:match('^([%w|_]+)%s-=%s-(.+)$');
  43. if(param and value ~= nil)then
  44. if(tonumber(value))then
  45. value = tonumber(value);
  46. elseif(value == 'true')then
  47. value = true;
  48. elseif(value == 'false')then
  49. value = false;
  50. end
  51. if(tonumber(param))then
  52. param = tonumber(param);
  53. end
  54. -- value trim spaces not all just ltrim,rtrim
  55. -- this is for all,not suit for betik key -> value=value:gsub('%s+', '')
  56. if value ~= nil and type(value) ~= "number" then
  57. value=ltrim(rtrim(value))
  58. end
  59. -- nested parametresiyle derle,pakur gibi kısımlar için
  60. -- indekslenerek array yapı olarak tutulması sağlanıyor.
  61. -- derle,pakur sıralı betik olmak zorundadır.
  62. if has_value(nested,section) then
  63. --count = count +1
  64. --data[section][count] = param .."@@"..value;
  65. table.insert(data[section],param .."@@"..value)
  66. else
  67. data[section][param] = value;
  68. end
  69. end
  70. end
  71. file:close();
  72. return data;
  73. end
  74. --- Saves all the data from a table to an INI file.
  75. --@param fileName The name of the INI file to fill. [string]
  76. --@param data The table containing all the data to store. [table]
  77. function LIP.save(fileName, data)
  78. assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  79. assert(type(data) == 'table', 'Parameter "data" must be a table.');
  80. local file = assert(io.open(fileName, 'w+b'), 'Error loading file :' .. fileName);
  81. local contents = '';
  82. for section, param in pairs(data) do
  83. contents = contents .. ('[%s]\n'):format(section);
  84. for key, value in pairs(param) do
  85. contents = contents .. ('%s=%s\n'):format(key, tostring(value));
  86. end
  87. contents = contents .. '\n';
  88. end
  89. file:write(contents);
  90. file:close();
  91. end
  92. -- check a variable in an array
  93. function has_value (tab, val)
  94. for index, value in ipairs(tab) do
  95. if value == val then
  96. return true
  97. end
  98. end
  99. return false
  100. end
  101. -- remove leading whitespace from string.
  102. -- http://en.wikipedia.org/wiki/Trim_(programming)
  103. function ltrim(s)
  104. return (s:gsub("^%s*", ""))
  105. end
  106. -- remove trailing whitespace from string.
  107. -- http://en.wikipedia.org/wiki/Trim_(programming)
  108. function rtrim(s)
  109. local n = #s
  110. while n > 0 and s:find("^%s", n) do n = n - 1 end
  111. return s:sub(1, n)
  112. end
  113. return LIP;