diffcol 3.23 KB
#!/usr/bin/perl


if ($#ARGV < 1)
{
  die "usage: diffcol file1.tab file2.tab [expn-reg] [expn-reg] [...] \n";
  
}

$File1Name     = shift;
$File2Name     = shift;

open(FILE1, $File1Name) || die "Can't open file $File1Name";
open(FILE2, $File2Name) || die "Can't open file $File2Name";

$HeaderCount = 0;
while(<FILE1>)
{
  last if (/^\s*\d+/);
  if (!/^#/ && !/^\s*$/ && !/.*\(.*\)/)
    {
      ($SignalName, $Foo) = split;
      $SignalName =~ s/\[.*\]//;
      $SignalNumbers[$HeaderCount] = $SignalName;
      $SignalNames{$SignalName} = sprintf("%.3d", $HeaderCount++-1);
    }
}
$File1LineNumber = $.;
$HeaderCount--;
$Line1 = $_;

while(<FILE2>)
{
  last if (/^\s*\d+/);
}
$File2LineNumber = $.;
$Line2 = $_;

if ($File1LineNumber != $File2LineNumber)
{
  die "Error, tab files from different dumps";
}

if ($#ARGV == -1)
{
  open(EXPAND, "expand 0-$HeaderCount |");
  @ExpandNumbers = <EXPAND>;
  close(EXPAND);
  chop @ExpandNumbers;
}

while ($#ARGV > -1)
{
  $ExpandString = shift;
  
  if ($ExpandString =~ /^\d/)
    {
      open(EXPAND, "expand $ExpandString |");
      @TempExpandNumbers = <EXPAND>;
      close(EXPAND);
      chop @TempExpandNumbers;
      @ExpandNumbers = (@ExpandNumbers, @TempExpandNumbers);
    }
  else
    {
      @SignalDumpNames = split(/,/, $ExpandString);

      foreach $i (0..$#SignalDumpNames)
	{
	  if ($SignalDumpNames[$i] =~ /-/)
	    {
	      ($Signal1, $Signal2) = split(/-/, $SignalDumpNames[$i]);

	      die "Error, signal $Signal1 undefined \n" if !($SignalNames{$Signal1});		
	      die "Error, signal $Signal2 undefined \n" if !($SignalNames{$Signal2});		

	      open(EXPAND, "expand $SignalNames{$Signal1}-$SignalNames{$Signal2} |");
	      @TempExpandNumbers = <EXPAND>;
	      close(EXPAND);
	      chop @TempExpandNumbers;
	      @ExpandNumbers = (@ExpandNumbers, @TempExpandNumbers);	      
	    }
	  else
	    {
		die "Error, signal $SignalDumpNames[$i] undefined \n" if !($SignalNames{$SignalDumpNames[$i]});
		@ExpandNumbers = (@ExpandNumbers, $SignalNames{$SignalDumpNames[$i]}); 
	    }
	}
    }
}

$VectorNumber = 0;

while ($Line1)
{
  die "Error, files misaligned" 
    if ((($Line1 =~ /^#/) && !($Line2 =~ /^#/)) || (!($Line1 =~ /^#/) && ($Line2 =~ /^#/)));

  if (($Line1 =~ /^#/) && ($Line2 =~ /^#/))
    {
      $Line1 = <FILE1>;
      $Line2 = <FILE2>;
      $File1LineNumber++;
      $File2LineNumber++;  
      next;
    }
  
  @Line1Values = split(/[ \t\n]+/, $Line1);
  @Line2Values = split(/[ \t\n]+/, $Line2);
  
  die "Error, different number of columns in input files" if ($#Line1Values != $#Line2Values);    
  
  foreach $i (0 .. $#ExpandNumbers)
    {
      die "Error, signal $ExpandNumbers[$i] out of range 0-$HeaderCount" if ($ExpandNumbers[$i] > $HeaderCount);
      
      if ($Line1Values[$ExpandNumbers[$i]] ne $Line2Values[$ExpandNumbers[$i]])
	{
	  printf("Vector %4.4d, line %4.4d\t\t%s[%-18.18s,%s]->%-18.18s %s[%-18.18s,%s]->%s \n",
		 $VectorNumber, $File1LineNumber,
		 $File1Name, $SignalNumbers[$ExpandNumbers[$i]], $ExpandNumbers[$i], $Line1Values[$ExpandNumbers[$i]], 
		 $File2Name, $SignalNumbers[$ExpandNumbers[$i]], $ExpandNumbers[$i], $Line2Values[$ExpandNumbers[$i]]);
	}
    }
  
  $Line1 = <FILE1>;
  $Line2 = <FILE2>;
  $File1LineNumber++;
  $File2LineNumber++;  
  $VectorNumber++;  
}