Class INI

Rough INI configuration file reader and writer.

When writing the file with INI:save(), sections are alphabetically ordered and comments are ignored. Use this only for your script, at your own risks.

Example


 -- myconfig.ini content is
 -- key=value
 -- [section A]
 -- key1 A=value1 A
 -- key2 A=value2 A
 -- [section B]
 -- key1 B=value1 B
 -- key2 B=value2 B
 local ini = INI:new("/path/to/myconfig.ini")
 print(ini:read("", "key")) -->'value'
 print(ini:read("Section A", "key2")) --> "value2 A"
 print(ini:read("Section Z", "key42")) --> nil
 print(ini:read("Section Z", "key42", "Universe")) --> Universe
 ini:write("Section A", "key1", "New value 1A")) --> value is replaced, but not yet saved
 ini:save() --> file is saved

Section

SectionSummary
FilesFiles utility functions

Summary

Return typeFunction and summary
INInew(string path)
Read an INI file (if exist).
 delete(string section, string key)
Delete section + key from the INI datas.
Mappairs(string section)
Get pairs of key/value of the requested section.
stringread(string section, string key, string defaultValue)
Read section + key value in INI datas.
booleanreadBoolean(string section, string key, boolean defaultValue)
Read a boolean section + key value in INI datas.
numberreadNumber(string section, string key, number defaultValue, number minValue, number maxValue)
Read a number section + key value in INI datas.
 save()
Save INI data to file.
tablesections()
Returns the sections of INI file/structure
 write(string section, string key, value)
Writes section + key value in INI datas.

Methods

INI:new(string path)

Read an INI file (if exist).

ParameterTypeDefaultDescription
pathstring Path to INI file to read, and that will be saved by INI:save(...)
Return
INI: INI

INI:sections()

Returns the sections of INI file/structure

Return
table: of strings. The "empty" section (pairs before first section) is not included.

INI:pairs(string section)

Get pairs of key/value of the requested section.

ParameterTypeDefaultDescription
sectionstring section
Return
Map: nil if section deson't exist in INI data structure

INI:read(string section, string key, string defaultValue)

Read section + key value in INI datas.

ParameterTypeDefaultDescription
sectionstring section
keystring key
defaultValuestringnilValue to return if no data found for section + key.
Return
string: Read string or nil
Errors
if section or key contains forbidden chars [, ] and =
if key is empty

INI:readNumber(string section, string key, number defaultValue, number minValue, number maxValue)

Read a number section + key value in INI datas.

If read value is not nil and minValue and/or maxValue are provided, the result is minValue <= result <= maxValue.

ParameterTypeDefaultDescription
sectionstring section
keystring key
defaultValuenumbernilValue to return if no data found for section + key.
minValuenumber If a value is read (not nil), ensure it is greater or equal to minValue.
maxValuenumber If a value is read (not nil), ensure it is lower or equal to maxValue.
Return
number: Read number or nil
Errors
if section or key contains forbidden chars [, ] and =
if key is empty

INI:readBoolean(string section, string key, boolean defaultValue)

Read a boolean section + key value in INI datas.

ParameterTypeDefaultDescription
sectionstring section
keystring key
defaultValuebooleannilValue to return if no data found for section + key.
Return
boolean: Read boolean or nil
Errors
if section or key contains forbidden chars [, ] and =
if key is empty

INI:write(string section, string key, value)

Writes section + key value in INI datas.

ParameterTypeDefaultDescription
sectionstring section
keystring key
value  string or number that are tostring()ed, nil removes the data.
Errors
if section or key contains forbidden chars [, ] and =
if key is empty

INI:delete(string section, string key)

Delete section + key from the INI datas.

ParameterTypeDefaultDescription
sectionstring section
keystring key
Errors
if section or key contains forbidden chars [, ] and =
if key is empty

INI:save()

Save INI data to file.