5 Home
Alexander Zhirov edited this page 2023-06-07 23:39:40 +03:00

readconf

Singleton for reading the configuration file required for your program.

Main features

  • Reading multiple configuration files (>=v0.3.0)
  • Separation of parameters by sections (>=v0.2.0)
  • Access to parameters and sections using keys and indexes (>=v0.3.0)
  • Commenting on lines

Examples of configuration files.

Simple configuration file

An example of a simple configuration file consists of strings of the format <parameter><separator><value>. The parameter has a strict notation using characters A-Z, a-z, 0-9, _, -. The value is specified by any set of characters (except special characters). The = or => symbol can act as a separator. Spaces or tabs are possible between all three parts of the template, as well as before or after. It is acceptable to read parameter with empty value.

parameter1=value1
parameter2=>value2
    parameter3=value3
    parameter4=>value4

_parameter5 = value5
parameter6 => value6
       parameter7  =   value7

    parameter8  =>   value8
parameter9      =value9
parameter-10      =>value10
parameter11 =    value11


parameter12_ =>    value12
parameter13 =

Commenting on lines

The library allows you to use comments as strings that do not match the template <parameter><separator><value>, and specifying them after the value using special characters to indicate a comment - ;, #, //, /*. The value cannot start with special characters. If you need to specify special characters at the beginning of the value, it is enough to separate the value with quotation marks or an apostrophe. There must be at least one space or tab character between the value and the special character, otherwise the value will be read as a whole.

This line will be a comment, since it does not match the basic template
parameter1  => value1        ; This will be a comment
parameter2  => value2        # This will be a comment
parameter3  => value3        // This will be a comment
parameter4  => value4 /* This will be a comment
parameter5  => value5;This will not be a comment
parameter6  => value6// This will also be a whole value
parameter7  => //value7      ;The value will be read as empty value
parameter8  => "//value8"    # Now the value is correctly
parameter9  => ';value9'     // The value is correctly too
parameter10 => '"value10"'   // Quotes inside
parameter11 => ; empty value

Sections

Sections provide the ability to divide parameters into groups. The main section is specified by the constant [], so when a request is made to an empty section, parameters from the main section will be read. If the configuration file contains only one named section, then the request goes to it. If there are several sections in the configuration file, you must specify a specific one (by default, the main section). Sections also support the ability to comment on a line. The name of the section is set in the same sequence of characters as the parameter.

parameter1  = value1
parameter_2 = value2

[first-section]             ; Creating the first section
parameter1  = value3        ; A section may contain similar parameter names
parameter_2 = value4

[second-section]            // Another section
parameter1  = value5
parameter_2 = value6

[]                          /* Addition of the main section
parameter3  = value7        /* A new parameter will be added
parameter1  = value8        /* And parameter1 will be redefined

[first-section]             # Addition of the first section
parameter3  = value9
parameter4  = value10

[_section]                  # Creating the new section
parameter1  = value11
parameter2  = value12

The configuration files themselves are located here.

Examples of reading configuration files

Reading a simple configuration file

A simple example of reading a single configuration file and getting parameter values from it. The simple.conf file is taken as the configuration file:

{
    ...
    rc.read("./examples/simple.conf");
    ...
}

Since a single file is being read, by default, it is accessed. cf is used as an alias for the configFile function:

{
    ...
    auto configFile  = rc.cf;
    ...
}

To read the parameters, you need to refer to the appropriate section. By default, the request is made to the main section. sn is used as an alias for the sectionName function:

{
    ...
    auto mainSection = configFile.sc;
    ...
}

Getting parameter values from the selected section is done via the key function or index:

{
    ...
    string val1 = mainSection.key("parameter1");
    string val2 = mainSection["parameter2"];
    ...
}

Checking the parameter for an empty value:

{
    ...
    assert(mainSection["parameter13"].empty);
    ...
}

Reading from various sections

Reading a configuration file with different sections. The section.conf file is taken as the configuration file:

{
    ...
    rc.read("./examples/sections.conf");
    auto configFile  = rc.cf;

    auto mainSection   = configFile.sn;
    auto firstSection  = configFile.sn("first-section");
    auto secondSection = configFile["second-section"];

    string val1 = mainSection.key("parameter1");
    string val2 = mainSection["parameter_2"];
    string val3 = firstSection["parameter1"];
    string val4 = firstSection["parameter_2"];
    string val5 = secondSection["parameter1"];
    string val6 = secondSection["parameter_2"];
    ...
}

Reading multiple configuration files

To read multiple configuration files, you need to associate the file with its alias. In this capacity, the file name itself acts by default. If the files are located in different directories but have the same names - in this case, you need to give them unique aliases. The simple.conf, section.conf and comments.conf files is taken as the configuration files:

{
    ...
    rc.read("./examples/simple.conf", "simple");
    rc.read("./examples/sections.conf");
    rc.read("./examples/comments.conf", "comments");
    ...
}

A specific configuration file is accessed via its alias:

{
    ...
    auto simpleConfig    = rc.cf("simple");
    auto sectionsConfig  = rc["sections.conf"];
    auto commentsConfig  = rc["comments"];
    ...
}

Next, reading the section and getting the values:

{
    ...
    auto simConMaiSec = simpleConfig.sn;
    string val1 = simConMaiSec.key("parameter1");
    string val2 = simConMaiSec["parameter2"];

    auto secConFirSec = sectionsConfig.sn("first-section");
    auto secConSecSec = sectionsConfig["second-section"];
    string val3 = secConFirSec["parameter4"];
    string val4 = secConSecSec["parameter1"];

    auto comConMaiSec = commentsConfig.sn;
    string val5 = comConMaiSec["parameter5"];
    ...
}