Read a configuration file: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(6 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.txt".File;
auto f = "readcfg.conf".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,235 ⟶ 3,289:
end
end</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
The congiguration.txt is in a zip file in Encode64 Binary part
We can export to disk or use it as is, through the buffer
 
to make it I use this:
<syntaxhighlight lang="m2000 interpreter">
Declare zip compressor
a$=str$(a$)
Method zip, "AddFromMemory",a$, "configuration.txt" as ok
Method zip,"CreateZipBuffer" as buf1
clipboard String$(eval$(buf1) as Encode64)
 
//where a$ defined (before)
a$={text here from first line
until last line
}
</syntaxhighlight>
 
 
To do Declare Zip Nothing is optional (for User forms isn't though)
 
 
<syntaxhighlight lang="m2000 interpreter">
module check(a$, id as list){
Document Export$
nl$={
}
dim L$() : L$()=piece$(a$,nl$)
if len(L$())=0 then exit
for i=0 to len(L$())-1
a$=trim$(L$(i))
b$=left$(a$, 1)
select case b$
case ";"
examineValue(true)
case >"#"
examineValue(false)
end select
next
Report Export$ // result or Clipboard Export$
Sub examineValue(NotUsed as boolean)
local i
if NotUsed then
a$=trim$(mid$(a$,2))+" "
b$=leftpart$(a$," ")
else
a$+=" "
b$=leftpart$(a$," ")
end if
a$=trim$(rightpart$(a$," "))
// optional = removed
if left$(a$,1)="=" then a$=trim$(mid$(a$,2))
// if not exist ignore it
if exist(id,ucase$(b$)) then
if len(a$)=0 then // we have a boolean
Export$=b$+" = "+if$(NotUsed->"false", "true")+nl$
else.if instr(a$,",")>0 then // multiple value
local a$()
a$()=piece$(a$,",")
for i=0 to len(a$())-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
VgjJyCxWyCxWSFRIzs9Ly0wvLUosyczPU0jLzElVyMxTKC5JzEtJLErBJp2WX5Sb
WMLLpczLpazgk5mXWqyQlJqemZeXmZeuUJ5ZkqGQqJCRWJyhkF+kkKhQnJqbmZyf
k5+nkFiUqpCZnpdflJqikFSpUJKRqpBYUJCTmQw2G2RYQVF+elFirp6CU05iXrZC
DthskLbEnOJ8PHrhGnm5QMbAPAdSlVaak5OXmJuqUJBYlJibWpJaxMvlFurj4+fo
66rglp+v4JRYlAQSRNaYqJCWWJZfWpRZkqqQVlSaWcLL5eYY5h8a5Bni6hYU6hmi
kJSYl5iXiK4rKT8/JzUxT6EkI7FEoTgjvzQnRSEpVaE4tYSXy8/V1SU4wNXVx9PP
HUkfTEtmsUJyfm5ual5JaopCfmkJL5e1QjBIS5Crr3+YqwtEizNKbOQXgCmQ9yDB
lJdfopCcWAyyMa84sySzLFVHIam0BC0SkUJCWSElsSQRbDmKNoXEvBSF3MRKkOsL
ilKLU4vKiAl4R5ibEnMUUgtLE3OKFYoz0/MUkhPzQCaVFqemKJTkKxSngpxQkorL
XWBHgcxLK8rPBVuJ5FM9eHinFOUXFCCcVZBYVJxapKcAdQqa4VATQH4qScxOVcgt
zSnJLMhBShfFcHeBjQTFRmKxHjiNpyamgNI2KFBKihIzc8AJPSOzJLW4IDE5VSGx
KL80LwXJ/dAYQREDB3RaZmpOSjHITPyZASVc/UM8XIPcHH09fSIVgjJKoSlWR8Ej
saioEp5+AVBLAQItABQAAAgIAO8FflU2rdfqSAIAANQDAAARAAAAAAAAAAAAIAAA
AAAAAABjb25maWd1cmF0aW9uLnR4dFBLBQYAAAAAAQABAD8AAAB3AgAAAAA=} as zip1
Declare zip compressor
method zip,"OpenZipBuf", zip1
method zip, "ExtractOneToBuffer", "configuration.txt" as buf
If true then
// save buf to file, the load to document as ANSI 1033
open "configuration.txt" for output as #f
put #f,buf, 1
close #f
document b$ : Load.doc b$, "configuration.txt", 1033
check b$, valid
else
check chr$(eval$(buf)), valid
end if
</syntaxhighlight>
{{out}}
<pre>FULLNAME = Foo Barber
FAVOURITEFRUIT = banana
NEEDSPEELING = true
SEEDSREMOVED = false
OTHERFAMILY(1) = Rhu Barber
OTHERFAMILY(2) = Harry Barber
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 5,645 ⟶ 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,485

edits