Clean and comment the code, strip quotes
This commit is contained in:
@ -4,16 +4,19 @@ namespace eval config_reader {
|
||||
variable version 2.0
|
||||
}
|
||||
|
||||
# return a list of section names from the dict
|
||||
proc config_reader::get_sections {the_dict} {
|
||||
upvar 1 $the_dict 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} {
|
||||
upvar 1 $the_dict db
|
||||
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} {
|
||||
upvar 1 $the_dict db
|
||||
set varname [string tolower $varname]
|
||||
@ -26,6 +29,7 @@ proc config_reader::get_var {the_dict section varname} {
|
||||
return [dict get $db $section $varname]
|
||||
}
|
||||
|
||||
# add a new section to the dictionary
|
||||
proc config_reader::add_section {the_dict section} {
|
||||
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} {
|
||||
upvar 1 $the_dict db
|
||||
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
|
||||
}
|
||||
|
||||
# Load the named configuration file and return the dict
|
||||
proc config_reader::parse_file {filename} {
|
||||
variable dictionary [dict create]
|
||||
variable cursection
|
||||
@ -58,9 +64,18 @@ proc config_reader::parse_file {filename} {
|
||||
config_reader::add_section dictionary $cursection
|
||||
}
|
||||
.*=.* {
|
||||
# split on the '=' and trim leading and trailing spaces
|
||||
set pair [split $line =]
|
||||
set name [string trim [lindex $pair 0] " "]
|
||||
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
|
||||
}
|
||||
default {
|
||||
@ -73,6 +88,7 @@ proc config_reader::parse_file {filename} {
|
||||
return $dictionary
|
||||
}
|
||||
|
||||
# Load the named file with parse_file print and return the dict for testing
|
||||
proc config_reader::dump {{the_config startup.ini}} {
|
||||
set my_dict [config_reader::parse_file $the_config]
|
||||
puts "Dict: $my_dict"
|
||||
@ -88,6 +104,7 @@ proc config_reader::parse_file {filename} {
|
||||
return $my_dict
|
||||
}
|
||||
|
||||
# This code is for i=unit testing in the tclsh executable
|
||||
if { "[lindex [split [info nameofexecutable] "/"] end]" == "tclsh"} {
|
||||
set filename "/tmp/test_[pid].ini"
|
||||
set sects [list]
|
||||
|
Reference in New Issue
Block a user