Clean and comment the code, strip quotes
This commit is contained in:
@ -4,16 +4,19 @@ namespace eval config_reader {
|
|||||||
variable version 2.0
|
variable version 2.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# return a list of section names from the dict
|
||||||
proc config_reader::get_sections {the_dict} {
|
proc config_reader::get_sections {the_dict} {
|
||||||
upvar 1 $the_dict db
|
upvar 1 $the_dict db
|
||||||
return [dict keys $db]
|
return [dict keys $db]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# return a list of variables in the named section of the dict
|
||||||
proc config_reader::get_variables {the_dict section} {
|
proc config_reader::get_variables {the_dict section} {
|
||||||
upvar 1 $the_dict db
|
upvar 1 $the_dict db
|
||||||
return [dict keys [dict get $db $section]]
|
return [dict keys [dict get $db $section]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# return the value of the named variable in the named section
|
||||||
proc config_reader::get_var {the_dict section varname} {
|
proc config_reader::get_var {the_dict section varname} {
|
||||||
upvar 1 $the_dict db
|
upvar 1 $the_dict db
|
||||||
set varname [string tolower $varname]
|
set varname [string tolower $varname]
|
||||||
@ -26,6 +29,7 @@ proc config_reader::get_var {the_dict section varname} {
|
|||||||
return [dict get $db $section $varname]
|
return [dict get $db $section $varname]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# add a new section to the dictionary
|
||||||
proc config_reader::add_section {the_dict section} {
|
proc config_reader::add_section {the_dict section} {
|
||||||
upvar 1 $the_dict db
|
upvar 1 $the_dict db
|
||||||
|
|
||||||
@ -34,6 +38,7 @@ proc config_reader::add_section {the_dict section} {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# add a new section/variable and set its value
|
||||||
proc config_reader::set_var {the_dict section varname value} {
|
proc config_reader::set_var {the_dict section varname value} {
|
||||||
upvar 1 $the_dict db
|
upvar 1 $the_dict db
|
||||||
if {![dict exists $db $section]} {
|
if {![dict exists $db $section]} {
|
||||||
@ -43,6 +48,7 @@ proc config_reader::set_var {the_dict section varname value} {
|
|||||||
dict set db $section $varname $value
|
dict set db $section $varname $value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Load the named configuration file and return the dict
|
||||||
proc config_reader::parse_file {filename} {
|
proc config_reader::parse_file {filename} {
|
||||||
variable dictionary [dict create]
|
variable dictionary [dict create]
|
||||||
variable cursection
|
variable cursection
|
||||||
@ -58,11 +64,20 @@ proc config_reader::parse_file {filename} {
|
|||||||
config_reader::add_section dictionary $cursection
|
config_reader::add_section dictionary $cursection
|
||||||
}
|
}
|
||||||
.*=.* {
|
.*=.* {
|
||||||
|
# split on the '=' and trim leading and trailing spaces
|
||||||
set pair [split $line =]
|
set pair [split $line =]
|
||||||
set name [string trim [lindex $pair 0] " "]
|
set name [string trim [lindex $pair 0] " "]
|
||||||
set value [string trim [lindex $pair 1] " "]
|
set value [string trim [lindex $pair 1] " "]
|
||||||
|
# Remove matching quotes on both ends (single, double and braces)
|
||||||
|
if { [string index $value 0] == "'" && [string index $value end] == "'" } {
|
||||||
|
set value [string range 1 end-1]
|
||||||
|
} elseif { [string index $value 0] == "\"" && [string index $value end] == "\"" } {
|
||||||
|
set value [string range 1 end-1]
|
||||||
|
} elseif { [string index $value 0] == "{" && [string index $value end] == "}" } {
|
||||||
|
set value [string range 1 end-1]
|
||||||
|
}
|
||||||
config_reader::set_var dictionary $cursection $name $value
|
config_reader::set_var dictionary $cursection $name $value
|
||||||
}
|
}
|
||||||
default {
|
default {
|
||||||
error "Error parsing $filename (line: $line_no): $line"
|
error "Error parsing $filename (line: $line_no): $line"
|
||||||
}
|
}
|
||||||
@ -73,21 +88,23 @@ proc config_reader::parse_file {filename} {
|
|||||||
return $dictionary
|
return $dictionary
|
||||||
}
|
}
|
||||||
|
|
||||||
proc config_reader::dump {{the_config startup.ini}} {
|
# Load the named file with parse_file print and return the dict for testing
|
||||||
set my_dict [config_reader::parse_file $the_config]
|
proc config_reader::dump {{the_config startup.ini}} {
|
||||||
puts "Dict: $my_dict"
|
set my_dict [config_reader::parse_file $the_config]
|
||||||
foreach s [config_reader::get_sections my_dict] {
|
puts "Dict: $my_dict"
|
||||||
set section "$s"
|
foreach s [config_reader::get_sections my_dict] {
|
||||||
puts "\n\[$section\]"
|
set section "$s"
|
||||||
foreach n [config_reader::get_variables my_dict $s] {
|
puts "\n\[$section\]"
|
||||||
set name "$n"
|
foreach n [config_reader::get_variables my_dict $s] {
|
||||||
set value "[config_reader::get_var my_dict $section $n]"
|
set name "$n"
|
||||||
puts " $name = $value"
|
set value "[config_reader::get_var my_dict $section $n]"
|
||||||
}
|
puts " $name = $value"
|
||||||
}
|
}
|
||||||
return $my_dict
|
|
||||||
}
|
}
|
||||||
|
return $my_dict
|
||||||
|
}
|
||||||
|
|
||||||
|
# This code is for i=unit testing in the tclsh executable
|
||||||
if { "[lindex [split [info nameofexecutable] "/"] end]" == "tclsh"} {
|
if { "[lindex [split [info nameofexecutable] "/"] end]" == "tclsh"} {
|
||||||
set filename "/tmp/test_[pid].ini"
|
set filename "/tmp/test_[pid].ini"
|
||||||
set sects [list]
|
set sects [list]
|
||||||
|
Reference in New Issue
Block a user