Read a configuration file: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 4 users not shown)
Line 1,292:
 
=={{header|D}}==
 
{{Incorrect|D|Optional '=' between parameter name and value is not handled.}}
 
<syntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.regex, std.getopt;
 
enum VarName(alias var) = var.stringof.toUpper;
 
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] : "";
Line 1,312 ⟶ 1,313:
 
void main(in string[] args) {
string fullName, favouriteFruit, otherFamily;
string[] otherFamily;
bool needsPeeling, seedsRemoved; // Default false.
 
auto f = "readcfg.txtconf".File;
 
foreach (line; f.byLine) {
auto opt = line.strip.idup;
 
setOpt!fullName(opt);
setOpt!favouriteFruit(opt);
Line 1,325 ⟶ 1,329:
}
 
writefln("%14ss = %s", VarName!fullName, fullName);
writefln("%14ss = %s", VarName!favouriteFruit, favouriteFruit);
writefln("%14ss = %s", VarName!needsPeeling, needsPeeling);
writefln("%14ss = %s", VarName!seedsRemoved, seedsRemoved);
writefln("%14ss = %s", VarName!otherFamily, otherFamily);
}</syntaxhighlight>
{{out}}
<pre> FULLNAMEfullName = Foo Barber
AVOURITEFRUITfavouriteFruit = banana
NEEDSPEELINGneedsPeeling = true
SEEDSREMOVEDseedsRemoved = false
OTHERFAMILYotherFamily = ["Rhu Barber", "Harry Barber", "John"]</pre>
 
=== Variant 2 ===
Line 1,924 ⟶ 1,928:
: # ( -- ) 1 PARSE 2DROP ; \ parse line and throw away
: = ( addr --) 1 PARSE trim ROT PLACE ; \ string assignment operator
synonym' ;# alias ; # \ 2nd comment operator is simple
 
FORTH DEFINITIONS
Line 1,967 ⟶ 1,971:
Rhu Barber
Harry Barber ok</PRE>
 
Note that parsing a config file using the forth text interpreter this way is probably only safe if you are the only one that edits the config file, as it can execute any forth word.
 
=={{header|Fortran}}==
Line 2,187 ⟶ 2,193:
Other family(0) = Rhu Barber
Other family(1) = Harry Barber
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn SaveConfiguration
CFDictionaryRef defaults = @{¬
@"FULLNAME" : @"Foo Barber",¬
@"FAVOURITEFRUIT" : @"banana",¬
@"NEEDSPEELING" : @YES,¬
@"SEEDSREMOVED" : @NO,¬
@"OTHERFAMILY" : @[@"Rhu Barber", @"Harry Barber"]}
UserDefaultsRegisterDefaults( defaults )
end fn
 
local fn ReadConfiguration
CFStringRef tempStr
CFStringRef fullname = fn UserDefaultsString( @"FULLNAME" )
CFStringRef favouritefruit = fn UserDefaultsString( @"FAVOURITEFRUIT" )
BOOL needspeeling = fn UserDefaultsBool( @"NEEDSPEELING" )
BOOL seedsremoved = fn UserDefaultsBool( @"SEEDSREMOVED" )
CFArrayRef otherfamily = fn UserDefaultsArray( @"OTHERFAMILY" )
printf @"Saved configuration:\n"
printf @"FULLNAME: %@", fullname
printf @"FAVOURITEFRUIT: %@", favouritefruit
if needspeeling == YES then tempStr = @"TRUE" else tempStr = @"FALSE"
printf @"NEEDSPEELING: %@", tempStr
if seedsremoved == YES then tempStr = @"TRUE" else tempStr = @"FALSE"
printf @"SEEDSREMOVED: %@", @"(undefined)"
printf @"OTHERFAMILY: %@, %@", otherfamily[0], otherfamily[1]
end fn
 
fn SaveConfiguration
fn ReadConfiguration
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Saved configuration:
 
FULLNAME: Foo Barber
FAVOURITEFRUIT: banana
NEEDSPEELING: TRUE
SEEDSREMOVED: (undefined)
OTHERFAMILY: Rhu Barber, Harry Barber
</pre>
 
Line 3,259 ⟶ 3,313:
 
<syntaxhighlight lang="m2000 interpreter">
module check(a$, id as list){
Document Export$
nl$={
Line 3,286 ⟶ 3,340:
end if
a$=trim$(rightpart$(a$," "))
// optional = removed
if len(a$)=0 then // we have a boolean
if left$(a$,1)="=" then a$=trim$(mid$(a$,2))
Export$=b$+" = "+if$(NotUsed->"false", "true")+nl$
// if not exist ignore it
else.if instr(a$,",")>0 then // multiple value
if exist(id,ucase$(b$)) then
local a$()
if len(a$)=0 then // we have a boolean
a$()=piece$(a$,",")
Export$=b$+" = "+if$(NotUsed->"false", "true")+nl$
for i=0 to len(a$())-1
else.if instr(a$,",")>0 then // multiple value
Export$=format$("{0}({1}) = {2}",b$,i+1, trim$(a$(i)))+nl$
next local a$()
a$()=piece$(a$,",")
else
Export$=b$+" for i=0 "+to len(a$+nl$())-1
Export$=format$("{0}({1}) = {2}",b$,i+1, trim$(a$(i)))+nl$
next
else
Export$=b$+" = "+a$+nl$
end if
end if
End Sub
}
valid=list:="FULLNAME", "FAVOURITEFRUIT", "NEEDSPEELING", "SEEDSREMOVED", "OTHERFAMILY"
binary{
UEsDBBQAAAgIAO8FflU2rdfqSAIAANQDAAARAAAAY29uZmlndXJhdGlvbi50eHRT
Line 3,324 ⟶ 3,384:
close #f
document b$ : Load.doc b$, "configuration.txt", 1033
check b$, valid
else
check chr$(eval$(buf)), valid
end if
</syntaxhighlight>
Line 3,337 ⟶ 3,397:
OTHERFAMILY(2) = Harry Barber
</pre>
 
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 5,748 ⟶ 5,807:
{{libheader|Wren-ioutil}}
Includes 'seeds removed' in the map (with a default value of false) even though it's commented out of the configuration file.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./ioutil" for FileUtil
 
class Configuration {
9,476

edits