aboutsummaryrefslogtreecommitdiff
path: root/lib/VarnishMib/MIBTable.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/VarnishMib/MIBTable.pm')
-rw-r--r--lib/VarnishMib/MIBTable.pm70
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/VarnishMib/MIBTable.pm b/lib/VarnishMib/MIBTable.pm
new file mode 100644
index 0000000..6aa959f
--- /dev/null
+++ b/lib/VarnishMib/MIBTable.pm
@@ -0,0 +1,70 @@
+package VarnishMib::MIBTable;
+use strict;
+use warnings;
+use parent 'VarnishMib::HashTable';
+use Carp;
+
+sub new {
+ my ($class, $type, $table, %args) = @_;
+ my @oids =
+ sort { $table->{$a}[1] cmp $table->{$b}[1] }
+ grep { $table->{$_}[0] eq 'DICT' } keys %{$table};
+
+ my @equiv;
+ my @uniq_oids;
+ for (my $i = 0; $i <= $#oids; $i++) {
+ unless ($i > 0
+ && $table->{$uniq_oids[-1]}[1] eq $table->{$oids[$i]}[1]) {
+ push @uniq_oids, $oids[$i];
+ }
+ push @equiv, $#uniq_oids;
+ }
+ my $self = bless $class->SUPER::new(%args), $class;
+ $self->{oids_all} = \@oids;
+ $self->{oids_uniq} = \@uniq_oids;
+ $self->{equiv} = \@equiv;
+ $self->{type} = $type;
+
+ $self->create([map { $table->{$_}[1] } @{$self->{oids_uniq}}]);
+ return $self;
+}
+
+sub format_oids {
+ my ($self, $fh) = @_;
+ $fh ||= \*STDOUT;
+
+ print $fh "enum ".$self->prefix."oid {\n";
+ for (my $i = 0; $i <= $#{$self->{oids_all}}; $i++) {
+ print $fh $self->indent . $self->{oids_all}[$i];
+ if ($i > 0
+ && $self->{oids_all}[$i] ne $self->{oids_uniq}[$self->{equiv}[$i]]) {
+ print $fh ' = ' . $self->{oids_uniq}[$self->{equiv}[$i]];
+ }
+ print $fh ",\n";
+ }
+ print $fh $self->indent . 'MAX_' . uc($self->prefix) . "OID\n";
+ print $fh "};\n\n";
+}
+
+sub format_program {
+ my ($self, $fh) = @_;
+ $fh ||= \*STDOUT;
+ $self->format_oids($fh);
+ $self->SUPER::format_program($self->{type}, $fh);
+}
+
+sub str {
+ my $self = shift;
+ my $str;
+ open(my $fh, '>', \$str) or croak "can't write to string";
+ $self->format_program($fh);
+ close $fh;
+ return $str;
+};
+
+use overload '""' => \&str;
+
+1;
+
+
+

Return to:

Send suggestions and report system problems to the System administrator.