Update a configuration file: Difference between revisions
→{{header|Julia}}
Line 2,076:
the configuration files's lines prior to other function appication.
"""
function cleansyntax!(
for (i, li) in enumerate(lines)
lines[i] = cleansyntax(li)
end
lines
end
isdisabled(line) = startswith(line, [';', '#'])
disable(line) = ";" * line
function disable!(lines, opt)
for (i, li) in enumerate(lines)
if isenabled(li) && splitline(li)[1] == uppercase(opt)
break # note: only first one found is disabled
end
Line 2,093 ⟶ 2,098:
enable(line) = isdisabled(line) ? line[2:end] : line
function enable!(lines, opt)
for (i, li) in enumerate(lines)
if isdisabled(li)
s = li[2:end]
if splitline(s)[1] == uppercase(opt)
break # note: only first one found is enabled
end
Line 2,115 ⟶ 2,120:
changeparam(line, newparam) = ((o, p) = splitline(enabled(line)); o * " " * newparam)
function changeparam!(lines, opt, newparam)
for (i, li) in enumerate(lines)
if isenabled(li)
o, p = splitline(li)
if o == opt
break # note: only first one found is changed
end
Line 2,144 ⟶ 2,149:
const cfg = activecfg(cfglines)
disable!(cfglines, "NEEDSPEELING")
Line 2,165 ⟶ 2,167:
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
NUMBEROFSTRAWBERRIES 62000
</pre>
|