Read a configuration file: Difference between revisions
m
→{{header|Wren}}: Minor tidy
No edit summary |
m (→{{header|Wren}}: Minor tidy) |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1,292:
=={{header|D}}==
<syntaxhighlight lang="d">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] : "";
Line 1,312 ⟶ 1,313:
void main(in string[] args) {
string fullName, favouriteFruit
string[] otherFamily;
bool needsPeeling, seedsRemoved; // Default false.
auto f = "readcfg.
foreach (line; f.byLine) {
auto opt = line.strip.idup;
setOpt!fullName(opt);
setOpt!favouriteFruit(opt);
Line 1,325 ⟶ 1,329:
}
writefln("%
writefln("%
writefln("%
writefln("%
writefln("%
}</syntaxhighlight>
{{out}}
<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
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
▲ if len(a$)=0 then // we have a boolean
a$()=piece$(a$,",")▼
▲ Export$=b$+" = "+if$(NotUsed->"false", "true")+nl$
▲ else.if instr(a$,",")>0 then // multiple value
Export$=format$("{0}({1}) = {2}",b$,i+1, trim$(a$(i)))+nl$▼
▲ a$()=piece$(a$,",")
else▼
▲ 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="
import "./ioutil" for FileUtil
class Configuration {
|