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
Section | Summary |
---|---|
Files | Files utility functions |
Summary
Return type | Function and summary |
---|---|
INI | new(string path) Read an INI file (if exist). |
Map | pairs(string section) Get pairs of key/value of the requested section. |
string | read(string section, string key, string defaultValue) Read section + key value in INI datas. |
save() Save INI data to file. | |
table | sections() 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).
Parameter | Type | Default | Description |
---|---|---|---|
path | string | 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.
Parameter | Type | Default | Description |
---|---|---|---|
section | string | 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.
Parameter | Type | Default | Description |
---|---|---|---|
section | string | section | |
key | string | key | |
defaultValue | string | nil | Value 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.
Parameter | Type | Default | Description |
---|---|---|---|
section | string | section | |
key | string | 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.