dev #1

Merged
alexander merged 9 commits from dev into master 2023-03-26 08:38:23 +00:00
3 changed files with 38 additions and 18 deletions
Showing only changes of commit 29c71dd6f9 - Show all commits

View File

@ -34,5 +34,6 @@
"targetName": "readconf", "targetName": "readconf",
"dependencies": { "dependencies": {
"singlog": "~>0.1.0" "singlog": "~>0.1.0"
} },
"version": "0.1.4"
} }

View File

@ -6,6 +6,11 @@ import std.regex;
import std.meta; import std.meta;
import singlog; import singlog;
/**
* Read config object
*/
alias rc = Config.file;
class Config class Config
{ {
private: private:
@ -15,7 +20,7 @@ private:
bool readed = false; bool readed = false;
const string pattern = "^( |\\t)*(((\\w(\\w|-)+)(( |\\t)*(=>|=){1}" const string pattern = "^( |\\t)*(((\\w(\\w|-)+)(( |\\t)*(=>|=){1}"
~ "( |\\t)*)(?!\\/(\\/|\\*))(([^ >\"'=\\n\\t#;].*?)|(\"(.+)\")" ~ "( |\\t)*)(?!\\/(\\/|\\*))(([^ >\"'=\\n\\t#;].*?)|(\"(.+)\")"
~ "|('(.+)')){1})|(\\[(\\w(\\w|-)+)\\])|(\\[()\\]))( |\\t)*" ~ "|('(.+)')){1})|(\\[(\\w(\\w|-)+)\\])|(\\[\\]))( |\\t)*"
~ "(( |\\t)(#|;|\\/\\/|\\/\\*).*)?$"; ~ "(( |\\t)(#|;|\\/\\/|\\/\\*).*)?$";
/** /**
@ -58,9 +63,9 @@ private:
} }
// values // values
int group = 11; int group = 11;
if (match[group][0] == '\'') if (match[group][0] == '\"')
group = 14; group = 14;
else if (match[group][0] == '\"') else if (match[group][0] == '\'')
group = 16; group = 16;
if (sectionName !in this.sections) if (sectionName !in this.sections)
@ -111,10 +116,24 @@ public:
readConfig(); readConfig();
} }
@property ConfigSection section(string section = "[]") /**
* Get the section
* Params:
* section = section name (default main "[]")
*/
@property ConfigSection sectionName(string section = "[]")
{ {
return sections[section]; return sections[section];
} }
/**
* Section name
*
* Get the section
* Params:
* section = section name (default main "[]")
*/
alias sn = sectionName;
} }
struct ConfigSection struct ConfigSection

View File

@ -2,20 +2,20 @@ import readconf;
unittest unittest
{ {
Config.file.read("./tests/settings.conf"); rc.read("./tests/settings.conf");
assert(Config.file.key("value1") == "This is the full value"); assert(rc.sn.key("value1") == "This is the full value");
assert(Config.file.key("value2") == "Take the value in quotation marks"); assert(rc.sn.key("value2") == "Take the value in quotation marks");
assert(Config.file.key("value3") == "Or take in apostrophes"); assert(rc.sn.key("value3") == "Or take in apostrophes");
assert(Config.file.key("value4") == "You can also comment"); assert(rc.sn.key("value4") == "You can also comment");
assert(Config.file.key("value5") == "So you can also comment"); assert(rc.sn.key("value5") == "So you can also comment");
assert(Config.file.key("value6") == "\"And you can even do that!\""); assert(rc.sn.key("value6") == "\"And you can even do that!\"");
assert(Config.file.key("value7") == "1234567890"); assert(rc.sn.key("value7") == "1234567890");
assert(Config.file.key("value8") == "12345.67890"); assert(rc.sn.key("value8") == "12345.67890");
assert(Config.file.key("value9") == "You can use large margins"); assert(rc.sn.key("value9") == "You can use large margins");
assert(Config.file.key("value10").empty); assert(rc.sn.key("value10").empty);
assert(Config.file.key("value11").empty); assert(rc.sn.key("value11").empty);
assert(Config.file.key("value12") == "//path"); assert(rc.sn.key("value12") == "//path");
} }