File: diff2.tcl

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (58 lines) | stat: -rw-r--r-- 1,435 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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