Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.5 KiB

  1. #####################################################
  2. # MergeFiles takes two files, assumed to be sorted, and
  3. # merges them together into one big sorted file.
  4. # Additionally, files under section headers [blah] will
  5. # remain sorted under those section headers.
  6. # Requires three(3) arguments
  7. #####################################################
  8. $i=0;
  9. $argc = @ARGV;
  10. $PGM = "MergeFiles:";
  11. die "Usage: MergeFiles file1 file2 outfile\n" if ( $argc < 3 );
  12. $file1 = $ARGV[0];
  13. $file2 = $ARGV[1];
  14. $outfile = $ARGV[2];
  15. die "$PGM $file1 does not exist!\n" if !(-e $file1);
  16. open(FILE1, "<$file1");
  17. die "$PGM $file2 does not exist!\n" if !(-e $file2);
  18. open(FILE2, "<$file2");
  19. # Make the path of the output file if necessary
  20. if( $outfile =~ /\\/ ){
  21. $outpath = "";
  22. @outarray = split(/\\/, $outfile);
  23. pop @outarray;
  24. $first = 1;
  25. foreach $dir (@outarray){
  26. if( !$first ){
  27. $outpath = $outpath . '\\';
  28. } else {
  29. $first = 0;
  30. }
  31. $outpath = $outpath . $dir;
  32. }
  33. if (!(-d $outpath)) { system "mkdir $outpath" }
  34. }
  35. open(OUTFILE, ">$outfile") || die "$PGM Could not open $outfile!\n";
  36. $linenum = 0;
  37. $head = 0;
  38. $curheader = "[_NONE_]";
  39. $headarray1[0] = $curheader;
  40. while(<FILE1>){
  41. # if( /^$/ ){ next } # Skip empty lines
  42. # if( /^;/ ){ next } # ';' for comment line
  43. if( /\[.*\]/ && !(/^;/) && !(/^$/) ){
  44. $heading = $_;
  45. chomp $heading;
  46. $head++;
  47. $headarray1[$head] = $heading;
  48. $curheader = $heading;
  49. $linenum = 0;
  50. } else {
  51. push @{$strarray1[$head]}, $_;
  52. $linenum++;
  53. }
  54. }
  55. close(FILE1);
  56. $linenum = 0;
  57. $head = 0;
  58. $curheader = "[_NONE_]";
  59. $headarray2[0] = $curheader;
  60. $IsDefHeader = 0;
  61. while(<FILE2>){
  62. # if( /^$/ ){ next } # Skip empty lines
  63. # if( /^;/ ){ next } # ';' for comment line
  64. if( /\[(.*)\]/ && !(/^;/) && !(/^$/) ){
  65. $heading = $_;
  66. chomp $heading;
  67. $IsDefHeader = 1;
  68. if( !($heading =~ /CoverageDefault/i) ){
  69. $head++;
  70. $headarray2[$head] = $heading;
  71. $IsDefHeader = 0;
  72. }
  73. $curheader = $heading;
  74. $linenum = 0;
  75. } else {
  76. if( $IsDefHeader ){
  77. push @defarray, $_;
  78. } else {
  79. push @{$strarray2[$head]}, $_;
  80. }
  81. $linenum++;
  82. }
  83. }
  84. close(FILE2);
  85. # For each item in the default items array, check to see if the
  86. # corresponding key exists in the environment, and if so, use that value.
  87. for $line (@defarray){
  88. chomp $line;
  89. if( $line =~ /^$/ ){ next } # Skip empty lines
  90. ($key, $value) = split('=', $line);
  91. if( $ENV{$key} ){
  92. $defhash{$key} = $ENV{$key};
  93. } else {
  94. $defhash{$key} = $value;
  95. }
  96. }
  97. # Iterate through the second file and set the defaults
  98. $head2 = 0;
  99. for $heading (@headarray2){
  100. print "$heading\n" if( $heading ne "[_NONE_]");
  101. for $line (@{$strarray2[$head2]}){
  102. for $key (keys(%defhash)){
  103. if( $line =~ $key ){
  104. $line =~ s/$key/$defhash{$key}/;
  105. }
  106. }
  107. }
  108. $head2++;
  109. }
  110. # Merge the two files together.
  111. $head1 = 0;
  112. for $heading (@headarray1){
  113. if( $heading ne "[_NONE_]"){
  114. print OUTFILE "$heading\n";
  115. }
  116. $match = 0;
  117. # Search for the same heading in the 2nd file and merge
  118. $head2 = 0;
  119. for $heading2 (@headarray2){
  120. if( $heading eq $heading2 ){ $match = 1; last;}
  121. $head2++;
  122. }
  123. # if we have a heading to merge, then merge the two.
  124. if( $match && !($matches{$heading}) ){
  125. $matches{$heading} = 1;
  126. # print "MATCH: $heading\n";
  127. $i = 0;
  128. $j = 0;
  129. @array1 = @{$strarray1[$head1]};
  130. @array2 = @{$strarray2[$head2]};
  131. $len1 = @array1;
  132. $len2 = @array2;
  133. # print "LEN1: $len1\tLEN2: $len2\n";
  134. while( ($i < $len1) && ($j < $len2) ){
  135. # Copy the lines to temp vars
  136. $test1 = $array1[$i];
  137. $test2 = $array2[$j];
  138. # Remove any directives and canonicalize to lowercase.
  139. $test1 =~ s/^@.*:(.*)/$1/;
  140. $test2 =~ s/^@.*:(.*)/$1/;
  141. $test1 =~ tr/A-Z/a-z/;
  142. $test2 =~ tr/A-Z/a-z/;
  143. # Merge two sorted lists (print the original lines)
  144. if( ($test1 =~ /^;/) || ($test1 =~ /^~/) ){
  145. print OUTFILE $array1[$i++];
  146. } elsif( $test1 lt $test2 ){
  147. print OUTFILE $array1[$i++];
  148. } elsif( $test1 gt $test2 ){
  149. print OUTFILE $array2[$j++];
  150. } elsif( $test1 eq $test2 ){
  151. print OUTFILE $array1[$i++];
  152. $j++;
  153. }
  154. }
  155. # Merge the remainder of each list, if any.
  156. while( $i < $len1 ){
  157. print OUTFILE $array1[$i++];
  158. }
  159. while( $j < $len2 ){
  160. print OUTFILE $array2[$j++];
  161. }
  162. } else {
  163. # otherwise, just print the list out.
  164. for $line (@{$strarray1[$head1]}){
  165. print OUTFILE $line;
  166. }
  167. }
  168. $head1++;
  169. }
  170. close(OUTFILE);