code examples

Alexander Zhirov 2023-03-30 09:42:55 +03:00
parent ade2bef1c7
commit f70d479b2f
1 changed files with 139 additions and 10 deletions

129
Home.md

@ -49,6 +49,135 @@ parameter6 => value6// This will also be a whole value
parameter7 => //value7 ;The value will not be read parameter7 => //value7 ;The value will not be read
parameter8 => "//value8" # Now the value is correctly parameter8 => "//value8" # Now the value is correctly
parameter9 => ';value9' // The value is correctly too parameter9 => ';value9' // The value is correctly too
parameter10 => '"value10"' // Quotes inside
``` ```
### Sections ### 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**.
```conf
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:
```d
{
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:
```d
{
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:
```d
{
auto mainSection = configFile.sc;
}
```
Getting parameter **values** from the selected **section** is done via the key function or index:
```d
{
string val1 = mainSection.key("parameter1");
string val2 = mainSection["parameter2"];
}
```
### Reading from various sections
Reading a configuration file with different **sections**. The [section.conf]() file is taken as the configuration file:
```d
{
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:
```d
{
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:
```d
{
auto simpleConfig = rc.cf("simple");
auto sectionsConfig = rc["sections.conf"];
auto commentsConfig = rc["comments"];
}
```
Next, reading the **section** and getting the **values**:
```d
{
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"];
}
```