Data Driven State Save/Restore


High Maintenance:

Drive state save/restore from a list of variables to save

Example: A set of save/restore procs

proc saveState {stateFile list} {
  set output [open $stateFile "w"]

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 }

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.

proc saveState {stateFile stateArray} {
  upvar $stateArray state 
  set output [open $stateFile "w"]
  puts $output "[list array set $stateArray [array get state]]"
  close $output
 }

# restore state source $stateFile

Advantage:

Disadvantage:


Alternate Implementation:

Use a naming convention define state indices to not save.

Example: A save proc, and associated restore call.

proc saveState {stateFile stateArray} {
  upvar $stateArray state 
  set output [open $stateFile "w"]

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

Advantage:

Disadvantage:


[Contents]
This document was translated by troff2html v0.21 on July 18, 1997.