Script to dump a datafile as Tcl script which reconstructs it

The following mk2tcl.tcl script can be used to dump and restore a wikit.tkd datafile (any MK datafile, in fact):


 # Dump a Metakit file as a set of Tcl commands, which when run will
 # reconstruct the datafile.  This probably won't work for binary data.
 #
 # 02oct02 [email protected]
 set argv0 [file tail $argv0]
 set setup {
     package require Mk4tcl
     set filename [lindex $argv 0]
     if {$filename eq ""} { puts stderr "Usage: $argv0 file"; exit 1 }
 }
 eval $setup
 mk::file open db $filename -readonly
 puts "# Generated by '$argv0 $filename' - [clock format [clock seconds]]"
 puts $setup
 puts {file delete $filename}
 puts {mk::file open db $filename}
 foreach v [mk::file views db] {
     puts [list mk::view layout db.$v [mk::view layout db.$v]]
 }
 puts "
 [string repeat # 75]
 "
 foreach v [mk::file views db] {
   mk::loop c db.$v {
     puts [linsert [mk::get $c] 0 mk::set $c]
   }
   puts [string repeat # 75]
 }
 puts [list mk::file commit db]

Example:

   tclkit mk2tcl.tcl wikit.tkd >mydump.tcl
   tclkit mydump.tcl newfile.tkd

This can also be used to compact a datafile, since the resulting datafile is created from scratch.


This script doesn't save any subviews. The following procedure could be helpful:

 # Arguments: a tag or view
 # Result: a string
 # 04dec03 [email protected]
 proc saveView {view} {
     set saveString ""
     mk::loop cursor $view {
         append saveString [linsert [mk::get $cursor] 0 mk::set $cursor] \n
         foreach prop [mk::view info $cursor] {
             # for each subview
             set prop [split $prop :]
             if {[string equal [lindex $prop 1] V]} {
                 set key [lindex $prop 0]
                 append saveString [saveView $cursor.$key]
             }
         }
     }
     return $saveString
 }    

Example:

 foreach v [mk::file views db] {
     puts [saveView db.$v]
 }