|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package getpart; |
|
|
|
|
|
use strict; |
|
|
use warnings; |
|
|
|
|
|
BEGIN { |
|
|
use base qw(Exporter); |
|
|
|
|
|
our @EXPORT = qw( |
|
|
compareparts |
|
|
fulltest |
|
|
getpart |
|
|
getpartattr |
|
|
loadarray |
|
|
loadtest |
|
|
partexists |
|
|
striparray |
|
|
writearray |
|
|
); |
|
|
} |
|
|
|
|
|
use Memoize; |
|
|
use MIME::Base64; |
|
|
|
|
|
my @xml; |
|
|
my $xmlfile; |
|
|
|
|
|
my $warning=0; |
|
|
my $trace=0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub normalize_part { |
|
|
push @_, $xmlfile; |
|
|
return join("\t", @_); |
|
|
} |
|
|
|
|
|
sub decode_hex { |
|
|
my $s = $_; |
|
|
|
|
|
$s =~ s/[^A-Fa-f0-9]//g; |
|
|
|
|
|
$s =~ s/([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg; |
|
|
return $s; |
|
|
} |
|
|
|
|
|
sub testcaseattr { |
|
|
my %hash; |
|
|
for(@xml) { |
|
|
if(($_ =~ /^ *\<testcase ([^>]*)/)) { |
|
|
my $attr=$1; |
|
|
while($attr =~ s/ *([^=]*)= *(\"([^\"]*)\"|([^\> ]*))//) { |
|
|
my ($var, $cont)=($1, $2); |
|
|
$cont =~ s/^\"(.*)\"$/$1/; |
|
|
$hash{$var}=$cont; |
|
|
} |
|
|
} |
|
|
} |
|
|
return %hash; |
|
|
} |
|
|
|
|
|
sub getpartattr { |
|
|
|
|
|
|
|
|
|
|
|
my ($section, $part)=@_; |
|
|
|
|
|
my %hash; |
|
|
my $inside=0; |
|
|
|
|
|
|
|
|
|
|
|
for(@xml) { |
|
|
|
|
|
if(!$inside && ($_ =~ /^ *\<$section/)) { |
|
|
$inside++; |
|
|
} |
|
|
if((1 ==$inside) && ( ($_ =~ /^ *\<$part ([^>]*)/) || |
|
|
!(defined($part)) ) |
|
|
) { |
|
|
$inside++; |
|
|
my $attr=$1; |
|
|
|
|
|
while($attr =~ s/ *([^=]*)= *(\"([^\"]*)\"|([^\> ]*))//) { |
|
|
my ($var, $cont)=($1, $2); |
|
|
$cont =~ s/^\"(.*)\"$/$1/; |
|
|
$hash{$var}=$cont; |
|
|
} |
|
|
last; |
|
|
} |
|
|
|
|
|
elsif((1 ==$inside) && ($_ =~ /^ *\<\/$section\>/)) { |
|
|
last; |
|
|
} |
|
|
elsif((2 ==$inside) && ($_ =~ /^ *\<\/$part/)) { |
|
|
$inside--; |
|
|
} |
|
|
} |
|
|
return %hash; |
|
|
} |
|
|
memoize('getpartattr', NORMALIZER => 'normalize_part'); |
|
|
|
|
|
sub getpart { |
|
|
my ($section, $part)=@_; |
|
|
|
|
|
my @this; |
|
|
my $inside=0; |
|
|
my $base64=0; |
|
|
my $hex=0; |
|
|
my $line; |
|
|
|
|
|
for(@xml) { |
|
|
$line++; |
|
|
if(!$inside && ($_ =~ /^ *\<$section/)) { |
|
|
$inside++; |
|
|
} |
|
|
elsif(($inside >= 1) && ($_ =~ /^ *\<$part[ \>]/)) { |
|
|
if($inside > 1) { |
|
|
push @this, $_; |
|
|
} |
|
|
elsif($_ =~ /$part [^>]*base64=/) { |
|
|
|
|
|
$base64=1; |
|
|
} |
|
|
elsif($_ =~ /$part [^>]*hex=/) { |
|
|
|
|
|
$hex=1; |
|
|
} |
|
|
$inside++; |
|
|
} |
|
|
elsif(($inside >= 2) && ($_ =~ /^ *\<\/$part[ \>]/)) { |
|
|
if($inside > 2) { |
|
|
push @this, $_; |
|
|
} |
|
|
$inside--; |
|
|
} |
|
|
elsif(($inside >= 1) && ($_ =~ /^ *\<\/$section/)) { |
|
|
if($inside > 1) { |
|
|
print STDERR "$xmlfile:$line:1: error: missing </$part> tag before </$section>\n"; |
|
|
@this = ("format error in $xmlfile"); |
|
|
} |
|
|
if($trace && @this) { |
|
|
print STDERR "*** getpart.pm: $section/$part returned data!\n"; |
|
|
} |
|
|
if($warning && !@this) { |
|
|
print STDERR "*** getpart.pm: $section/$part returned empty!\n"; |
|
|
} |
|
|
if($base64) { |
|
|
|
|
|
for(@this) { |
|
|
my $decoded = decode_base64($_); |
|
|
$_ = $decoded; |
|
|
} |
|
|
} |
|
|
elsif($hex) { |
|
|
|
|
|
for(@this) { |
|
|
my $decoded = decode_hex($_); |
|
|
$_ = $decoded; |
|
|
} |
|
|
} |
|
|
return @this; |
|
|
} |
|
|
elsif($inside >= 2) { |
|
|
push @this, $_; |
|
|
} |
|
|
} |
|
|
if($trace && @this) { |
|
|
|
|
|
|
|
|
print STDERR "*** getpart.pm: $section/$part returned data!\n"; |
|
|
} |
|
|
if($warning && !@this) { |
|
|
|
|
|
|
|
|
print STDERR "*** getpart.pm: $section/$part returned empty!\n"; |
|
|
} |
|
|
return @this; |
|
|
} |
|
|
memoize('getpart', NORMALIZER => 'normalize_part'); |
|
|
|
|
|
sub partexists { |
|
|
my ($section, $part)=@_; |
|
|
|
|
|
my $inside = 0; |
|
|
|
|
|
for(@xml) { |
|
|
if(!$inside && ($_ =~ /^ *\<$section/)) { |
|
|
$inside++; |
|
|
} |
|
|
elsif((1 == $inside) && ($_ =~ /^ *\<$part[ \>]/)) { |
|
|
return 1; |
|
|
} |
|
|
elsif((1 == $inside) && ($_ =~ /^ *\<\/$section/)) { |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub loadtest { |
|
|
my ($file)=@_; |
|
|
|
|
|
if(defined $xmlfile && $file eq $xmlfile) { |
|
|
|
|
|
return |
|
|
} |
|
|
|
|
|
undef @xml; |
|
|
$xmlfile = ""; |
|
|
|
|
|
if(open(my $xmlh, "<", "$file")) { |
|
|
binmode $xmlh; |
|
|
while(<$xmlh>) { |
|
|
push @xml, $_; |
|
|
} |
|
|
close($xmlh); |
|
|
} |
|
|
else { |
|
|
|
|
|
if($warning) { |
|
|
print STDERR "file $file wouldn't open!\n"; |
|
|
} |
|
|
return 1; |
|
|
} |
|
|
$xmlfile = $file; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sub fulltest { |
|
|
return @xml; |
|
|
} |
|
|
|
|
|
|
|
|
sub savetest { |
|
|
my ($file)=@_; |
|
|
|
|
|
if(open(my $xmlh, ">", "$file")) { |
|
|
binmode $xmlh; |
|
|
for(@xml) { |
|
|
print $xmlh $_; |
|
|
} |
|
|
close($xmlh); |
|
|
} |
|
|
else { |
|
|
|
|
|
if($warning) { |
|
|
print STDERR "file $file wouldn't open!\n"; |
|
|
} |
|
|
return 1; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub striparray { |
|
|
my ($pattern, $arrayref) = @_; |
|
|
|
|
|
my @array; |
|
|
|
|
|
for(@$arrayref) { |
|
|
if($_ !~ /$pattern/) { |
|
|
push @array, $_; |
|
|
} |
|
|
} |
|
|
return @array; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub compareparts { |
|
|
my ($firstref, $secondref)=@_; |
|
|
|
|
|
my $first = join("", @$firstref); |
|
|
my $second = join("", @$secondref); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($first ne $second) { |
|
|
return 1; |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub writearray { |
|
|
my ($filename, $arrayref)=@_; |
|
|
|
|
|
open(my $temp, ">", "$filename") || die "Failure writing file"; |
|
|
binmode($temp,":raw"); |
|
|
for(@$arrayref) { |
|
|
print $temp $_; |
|
|
} |
|
|
close($temp) || die "Failure writing file"; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub loadarray { |
|
|
my ($filename)=@_; |
|
|
my @array; |
|
|
|
|
|
if (open(my $temp, "<", "$filename")) { |
|
|
while(<$temp>) { |
|
|
push @array, $_; |
|
|
} |
|
|
close($temp); |
|
|
} |
|
|
return @array; |
|
|
} |
|
|
|
|
|
|
|
|
1; |
|
|
|