Daniel_I_Am

LIP.lua

Jul 15th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. --[[
  2. Copyright (c) 2012 Carreras Nicolas
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. --]]
  22. --- Lua INI Parser.
  23. -- It has never been that simple to use INI files with Lua.
  24. --@author Dynodzzo
  25.  
  26. local LIP = {};
  27.  
  28. --- Returns a table containing all the data from the INI file.
  29. --@param fileName The name of the INI file to parse. [string]
  30. --@return The table containing all data from the INI file. [table]
  31. function LIP.load(fileName)
  32. assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  33. local file = assert(io.open(fileName, 'r'), 'Error loading file : ' .. fileName);
  34. local data = {};
  35. local section;
  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. data[section][param] = value;
  55. end
  56. end
  57. file:close();
  58. return data;
  59. end
  60.  
  61. --- Saves all the data from a table to an INI file.
  62. --@param fileName The name of the INI file to fill. [string]
  63. --@param data The table containing all the data to store. [table]
  64. function LIP.save(fileName, data)
  65. assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  66. assert(type(data) == 'table', 'Parameter "data" must be a table.');
  67. local file = assert(io.open(fileName, 'wb'), 'Error loading file :' .. fileName);
  68. local contents = '';
  69. for section, param in pairs(data) do
  70. contents = contents .. ('[%s]\n'):format(section);
  71. for key, value in pairs(param) do
  72. contents = contents .. ('%s=%s\n'):format(key, tostring(value));
  73. end
  74. contents = contents .. '\n';
  75. end
  76. file:write(contents);
  77. file:close();
  78. end
  79.  
  80. return LIP;
Advertisement
Add Comment
Please, Sign In to add comment