inifile.lua |
|||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
For LÖVE 0.10.2 inifile.lua by bartbes is a library for reading and saving INI files. It can be found here (the version on this page is a little bit different). |
|
||||||||||||||||
About INI files |
|
||||||||||||||||
From Wikipedia...
What inifile.lua can do
Using inifile.luainifile.lua creates a table with two functions, parse and save.
inifile.lua also returns the table it creates, so the user can choose their own name for it. For example...
When read by inifile.lua, INI files are saved in the format of
For example, this INI file...
... will result in a table equivalent to this:
Here is a usage example:
inifile.lua can also take tables in the format of the one above and save them as INI files. Here is a usage example:
The resulting file may look like:
As keys in Lua are unordered, the order of the output is also unknown. Okay so let's actually read inifile.lua! |
|
||||||||||||||||
inifile is the table which will hold the two functions. |
inifile = {}
|
||||||||||||||||
lines will store the function for reading through lines in a file, and write will store the function for writing a string to a file. These functions will be different depending on whether inifile.lua thinks it's in a LÖVE program or not. These two variables are local, which means they won't be accessible outside of this file. Note that variables can be declared to be local without giving them any value. |
local lines
local write
|
||||||||||||||||
In Lua, anything that is not nil or false is true in a conditional statement. In a LÖVE program, love is a table which holds all the LÖVE-related functions. If love is true, then it is assumed that it is being used within LÖVE. |
if love then
|
||||||||||||||||
Variables can be set to functions, just like they can be set to numbers or strings. love.filesystem.lines is an iterator which iterates over the lines in the file. |
lines = love.filesystem.lines
|
||||||||||||||||
love.filesystem.write writes a string to a file. |
write = love.filesystem.write
else
lines = function(name) return assert(io.open(name)):lines() end
write = function(name, contents) return assert(io.open(name, "w")):write(contents) end
end
|
||||||||||||||||
inifile.parseinifile.parse works like this:
|
function inifile.parse(name)
|
||||||||||||||||
t is the table which will be returned at the end of this function. |
local t = {}
|
||||||||||||||||
section will store the current section (that is |
local section
|
||||||||||||||||
The lines in the file are looped through using a generic for loop with the lines function that was previously defined. line is each line as a string. If this is running in a LÖVE program, this line is equivalent to |
for line in lines(name) do
|
||||||||||||||||
s will be the section name, or nil if this line isn't a line with a section name. Strings in Lua have methods, which are equivalent to the functions in the string library. For example, The string.match function returns substrings by matching patterns. How this pattern worksline:match("^%[([^%]]+)%]$") Starts with [ (% is the escape character.) Things that are used in this pattern
|
local s = line:match("^%[([^%]]+)%]$")
|
||||||||||||||||
If the last line didn't return nil and this is the start of a section... |
if s then
|
||||||||||||||||
The value of s is saved in the variable section, so it can be retained for lines which aren't section titles (when s will be nil). |
section = s
|
||||||||||||||||
If there is already a section with the same name (that is, Lua's disjunction operator or returns its first argument if it's not nil and false, otherwise it returns its second argument. |
t[section] = t[section] or {}
end
|
||||||||||||||||
If this is a line which has a key and a value, the variables key and value are set to them. line:match("^(%w+)%s-=%s-(.+)$") How this pattern worksStarts with: Things that are used in this pattern
|
local key, value = line:match("^(%w+)%s-=%s-(.+)$")
|
||||||||||||||||
Checks if the last line returned two values (i.e. neither is nil). |
if key and value then
|
||||||||||||||||
If value is a number, it is set to its number equivalent, rather than a string. tonumber will return the number equivalent of the string it is given, or nil if it has no number equivalent (for example, |
if tonumber(value) then value = tonumber(value) end
|
||||||||||||||||
If value has the name of a boolean value, value is set to the actual boolean value instead of a string. |
if value == "true" then value = true end
if value == "false" then value = false end
|
||||||||||||||||
The value is inserted into the section table with its key. |
t[section][key] = value
end
end
|
||||||||||||||||
The filled table is returned. |
return t
end
|
||||||||||||||||
inifile.saveinifile.save takes two arguments:
inifile.save works like this:
|
function inifile.save(name, t)
|
||||||||||||||||
contents will hold the string which will be written to the file. |
local contents = ""
|
||||||||||||||||
t is looped through using pairs, with section being the name of each section and s being the table of keys and values contained in that section. |
for section, s in pairs(t) do
|
||||||||||||||||
The name of the section [surrounded by brackets] is added to contents. String methods can be used on literal strings if the strings are within parentheses. In this case, string.format can take a specially formatted string and fills in the values with variables it's given. For example, instead of writing For more information on the symbols string.format can use, see this. \n is the escape sequence for new lines. |
contents = contents .. ("[%s]\n"):format(section)
|
||||||||||||||||
s is looped through using pairs, with key and value being the keys and values of this section. |
for key, value in pairs(s) do
|
||||||||||||||||
If key was the string |
contents = contents .. ("%s=%s\n"):format(key, tostring(value))
end
|
||||||||||||||||
A new line is added after every section. |
contents = contents .. "\n"
end
|
||||||||||||||||
The file is saved to the save directory with the filename of the argument name and the string of contents. |
write(name, contents)
end
|
||||||||||||||||
The table is returned so the user can choose their own name for it. |
return inifile
|
||||||||||||||||
inifile.lua is copyright Bart van Strien, released under the Simplified BSD License. Copyright is waived on all original parts of this code walkthrough. |
|