Read a configuration file: Difference between revisions
→Variant 2
Line 1,342:
<syntaxhighlight lang="d">
import std.stdio, std.string, std.conv, std.regex, std.algorithm;
auto reNameValue = ctRegex!(`^(\w+)\s*=?\s*(\S.*)?`);// ctRegex creates regexp parser at compile time
// print
void PrintMembers(Config c)
{
Line 1,355 ⟶ 1,356:
auto cfg = new Config;
auto f = args[1].File;// open config given in command line
foreach (line; f.byLineCopy.map!(s => s.strip).filter!(s => !s.empty && s[0] != '#' && s[0] != ';')) {// free loop from unnecessary lines
auto
if (
switch(m[1].toUpper) {
case `FULLNAME`: cfg.FullName = m[2]; break;
case `FAVOURITEFRUIT`: cfg.FavouriteFruit = m[2]; break;
case `NEEDSPEELING`: cfg.needsPeeling = (m[2].toUpper != `FALSE`); break;
case `SEEDSREMOVED`: cfg.seedsRemoved = (m[2].toUpper != `FALSE`); break;
case `OTHERFAMILY`: cfg.otherFamily = split(m[2], regex(`\s*,\s*`)); break;// regex allows to avoid 'strip' step
default:
writeln(`Unknown config variable: ` ~ m[1]);
}
}
|