diff options
author | William Joye <wjoye@cfa.harvard.edu> | 2016-10-27 19:39:39 (GMT) |
---|---|---|
committer | William Joye <wjoye@cfa.harvard.edu> | 2016-10-27 19:39:39 (GMT) |
commit | ea28451286d3ea4a772fa174483f9a7a66bb1ab3 (patch) | |
tree | 6ee9d8a7848333a7ceeee3b13d492e40225f8b86 /tcllib/examples/struct | |
parent | b5ca09bae0d6a1edce939eea03594dd56383f2c8 (diff) | |
parent | 7c621da28f07e449ad90c387344f07a453927569 (diff) | |
download | blt-ea28451286d3ea4a772fa174483f9a7a66bb1ab3.zip blt-ea28451286d3ea4a772fa174483f9a7a66bb1ab3.tar.gz blt-ea28451286d3ea4a772fa174483f9a7a66bb1ab3.tar.bz2 |
Merge commit '7c621da28f07e449ad90c387344f07a453927569' as 'tcllib'
Diffstat (limited to 'tcllib/examples/struct')
-rw-r--r-- | tcllib/examples/struct/README | 16 | ||||
-rw-r--r-- | tcllib/examples/struct/diff.tcl | 55 | ||||
-rw-r--r-- | tcllib/examples/struct/diff2.tcl | 58 |
3 files changed, 129 insertions, 0 deletions
diff --git a/tcllib/examples/struct/README b/tcllib/examples/struct/README new file mode 100644 index 0000000..f2927c1 --- /dev/null +++ b/tcllib/examples/struct/README @@ -0,0 +1,16 @@ +This directory contains some examples regarding the usage of struct +funtionality. For example a small diff tool based on + + struct::list longestCommonSubsequence. + +======================================================================= + +Example operations: + + tclsh ./diff2.tcl diff.tcl diff2.tcl + + Differences between the diff-tools in pseudo-'patch' form. + + tclsh ./diff.tcl diff.tcl diff2.tcl + + Differences between the diff-tools side by side. diff --git a/tcllib/examples/struct/diff.tcl b/tcllib/examples/struct/diff.tcl new file mode 100644 index 0000000..2e2b430 --- /dev/null +++ b/tcllib/examples/struct/diff.tcl @@ -0,0 +1,55 @@ +#!/usr/bin/env tclsh +## -*- tcl -*- +# MAIN PROGRAM +# +# Usage: +# diff.tcl file1 file2 +# +# Output: +# Puts out a list of lines consisting of: +# n1<TAB>n2<TAB>line +# +# where n1 is a line number in the first file, and n2 is a line number in the second file. +# The line is the text of the line. If a line appears in the first file but not the second, +# n2 is omitted, and conversely, if it appears in the second file but not the first, n1 +# is omitted. + +package require struct + +# Open the files and read the lines into memory + +set f1 [open [lindex $argv 0] r] +set lines1 [split [read $f1] \n] +close $f1 + +set f2 [open [lindex $argv 1] r] +set lines2 [split [read $f2] \n] +close $f2 + +set i 0 +set j 0 + +::struct::list assign [::struct::list longestCommonSubsequence $lines1 $lines2] x1 x2 + +foreach p $x1 q $x2 { + while { $i < $p } { + set l [lindex $lines1 $i] + puts "[incr i]\t\t$l" + } + while { $j < $q } { + set m [lindex $lines2 $j] + puts "\t[incr j]\t$m" + } + set l [lindex $lines1 $i] + puts "[incr i]\t[incr j]\t$l" +} +while { $i < [llength $lines1] } { + set l [lindex $lines1 $i] + puts "[incr i]\t\t$l" +} +while { $j < [llength $lines2] } { + set m [lindex $lines2 $j] + puts "\t[incr j]\t$m" +} + +exit diff --git a/tcllib/examples/struct/diff2.tcl b/tcllib/examples/struct/diff2.tcl new file mode 100644 index 0000000..a3e8526 --- /dev/null +++ b/tcllib/examples/struct/diff2.tcl @@ -0,0 +1,58 @@ +#!/usr/bin/env tclsh +## -*- tcl -*- +# MAIN PROGRAM +# +# Usage: +# diff2.tcl file1 file2 +# +# Output: +# Puts out a list of lines describing the changes from file1 to file2 +# in a format similar to 'patch'. It not the same as patch, but could +# be modified to be exactly the same. + +package require struct + +# Open the files and read the lines into memory + +set f1 [open [lindex $argv 0] r] +set lines1 [split [read $f1] \n] +close $f1 + +set f2 [open [lindex $argv 1] r] +set lines2 [split [read $f2] \n] +close $f2 + +set i 0 +set j 0 + +::struct::list assign [::struct::list longestCommonSubsequence $lines1 $lines2] x1 x2 + +set chunks 0 +foreach chunk [::struct::list lcsInvert2 $x1 $x2 [llength $lines1] [llength $lines2]] { + set chunks 1 + puts =========================================== + puts $chunk + puts ------------------------------------------- + + ::struct::list assign [lindex $chunk 1] b1 e1 + ::struct::list assign [lindex $chunk 2] b2 e2 + + switch -exact -- [lindex $chunk 0] { + changed { + puts "< [join [lrange $lines1 $b1 $e1] "\n< "]" + puts "---" + puts "> [join [lrange $lines2 $b2 $e2] "\n> "]" + } + added { + puts "> [join [lrange $lines2 $b2 $e2] "\n> "]" + } + deleted { + puts "< [join [lrange $lines1 $b1 $e1] "\n< "]" + } + } +} +if {$chunks} { + puts =========================================== +} + +exit |