This commit is contained in:
Alexander Zhirov 2023-03-24 02:26:02 +03:00
parent c01e521c33
commit 6409917cbe
9 changed files with 95 additions and 77 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
.dub
*.o
lib
bin

View File

@ -2,23 +2,11 @@
Singleton for reading the configuration file required for your program.
The `settings.conf` file:
## Quick start
```conf
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')
```
The `settings.conf` file (see the [tests](tests/)):
![matches.png](img/matches.png)
Read `settings.conf` file:
@ -33,33 +21,22 @@ void main()
foreach (key, param; Config.file.keys())
writefln("%s => %s", key, param);
int val7Int = Config.file.key("value7").toInt;
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
);
writeln(Config.file.key("value1"));
}
```
Result:
![matches.png](img/matches.png)
```
value1 => This is the full value
value2 => Take the value in quotation marks
value3 => Or take in apostrophes
value4 => You can also comment
value5 => So you can also comment
value6 => And you can even do that!
value6 =>
value7 => 1234567890
value8 => 12345.67890
value9 => You can use large margins
value12 => //path
val7Int = 1234567890; val8Float = 12345.7; val8Int = 0; val5Float = 0;
This is the full value
```

View File

@ -7,11 +7,33 @@
"license": "GPL-2.0",
"copyright": "© Alexander Zhirov, 2023",
"description": "Singleton for simple reading of the configuration file",
"configurations": [
{
"name": "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",
"dependencies": {
"singlog": "~>0.1.0"
},
"version": "0.0.1"
"version": "0.1.0"
}

View File

@ -2,6 +2,7 @@
"fileVersion": 1,
"versions": {
"datefmt": "1.0.4",
"silly": "1.1.1",
"singlog": "0.1.0"
}
}

View File

@ -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')

Binary file not shown.

Before

Width:  |  Height:  |  Size: 69 KiB

After

Width:  |  Height:  |  Size: 68 KiB

View File

@ -31,34 +31,6 @@ private:
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
* Returns: default string value
@ -69,6 +41,17 @@ private:
}
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;
}
string pattern = "^ *(\\w+)(( +=> +)|( += +))(?!\\/\\/)(([^ >\"'\\n#;].*?)|
(\"(.+?)\")|('(.+?)')){1} *( #.*?)?( ;.*?)?( \\/\\/.*)?$";
string pattern = "^ *(\\w+)(( +=> +)|( += +))(?!\\/\\/)(([^ >\"'\\n#;].*?)|"
~ "(\"(.+?)\")|('(.+?)')){1} *( #.*?)?( ;.*?)?( \\/\\/.*)?$";
auto regular = regex(pattern, "m");
while (!configuration.eof())

13
tests/settings.conf Normal file
View File

@ -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')

34
tests/test.d Normal file
View File

@ -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"));
// }