FeedTheChunk

LIP.lua

Aug 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 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.    Moidified to work with OpenComputers
  23.    
  24. --]]
  25. --- Lua INI Parser.
  26. -- It has never been that simple to use INI files with Lua.
  27. --@author Dynodzzo
  28.  
  29. local LIP = {};
  30.  
  31. --- Returns a table containing all the data from the INI file.
  32. --@param fileName The name of the INI file to parse. [string]
  33. --@return The table containing all data from the INI file. [table]
  34. function LIP.load(fileName)
  35.    assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  36.    local file = assert(io.open(fileName, 'r'), 'Error loading file : ' .. fileName);
  37.    local data = {};
  38.    local section;
  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.          data[section][param] = value;
  58.       end
  59.    end
  60.    file:close();
  61.    return data;
  62. end
  63.  
  64. --- Saves all the data from a table to an INI file.
  65. --@param fileName The name of the INI file to fill. [string]
  66. --@param data The table containing all the data to store. [table]
  67. function LIP.save(fileName, data)
  68.    assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  69.    assert(type(data) == 'table', 'Parameter "data" must be a table.');
  70.    local file = assert(io.open(fileName, 'wb'), 'Error loading file :' .. fileName);
  71.    local contents = '';
  72.    for section, param in pairs(data) do
  73.       contents = contents .. ('[%s]\n'):format(section);
  74.       for key, value in pairs(param) do
  75.          contents = contents .. ('%s=%s\n'):format(key, tostring(value));
  76.       end
  77.       contents = contents .. '\n';
  78.    end
  79.    file:write(contents);
  80.    file:close();
  81.    end
  82.  
  83. return LIP;
Advertisement
Add Comment
Please, Sign In to add comment