getpart.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #use strict;
  2. my @xml;
  3. my $warning=0;
  4. my $trace=0;
  5. sub getpartattr {
  6. # if $part is undefined (ie only one argument) then
  7. # return the attributes of the section
  8. my ($section, $part)=@_;
  9. my %hash;
  10. my $inside=0;
  11. # print "Section: $section, part: $part\n";
  12. for(@xml) {
  13. # print "$inside: $_";
  14. if(!$inside && ($_ =~ /^ *\<$section/)) {
  15. $inside++;
  16. }
  17. if((1 ==$inside) && ( ($_ =~ /^ *\<$part([^>]*)/) ||
  18. !(defined($part)) )
  19. ) {
  20. $inside++;
  21. my $attr=$1;
  22. my @p=split("[ \t]", $attr);
  23. my $assign;
  24. foreach $assign (@p) {
  25. # $assign is a 'name="contents"' pair
  26. if($assign =~ / *([^=]*)=\"([^\"]*)\"/) {
  27. # *with* quotes
  28. $hash{$1}=$2;
  29. }
  30. elsif($assign =~ / *([^=]*)=([^\"]*)/) {
  31. # *without* quotes
  32. $hash{$1}=$2;
  33. }
  34. }
  35. last;
  36. }
  37. elsif((2 ==$inside) && ($_ =~ /^ *\<\/$part/)) {
  38. $inside--;
  39. }
  40. }
  41. return %hash;
  42. }
  43. sub getpart {
  44. my ($section, $part)=@_;
  45. my @this;
  46. my $inside=0;
  47. # print "Section: $section, part: $part\n";
  48. for(@xml) {
  49. # print "$inside: $_";
  50. if(!$inside && ($_ =~ /^ *\<$section/)) {
  51. $inside++;
  52. }
  53. elsif((1 ==$inside) && ($_ =~ /^ *\<$part[ \>]/)) {
  54. $inside++;
  55. }
  56. elsif((2 ==$inside) && ($_ =~ /^ *\<\/$part/)) {
  57. $inside--;
  58. }
  59. elsif((1==$inside) && ($_ =~ /^ *\<\/$section/)) {
  60. if($trace) {
  61. print STDERR "*** getpart.pm: $section/$part returned data!\n";
  62. }
  63. if(!@this && $warning) {
  64. print STDERR "*** getpart.pm: $section/$part returned empty!\n";
  65. }
  66. return @this;
  67. }
  68. elsif(2==$inside) {
  69. push @this, $_;
  70. }
  71. }
  72. if($warning) {
  73. print STDERR "*** getpart.pm: $section/$part returned empty!\n";
  74. }
  75. return @this; #empty!
  76. }
  77. sub loadtest {
  78. my ($file)=@_;
  79. undef @xml;
  80. if(open(XML, "<$file")) {
  81. binmode XML; # for crapage systems, use binary
  82. while(<XML>) {
  83. push @xml, $_;
  84. }
  85. close(XML);
  86. }
  87. else {
  88. # failure
  89. if($warning) {
  90. print STDERR "file $file wouldn't open!\n";
  91. }
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. #
  97. # Strip off all lines that match the specified pattern and return
  98. # the new array.
  99. #
  100. sub striparray {
  101. my ($pattern, $arrayref) = @_;
  102. my @array;
  103. for(@$arrayref) {
  104. if($_ !~ /$pattern/) {
  105. push @array, $_;
  106. }
  107. }
  108. return @array;
  109. }
  110. #
  111. # pass array *REFERENCES* !
  112. #
  113. sub compareparts {
  114. my ($firstref, $secondref)=@_;
  115. my $sizefirst=scalar(@$firstref);
  116. my $sizesecond=scalar(@$secondref);
  117. if($sizefirst != $sizesecond) {
  118. return -1;
  119. }
  120. for(1 .. $sizefirst) {
  121. my $index = $_ - 1;
  122. if($firstref->[$index] ne $secondref->[$index]) {
  123. (my $aa = $firstref->[$index]) =~ s/\r+\n$/\n/;
  124. (my $bb = $secondref->[$index]) =~ s/\r+\n$/\n/;
  125. if($aa ne $bb) {
  126. return 1+$index;
  127. }
  128. }
  129. }
  130. return 0;
  131. }
  132. #
  133. # Write a given array to the specified file
  134. #
  135. sub writearray {
  136. my ($filename, $arrayref)=@_;
  137. open(TEMP, ">$filename");
  138. binmode(TEMP,":raw"); # cygwin fix by Kevin Roth
  139. for(@$arrayref) {
  140. print TEMP $_;
  141. }
  142. close(TEMP);
  143. }
  144. #
  145. # Load a specified file an return it as an array
  146. #
  147. sub loadarray {
  148. my ($filename)=@_;
  149. my @array;
  150. open(TEMP, "<$filename");
  151. while(<TEMP>) {
  152. push @array, $_;
  153. }
  154. close(TEMP);
  155. return @array;
  156. }
  157. #
  158. # Given two array references, this function will store them in two
  159. # temporary files, run 'diff' on them, store the result, remove the
  160. # temp files and return the diff output!
  161. #
  162. sub showdiff {
  163. my ($firstref, $secondref)=@_;
  164. my $file1=".generated";
  165. my $file2=".expected";
  166. open(TEMP, ">$file1");
  167. for(@$firstref) {
  168. print TEMP $_;
  169. }
  170. close(TEMP);
  171. open(TEMP, ">$file2");
  172. for(@$secondref) {
  173. print TEMP $_;
  174. }
  175. close(TEMP);
  176. my @out = `diff -u $file2 $file1`;
  177. unlink $file1, $file2;
  178. return @out;
  179. }
  180. 1;