ConfigParser

Config reads all properties of the application from an INI style of the following format:

// --------------------------
// Config Example
// --------------------------

; Database config parameters
[DATABASE]
table1 = "name_of_table1"
table2 = "name_of_table2"

; An example of a multi-value parameter
fields = "create_time"
         "update_time"
         "count"

; Logging config parameters
[LOGGING]
level = info
file = "access.log"

The properties defined in the file are read and stored in an internal array, which can then be accessed through get and set methods as follows:

The parseFile() method only needs to be called once, though may be called multiple times if the config file needs to be re-read from the file on disk.

TODO: At the moment it's possible to set a key (se set), but it's not possible to write back to the file.

Constructors

this
this()

Constructor

this
this(istring config)

Constructor

Members

Aliases

String
alias String = istring

Config Keys and Properties

Functions

exists
bool exists(cstring category, cstring key)

Checks if Key exists in Category

get
T get(cstring category, cstring key, T default_value)

Non-strict method to get the value of a config key into the specified output value. If the config key does not exist, the given default value is returned.

get
void get(T value, cstring category, cstring key, T default_value)

Alternative form non-strict config value getter, returning the retrieved value via a reference. (For interface consistency with the reference version of getStrict(), above.)

getList
T[] getList(cstring category, cstring key, T[] default_value)

Non-strict method to get a multi-line value. The existence or non-existence of the key is returned. If the configuration key cannot be found, the output list remains unchanged.

getListStrict
T[] getListStrict(cstring category, cstring key)

Strict method to get a multi-line value. If the requested key cannot be found, an exception is thrown.

getStrict
T getStrict(cstring category, cstring key)

Strict method to get the value of a config key. If the requested key cannot be found, an exception is thrown.

getStrict
void getStrict(T value, cstring category, cstring key)

Alternative form strict config value getter, returning the retrieved value via a reference. (The advantage being that the template type can then be inferred by the compiler.)

isEmpty
bool isEmpty()

Tells whether the config object has no values loaded.

iterateCategory
VarIterator!(T) iterateCategory(cstring category)

Returns an iterator over keys or key/value pairs in a category. The values are converted to T, unless T is istring.

opApply
int opApply(int delegate(ref istring x) dg)

Iterator. Iterates over categories of the config file

parseFile
void parseFile(istring file_path, bool clean_old)

Read Config File

parseString
void parseString(T[] str, bool clean_old)

Parse a string

print
void print(FormatOutput output)

Prints the current configuration to the given formatted text stream.

remove
void remove(istring category, istring key)

Remove Config-Key Property

set
void set(istring category, istring key, istring value)

Set Config-Key Property

Structs

VarIterator
struct VarIterator(T = istring)

Variable Iterator. Iterates over keys or key/value pairs of a category. The values are converted to T, unless T is istring.

Examples

Usage example

void main ()
{
    // Read config file from disk
    scope config = new ConfigParser("etc/config.ini");

    // Read a single value
    istring value = config.getStrict!(istring)("category", "key");

    // Set a single value
    config.set("category", "key", "new value");

    // Read a multi-line value
    istring[] values = config.getListStrict("category", "key");
}

Meta