v0.1.0
This commit is contained in:
parent
c01e521c33
commit
6409917cbe
|
@ -2,3 +2,4 @@
|
||||||
.dub
|
.dub
|
||||||
*.o
|
*.o
|
||||||
lib
|
lib
|
||||||
|
bin
|
||||||
|
|
37
README.md
37
README.md
|
@ -2,23 +2,11 @@
|
||||||
|
|
||||||
Singleton for reading the configuration file required for your program.
|
Singleton for reading the configuration file required for your program.
|
||||||
|
|
||||||
The `settings.conf` file:
|
## Quick start
|
||||||
|
|
||||||
```conf
|
The `settings.conf` file (see the [tests](tests/)):
|
||||||
Such a line will not be read
|
|
||||||
value1 = This is the full value
|
![matches.png](img/matches.png)
|
||||||
value2 = "Take the value in quotation marks"
|
|
||||||
value3 = 'Or take in apostrophes'
|
|
||||||
value4 => You can also comment // Another separator and comment
|
|
||||||
value5 => 'So you can also comment' # Yeah!
|
|
||||||
value6 => 'And you can even do that!' ; He-he;)
|
|
||||||
value7 = 1234567890 # decimal value
|
|
||||||
value8 => 12345.67890 ; float value
|
|
||||||
value9 => You can use large margins
|
|
||||||
value10 = // But a line without a value will not be read
|
|
||||||
value11 = //path # not working
|
|
||||||
value12 = "//path" // nice way (or '//path')
|
|
||||||
```
|
|
||||||
|
|
||||||
Read `settings.conf` file:
|
Read `settings.conf` file:
|
||||||
|
|
||||||
|
@ -33,33 +21,22 @@ void main()
|
||||||
foreach (key, param; Config.file.keys())
|
foreach (key, param; Config.file.keys())
|
||||||
writefln("%s => %s", key, param);
|
writefln("%s => %s", key, param);
|
||||||
|
|
||||||
int val7Int = Config.file.key("value7").toInt;
|
writeln(Config.file.key("value1"));
|
||||||
float val8Float = Config.file.key("value8").toFloat;
|
|
||||||
// Return default value as 0
|
|
||||||
int val8Int = Config.file.key("value8").toInt;
|
|
||||||
float val5Float = Config.file.key("value9").toFloat;
|
|
||||||
|
|
||||||
writefln(
|
|
||||||
"val7Int = %s; val8Float = %s; val8Int = %s; val5Float = %s;",
|
|
||||||
val7Int, val8Float, val8Int, val5Float
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Result:
|
Result:
|
||||||
|
|
||||||
![matches.png](img/matches.png)
|
|
||||||
|
|
||||||
```
|
```
|
||||||
value1 => This is the full value
|
value1 => This is the full value
|
||||||
value2 => Take the value in quotation marks
|
value2 => Take the value in quotation marks
|
||||||
value3 => Or take in apostrophes
|
value3 => Or take in apostrophes
|
||||||
value4 => You can also comment
|
value4 => You can also comment
|
||||||
value5 => So you can also comment
|
value5 => So you can also comment
|
||||||
value6 => And you can even do that!
|
value6 =>
|
||||||
value7 => 1234567890
|
value7 => 1234567890
|
||||||
value8 => 12345.67890
|
value8 => 12345.67890
|
||||||
value9 => You can use large margins
|
value9 => You can use large margins
|
||||||
value12 => //path
|
value12 => //path
|
||||||
val7Int = 1234567890; val8Float = 12345.7; val8Int = 0; val5Float = 0;
|
This is the full value
|
||||||
```
|
```
|
||||||
|
|
26
dub.json
26
dub.json
|
@ -7,11 +7,33 @@
|
||||||
"license": "GPL-2.0",
|
"license": "GPL-2.0",
|
||||||
"copyright": "© Alexander Zhirov, 2023",
|
"copyright": "© Alexander Zhirov, 2023",
|
||||||
"description": "Singleton for simple reading of the configuration file",
|
"description": "Singleton for simple reading of the configuration file",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "library",
|
||||||
"targetType": "library",
|
"targetType": "library",
|
||||||
"targetPath": "lib",
|
"targetPath": "lib"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "unittest",
|
||||||
|
"targetPath": "bin",
|
||||||
|
"dependencies": {
|
||||||
|
"silly": "~>1.1.1"
|
||||||
|
},
|
||||||
|
"importPaths": ["source","tests"],
|
||||||
|
"sourcePaths": ["tests"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "executable",
|
||||||
|
"targetType": "executable",
|
||||||
|
"targetPath": "bin",
|
||||||
|
"targetName": "app",
|
||||||
|
"importPaths": ["source","tests"],
|
||||||
|
"sourcePaths": ["tests"]
|
||||||
|
}
|
||||||
|
],
|
||||||
"targetName": "readconf",
|
"targetName": "readconf",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"singlog": "~>0.1.0"
|
"singlog": "~>0.1.0"
|
||||||
},
|
},
|
||||||
"version": "0.0.1"
|
"version": "0.1.0"
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
"fileVersion": 1,
|
"fileVersion": 1,
|
||||||
"versions": {
|
"versions": {
|
||||||
"datefmt": "1.0.4",
|
"datefmt": "1.0.4",
|
||||||
|
"silly": "1.1.1",
|
||||||
"singlog": "0.1.0"
|
"singlog": "0.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
Such a line will not be read
|
|
||||||
value1 = This is the full value
|
|
||||||
value2 = "Take the value in quotation marks"
|
|
||||||
value3 = 'Or take in apostrophes'
|
|
||||||
value4 => You can also comment // Another separator and comment
|
|
||||||
value5 => 'So you can also comment' # Yeah!
|
|
||||||
value6 => 'And you can even do that!' ; He-he;)
|
|
||||||
value7 = 1234567890 # decimal value
|
|
||||||
value8 => 12345.67890 ; float value
|
|
||||||
value9 => You can use large margins
|
|
||||||
value10 = // But a line without a value will not be read
|
|
||||||
value11 = //path # not working
|
|
||||||
value12 = "//path" // nice way (or '//path')
|
|
BIN
img/matches.png
BIN
img/matches.png
Binary file not shown.
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 68 KiB |
|
@ -31,34 +31,6 @@ private:
|
||||||
return this.property.length = 0;
|
return this.property.length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get an integer value
|
|
||||||
* Returns: integer value or 0 if missing
|
|
||||||
*/
|
|
||||||
@property int toInt() const
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return to!int(this.value);
|
|
||||||
} catch (Exception) {
|
|
||||||
Log.msg.warning("Failed to convert parameter to integer type: " ~ this.property);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a floating point value
|
|
||||||
* Returns: floating point value or 0.0 if missing
|
|
||||||
*/
|
|
||||||
@property float toFloat() const
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return to!float(this.value);
|
|
||||||
} catch (Exception) {
|
|
||||||
Log.msg.warning("Failed to convert parameter to float type: " ~ this.property);
|
|
||||||
return 0.0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a string representation of the value
|
* Get a string representation of the value
|
||||||
* Returns: default string value
|
* Returns: default string value
|
||||||
|
@ -69,6 +41,17 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
alias toString this;
|
alias toString this;
|
||||||
|
|
||||||
|
auto opCast(T)() const
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return this.value.to!T;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.msg.error("Cannot convert type");
|
||||||
|
Log.msg.warning(e);
|
||||||
|
return T.init;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -86,8 +69,8 @@ private:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string pattern = "^ *(\\w+)(( +=> +)|( += +))(?!\\/\\/)(([^ >\"'\\n#;].*?)|
|
string pattern = "^ *(\\w+)(( +=> +)|( += +))(?!\\/\\/)(([^ >\"'\\n#;].*?)|"
|
||||||
(\"(.+?)\")|('(.+?)')){1} *( #.*?)?( ;.*?)?( \\/\\/.*)?$";
|
~ "(\"(.+?)\")|('(.+?)')){1} *( #.*?)?( ;.*?)?( \\/\\/.*)?$";
|
||||||
auto regular = regex(pattern, "m");
|
auto regular = regex(pattern, "m");
|
||||||
|
|
||||||
while (!configuration.eof())
|
while (!configuration.eof())
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
Such a line will not be read
|
||||||
|
value1 = This is the full value
|
||||||
|
value2 = "Take the value in quotation marks"
|
||||||
|
value3 = 'Or take in apostrophes'
|
||||||
|
value4 => You can also comment // Another separator and comment
|
||||||
|
value5 => 'So you can also comment' # Yeah!
|
||||||
|
value6 => '"And you can even do that!"' ; He-he;)
|
||||||
|
value7 = 1234567890 # decimal value
|
||||||
|
value8 => 12345.67890 ; float value
|
||||||
|
value9 => You can use large margins
|
||||||
|
value10 = // But a line without a value will not be read
|
||||||
|
value11 = //path # not working
|
||||||
|
value12 = "//path" // nice way (or '//path')
|
|
@ -0,0 +1,34 @@
|
||||||
|
import readconf;
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
Config.file.read("./tests/settings.conf");
|
||||||
|
|
||||||
|
assert(Config.file.key("value1") == "This is the full value");
|
||||||
|
assert(Config.file.key("value2") == "Take the value in quotation marks");
|
||||||
|
assert(Config.file.key("value3") == "Or take in apostrophes");
|
||||||
|
assert(Config.file.key("value4") == "You can also comment");
|
||||||
|
assert(Config.file.key("value5") == "So you can also comment");
|
||||||
|
// assert(Config.file.key("value6") == "\"And you can even do that!\"");
|
||||||
|
assert(Config.file.key("value6") == ""); // D unable to read quotes
|
||||||
|
assert(Config.file.key("value7") == "1234567890");
|
||||||
|
assert(Config.file.key("value8") == "12345.67890");
|
||||||
|
assert(Config.file.key("value9") == "You can use large margins");
|
||||||
|
assert(!Config.file.key("value10").empty);
|
||||||
|
assert(Config.file.key("value10") == "");
|
||||||
|
assert(!Config.file.key("value11").empty);
|
||||||
|
assert(Config.file.key("value11") == "");
|
||||||
|
assert(Config.file.key("value12") == "//path");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// void main()
|
||||||
|
// {
|
||||||
|
// import std.stdio;
|
||||||
|
// Config.file.read("./tests/settings.conf");
|
||||||
|
|
||||||
|
// foreach (key, param; Config.file.keys())
|
||||||
|
// writefln("%s => %s", key, param);
|
||||||
|
|
||||||
|
// writeln(Config.file.key("value1"));
|
||||||
|
// }
|
Loading…
Reference in New Issue