diff --git a/source/readconf.d b/source/readconf.d index 494ca30..c1cc86f 100644 --- a/source/readconf.d +++ b/source/readconf.d @@ -164,6 +164,20 @@ public: * configName = config name (by default the name of the configuration file) */ alias cf = configFile; + + @property ConfigFile opIndex(string configName = "") + { + if (configName.length == 0) + { + if (configs.length == 1) + return configs[configs.byKey.front]; + else + throw new Exception("More than one configuration file has been read. " + ~ "It is necessary to specify the name of a specific"); + } + + return configName in configs ? configs[configName] : ConfigFile(configName); + } } struct ConfigFile @@ -187,6 +201,8 @@ struct ConfigFile throw new Exception("The configuration file does not exist"); if (section == mainSection && sections.length == 1) return sections[sections.byKey.front]; + if (section.length == 0) + section = mainSection; return section in sections ? sections[section] : ConfigSection(section); } @@ -206,6 +222,17 @@ struct ConfigFile this.sections[sectionName].add(parameter); } + + @property ConfigSection opIndex(string section = mainSection) + { + if (!this.exist) + throw new Exception("The configuration file does not exist"); + if (section == mainSection && sections.length == 1) + return sections[sections.byKey.front]; + if (section.length == 0) + section = mainSection; + return section in sections ? sections[section] : ConfigSection(section); + } } struct ConfigSection @@ -230,6 +257,8 @@ struct ConfigSection */ ConfigParameter key(string key) { + if (key.length == 0) + throw new Exception("The key cannot be empty"); if (this.empty) throw new Exception("The selected section has no parameters or does not exist"); return key in this.parameters ? this.parameters[key] : ConfigParameter(key); @@ -250,6 +279,15 @@ struct ConfigSection Log.msg.warning("The parameter exists but will be overwritten"); this.parameters[parameter.property] = parameter; } + + ConfigParameter opIndex(string key) + { + if (key.length == 0) + throw new Exception("The key cannot be empty"); + if (this.empty) + throw new Exception("The selected section has no parameters or does not exist"); + return key in this.parameters ? this.parameters[key] : ConfigParameter(key); + } } /** diff --git a/tests/test.d b/tests/test.d index b5390d8..1e4fd59 100644 --- a/tests/test.d +++ b/tests/test.d @@ -7,7 +7,7 @@ unittest rc.read("./tests/database.conf", "pgconf"); assert(rc.cf("old").sn.key("value1") == "text without quotes"); - assert(rc.cf("old").sn.key("value2") == "Yes!"); + assert(rc["old"][""]["value2"] == "Yes!"); assert(rc.cf("old").sn.key("value3") == "value in apostrophes"); assert(rc.cf("old").sn.key("value4") == "1000"); assert(rc.cf("old").sn.key("value5") == "0.000"); @@ -27,7 +27,7 @@ unittest auto pgconf2 = rc.cf("pgconf"); - assert(pgconf2.sn.key("host") == "//myhost"); + assert(pgconf2[]["host"] == "//myhost"); assert(pgconf2.sn.key("port").to!int == 5432); assert(pgconf2.sn.key("name") == "mydatabase"); assert(pgconf2.sn.key("password") == "/&#BD&@MXLE");