diff --git a/.gitignore b/.gitignore index 3a55aef..a28a209 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .dub *.o lib +bin diff --git a/README.md b/README.md index 471accd..099e89d 100644 --- a/README.md +++ b/README.md @@ -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: @@ -32,34 +20,23 @@ 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 ``` diff --git a/dub.json b/dub.json index 2408d48..b75deda 100644 --- a/dub.json +++ b/dub.json @@ -7,11 +7,33 @@ "license": "GPL-2.0", "copyright": "© Alexander Zhirov, 2023", "description": "Singleton for simple reading of the configuration file", - "targetType": "library", - "targetPath": "lib", + "configurations": [ + { + "name": "library", + "targetType": "library", + "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" +} \ No newline at end of file diff --git a/dub.selections.json b/dub.selections.json index 716e57f..377bd39 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -2,6 +2,7 @@ "fileVersion": 1, "versions": { "datefmt": "1.0.4", + "silly": "1.1.1", "singlog": "0.1.0" } } diff --git a/example/settings.conf.sample b/example/settings.conf.sample deleted file mode 100644 index a8efaae..0000000 --- a/example/settings.conf.sample +++ /dev/null @@ -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') diff --git a/img/matches.png b/img/matches.png index 8f05290..c3c3695 100644 Binary files a/img/matches.png and b/img/matches.png differ diff --git a/source/readconf.d b/source/readconf.d index f4ebc37..c9aa760 100644 --- a/source/readconf.d +++ b/source/readconf.d @@ -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()) diff --git a/tests/settings.conf b/tests/settings.conf new file mode 100644 index 0000000..0e7ef7d --- /dev/null +++ b/tests/settings.conf @@ -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') diff --git a/tests/test.d b/tests/test.d new file mode 100644 index 0000000..bfb5424 --- /dev/null +++ b/tests/test.d @@ -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")); +// }