RosettaCodeData/Task/Read-a-configuration-file/Tcl/read-a-configuration-file.tcl
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

46 lines
1.3 KiB
Tcl

proc readConfig {filename {defaults {}}} {
global cfg
# Read the file in
set f [open $filename]
set contents [read $f]
close $f
# Set up the defaults, if supplied
foreach {var defaultValue} $defaults {
set cfg($var) $defaultValue
}
# Parse the file's contents
foreach line [split $contents "\n"] {
set line [string trim $line]
# Skip comments
if {[string match "#*" $line] || [string match ";*" $line]} continue
# Skip blanks
if {$line eq ""} continue
if {[regexp {^\w+$} $line]} {
# Boolean case
set cfg([string tolower $line]) true
} elseif {[regexp {^(\w+)\s+([^,]+)$} $line -> var value]} {
# Simple value case
set cfg([string tolower $var]) $value
} elseif {[regexp {^(\w+)\s+(.+)$} $line -> var listValue]} {
# List value case
set cfg([string tolower $var]) {}
foreach value [split $listValue ","] {
lappend cfg([string tolower $var]) [string trim $value]
}
} else {
error "malformatted config file: $filename"
}
}
}
# Need to supply some default values due to config file grammar ambiguities
readConfig "fruit.cfg" {
needspeeling false
seedsremoved false
}
puts "Full name: $cfg(fullname)"
puts "Favourite: $cfg(favouritefruit)"
puts "Peeling? $cfg(needspeeling)"
puts "Unseeded? $cfg(seedsremoved)"
puts "Family: $cfg(otherfamily)"