Subj : text_compare To : All From : Angus Mcleod Date : Thu Feb 03 2005 11:39 am *Seems* to work.... Anyone want to do some tests? ----------8<----------------------------------------- #!/usr/bin/perl -w use FileHandle; # "Who don't like it, leave!" -- Dr. Eric Williams. $original = shift @ARGV || "/sbbs/ctrl/text.dat.OLD"; $modified = shift @ARGV || "/sbbs/ctrl/text.dat"; $updated = shift @ARGV || "/sbbs/ctrl/text.dat.NEW"; $CR = chr(13); $LF = chr(10); $HT = chr(9); $BS = chr(8); $BEL = chr(7); $FF = chr(12); $VT = chr(11); # Convert escape sequences sub unescape($) { my $line = shift; $line =~ s/\\(\d{1,3})/chr($1)/egs; $line =~ s/\\\\/\\/gs; $line =~ s/\\\?/?/gs; $line =~ s/\\x([[:xdigit:]]{1,2})/chr(hex($1))/egs; $line =~ s/\\'/'/gs; $line =~ s/\\"/"/gs; $line =~ s/\\r/$CR/gs; $line =~ s/\\n/$LF/gs; $line =~ s/\\t/$HT/gs; $line =~ s/\\b/$BS/gs; $line =~ s/\\a/$BEL/gs; $line =~ s/\\f/$FF/gs; $line =~ s/\\v/$VT/gs; $line =~ s/\\//gs; return $line; } # read handle, return text in raw (for printing) # and unescaped (for comparing) forms. sub read_text($) { my $handle = shift; my @raw; my ($line, $text, $cont, $i, $j); if ( !$handle->eof() ) { $line = $handle->getline(); # read the line push @raw, $line; # save it in raw form $line =~ /^\s*[#\n]/ && next; # skip if comment/blank $i = index $line, '"'; # get Synchronet text $j = rindex $line, '"'; last unless (($i >= 0) && ($j > $i)); $text = substr $line, $i+1, $j-$i-1; $cont = index $line, "\\", $j+1; # expect continuation? while (($cont==($j+1)) && (!$handle->eof())) { $line = $handle->getline(); # read the line push @raw, $line; # save it in raw form # there should be no comments between extensions $i = index $line, '"'; # pick out the Synchronet text $j = rindex $line, '"'; last unless (($i >= 0) && ($j > $i)); $text .= substr $line, $i+1, $j-$i-1; # ADD Synchronet text $cont = index $line, "\\", $j+1; # expect another continuation? } } $text = unescape( $text ); return (\@raw, \$text) ; } $ori = new FileHandle $original or die "Original: \"$original\" $!"; $mod = new FileHandle $modified or die "Modified: \"$modified\"$!"; $upd = new FileHandle $updated or die "Updated: \"$updated\"$!"; while ((!$ori->eof()) && (!$mod->eof()) && (!$upd->eof())) { ($oraw, $otext) = read_text( $ori ); ($mraw, $mtext) = read_text( $mod ); ($uraw, $utext) = read_text( $upd ); if ($$otext eq $$utext) { # No update: users version can be used print @$mraw; } elsif (($$otext eq $$mtext) || ($$mtext eq $$utext)) { # No mod: new version can be used print @$uraw; } else { # WTF? print STDERR "Original:\n@$oraw\n\n"; print STDERR "Modified:\n@$mraw\n\n"; print STDERR "Updated:\n@$uraw\n\n"; print STDERR "Use [M]odified or [U]pdated? "; if (=~/^m/i) { print @$mraw } else { print @$uraw } # Ugh! } } $ori->close(); $mod->close(); $upd->close(); ----------8<----------------------------------------- Can't help but feel I'm forgetting to do something..... --- þ Synchronet þ Great programs on the Synchronet Channel at The ANJO BBS .