Read a configuration file: Difference between revisions

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 members of Config classmembers w/o hardcoding names
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
foreach (line; f.byLine) {
auto optm = matchFirst(line.strip.idup, reNameValue);
if (optm.lengthempty) =={ 0writeln(`Wrong ||config opt[0]line: ==` '#'~ || opt[0] == 'line);') continue; }
 
auto m = matchFirst(opt, reNameValue);
if (m.empty) { writeln(`Wrong config line: ` ~ opt); continue; }
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]);
}
}
7

edits