Saturday, November 08, 2008

Tcl and Ini files

The NewStars client GUI is proceeding pretty well. I am finally getting tired of navigating from the client start directory to the regression directory (up two! src/testfiles/twop).

This is the implementation for the "Recent file list" in Tcl:

Create the resource file if it doesn't exist, or process it if it does:

if {![file exists [file join ~ $resourceFileName]]} {
close [open [file join ~ $resourceFileName] w]
} else {
eval [read [open [file join ~ $resourceFileName]]]
}


The ini file is a series of Tcl statements. That way, I can just "eval" the file to bring it in.

Then to handle saving the preferences:

rename exit origExit
proc exit {} {
set f [open [file join ~ $::resourceFileName] w]
foreach n [lsort [array names nsGui::prevFiles]] {
puts $f "set nsGui::prevFiles($n) \{$nsGui::prevFiles($n)\}"
}
origExit
}


The previous files is a map from the numbers ("1" to "9") to full path filenames. I can grab all the set values (which may be none) and write them out to that file.

By renaming "exit", I can trap any point in the program that is trying to exit. That way, I'm sure that the preferences are saved.

No comments: