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).
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.
 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: string
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:save()

Save INI data to file.