Inifile

Inifile

Inifile is a lua library for parsing and writing ini files. It supports multiple backends, these are:

  • io: The lua i/o library backend, the default.
  • memory: The memory backend uses the filename as input, and returns the output on write.
  • love: Uses LÖVE's love.filesystem, this is the default when LÖVE is detected.

Data format

This section is about the format of the lua table corresponding to an ini file, it is not about the ini file format.

As ini files are made up of sections, each containing keys and values. The resulting lua table is therefore a simple two-level table:

    {
        section1 = {
            key1 = value1,
            key2 = value2,
        },
    
        section2 = {
            key3 = value3,
            key4 = value4,
        },
    }

Additionally, any table read in using inifile.parse has a metafield __inifile that stores comments and ordering present in the original file. This data can later be used by inifile.save to restore this metadata as accurately as possible. The metadata aims to preserve as much of the original document's content (including comments) and structure, but does not store everything. For example, no whitespace information is preserved, so the layout of the resulting document might differ. After the first "copy", any subsequent "copies" should be byte-for-byte equal, as it is fully deterministic for existing keys and sections.

Functions

table = inifile.parse(name, backend = <default>)

This function takes a filename (or data string, depending on the backend), and parses the corresponding ini file, producing a table. The structure of this table is defined in the Data format section.

result = inifile.save(name, table, backend = <default>)

This function takes a table in the right format (see Data format) and writes it to the corresponding file.
Note that the memory backend is the only one that produces a result (and the filename is ignored).