Jump to content

Update a configuration file: Difference between revisions

m
Line 2,056:
 
=={{header|Julia}}==
<lang julia>function cleansyntax(line)
<lang julia>stripbadchars(s) = join(filter(c -> isascii(c[1]) && !iscntrl(c[1]), split(s, "")), "")
function cleansyntax(line)
line = strip(line)
if line == ""
Line 2,069 ⟶ 2,068:
line = p == nothing ? uppercase(o) : uppercase(o) * " " * p
end
<lang julia>stripbadchars(s) = join(filter(c -> isascii(c[1]) && !iscntrl(c[1]), split(sline, "")), "")
stripbadchars(line)
end
 
Line 2,078 ⟶ 2,077:
the configuration files's lines prior to other function appication.
"""
cleansyntax!(lines) = (for (i, li) in enumerate(lines) lines[i] = cleansyntax(li) end; lines)
function cleansyntax!(lines)
 
for (i, li) in enumerate(lines)
isdisabled(line) = startswith(line, [';', '#'])
lines[i] = cleansyntax(li)
isenabled(line) = !isdisabled(line)
 
function splitline(line)
arr = split(line, r"\s+", limit=2)
if length(arr) < 2
return (arr[1], nothing)
end
return (arr[1], arr[2])
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)
lines[i] = disable(";" * li)
break # note: only first one found is disabled
end
Line 2,097 ⟶ 2,100:
end
 
isenabled(line) = !isdisabled(line)
enable(line) = isdisabled(line) ? line[2:end] : line
function enable!(lines, opt)
for (i, li) in enumerate(lines)
Line 2,112 ⟶ 2,113:
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 * " " * string(newparam))
function changeparam!(lines, opt, newparam)
for (i, li) in enumerate(lines)
Line 2,162 ⟶ 2,154:
changeparam!(cfglines, "NUMBEROFSTRAWBERRIES", 62000)
 
cfghconst cfw = open(filename, "w")
for li in cfglines
if li != ""
Line 2,168 ⟶ 2,160:
li = ""
end
write(cfghcfw, li * "\n")
end
end
4,108

edits

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