X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=contrib%2FextractDS.px;h=bdc4b3d2b0641e303704ec9dc1f96abb3cf95e2a;hb=756704084860d8ad21db61f2a677cbd80b008be4;hp=5826c6538a646275cc68d2095bc81d2e8e48ab39;hpb=44e4d530c8db2e078cd73ab254d1be41a5f2812c;p=collectd.git diff --git a/contrib/extractDS.px b/contrib/extractDS.px index 5826c653..bdc4b3d2 100755 --- a/contrib/extractDS.px +++ b/contrib/extractDS.px @@ -19,23 +19,22 @@ the L module. =cut use Getopt::Long ('GetOptions'); -use XML::Simple (qw(xml_in xml_out)); use Data::Dumper (); our $InFile; -our $InDS; +our $InDS = []; our $OutFile; -our $OutDS; +our $OutDS = []; GetOptions ("infile|i=s" => \$InFile, - "inds|s=s" => \$InDS, + "inds|s=s" => sub { push (@$InDS, $_[1]); }, "outfile|o=s" => \$OutFile, - "outds|d=s" => \$OutDS) or exit (1); + "outds|d=s" => sub { push (@$OutDS, $_[1]); }) + or exit (1); -if (!$InFile || !$OutFile || !$InDS || !$OutDS) +if (!$InFile || !$OutFile || !@$InDS || !@$OutDS) { - print "$InFile $InDS $OutFile $OutDS\n"; - print STDERR "Usage: $0 -i -I -o -O \n"; + print STDERR "Usage: $0 -i -s -o -d \n"; exit (1); } if (!-f $InFile) @@ -48,19 +47,23 @@ if (-f $OutFile) print STDERR "Output file does exist\n"; exit (1); } +if ((1 + @$InDS) != (1 + @$OutDS)) +{ + print STDERR "You need the same amount of in- and out-DSes\n"; + exit (1); +} -extract_ds ($InFile, $OutFile, $InDS, $OutDS); +extract_ds ($InFile, $OutFile); exit (0); { -my $ds_index = -1; -my $current_index = -1; +my $ds_index; +my $current_index; # state 0 == searching for DS index # state 1 == parse RRA header -# state 2 == parse in RRA header -# state 3 == parse values -my $state = 0; -my $out_cache = ''; +# state 2 == parse values +my $state; +my $out_cache; sub handle_line { my $fh = shift; @@ -68,48 +71,75 @@ sub handle_line if (!defined ($state)) { - $ds_index = -1; $current_index = -1; $state = 0; - $out_cache = ''; + $out_cache = []; + + # $ds_index->[new_index] = old_index + $ds_index = []; + for (my $i = 0; $i < @$InDS; $i++) + { + $ds_index->[$i] = -1; + } } if ($state == 0) { if ($line =~ m//) { - $out_cache = $line; $current_index++; + $out_cache->[$current_index] = $line; } elsif ($line =~ m#\s*([^<\s]+)\s*#) { - if ($1 eq $InDS) + # old_index == $current_index + # new_index == $i + for (my $i = 0; $i < @$InDS; $i++) { - $ds_index = $current_index; - $out_cache .= "\t\t$OutDS\n"; + next if ($ds_index->[$i] >= 0); + + if ($1 eq $InDS->[$i]) + { + $line =~ s#\s*([^<\s]+)\s*# $OutDS->[$i] #; + $ds_index->[$i] = $current_index; + last; + } } + + $out_cache->[$current_index] .= $line; } elsif ($line =~ m##) { - $out_cache .= $line; - if ($ds_index == $current_index) - { - print $fh $out_cache; - } + $out_cache->[$current_index] .= $line; } elsif ($line =~ m##) { + # Print out all the DS definitions we need + for (my $new_index = 0; $new_index < @$InDS; $new_index++) + { + my $old_index = $ds_index->[$new_index]; + print $fh $out_cache->[$old_index]; + } + + # Clear the cache - it's used in state1, too. + for (my $i = 0; $i <= $current_index; $i++) + { + $out_cache->[$i] = ''; + } + print $fh $line; $current_index = -1; $state = 1; } elsif ($current_index == -1) { + # Print all the lines before the first DS definition print $fh $line; } else { - $out_cache .= $line; + # Something belonging to a DS-definition + $out_cache->[$current_index] .= $line; } } elsif ($state == 1) @@ -117,37 +147,44 @@ sub handle_line if ($line =~ m##) { $current_index++; - if ($current_index == $ds_index) + $out_cache->[$current_index] .= $line; + } + elsif ($line =~ m##) + { + # Print out all the DS definitions we need + for (my $new_index = 0; $new_index < @$InDS; $new_index++) { - print $fh $line; + my $old_index = $ds_index->[$new_index]; + print $fh $out_cache->[$old_index]; + } + + # Clear the cache + for (my $i = 0; $i <= $current_index; $i++) + { + $out_cache->[$i] = ''; } - if ($line =~ m##) { $state = 1; } - else { $state = 2; } - } - elsif ($line =~ m##) - { print $fh $line; - $state = 3; + $current_index = -1; } - else + elsif ($line =~ m##) { print $fh $line; + $state = 2; } - } - elsif ($state == 2) - { - if ($current_index == $ds_index) + elsif ($current_index == -1) { - print STDERR $line; + # Print all the lines before the first DS definition + # and after cdp_prep print $fh $line; } - if ($line =~ m##) + else { - $state = 1; + # Something belonging to a DS-definition + $out_cache->[$current_index] .= $line; } } - else + elsif ($state == 2) { if ($line =~ m##) { @@ -157,33 +194,42 @@ sub handle_line } else { - my $line_begin = "\t\t"; - $current_index = 0; + my @values = (); + my $i; + my $output = "\t\t"; + if ($line =~ m#()#) { - $line_begin .= "$1 "; + $output .= "$1 "; } + $output .= " "; - while ($line =~ m#\s*([^<\s]+)\s*#) + $i = 0; + while ($line =~ m#\s*([^<\s]+)\s*#g) { - my $value = $1; - if ($current_index == $ds_index) - { - print $fh "$line_begin $value \n"; - last; - } - $current_index++; + $values[$i] = $1; + $i++; } + + for (my $new_index = 0; $new_index < @$InDS; $new_index++) + { + my $old_index = $ds_index->[$new_index]; + $output .= ' ' . $values[$old_index] . ' '; + } + $output .= "\n"; + print $fh $output; } } + else + { + die; + } }} # handle_line sub extract_ds { my $in_file = shift; my $out_file = shift; - my $in_ds = shift; - my $out_ds = shift; my $in_fh; my $out_fh;