High Maintenance:
Drive state save/restore from a list of variables to save
Example: A set of save/restore procs
foreach variable $list {
eval "global $variable"
puts $output "[list $variable [subst $$variable]]"
}
close $output
}
proc restoreState {stateFile} {
set input [open $stateFile "r"]
while {![eof $input]} {
set len [gets $input list]
if {$len > 0} {
eval "global [lindex $list 0]"
eval "[list set [lindex $list 0] [lindex $list 1]]"
}
}
close $input
}
proc saveState {stateFile list} {
set output [open $stateFile "w"]
Advantage:
Disadvantage:
Low Maintenance:
Keep state in arrays, and use those indices to drive save/restore. Save state as a set of Tcl commands.
Example: A save proc, and associated restore call.
# restore state
source $stateFile
proc saveState {stateFile stateArray} {
upvar $stateArray state
set output [open $stateFile "w"]
puts $output "[list array set $stateArray [array get state]]"
close $output
}
Advantage:
Disadvantage:
Use a naming convention define state indices to not save.
Example: A save proc, and associated restore call.
foreach index [array names state] {
if {[string first "nosave" $index] != 0} {
puts $output "[list set $stateArray\ndex) $state($index)]"
}
}
close $output
}
# restore state
source $stateFile
proc saveState {stateFile stateArray} {
upvar $stateArray state
set output [open $stateFile "w"]
Advantage:
Disadvantage: