Jump to content

Update a configuration file: Difference between revisions

(→‎{{header|Perl 6}}: Update broken example. use a module for tempfile, explicitly close file)
Line 2,053:
NUMBEROFBANANAS 1024
NUMBEROFSTRAWBERRIES 62000</pre>
 
 
=={{header|Julia}}==
<lang julia>stripbadchars(s) = join(filter(c -> isascii(c[1]) && !iscntrl(c[1]), split(s, "")), "")
function cleansyntax(line)
line = strip(line)
if length(line) == 0 || line[1] == '#'
return line
elseif line[1] == ';'
line = replace(line, r"^;[;]+" => ";")
else # active option
o, p = splitline(line)
line = p == nothing ? uppercase(o) : uppercase(o) * " " * p
end
stripbadchars(line)
end
 
"""
cleansyntax!(lines)
 
To simplify subsequent code, this is assumed to be run to clean up
the configuration files's lines prior to other function appication.
"""
cleansyntax!(lines) = map(li -> cleansyntax(li), lines)
 
isdisabled(line) = startswith(line, [';', '#'])
disable(line) = ";" * line
function disable!(lines, opt)
for li in lines
if isenabled(li) && splitline(li)[1] == uppercase(opt)
li = disable(li)
break # note: only first one found is disabled
end
end
lines
end
 
isenabled(line) = !isdisabled(line)
enable(line) = isdisabled(line) ? line[2:end] : line
function enable!(lines, opt)
for li in lines
if isdisabled(li)
s = li[2:end]
if splitline(s)[1] == uppercase(opt)
li = s
break # note: only first one found is enabled
end
end
end
lines
end
 
function splitline(line)
arr = split(line, r"\s+", limit=2)
if length(arr) < 2
return (arr[1], nothing)
end
return (arr[1], arr[2])
end
 
changeparam(line, newparam) = ((o, p) = splitline(enabled(line)); o * " " * newparam)
function changeparam!(lines, opt, newparam)
for li in lines
if isenabled(li)
o, p = splitline(li)
if o == opt
li = o * " " * string(newparam)
break # note: only first one found is changed
end
end
end
lines
end
 
function activecfg(lines)
cfgdict = Dict()
for li in lines
if isenabled(li)
o, p = splitline(li)
cfgdict[o] = p
end
end
cfgdict
end
 
const filename = "fruit.cfg"
const cfh = open(filename)
const cfglines = cleansyntax!(readlines(cfh))
close(cfh)
 
const cfg = activecfg(cfglines)
 
println(cfglines)
println(cfg)
 
disable!(cfglines, "NEEDSPEELING")
enable!(cfglines, "SEEDSREMOVED")
changeparam!(cfglines, "NUMBEROFBANANAS", 1024)
 
if !haskey(cfg, "NUMBEROFSTRAWBERRIES")
push!(cfglines, "NUMBEROFSTRAWBERRIES 62000")
end
cfg["NUMBEROFSTRAWBERRIES"] = 62000
changeparam!(cfglines, "NUMBEROFSTRAWBERRIES", 62000)
 
cfgh = open(filename, "w")
for li in cfglines
if li != ""
write(cfgh, li * "\n")
end
end
</lang> {{output}} <pre>
Contents of the revised file:
# This is a configuration file in standard configuration file format
#
# Lines begininning with a hash or a semicolon are ignored by the application
# program. Blank lines are also ignored by the application program.
# The first word on each non comment line is the configuration option.
# Remaining words or numbers on the line are configuration parameter
# data fields.
# Note that configuration option names are not case sensitive. However,
# configuration parameter data is case sensitive and the lettercase must
# be preserved.
# This is a favourite fruit
FAVOURITEFRUIT banana
# This is a boolean that should be set
NEEDSPEELING
# This boolean is commented out
; SEEDSREMOVED
# How many bananas we have
NUMBEROFBANANAS 48
NUMBEROFSTRAWBERRIES 62000
</pre>
 
=={{header|Kotlin}}==
4,108

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.