How to resolve the algorithm Read a configuration file step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Read a configuration file step by step in the D programming language
Table of Contents
Problem Statement
The task is to read a configuration file in standard configuration file format, and set variables accordingly. For this task, we have a configuration file as follows:
For the task we need to set four variables according to the configuration entries as follows:
We also have an option that contains multiple parameters. These may be stored in an array.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Read a configuration file step by step in the D programming language
Source code in the d programming language
import std.stdio, std.string, std.conv, std.regex, std.getopt;
enum VarName(alias var) = var.stringof;
void setOpt(alias Var)(in string line) {
auto m = match(line, regex(`^(?i)` ~ VarName!Var ~ `(?-i)(\s*=?\s+(.*))?`));
if (!m.empty) {
static if (is(typeof(Var) == string[]))
Var = m.captures.length > 2 ? m.captures[2].split(regex(`\s*,\s*`)) : [""];
static if (is(typeof(Var) == string))
Var = m.captures.length > 2 ? m.captures[2] : "";
static if (is(typeof(Var) == bool))
Var = true;
static if (is(typeof(Var) == int))
Var = m.captures.length > 2 ? to!int(m.captures[2]) : 0;
}
}
void main(in string[] args) {
string fullName, favouriteFruit;
string[] otherFamily;
bool needsPeeling, seedsRemoved; // Default false.
auto f = "readcfg.conf".File;
foreach (line; f.byLine) {
auto opt = line.strip.idup;
setOpt!fullName(opt);
setOpt!favouriteFruit(opt);
setOpt!needsPeeling(opt);
setOpt!seedsRemoved(opt);
setOpt!otherFamily(opt);
}
writefln("%s = %s", VarName!fullName, fullName);
writefln("%s = %s", VarName!favouriteFruit, favouriteFruit);
writefln("%s = %s", VarName!needsPeeling, needsPeeling);
writefln("%s = %s", VarName!seedsRemoved, seedsRemoved);
writefln("%s = %s", VarName!otherFamily, otherFamily);
}
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 Config members w/o hardcoding names
void PrintMembers(Config c)
{
foreach(M; __traits(derivedMembers, Config))
writeln(M ~ ` = `, __traits(getMember, c, M));
}
void main(in string[] args /* arg[0] is EXE name */) {
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 m = matchFirst(line, reNameValue);
if (m.empty) { writeln(`Wrong config line: ` ~ line); 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]);
}
}
PrintMembers(cfg);
}
class Config
{
string FullName;
string FavouriteFruit;
bool needsPeeling;
bool seedsRemoved;
string[] otherFamily;
}
You may also check:How to resolve the algorithm Shortest common supersequence step by step in the Racket programming language
You may also check:How to resolve the algorithm Assertions step by step in the Phix programming language
You may also check:How to resolve the algorithm Hello world/Web server step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Anagrams step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the MUMPS programming language