Jump to content

Update a configuration file: Difference between revisions

mNo edit summary
Line 2,399:
$*SPEC.catfile: $*SPEC.tmpdir, ("a".."z").roll(20).join
}</lang>
 
=={{header|Phix}}==
Very basic (and contains most of the code from the read configuration file example)<br>
Note in particular there is no real attempt to distinguish between booleans and integers.
<lang Phix>integer fn = open("RCTEST.INI","r")
sequence lines = get_text(fn,GT_LF_STRIPPED)
close(fn)
constant dini = new_dict()
for i=1 to length(lines) do
string li = trim(lines[i])
if length(li)
and not find(li[1],"#;") then
integer k = find(' ',li)
if k!=0 then
string rest = li[k+1..$]
li = upper(li[1..k-1])
putd(li,rest,dini)
else
putd(upper(li),1,dini)
end if
end if
end for
 
deld("NEEDSPEELING",dini)
setd("SEEDSREMOVED",1,dini)
setd("NUMBEROFBANANAS",1024,dini)
setd("NUMBEROFSTRAWBERRIES",62000,dini)
 
for i=1 to length(lines) do
string li = trim(lines[i])
if length(li)
and li[1]!='#' then
if li[1]=';' then
li = trim(li[2..$])
end if
integer k = find(' ',li)
if k!=0 then
string rest = li[k+1..$]
li = upper(li[1..k-1])
k = getd_index(li,dini)
if k=0 then
lines[i] = "; "&li&" "&rest
else
object o = getd_by_index(k,dini)
if not string(o) then o = sprint(o) end if
lines[i] = li&" "&o
deld(li,dini)
end if
else
if getd(li,dini) then
lines[i] = li
deld(li,dini)
else
lines[i] = "; "&li
end if
end if
end if
end for
function visitor(object key, object data, object /*user_data*/)
lines = append(lines,key&" "&sprint(data))
return 1
end function
traverse_dict(routine_id("visitor"),0,dini)
fn = open("RCTEST.INI","w")
puts(fn,join(lines,"\n"))
close(fn)</lang>
Resulting RCTEST.INI file:
<pre>
# 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 1024
NUMBEROFSTRAWBERRIES 62000
</pre>
 
=={{header|PicoLisp}}==
7,822

edits

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