+ else
+ {
+ $new_val = $step_factor_down * $old_val;
+ }
+ $line = "<pdp_per_row> $new_val </pdp_per_row>\n";
+ }
+
+ post_line ($line, $index + 1);
+}} # handle_line_step
+
+#
+# The _add RRA_ handler
+#
+{
+my $add_rra_done;
+my $num_ds;
+sub handle_line_add_rra
+{
+ my $line = shift;
+ my $index = shift;
+
+ my $post = sub { for (@_) { post_line ($_, $index + 1); } };
+
+ $num_ds ||= 0;
+
+ if (!@$NewRRAs || $add_rra_done)
+ {
+ $post->($line);
+ return;
+ }
+
+ if ($line =~ m#<ds>#i)
+ {
+ $num_ds++;
+ }
+ elsif ($line =~ m#<rra>#i)
+ {
+ for (my $i = 0; $i < @$NewRRAs; $i++)
+ {
+ my $rra = $NewRRAs->[$i];
+ my $temp;
+ $post->("\t<rra>\n",
+ "\t\t<cf> $rra->{'cf'} </cf>\n",
+ "\t\t<pdp_per_row> $rra->{'steps'} </pdp_per_row>\n",
+ "\t\t<params>\n",
+ "\t\t\t<xff> $rra->{'xff'} </xff>\n",
+ "\t\t</params>\n",
+ "\t\t<cdp_prep>\n");
+
+ for (my $j = 0; $j < $num_ds; $j++)
+ {
+ $post->("\t\t\t<ds>\n",
+ "\t\t\t\t<primary_value> NaN </primary_value>\n",
+ "\t\t\t\t<secondary_value> NaN </secondary_value>\n",
+ "\t\t\t\t<value> NaN </value>\n",
+ "\t\t\t\t<unknown_datapoints> 0 </unknown_datapoints>\n",
+ "\t\t\t</ds>\n");
+ }
+
+ $post->("\t\t</cdp_prep>\n", "\t\t<database>\n");
+ $temp = "\t\t\t<row>" . join ('', map { "<v> NaN </v>" } (1 .. $num_ds)) . "</row>\n";
+ for (my $j = 0; $j < $rra->{'rows'}; $j++)
+ {
+ $post->($temp);
+ }
+ $post->("\t\t</database>\n");
+ }
+ }
+
+ $post->($line);
+}} # handle_line_add_rra
+
+#
+# The _output_ handler
+#
+{
+my $fh;
+sub set_output
+{
+ $fh = shift;
+}
+
+sub handle_line_output
+{
+ my $line = shift;
+ my $index = shift;
+
+ if (!defined ($fh))
+ {
+ post_line ($line, $index + 1);
+ return;
+ }
+
+ print $fh $line;
+}} # handle_line_output
+
+#
+# Dispatching logic
+#
+{
+my @handlers = ();
+sub add_handler
+{
+ my $handler = shift;
+
+ die unless (ref ($handler) eq 'CODE');
+ push (@handlers, $handler);
+} # add_handler
+
+sub post_line
+{
+ my $line = shift;
+ my $index = shift;
+
+ if (0)
+ {
+ my $copy = $line;
+ chomp ($copy);
+ print "DEBUG: post_line ($copy, $index);\n";